5 #include "pthread_impl.h"
10 struct clone_start_args {
16 static int clone_start(void *arg)
18 struct clone_start_args *csa = arg;
20 __restore_sigs(&csa->sigmask);
21 return csa->func(csa->arg);
24 int clone(int (*func)(void *), void *stack, int flags, void *arg, ...)
26 struct clone_start_args csa;
28 pid_t *ptid = 0, *ctid = 0;
31 /* Flags that produce an invalid thread/TLS state are disallowed. */
32 int badflags = CLONE_THREAD | CLONE_SETTLS | CLONE_CHILD_CLEARTID;
34 if ((flags & badflags) || !stack)
35 return __syscall_ret(-EINVAL);
38 if (flags & (CLONE_PIDFD | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID))
39 ptid = va_arg(ap, pid_t *);
40 if (flags & CLONE_CHILD_SETTID) {
41 tls = va_arg(ap, void *);
42 ctid = va_arg(ap, pid_t *);
46 /* If CLONE_VM is used, it's impossible to give the child a consistent
47 * thread structure. In this case, the best we can do is assume the
48 * caller is content with an extremely restrictive execution context
49 * like the one vfork() would provide. */
50 if (flags & CLONE_VM) return __syscall_ret(
51 __clone(func, stack, flags, arg, ptid, tls, ctid));
53 __block_all_sigs(&csa.sigmask);
56 /* Setup the a wrapper start function for the child process to do
57 * mimic _Fork in producing a consistent execution state. */
60 int ret = __clone(clone_start, stack, flags, &csa, ptid, tls, ctid);
63 __restore_sigs(&csa.sigmask);
64 return __syscall_ret(ret);