rthread_fork.c revision 1.5
1/*	$OpenBSD: rthread_fork.c,v 1.5 2012/08/22 22:34:57 matthew Exp $ */
2
3/*
4 * Copyright (c) 2008 Kurt Miller <kurt@openbsd.org>
5 * Copyright (c) 2008 Philip Guenther <guenther@gmail.com>
6 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Neither the name of the author nor the names of any co-contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: /repoman/r/ncvs/src/lib/libc_r/uthread/uthread_atfork.c,v 1.1 2004/12/10 03:36:45 grog Exp $
31 */
32
33#include <errno.h>
34#include <pthread.h>
35#include <stdlib.h>
36#include <unistd.h>
37
38#include "thread_private.h"	/* in libc/include */
39
40#include "rthread.h"
41
42struct rthread_atfork {
43	TAILQ_ENTRY(rthread_atfork) next;
44	void (*prepare)(void);
45	void (*parent)(void);
46	void (*child)(void);
47};
48
49static TAILQ_HEAD(atfork_listhead, rthread_atfork) _atfork_list =
50    TAILQ_HEAD_INITIALIZER(_atfork_list);
51
52static _spinlock_lock_t _atfork_lock = _SPINLOCK_UNLOCKED;
53
54pid_t   _thread_sys_fork(void);
55pid_t   _thread_sys_vfork(void);
56pid_t	_dofork(int);
57
58pid_t
59_dofork(int is_vfork)
60{
61	pthread_t me;
62	pid_t (*sys_fork)(void);
63	pid_t newid;
64
65	sys_fork = is_vfork ? &_thread_sys_vfork : &_thread_sys_fork;
66
67	if (!_threads_ready)
68		return sys_fork();
69
70	me = pthread_self();
71
72	/*
73	 * Protect important libc/ld.so critical areas across the fork call.
74	 * dlclose() will grab the atexit lock via __cxa_finalize() so lock
75	 * the dl_lock first. malloc()/free() can grab the arc4 lock so lock
76	 * malloc_lock first. Finally lock the bind_lock last so that any lazy
77	 * binding in the other locking functions can succeed.
78	 */
79
80#if defined(__ELF__) && defined(__PIC__)
81	_rthread_dl_lock(0);
82#endif
83
84	_thread_atexit_lock();
85	_thread_malloc_lock();
86	_thread_arc4_lock();
87
88#if defined(__ELF__) && defined(__PIC__)
89	_rthread_bind_lock(0);
90#endif
91
92	newid = sys_fork();
93
94#if defined(__ELF__) && defined(__PIC__)
95	_rthread_bind_lock(1);
96#endif
97
98	_thread_arc4_unlock();
99	_thread_malloc_unlock();
100	_thread_atexit_unlock();
101
102#if defined(__ELF__) && defined(__PIC__)
103	_rthread_dl_lock(1);
104#endif
105
106	if (newid == 0) {
107		/* update this thread's structure */
108		me->tid = getthrid();
109		me->donesem.lock = _SPINLOCK_UNLOCKED;
110		me->flags &= ~THREAD_DETACHED;
111		me->flags_lock = _SPINLOCK_UNLOCKED;
112
113		/* this thread is the initial thread for the new process */
114		_initial_thread = *me;
115
116		/* reinit the thread list */
117		LIST_INIT(&_thread_list);
118		LIST_INSERT_HEAD(&_thread_list, &_initial_thread, threads);
119		_thread_lock = _SPINLOCK_UNLOCKED;
120
121		/* single threaded now */
122		__isthreaded = 0;
123	}
124	return newid;
125}
126
127pid_t
128fork(void)
129{
130	struct rthread_atfork *p;
131	pid_t newid;
132
133	_spinlock(&_atfork_lock);
134	TAILQ_FOREACH_REVERSE(p, &_atfork_list, atfork_listhead, next)
135		if (p->prepare)
136			p->prepare();
137	newid = _dofork(0);
138	if (newid == 0) {
139		TAILQ_FOREACH(p, &_atfork_list, next)
140			if (p->child)
141				p->child();
142	} else {
143		TAILQ_FOREACH(p, &_atfork_list, next)
144			if (p->parent)
145				p->parent();
146	}
147	_spinunlock(&_atfork_lock);
148	return newid;
149}
150
151pid_t
152vfork(void)
153{
154	return _dofork(1);
155}
156
157int
158pthread_atfork(void (*prepare)(void), void (*parent)(void),
159    void (*child)(void))
160{
161	struct rthread_atfork *af;
162
163	if ((af = malloc(sizeof *af)) == NULL)
164		return (ENOMEM);
165
166	af->prepare = prepare;
167	af->parent = parent;
168	af->child = child;
169	_spinlock(&_atfork_lock);
170	TAILQ_INSERT_TAIL(&_atfork_list, af, next);
171	_spinunlock(&_atfork_lock);
172	return (0);
173}
174