c4662a8cad8d4b8e60fc2ea7906eaeef0c4a6a67
[dragonfly.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: src/lib/libc_r/uthread/uthread_fork.c,v 1.19.2.7 2002/10/22 14:44:03 fjoe Exp $
33  */
34
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 pid_t
44 _fork(void)
45 {
46         struct pthread  *curthread = _get_curthread();
47         struct pthread_atfork *af;
48         int             i;
49         pid_t           ret;
50         pthread_t       pthread;
51         pthread_t       pthread_save;
52
53         /*
54          * Defer signals to protect the scheduling queues from access
55          * by the signal handler:
56          */
57         _thread_kern_sig_defer();
58
59         _pthread_mutex_lock(&_atfork_mutex);
60         
61         /* Run down atfork prepare handlers. */
62         TAILQ_FOREACH_REVERSE(af, &_atfork_list, atfork_head, qe) {
63                 if (af->prepare != NULL)
64                         af->prepare();
65         }
66
67         /* Fork a new process: */
68         if ((ret = __sys_fork()) != 0) {
69                 /* Run down atfork parent handlers. */
70                 TAILQ_FOREACH(af, &_atfork_list, qe) {
71                         if (af->parent != NULL)
72                                 af->parent();
73                 }
74                 _pthread_mutex_unlock(&_atfork_mutex);
75         } else {
76                 /* Close the pthread kernel pipe: */
77                 __sys_close(_thread_kern_pipe[0]);
78                 __sys_close(_thread_kern_pipe[1]);
79
80                 /* Reset signals pending for the running thread: */
81                 sigemptyset(&curthread->sigpend);
82
83                 _thread_mksigpipe();
84
85                 /* Reinitialize the GC mutex: */
86                 if (_mutex_reinit(&_gc_mutex) != 0) {
87                         /* Abort this application: */
88                         PANIC("Cannot initialize GC mutex for forked process");
89                 }
90                 /* Reinitialize the GC condition variable: */
91                 else if (_cond_reinit(&_gc_cond) != 0) {
92                         /* Abort this application: */
93                         PANIC("Cannot initialize GC condvar for forked process");
94                 }
95                 /* Initialize the ready queue: */
96                 else if (_pq_init(&_readyq) != 0) {
97                         /* Abort this application: */
98                         PANIC("Cannot initialize priority ready queue.");
99                 } else {
100                         /*
101                          * Enter a loop to remove all threads other than
102                          * the running thread from the thread list:
103                          */
104                         pthread = TAILQ_FIRST(&_thread_list);
105                         while (pthread != NULL) {
106                                 /* Save the thread to be freed: */
107                                 pthread_save = pthread;
108
109                                 /*
110                                  * Advance to the next thread before
111                                  * destroying the current thread:
112                                  */
113                                 pthread = TAILQ_NEXT(pthread, tle);
114
115                                 /* Make sure this isn't the running thread: */
116                                 if (pthread_save != curthread) {
117                                         /* Remove this thread from the list: */
118                                         TAILQ_REMOVE(&_thread_list,
119                                             pthread_save, tle);
120
121                                         if (pthread_save->attr.stackaddr_attr ==
122                                             NULL && pthread_save->stack != NULL) {
123                                                 if (pthread_save->attr.stacksize_attr
124                                                     == PTHREAD_STACK_DEFAULT) {
125                                                         /*
126                                                          * Default-size stack.
127                                                          * Cache it:
128                                                          */
129                                                         struct stack    *spare_stack;
130
131                                                         spare_stack
132                                                             = (pthread_save->stack
133                                                             + PTHREAD_STACK_DEFAULT
134                                                             - sizeof(struct stack));
135                                                         SLIST_INSERT_HEAD(&_stackq,
136                                                             spare_stack, qe);
137                                                 } else
138                                                         /*
139                                                          * Free the stack of
140                                                          * the dead thread:
141                                                          */
142                                                         free(pthread_save->stack);
143                                         }
144
145                                         if (pthread_save->specific_data != NULL)
146                                                 free(pthread_save->specific_data);
147
148                                         if (pthread_save->poll_data.fds != NULL)
149                                                 free(pthread_save->poll_data.fds);
150
151                                         _rtld_free_tls(pthread_save->tcb);
152                                         free(pthread_save);
153                                 }
154                         }
155
156                         /* Treat the current thread as the initial thread: */
157                         _thread_initial = curthread;
158
159                         /* Re-init the dead thread list: */
160                         TAILQ_INIT(&_dead_list);
161
162                         /* Re-init the waiting and work queues. */
163                         TAILQ_INIT(&_waitingq);
164                         TAILQ_INIT(&_workq);
165
166                         /* Re-init the threads mutex queue: */
167                         TAILQ_INIT(&curthread->mutexq);
168
169                         /* No spinlocks yet: */
170                         _spinblock_count = 0;
171
172                         /* Don't queue signals yet: */
173                         _queue_signals = 0;
174
175                         /* Initialize the scheduling switch hook routine: */
176                         _sched_switch_hook = NULL;
177
178                         /* Clear out any locks in the file descriptor table: */
179                         for (i = 0; i < _thread_dtablesize; i++) {
180                                 if (_thread_fd_table[i] != NULL) {
181                                         /* Initialise the file locks: */
182                                         memset(&_thread_fd_table[i]->lock, 0,
183                                             sizeof(_thread_fd_table[i]->lock));
184                                         _thread_fd_table[i]->r_owner = NULL;
185                                         _thread_fd_table[i]->w_owner = NULL;
186                                         _thread_fd_table[i]->r_fname = NULL;
187                                         _thread_fd_table[i]->w_fname = NULL;
188                                         _thread_fd_table[i]->r_lineno = 0;
189                                         _thread_fd_table[i]->w_lineno = 0;
190                                         _thread_fd_table[i]->r_lockcount = 0;
191                                         _thread_fd_table[i]->w_lockcount = 0;
192
193                                         /* Initialise the read/write queues: */
194                                         TAILQ_INIT(&_thread_fd_table[i]->r_queue);
195                                         TAILQ_INIT(&_thread_fd_table[i]->w_queue);
196                                 }
197                         }
198                 }
199                 /* Run down atfork child handlers. */
200                 TAILQ_FOREACH(af, &_atfork_list, qe) {
201                         if (af->child != NULL)
202                                 af->child();
203                         }
204                 _mutex_reinit(&_atfork_mutex);
205         }
206
207         /*
208          * Undefer and handle pending signals, yielding if necessary:
209          */
210         _thread_kern_sig_undefer();
211
212         /* Return the process ID: */
213         return (ret);
214 }
215
216 __strong_reference(_fork, fork);