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