fork.c 816 B

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