1
0

fork.c 698 B

12345678910111213141516171819202122232425262728293031323334353637
  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. pid_t fork(void)
  12. {
  13. pid_t ret;
  14. sigset_t set;
  15. __fork_handler(-1);
  16. __block_all_sigs(&set);
  17. #ifdef SYS_fork
  18. ret = __syscall(SYS_fork);
  19. #else
  20. ret = __syscall(SYS_clone, SIGCHLD, 0);
  21. #endif
  22. if (!ret) {
  23. pthread_t self = __pthread_self();
  24. self->tid = __syscall(SYS_gettid);
  25. self->robust_list.off = 0;
  26. self->robust_list.pending = 0;
  27. self->next = self->prev = self;
  28. __thread_list_lock = 0;
  29. libc.threads_minus_1 = 0;
  30. }
  31. __restore_sigs(&set);
  32. __fork_handler(!ret);
  33. return __syscall_ret(ret);
  34. }