rthread_fork.c revision 1.20
1/*	$OpenBSD: rthread_fork.c,v 1.20 2017/07/27 16:35:08 tedu Exp $ */
2
3/*
4 * Copyright (c) 2008 Kurt Miller <kurt@openbsd.org>
5 * Copyright (c) 2008 Philip Guenther <guenther@openbsd.org>
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#ifndef NO_PIC
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 <tib.h>
43#include <unistd.h>
44
45#include "thread_private.h"	/* in libc/include */
46
47#include "rthread.h"
48#include "rthread_cb.h"
49
50/* make {fork,vfork,getthrid} call _thread_sys_{fork,vfork,getthrid} */
51REDIRECT_SYSCALL(fork);
52REDIRECT_SYSCALL(vfork);
53REDIRECT_SYSCALL(getthrid);
54
55static pid_t
56_dofork(pid_t (*sys_fork)(void))
57{
58	pthread_t me;
59	pid_t newid;
60	int i;
61	extern int _post_threaded;
62
63	if (!_threads_ready)
64		return sys_fork();
65
66	me = pthread_self();
67
68	/*
69	 * Protect important libc/ld.so critical areas across the fork call.
70	 * dlclose() will grab the atexit lock via __cxa_finalize() so lock
71	 * the dl_lock first. malloc()/free() can use arc4random(), so lock
72	 * malloc_lock before arc4_lock
73	 */
74
75#ifndef NO_PIC
76	if (_DYNAMIC)
77		_rthread_dl_lock(0);
78#endif
79
80	_thread_atexit_lock();
81	for (i = 0; i < _MALLOC_MUTEXES; i++)
82		_thread_malloc_lock(i);
83	_thread_arc4_lock();
84
85	newid = sys_fork();
86
87	_thread_arc4_unlock();
88	if (newid == 0)
89		_thread_malloc_reinit();
90	else
91		for (i = 0; i < _MALLOC_MUTEXES; i++)
92			_thread_malloc_unlock(i);
93	_thread_atexit_unlock();
94
95	if (newid == 0) {
96		struct tib *tib = me->tib;
97#ifndef NO_PIC
98		/* reinitialize the lock in the child */
99		if (_DYNAMIC)
100			_rthread_dl_lock(2);
101#endif
102		/* update this thread's structure */
103		tib->tib_tid = getthrid();
104		me->donesem.lock = _SPINLOCK_UNLOCKED;
105		me->flags_lock = _SPINLOCK_UNLOCKED;
106
107		/* reinit the thread list */
108		LIST_INIT(&_thread_list);
109		LIST_INSERT_HEAD(&_thread_list, me, threads);
110		_thread_lock = _SPINLOCK_UNLOCKED;
111
112		/* single threaded now */
113		__isthreaded = 0;
114		_post_threaded = 1;
115	}
116#ifndef NO_PIC
117	else if (_DYNAMIC)
118		_rthread_dl_lock(1);
119#endif
120	return newid;
121}
122
123pid_t
124_thread_fork(void)
125{
126	return _dofork(&fork);
127}
128
129pid_t
130_thread_vfork(void)
131{
132	return _dofork(&vfork);
133}
134