clone.c 970 B

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