2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/signalvar.h>
40 #include "libc_private.h"
41 #include "thr_private.h"
43 LT10_COMPAT_PRIVATE(_fork);
44 LT10_COMPAT_DEFAULT(fork);
46 __weak_reference(_fork, fork);
51 sigset_t sigset, oldset;
52 struct pthread *curthread;
53 struct pthread_atfork *af;
57 curthread = _get_curthread();
59 if (!_kse_isthreaded()) {
61 __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
65 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask,
68 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
73 * Masks all signals until we reach a safe point in
74 * _kse_single_thread, and the signal masks will be
75 * restored in that function, for M:N thread, all
76 * signals were already masked in kernel atomically,
77 * we only need to do this for bound thread.
79 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
81 __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
84 _pthread_mutex_lock(&_thr_atfork_mutex);
86 /* Run down atfork prepare handlers. */
87 TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
88 if (af->prepare != NULL)
92 /* Fork a new process: */
93 if (_kse_isthreaded() != 0) {
96 if ((ret = __sys_fork()) == 0) {
100 /* Kernel signal mask is restored in _kse_single_thread */
101 _kse_single_thread(curthread);
103 /* Run down atfork child handlers. */
104 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
105 if (af->child != NULL)
108 _thr_mutex_reinit(&_thr_atfork_mutex);
110 if (_kse_isthreaded() != 0) {
114 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
115 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
117 /* Run down atfork parent handlers. */
118 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
119 if (af->parent != NULL)
122 _pthread_mutex_unlock(&_thr_atfork_mutex);
126 /* Return the process ID: */