]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc_r/uthread/uthread_fork.c
This commit was generated by cvs2svn to compensate for changes in r161389,
[FreeBSD/FreeBSD.git] / lib / libc_r / uthread / uthread_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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 #include <sys/param.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <pthread.h>
41 #include "pthread_private.h"
42
43 static void     free_thread_resources(struct pthread *thread);
44
45 __weak_reference(_fork, fork);
46
47 pid_t
48 _fork(void)
49 {
50         struct pthread  *curthread = _get_curthread();
51         struct pthread_atfork *af;
52         int             i, flags, use_deadlist = 0;
53         pid_t           ret;
54         pthread_t       pthread;
55         pthread_t       pthread_save;
56
57         /*
58          * Defer signals to protect the scheduling queues from access
59          * by the signal handler:
60          */
61         _thread_kern_sig_defer();
62
63         _pthread_mutex_lock(&_atfork_mutex);
64
65         /* Run down atfork prepare handlers. */
66         TAILQ_FOREACH_REVERSE(af, &_atfork_list, atfork_head, qe) {
67                 if (af->prepare != NULL)
68                         af->prepare();
69         }
70
71         /* Fork a new process: */
72         if ((ret = __sys_fork()) != 0) {
73                 /* Run down atfork parent handlers. */
74                 TAILQ_FOREACH(af, &_atfork_list, qe) {
75                         if (af->parent != NULL)
76                         af->parent();
77                 }
78                 _pthread_mutex_unlock(&_atfork_mutex);
79
80         } else {
81                 /* Close the pthread kernel pipe: */
82                 __sys_close(_thread_kern_pipe[0]);
83                 __sys_close(_thread_kern_pipe[1]);
84
85                 /* Reset signals pending for the running thread: */
86                 sigemptyset(&curthread->sigpend);
87
88                 /*
89                  * Create a pipe that is written to by the signal handler to
90                  * prevent signals being missed in calls to
91                  * __sys_select: 
92                  */
93                 if (__sys_pipe(_thread_kern_pipe) != 0) {
94                         /* Cannot create pipe, so abort: */
95                         PANIC("Cannot create pthread kernel pipe for forked process");
96                 }
97                 /* Get the flags for the read pipe: */
98                 else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) {
99                         /* Abort this application: */
100                         abort();
101                 }
102                 /* Make the read pipe non-blocking: */
103                 else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) {
104                         /* Abort this application: */
105                         abort();
106                 }
107                 /* Get the flags for the write pipe: */
108                 else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) {
109                         /* Abort this application: */
110                         abort();
111                 }
112                 /* Make the write pipe non-blocking: */
113                 else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) {
114                         /* Abort this application: */
115                         abort();
116                 }
117                 /* Reinitialize the GC mutex: */
118                 else if (_mutex_reinit(&_gc_mutex) != 0) {
119                         /* Abort this application: */
120                         PANIC("Cannot initialize GC mutex for forked process");
121                 }
122                 /* Reinitialize the GC condition variable: */
123                 else if (_cond_reinit(&_gc_cond) != 0) {
124                         /* Abort this application: */
125                         PANIC("Cannot initialize GC condvar for forked process");
126                 }
127                 /* Initialize the ready queue: */
128                 else if (_pq_init(&_readyq) != 0) {
129                         /* Abort this application: */
130                         PANIC("Cannot initialize priority ready queue.");
131                 } else {
132                         /*
133                          * Enter a loop to remove all threads other than
134                          * the running thread from the thread list:
135                          */
136                         if ((pthread = TAILQ_FIRST(&_thread_list)) == NULL) {
137                                 pthread = TAILQ_FIRST(&_dead_list);
138                                 use_deadlist = 1;
139                         }
140                         while (pthread != NULL) {
141                                 /* Save the thread to be freed: */
142                                 pthread_save = pthread;
143
144                                 /*
145                                  * Advance to the next thread before
146                                  * destroying the current thread:
147                                  */
148                                 if (use_deadlist != 0)
149                                         pthread = TAILQ_NEXT(pthread, dle);
150                                 else
151                                         pthread = TAILQ_NEXT(pthread, tle);
152
153                                 /* Make sure this isn't the running thread: */
154                                 if (pthread_save != curthread) {
155                                         /*
156                                          * Remove this thread from the
157                                          * appropriate list:
158                                          */
159                                         if (use_deadlist != 0)
160                                                 TAILQ_REMOVE(&_thread_list,
161                                                     pthread_save, dle);
162                                         else
163                                                 TAILQ_REMOVE(&_thread_list,
164                                                     pthread_save, tle);
165
166                                         free_thread_resources(pthread_save);
167                                 }
168
169                                 /*
170                                  * Switch to the deadlist when the active
171                                  * thread list has been consumed.  This can't
172                                  * be at the top of the loop because it is
173                                  * used to determine to which list the thread
174                                  * belongs (when it is removed from the list).
175                                  */
176                                 if (pthread == NULL) {
177                                         pthread = TAILQ_FIRST(&_dead_list);
178                                         use_deadlist = 1;
179                                 }
180                         }
181
182                         /* Treat the current thread as the initial thread: */
183                         _thread_initial = curthread;
184
185                         /* Re-init the dead thread list: */
186                         TAILQ_INIT(&_dead_list);
187
188                         /* Re-init the waiting and work queues. */
189                         TAILQ_INIT(&_waitingq);
190                         TAILQ_INIT(&_workq);
191
192                         /* Re-init the threads mutex queue: */
193                         TAILQ_INIT(&curthread->mutexq);
194
195                         /* No spinlocks yet: */
196                         _spinblock_count = 0;
197
198                         /* Don't queue signals yet: */
199                         _queue_signals = 0;
200
201                         /* Initialize the scheduling switch hook routine: */
202                         _sched_switch_hook = NULL;
203
204                         /* Clear out any locks in the file descriptor table: */
205                         for (i = 0; i < _thread_dtablesize; i++) {
206                                 if (_thread_fd_table[i] != NULL) {
207                                         /* Initialise the file locks: */
208                                         memset(&_thread_fd_table[i]->lock, 0,
209                                             sizeof(_thread_fd_table[i]->lock));
210                                         _thread_fd_table[i]->r_owner = NULL;
211                                         _thread_fd_table[i]->w_owner = NULL;
212                                         _thread_fd_table[i]->r_fname = NULL;
213                                         _thread_fd_table[i]->w_fname = NULL;
214                                         _thread_fd_table[i]->r_lineno = 0;;
215                                         _thread_fd_table[i]->w_lineno = 0;;
216                                         _thread_fd_table[i]->r_lockcount = 0;;
217                                         _thread_fd_table[i]->w_lockcount = 0;;
218
219                                         /* Initialise the read/write queues: */
220                                         TAILQ_INIT(&_thread_fd_table[i]->r_queue);
221                                         TAILQ_INIT(&_thread_fd_table[i]->w_queue);
222                                 }
223                         }
224                 }
225                 /* Run down atfork child handlers. */
226                 TAILQ_FOREACH(af, &_atfork_list, qe) {
227                         if (af->child != NULL)
228                                 af->child();
229                 }
230                 _mutex_reinit(&_atfork_mutex);
231         }
232
233
234         /*
235          * Undefer and handle pending signals, yielding if necessary:
236          */
237         _thread_kern_sig_undefer();
238
239         /* Return the process ID: */
240         return (ret);
241 }
242
243 static void
244 free_thread_resources(struct pthread *thread)
245 {
246
247         /* Check to see if the threads library allocated the stack. */
248         if ((thread->attr.stackaddr_attr == NULL) && (thread->stack != NULL)) {
249                 /*
250                  * Since this is being called from fork, we are currently single
251                  * threaded so there is no need to protect the call to
252                  * _thread_stack_free() with _gc_mutex.
253                  */
254                 _thread_stack_free(thread->stack, thread->attr.stacksize_attr,
255                     thread->attr.guardsize_attr);
256         }
257
258         if (thread->specific != NULL)
259                 free(thread->specific);
260
261         if (thread->poll_data.fds != NULL)
262                 free(thread->poll_data.fds);
263
264         free(thread);
265 }