clone.c 994 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. * I don't know if we can be absolutely certain that the fn and arg
  14. * parameters are preserved when returning as the child. If the
  15. * compiler stores them in registers (r0-r7), they should be.
  16. */
  17. int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, ...)
  18. {
  19. register int (*_fn)(void *arg) = fn;
  20. register void *_arg = arg;
  21. int err;
  22. /* Sanity check the arguments */
  23. err = -EINVAL;
  24. if (!fn)
  25. goto syscall_error;
  26. if (!child_stack)
  27. goto syscall_error;
  28. err = INLINE_SYSCALL(clone, 2, flags, child_stack);
  29. if (err < 0)
  30. goto syscall_error;
  31. else if (err != 0)
  32. return err;
  33. _exit(_fn(_arg));
  34. syscall_error:
  35. __set_errno (-err);
  36. return -1;
  37. }