2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2016-05-30 02:03:51 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2010 Lennart Poettering
|
|
|
|
Copyright 2016 Michael Karcher
|
|
|
|
***/
|
|
|
|
|
2018-05-31 05:06:33 +02:00
|
|
|
#include <errno.h>
|
2016-05-30 02:03:51 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* raw_clone() - uses clone to create a new process with clone flags
|
|
|
|
* @flags: Flags to pass to the clone system call
|
|
|
|
*
|
2017-12-29 16:45:04 +01:00
|
|
|
* Uses the clone system call to create a new process with the cloning flags and termination signal passed in the flags
|
|
|
|
* parameter. Opposed to glibc's clone funtion, using this function does not set up a separate stack for the child, but
|
|
|
|
* relies on copy-on-write semantics on the one stack at a common virtual address, just as fork does.
|
2016-05-30 02:03:51 +02:00
|
|
|
*
|
2017-12-29 16:45:04 +01:00
|
|
|
* To obtain copy-on-write semantics, flags must not contain CLONE_VM, and thus CLONE_THREAD and CLONE_SIGHAND (which
|
|
|
|
* require CLONE_VM) are not usable.
|
|
|
|
*
|
|
|
|
* Additionally, as this function does not pass the ptid, newtls and ctid parameters to the kernel, flags must not
|
|
|
|
* contain CLONE_PARENT_SETTID, CLONE_CHILD_SETTID, CLONE_CHILD_CLEARTID or CLONE_SETTLS.
|
2016-05-30 02:03:51 +02:00
|
|
|
*
|
|
|
|
* Returns: 0 in the child process and the child process id in the parent.
|
|
|
|
*/
|
2017-12-29 16:45:04 +01:00
|
|
|
static inline pid_t raw_clone(unsigned long flags) {
|
|
|
|
pid_t ret;
|
|
|
|
|
2016-05-30 02:03:51 +02:00
|
|
|
assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
|
|
|
|
CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0);
|
2017-02-08 16:21:11 +01:00
|
|
|
#if defined(__s390x__) || defined(__s390__) || defined(__CRIS__)
|
|
|
|
/* On s390/s390x and cris the order of the first and second arguments
|
2016-05-30 02:03:51 +02:00
|
|
|
* of the raw clone() system call is reversed. */
|
2017-12-29 16:45:04 +01:00
|
|
|
ret = (pid_t) syscall(__NR_clone, NULL, flags);
|
2018-05-24 16:48:55 +02:00
|
|
|
#elif defined(__sparc__)
|
2016-05-30 02:03:51 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-05-24 16:48:55 +02:00
|
|
|
* sparc always returns the other process id in %o0, and
|
2016-05-30 02:03:51 +02:00
|
|
|
* a boolean flag whether this is the child or the parent in
|
|
|
|
* %o1. Inline assembly is needed to get the flag returned
|
|
|
|
* in %o1.
|
|
|
|
*/
|
2018-05-31 05:06:33 +02:00
|
|
|
int in_child, child_pid, error;
|
2017-12-29 16:45:04 +01:00
|
|
|
|
2018-05-31 05:06:33 +02:00
|
|
|
asm volatile("mov %3, %%g1\n\t"
|
|
|
|
"mov %4, %%o0\n\t"
|
2016-05-30 02:03:51 +02:00
|
|
|
"mov 0 , %%o1\n\t"
|
2018-05-24 16:48:55 +02:00
|
|
|
#if defined(__arch64__)
|
2016-05-30 02:03:51 +02:00
|
|
|
"t 0x6d\n\t"
|
2018-05-24 16:48:55 +02:00
|
|
|
#else
|
|
|
|
"t 0x10\n\t"
|
|
|
|
#endif
|
2018-05-31 05:06:33 +02:00
|
|
|
"addx %%g0, 0, %2\n\t"
|
2016-05-30 02:03:51 +02:00
|
|
|
"mov %%o1, %0\n\t"
|
|
|
|
"mov %%o0, %1" :
|
2018-05-31 05:06:33 +02:00
|
|
|
"=r"(in_child), "=r"(child_pid), "=r"(error) :
|
2016-05-30 02:03:51 +02:00
|
|
|
"i"(__NR_clone), "r"(flags) :
|
2018-05-31 05:06:33 +02:00
|
|
|
"%o1", "%o0", "%g1" "cc" );
|
2017-12-29 16:45:04 +01:00
|
|
|
|
2018-05-31 05:06:33 +02:00
|
|
|
if (error) {
|
|
|
|
errno = child_pid;
|
|
|
|
ret = -1;
|
|
|
|
} else
|
|
|
|
ret = in_child ? 0 : child_pid;
|
2016-05-30 02:03:51 +02:00
|
|
|
}
|
|
|
|
#else
|
2017-12-29 16:45:04 +01:00
|
|
|
ret = (pid_t) syscall(__NR_clone, flags, NULL);
|
2016-05-30 02:03:51 +02:00
|
|
|
#endif
|
2017-12-29 16:45:04 +01:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
reset_cached_pid();
|
|
|
|
|
|
|
|
return ret;
|
2016-05-30 02:03:51 +02:00
|
|
|
}
|