rthread_fork.c revision 1.10
1/*	$OpenBSD: rthread_fork.c,v 1.10 2013/11/29 16:27:40 guenther 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#if defined(__ELF__)
34#include <sys/types.h>
35#include <sys/exec_elf.h>
36#pragma weak _DYNAMIC
37#endif
38
39#include <errno.h>
40#include <pthread.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <signal.h>
44
45#include "thread_private.h"	/* in libc/include */
46
47#include "rthread.h"
48
49struct rthread_atfork {
50	TAILQ_ENTRY(rthread_atfork) next;
51	void (*prepare)(void);
52	void (*parent)(void);
53	void (*child)(void);
54};
55
56static TAILQ_HEAD(atfork_listhead, rthread_atfork) _atfork_list =
57    TAILQ_HEAD_INITIALIZER(_atfork_list);
58
59static struct _spinlock _atfork_lock = _SPINLOCK_UNLOCKED;
60
61pid_t   _thread_sys_fork(void);
62pid_t   _thread_sys_vfork(void);
63pid_t	_dofork(int);
64
65pid_t
66_dofork(int is_vfork)
67{
68	pthread_t me;
69	pid_t (*sys_fork)(void);
70	pid_t newid;
71#if defined(__ELF__)
72	sigset_t nmask, omask;
73#endif
74
75	sys_fork = is_vfork ? &_thread_sys_vfork : &_thread_sys_fork;
76
77	if (!_threads_ready)
78		return sys_fork();
79
80	me = pthread_self();
81
82	/*
83	 * Protect important libc/ld.so critical areas across the fork call.
84	 * dlclose() will grab the atexit lock via __cxa_finalize() so lock
85	 * the dl_lock first. malloc()/free() can grab the arc4 lock so lock
86	 * malloc_lock first. Finally lock the bind_lock last so that any lazy
87	 * binding in the other locking functions can succeed.
88	 */
89
90#if defined(__ELF__)
91	if (_DYNAMIC)
92		_rthread_dl_lock(0);
93#endif
94
95	_thread_atexit_lock();
96	_thread_malloc_lock();
97	_thread_arc4_lock();
98
99#if defined(__ELF__)
100	if (_DYNAMIC) {
101		sigfillset(&nmask);
102		_thread_sys_sigprocmask(SIG_BLOCK, &nmask, &omask);
103		_rthread_bind_lock(0);
104	}
105#endif
106
107	newid = sys_fork();
108
109#if defined(__ELF__)
110	if (_DYNAMIC) {
111		_rthread_bind_lock(1);
112		_thread_sys_sigprocmask(SIG_SETMASK, &omask, NULL);
113	}
114#endif
115
116	_thread_arc4_unlock();
117	_thread_malloc_unlock();
118	_thread_atexit_unlock();
119
120#if defined(__ELF__)
121	if (_DYNAMIC)
122		_rthread_dl_lock(1);
123#endif
124
125	if (newid == 0) {
126		/* update this thread's structure */
127		me->tid = getthrid();
128		me->donesem.lock = _SPINLOCK_UNLOCKED_ASSIGN;
129		me->flags &= ~THREAD_DETACHED;
130		me->flags_lock = _SPINLOCK_UNLOCKED_ASSIGN;
131
132		/* this thread is the initial thread for the new process */
133		me->flags |= THREAD_ORIGINAL;
134
135		/* reinit the thread list */
136		LIST_INIT(&_thread_list);
137		LIST_INSERT_HEAD(&_thread_list, me, threads);
138		_thread_lock = _SPINLOCK_UNLOCKED_ASSIGN;
139
140		/* single threaded now */
141		__isthreaded = 0;
142	}
143	return newid;
144}
145
146pid_t
147fork(void)
148{
149	struct rthread_atfork *p;
150	pid_t newid;
151
152	_spinlock(&_atfork_lock);
153	TAILQ_FOREACH_REVERSE(p, &_atfork_list, atfork_listhead, next)
154		if (p->prepare)
155			p->prepare();
156	newid = _dofork(0);
157	if (newid == 0) {
158		TAILQ_FOREACH(p, &_atfork_list, next)
159			if (p->child)
160				p->child();
161	} else {
162		TAILQ_FOREACH(p, &_atfork_list, next)
163			if (p->parent)
164				p->parent();
165	}
166	_spinunlock(&_atfork_lock);
167	return newid;
168}
169
170pid_t
171vfork(void)
172{
173	return _dofork(1);
174}
175
176int
177pthread_atfork(void (*prepare)(void), void (*parent)(void),
178    void (*child)(void))
179{
180	struct rthread_atfork *af;
181
182	if ((af = malloc(sizeof *af)) == NULL)
183		return (ENOMEM);
184
185	af->prepare = prepare;
186	af->parent = parent;
187	af->child = child;
188	_spinlock(&_atfork_lock);
189	TAILQ_INSERT_TAIL(&_atfork_list, af, next);
190	_spinunlock(&_atfork_lock);
191	return (0);
192}
193