_Fork.c 789 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include "syscall.h"
  4. #include "libc.h"
  5. #include "lock.h"
  6. #include "pthread_impl.h"
  7. #include "aio_impl.h"
  8. static void dummy(int x) { }
  9. weak_alias(dummy, __aio_atfork);
  10. pid_t _Fork(void)
  11. {
  12. pid_t ret;
  13. sigset_t set;
  14. __block_all_sigs(&set);
  15. LOCK(__abort_lock);
  16. #ifdef SYS_fork
  17. ret = __syscall(SYS_fork);
  18. #else
  19. ret = __syscall(SYS_clone, SIGCHLD, 0);
  20. #endif
  21. if (!ret) {
  22. pthread_t self = __pthread_self();
  23. self->tid = __syscall(SYS_gettid);
  24. self->robust_list.off = 0;
  25. self->robust_list.pending = 0;
  26. self->next = self->prev = self;
  27. __thread_list_lock = 0;
  28. libc.threads_minus_1 = 0;
  29. if (libc.need_locks) libc.need_locks = -1;
  30. }
  31. UNLOCK(__abort_lock);
  32. if (!ret) __aio_atfork(1);
  33. __restore_sigs(&set);
  34. return __syscall_ret(ret);
  35. }