lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | |
| 15 | /* mods for uClibc: removed strong alias and defined funcs properly */ |
| 16 | |
| 17 | /* The "atfork" stuff */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #ifdef __ARCH_USE_MMU__ |
| 22 | |
| 23 | #include <bits/uClibc_mutex.h> |
| 24 | #include <stddef.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include "pthread.h" |
| 28 | #include "internals.h" |
| 29 | |
| 30 | struct handler_list { |
| 31 | void (*handler)(void); |
| 32 | struct handler_list * next; |
| 33 | }; |
| 34 | |
| 35 | static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER; |
| 36 | static struct handler_list * pthread_atfork_prepare = NULL; |
| 37 | static struct handler_list * pthread_atfork_parent = NULL; |
| 38 | static struct handler_list * pthread_atfork_child = NULL; |
| 39 | |
| 40 | #ifdef __MALLOC__ |
| 41 | __UCLIBC_MUTEX_EXTERN(__malloc_heap_lock); |
| 42 | __UCLIBC_MUTEX_EXTERN(__malloc_sbrk_lock); |
| 43 | #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__ |
| 44 | __UCLIBC_MUTEX_EXTERN(__malloc_mmb_heap_lock); |
| 45 | #endif |
| 46 | #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__) |
| 47 | __UCLIBC_MUTEX_EXTERN(__malloc_lock); |
| 48 | #endif |
| 49 | |
| 50 | static void pthread_insert_list(struct handler_list ** list, |
| 51 | void (*handler)(void), |
| 52 | struct handler_list * newlist, |
| 53 | int at_end) |
| 54 | { |
| 55 | if (handler == NULL) return; |
| 56 | if (at_end) { |
| 57 | while(*list != NULL) list = &((*list)->next); |
| 58 | } |
| 59 | newlist->handler = handler; |
| 60 | newlist->next = *list; |
| 61 | *list = newlist; |
| 62 | } |
| 63 | |
| 64 | struct handler_list_block { |
| 65 | struct handler_list prepare, parent, child; |
| 66 | }; |
| 67 | |
| 68 | int pthread_atfork(void (*prepare)(void), |
| 69 | void (*parent)(void), |
| 70 | void (*child)(void)) |
| 71 | { |
| 72 | struct handler_list_block * block = |
| 73 | (struct handler_list_block *) malloc(sizeof(struct handler_list_block)); |
| 74 | if (block == NULL) return ENOMEM; |
| 75 | __pthread_mutex_lock(&pthread_atfork_lock); |
| 76 | /* "prepare" handlers are called in LIFO */ |
| 77 | pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0); |
| 78 | /* "parent" handlers are called in FIFO */ |
| 79 | pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1); |
| 80 | /* "child" handlers are called in FIFO */ |
| 81 | pthread_insert_list(&pthread_atfork_child, child, &block->child, 1); |
| 82 | __pthread_mutex_unlock(&pthread_atfork_lock); |
| 83 | return 0; |
| 84 | } |
| 85 | /*strong_alias (__pthread_atfork, pthread_atfork)*/ |
| 86 | |
| 87 | static __inline__ void pthread_call_handlers(struct handler_list * list) |
| 88 | { |
| 89 | for (/*nothing*/; list != NULL; list = list->next) (list->handler)(); |
| 90 | } |
| 91 | |
| 92 | void __pthread_once_fork_prepare(void); |
| 93 | void __pthread_once_fork_child(void); |
| 94 | void __pthread_once_fork_parent(void); |
| 95 | |
| 96 | extern __typeof(fork) __libc_fork; |
| 97 | |
| 98 | pid_t __fork(void) attribute_hidden; |
| 99 | pid_t __fork(void) |
| 100 | { |
| 101 | pid_t pid; |
| 102 | struct handler_list * prepare, * child, * parent; |
| 103 | |
| 104 | __pthread_mutex_lock(&pthread_atfork_lock); |
| 105 | prepare = pthread_atfork_prepare; |
| 106 | child = pthread_atfork_child; |
| 107 | parent = pthread_atfork_parent; |
| 108 | pthread_call_handlers(prepare); |
| 109 | |
| 110 | __pthread_once_fork_prepare(); |
| 111 | #ifdef __MALLOC__ |
| 112 | __pthread_mutex_lock(&__malloc_sbrk_lock); |
| 113 | __pthread_mutex_lock(&__malloc_heap_lock); |
| 114 | #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__ |
| 115 | __pthread_mutex_lock(&__malloc_mmb_heap_lock); |
| 116 | #endif |
| 117 | #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__) |
| 118 | __pthread_mutex_lock(&__malloc_lock); |
| 119 | #endif |
| 120 | |
| 121 | pid = __libc_fork(); |
| 122 | if (pid == 0) { |
| 123 | #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__) |
| 124 | __libc_lock_init_recursive(__malloc_lock); |
| 125 | #elif defined(__MALLOC__) |
| 126 | #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__ |
| 127 | __libc_lock_init_adaptive(__malloc_mmb_heap_lock); |
| 128 | #endif |
| 129 | __libc_lock_init_adaptive(__malloc_heap_lock); |
| 130 | __libc_lock_init(__malloc_sbrk_lock); |
| 131 | #endif |
| 132 | __libc_lock_init_adaptive(pthread_atfork_lock); |
| 133 | __pthread_reset_main_thread(); |
| 134 | __fresetlockfiles(); |
| 135 | __pthread_once_fork_child(); |
| 136 | pthread_call_handlers(child); |
| 137 | } else { |
| 138 | #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__) |
| 139 | __pthread_mutex_unlock(&__malloc_lock); |
| 140 | #elif defined(__MALLOC__) |
| 141 | #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__ |
| 142 | __pthread_mutex_unlock(&__malloc_mmb_heap_lock); |
| 143 | #endif |
| 144 | __pthread_mutex_unlock(&__malloc_heap_lock); |
| 145 | __pthread_mutex_unlock(&__malloc_sbrk_lock); |
| 146 | #endif |
| 147 | __pthread_mutex_unlock(&pthread_atfork_lock); |
| 148 | __pthread_once_fork_parent(); |
| 149 | pthread_call_handlers(parent); |
| 150 | } |
| 151 | return pid; |
| 152 | } |
| 153 | strong_alias(__fork,fork) |
| 154 | |
| 155 | pid_t vfork(void) |
| 156 | { |
| 157 | return __fork(); |
| 158 | } |
| 159 | |
| 160 | #else |
| 161 | |
| 162 | /* We can't support pthread_atfork without MMU, since we don't have |
| 163 | fork(), and we can't offer the correct semantics for vfork(). */ |
| 164 | int pthread_atfork(void (*prepare)(void), |
| 165 | void (*parent)(void), |
| 166 | void (*child)(void)) |
| 167 | { |
| 168 | /* ENOMEM is probably pushing it a little bit. |
| 169 | Take it as `no *virtual* memory' :-) */ |
| 170 | errno = ENOMEM; |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | #endif |