1 /*
2 * Copyright (C) 2004 Atmel Corporation
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License. See the file "COPYING.LIB" in the main directory of this
6 * archive for more details.
7 */
8 #include <sched.h>
9 #include <errno.h>
10 #include <sys/syscall.h>
11 #include <unistd.h>
12
13 /*
14 * I don't know if we can be absolutely certain that the fn and arg
15 * parameters are preserved when returning as the child. If the
16 * compiler stores them in registers (r0-r7), they should be.
17 */
clone(int (* fn)(void * arg),void * child_stack,int flags,void * arg,...)18 int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, ...)
19 {
20 register int (*_fn)(void *arg) = fn;
21 register void *_arg = arg;
22 int err;
23
24 /* Sanity check the arguments */
25 err = -EINVAL;
26 if (!fn)
27 goto syscall_error;
28 if (!child_stack)
29 goto syscall_error;
30
31 err = INLINE_SYSCALL(clone, 2, flags, child_stack);
32 if (err < 0)
33 goto syscall_error;
34 else if (err != 0)
35 return err;
36
37 _exit(_fn(_arg));
38
39 syscall_error:
40 __set_errno (-err);
41 return -1;
42 }
43