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