ptfork.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifdef __ARCH_USE_MMU__
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include "pthread.h"
  22. #include "internals.h"
  23. struct handler_list {
  24. void (*handler)(void);
  25. struct handler_list * next;
  26. };
  27. static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
  28. static struct handler_list * pthread_atfork_prepare = NULL;
  29. static struct handler_list * pthread_atfork_parent = NULL;
  30. static struct handler_list * pthread_atfork_child = NULL;
  31. static void pthread_insert_list(struct handler_list ** list,
  32. void (*handler)(void),
  33. struct handler_list * newlist,
  34. int at_end)
  35. {
  36. if (handler == NULL) return;
  37. if (at_end) {
  38. while(*list != NULL) list = &((*list)->next);
  39. }
  40. newlist->handler = handler;
  41. newlist->next = *list;
  42. *list = newlist;
  43. }
  44. struct handler_list_block {
  45. struct handler_list prepare, parent, child;
  46. };
  47. int pthread_atfork(void (*prepare)(void),
  48. void (*parent)(void),
  49. void (*child)(void))
  50. {
  51. struct handler_list_block * block =
  52. (struct handler_list_block *) malloc(sizeof(struct handler_list_block));
  53. if (block == NULL) return ENOMEM;
  54. __pthread_mutex_lock(&pthread_atfork_lock);
  55. /* "prepare" handlers are called in LIFO */
  56. pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
  57. /* "parent" handlers are called in FIFO */
  58. pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
  59. /* "child" handlers are called in FIFO */
  60. pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
  61. __pthread_mutex_unlock(&pthread_atfork_lock);
  62. return 0;
  63. }
  64. //strong_alias (__pthread_atfork, pthread_atfork)
  65. static __inline__ void pthread_call_handlers(struct handler_list * list)
  66. {
  67. for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
  68. }
  69. extern __typeof(fork) __libc_fork;
  70. pid_t __fork(void) attribute_hidden;
  71. pid_t __fork(void)
  72. {
  73. pid_t pid;
  74. struct handler_list * prepare, * child, * parent;
  75. __pthread_mutex_lock(&pthread_atfork_lock);
  76. prepare = pthread_atfork_prepare;
  77. child = pthread_atfork_child;
  78. parent = pthread_atfork_parent;
  79. __pthread_mutex_unlock(&pthread_atfork_lock);
  80. pthread_call_handlers(prepare);
  81. pid = __libc_fork();
  82. if (pid == 0) {
  83. __pthread_reset_main_thread();
  84. __fresetlockfiles();
  85. pthread_call_handlers(child);
  86. } else {
  87. pthread_call_handlers(parent);
  88. }
  89. return pid;
  90. }
  91. strong_alias(__fork,fork)
  92. pid_t vfork(void)
  93. {
  94. return __fork();
  95. }
  96. #else
  97. /* We can't support pthread_atfork without MMU, since we don't have
  98. fork(), and we can't offer the correct semantics for vfork(). */
  99. int pthread_atfork(void (*prepare)(void),
  100. void (*parent)(void),
  101. void (*child)(void))
  102. {
  103. /* ENOMEM is probably pushing it a little bit.
  104. Take it as `no *virtual* memory' :-) */
  105. errno = ENOMEM;
  106. return -1;
  107. }
  108. #endif