ptfork.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* The "atfork" stuff */
  15. #include <errno.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include "pthread.h"
  20. #include "internals.h"
  21. #include <bits/libc-lock.h>
  22. #include "fork.h"
  23. extern int __libc_fork (void);
  24. pid_t __pthread_fork (struct fork_block *b)
  25. {
  26. pid_t pid;
  27. list_t *runp;
  28. __libc_lock_lock (b->lock);
  29. /* Run all the registered preparation handlers. In reverse order. */
  30. list_for_each_prev (runp, &b->prepare_list)
  31. {
  32. struct fork_handler *curp;
  33. curp = list_entry (runp, struct fork_handler, list);
  34. curp->handler ();
  35. }
  36. __pthread_once_fork_prepare();
  37. __flockfilelist();
  38. pid = ARCH_FORK ();
  39. if (pid == 0) {
  40. __pthread_reset_main_thread();
  41. __fresetlockfiles();
  42. __pthread_once_fork_child();
  43. /* Run the handlers registered for the child. */
  44. list_for_each (runp, &b->child_list)
  45. {
  46. struct fork_handler *curp;
  47. curp = list_entry (runp, struct fork_handler, list);
  48. curp->handler ();
  49. }
  50. __libc_lock_init (b->lock);
  51. } else {
  52. __funlockfilelist();
  53. __pthread_once_fork_parent();
  54. /* Run the handlers registered for the parent. */
  55. list_for_each (runp, &b->parent_list)
  56. {
  57. struct fork_handler *curp;
  58. curp = list_entry (runp, struct fork_handler, list);
  59. curp->handler ();
  60. }
  61. __libc_lock_unlock (b->lock);
  62. }
  63. return pid;
  64. }
  65. #ifdef SHARED
  66. pid_t __fork (void)
  67. {
  68. return __libc_fork ();
  69. }
  70. weak_alias (__fork, fork);
  71. pid_t __vfork(void)
  72. {
  73. return __libc_fork ();
  74. }
  75. weak_alias (__vfork, vfork);
  76. #endif