]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libkse/thread/thr_fork.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libkse / thread / thr_fork.c
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <errno.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <pthread.h>
37 #include <spinlock.h>
38 #include <sys/signalvar.h>
39
40 #include "libc_private.h"
41 #include "thr_private.h"
42
43 LT10_COMPAT_PRIVATE(_fork);
44 LT10_COMPAT_DEFAULT(fork);
45
46 __weak_reference(_fork, fork);
47
48 pid_t
49 _fork(void)
50 {
51         sigset_t sigset, oldset;
52         struct pthread *curthread;
53         struct pthread_atfork *af;
54         pid_t ret;
55         int errsave;
56
57         curthread = _get_curthread();
58
59         if (!_kse_isthreaded()) {
60                 SIGFILLSET(sigset);
61                 __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
62                 ret = __sys_fork();
63                 if (ret == 0)
64                         /* Child */
65                         __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask,
66                             NULL);
67                 else
68                         __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
69                 return (ret);
70         }
71
72         /*
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.
78          */
79         if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
80                 SIGFILLSET(sigset);
81                 __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
82         }
83
84         _pthread_mutex_lock(&_thr_atfork_mutex);
85
86         /* Run down atfork prepare handlers. */
87         TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
88                 if (af->prepare != NULL)
89                         af->prepare();
90         }
91
92         /* Fork a new process: */
93         if (_kse_isthreaded() != 0) {
94                 _malloc_prefork();
95         }
96         if ((ret = __sys_fork()) == 0) {
97                 /* Child process */
98                 errsave = errno; 
99
100                 /* Kernel signal mask is restored in _kse_single_thread */
101                 _kse_single_thread(curthread);
102
103                 /* Run down atfork child handlers. */
104                 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
105                         if (af->child != NULL)
106                                 af->child();
107                 }
108                 _thr_mutex_reinit(&_thr_atfork_mutex);
109         } else {
110                 if (_kse_isthreaded() != 0) {
111                         _malloc_postfork();
112                 }
113                 errsave = errno; 
114                 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
115                         __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
116                 }
117                 /* Run down atfork parent handlers. */
118                 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
119                         if (af->parent != NULL)
120                                 af->parent();
121                 }
122                 _pthread_mutex_unlock(&_thr_atfork_mutex);
123         }
124         errno = errsave;
125
126         /* Return the process ID: */
127         return (ret);
128 }