ptfork.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /* psm: have no idea why these are here, sjhill? */
  66. #if 0 /*def SHARED*/
  67. pid_t __fork (void)
  68. {
  69. return __libc_fork ();
  70. }
  71. weak_alias (__fork, fork)
  72. pid_t __vfork(void)
  73. {
  74. return __libc_fork ();
  75. }
  76. weak_alias (__vfork, vfork)
  77. #endif