ptfork.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /* mods for uClibc: removed strong alias and defined funcs properly */
  15. /* The "atfork" stuff */
  16. #include <errno.h>
  17. #include <stddef.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include "pthread.h"
  21. #include "internals.h"
  22. struct handler_list {
  23. void (*handler)(void);
  24. struct handler_list * next;
  25. };
  26. static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
  27. static struct handler_list * pthread_atfork_prepare = NULL;
  28. static struct handler_list * pthread_atfork_parent = NULL;
  29. static struct handler_list * pthread_atfork_child = NULL;
  30. static void pthread_insert_list(struct handler_list ** list,
  31. void (*handler)(void),
  32. struct handler_list * newlist,
  33. int at_end)
  34. {
  35. if (handler == NULL) return;
  36. if (at_end) {
  37. while(*list != NULL) list = &((*list)->next);
  38. }
  39. newlist->handler = handler;
  40. newlist->next = *list;
  41. *list = newlist;
  42. }
  43. struct handler_list_block {
  44. struct handler_list prepare, parent, child;
  45. };
  46. int pthread_atfork(void (*prepare)(void),
  47. void (*parent)(void),
  48. void (*child)(void))
  49. {
  50. struct handler_list_block * block =
  51. (struct handler_list_block *) malloc(sizeof(struct handler_list_block));
  52. if (block == NULL) return ENOMEM;
  53. pthread_mutex_lock(&pthread_atfork_lock);
  54. /* "prepare" handlers are called in LIFO */
  55. pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
  56. /* "parent" handlers are called in FIFO */
  57. pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
  58. /* "child" handlers are called in FIFO */
  59. pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
  60. pthread_mutex_unlock(&pthread_atfork_lock);
  61. return 0;
  62. }
  63. //strong_alias (__pthread_atfork, pthread_atfork)
  64. static inline void pthread_call_handlers(struct handler_list * list)
  65. {
  66. for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
  67. }
  68. #ifdef __ARCH_HAS_MMU__
  69. extern int __libc_fork(void);
  70. pid_t __fork(void)
  71. {
  72. pid_t pid;
  73. struct handler_list * prepare, * child, * parent;
  74. pthread_mutex_lock(&pthread_atfork_lock);
  75. prepare = pthread_atfork_prepare;
  76. child = pthread_atfork_child;
  77. parent = pthread_atfork_parent;
  78. pthread_mutex_unlock(&pthread_atfork_lock);
  79. pthread_call_handlers(prepare);
  80. pid = __libc_fork();
  81. if (pid == 0) {
  82. __pthread_reset_main_thread();
  83. __fresetlockfiles();
  84. pthread_call_handlers(child);
  85. } else {
  86. pthread_call_handlers(parent);
  87. }
  88. return pid;
  89. }
  90. weak_alias (__fork, fork);
  91. pid_t __vfork(void)
  92. {
  93. return __fork();
  94. }
  95. weak_alias (__vfork, vfork);
  96. #else
  97. pid_t __vfork(void)
  98. {
  99. pid_t pid;
  100. struct handler_list * prepare, * child, * parent;
  101. pthread_mutex_lock(&pthread_atfork_lock);
  102. prepare = pthread_atfork_prepare;
  103. child = pthread_atfork_child;
  104. parent = pthread_atfork_parent;
  105. pthread_mutex_unlock(&pthread_atfork_lock);
  106. pthread_call_handlers(prepare);
  107. pid = __libc_vfork();
  108. if (pid == 0) {
  109. __pthread_reset_main_thread();
  110. __fresetlockfiles();
  111. pthread_call_handlers(child);
  112. } else {
  113. pthread_call_handlers(parent);
  114. }
  115. return pid;
  116. }
  117. weak_alias (__vfork, vfork);
  118. #endif