ptfork.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. pid_t __pthread_fork (struct fork_block *b)
  24. {
  25. pid_t pid;
  26. list_t *runp;
  27. __libc_lock_lock (b->lock);
  28. /* Run all the registered preparation handlers. In reverse order. */
  29. list_for_each_prev (runp, &b->prepare_list)
  30. {
  31. struct fork_handler *curp;
  32. curp = list_entry (runp, struct fork_handler, list);
  33. curp->handler ();
  34. }
  35. __pthread_once_fork_prepare();
  36. __flockfilelist();
  37. pid = ARCH_FORK ();
  38. if (pid == 0) {
  39. __pthread_reset_main_thread();
  40. __fresetlockfiles();
  41. __pthread_once_fork_child();
  42. /* Run the handlers registered for the child. */
  43. list_for_each (runp, &b->child_list)
  44. {
  45. struct fork_handler *curp;
  46. curp = list_entry (runp, struct fork_handler, list);
  47. curp->handler ();
  48. }
  49. __libc_lock_init (b->lock);
  50. } else {
  51. __funlockfilelist();
  52. __pthread_once_fork_parent();
  53. /* Run the handlers registered for the parent. */
  54. list_for_each (runp, &b->parent_list)
  55. {
  56. struct fork_handler *curp;
  57. curp = list_entry (runp, struct fork_handler, list);
  58. curp->handler ();
  59. }
  60. __libc_lock_unlock (b->lock);
  61. }
  62. return pid;
  63. }
  64. /* psm: have no idea why these are here, sjhill? */
  65. #if 0 /*def 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