ptfork.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. extern int __libc_fork(void);
  69. pid_t __fork(void)
  70. {
  71. pid_t pid;
  72. struct handler_list * prepare, * child, * parent;
  73. pthread_mutex_lock(&pthread_atfork_lock);
  74. prepare = pthread_atfork_prepare;
  75. child = pthread_atfork_child;
  76. parent = pthread_atfork_parent;
  77. pthread_mutex_unlock(&pthread_atfork_lock);
  78. pthread_call_handlers(prepare);
  79. pid = __libc_fork();
  80. if (pid == 0) {
  81. __pthread_reset_main_thread();
  82. __fresetlockfiles();
  83. pthread_call_handlers(child);
  84. } else {
  85. pthread_call_handlers(parent);
  86. }
  87. return pid;
  88. }
  89. weak_alias (__fork, fork);
  90. pid_t __vfork(void)
  91. {
  92. return __fork();
  93. }
  94. weak_alias (__vfork, vfork);