1 /* $NetBSD: pthread_atfork.c,v 1.4 2005/06/01 05:30:54 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Nathan J. Williams. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 __RCSID("$NetBSD: pthread_atfork.c,v 1.4 2005/06/01 05:30:54 lukem Exp $"); 42 #endif /* LIBC_SCCS and not lint */ 43 44 #define __LIBC12_SOURCE__ 45 46 #include "namespace.h" 47 48 #include <errno.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 #include <sys/queue.h> 52 #include "reentrant.h" 53 54 #ifdef __weak_alias 55 __weak_alias(pthread_atfork, _pthread_atfork) 56 __weak_alias(fork, _fork) 57 #endif /* __weak_alias */ 58 59 pid_t __fork __P((void)); /* XXX */ 60 61 struct atfork_callback { 62 SIMPLEQ_ENTRY(atfork_callback) next; 63 void (*fn)(void); 64 }; 65 66 /* 67 * Hypothetically, we could protect the queues with a rwlock which is 68 * write-locked by pthread_atfork() and read-locked by fork(), but 69 * since the intended use of the functions is obtaining locks to hold 70 * across the fork, forking is going to be serialized anyway. 71 */ 72 static mutex_t atfork_lock = MUTEX_INITIALIZER; 73 SIMPLEQ_HEAD(atfork_callback_q, atfork_callback); 74 75 static struct atfork_callback_q prepareq = SIMPLEQ_HEAD_INITIALIZER(prepareq); 76 static struct atfork_callback_q parentq = SIMPLEQ_HEAD_INITIALIZER(parentq); 77 static struct atfork_callback_q childq = SIMPLEQ_HEAD_INITIALIZER(childq); 78 79 int 80 pthread_atfork(void (*prepare)(void), void (*parent)(void), 81 void (*child)(void)) 82 { 83 struct atfork_callback *newprepare, *newparent, *newchild; 84 85 newprepare = newparent = newchild = NULL; 86 87 if (prepare != NULL) { 88 newprepare = malloc(sizeof(struct atfork_callback)); 89 if (newprepare == NULL) 90 return ENOMEM; 91 newprepare->fn = prepare; 92 } 93 94 if (parent != NULL) { 95 newparent = malloc(sizeof(struct atfork_callback)); 96 if (newparent == NULL) { 97 if (newprepare != NULL) 98 free(newprepare); 99 return ENOMEM; 100 } 101 newparent->fn = parent; 102 } 103 104 if (child != NULL) { 105 newchild = malloc(sizeof(struct atfork_callback)); 106 if (newchild == NULL) { 107 if (newprepare != NULL) 108 free(newprepare); 109 if (newparent != NULL) 110 free(newparent); 111 return ENOMEM; 112 } 113 newchild->fn = child; 114 } 115 116 mutex_lock(&atfork_lock); 117 /* 118 * The order in which the functions are called is specified as 119 * LIFO for the prepare handler and FIFO for the others; insert 120 * at the head and tail as appropriate so that SIMPLEQ_FOREACH() 121 * produces the right order. 122 */ 123 if (prepare) 124 SIMPLEQ_INSERT_HEAD(&prepareq, newprepare, next); 125 if (parent) 126 SIMPLEQ_INSERT_TAIL(&parentq, newparent, next); 127 if (child) 128 SIMPLEQ_INSERT_TAIL(&childq, newchild, next); 129 mutex_unlock(&atfork_lock); 130 131 return 0; 132 } 133 134 pid_t 135 fork(void) 136 { 137 struct atfork_callback *iter; 138 pid_t ret; 139 140 mutex_lock(&atfork_lock); 141 SIMPLEQ_FOREACH(iter, &prepareq, next) 142 (*iter->fn)(); 143 144 ret = __fork(); 145 146 if (ret != 0) { 147 /* 148 * We are the parent. It doesn't matter here whether 149 * the fork call succeeded or failed. 150 */ 151 SIMPLEQ_FOREACH(iter, &parentq, next) 152 (*iter->fn)(); 153 mutex_unlock(&atfork_lock); 154 } else { 155 /* We are the child */ 156 SIMPLEQ_FOREACH(iter, &childq, next) 157 (*iter->fn)(); 158 /* 159 * Note: We are explicitly *not* unlocking 160 * atfork_lock. Unlocking atfork_lock is problematic, 161 * because if any threads in the parent blocked on it 162 * between the initial lock and the fork() syscall, 163 * unlocking in the child will try to schedule 164 * threads, and either the internal mutex interlock or 165 * the runqueue spinlock could have been held at the 166 * moment of fork(). Since the other threads do not 167 * exist in this process, the spinlock will never be 168 * unlocked, and we would wedge. 169 * Instead, we reinitialize atfork_lock, since we know 170 * that the state of the atfork lists is consistent here, 171 * and that there are no other threads to be affected by 172 * the forcible cleaning of the queue. 173 * This permits double-forking to work, although 174 * it requires knowing that it's "safe" to initialize 175 * a locked mutex in this context. 176 * 177 * The problem exists for users of this interface, 178 * too, since the intented use of pthread_atfork() is 179 * to acquire locks across the fork call to ensure 180 * that the child sees consistent state. There's not 181 * much that can usefully be done in a child handler, 182 * and conventional wisdom discourages using them, but 183 * they're part of the interface, so here we are... 184 */ 185 mutex_init(&atfork_lock, NULL); 186 } 187 188 return ret; 189 } 190