]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1994-2019 Free Software Foundation, Inc. |
478b92f0 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
478b92f0 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 8 | |
478b92f0 UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 RM |
17 | |
18 | #include <errno.h> | |
19 | #include <unistd.h> | |
20 | #include <hurd.h> | |
21 | #include <hurd/signal.h> | |
dc33bef3 | 22 | #include <hurd/threadvar.h> |
28f540f4 | 23 | #include <setjmp.h> |
8f480b4b | 24 | #include <thread_state.h> |
28f540f4 RM |
25 | #include <sysdep.h> /* For stack growth direction. */ |
26 | #include "set-hooks.h" | |
27 | #include <assert.h> | |
28 | #include "hurdmalloc.h" /* XXX */ | |
3ba7b383 | 29 | #include <tls.h> |
29d79486 | 30 | #include <malloc/malloc-internal.h> |
28f540f4 | 31 | |
6b87a564 UD |
32 | #undef __fork |
33 | ||
28f540f4 RM |
34 | |
35 | /* Things that want to be locked while forking. */ | |
3fe9de0d | 36 | symbol_set_declare (_hurd_fork_locks) |
28f540f4 RM |
37 | |
38 | ||
7b7bab13 ST |
39 | /* Application callbacks registered through pthread_atfork. */ |
40 | DEFINE_HOOK (_hurd_atfork_prepare_hook, (void)); | |
41 | DEFINE_HOOK (_hurd_atfork_child_hook, (void)); | |
42 | DEFINE_HOOK (_hurd_atfork_parent_hook, (void)); | |
43 | ||
28f540f4 RM |
44 | /* Things that want to be called before we fork, to prepare the parent for |
45 | task_create, when the new child task will inherit our address space. */ | |
46 | DEFINE_HOOK (_hurd_fork_prepare_hook, (void)); | |
47 | ||
48 | /* Things that want to be called when we are forking, with the above all | |
49 | locked. They are passed the task port of the child. The child process | |
50 | is all set up except for doing proc_child, and has no threads yet. */ | |
51 | DEFINE_HOOK (_hurd_fork_setup_hook, (void)); | |
52 | ||
53 | /* Things to be run in the child fork. */ | |
54 | DEFINE_HOOK (_hurd_fork_child_hook, (void)); | |
55 | ||
56 | /* Things to be run in the parent fork. */ | |
57 | DEFINE_HOOK (_hurd_fork_parent_hook, (void)); | |
58 | ||
59 | ||
60 | /* Clone the calling process, creating an exact copy. | |
61 | Return -1 for errors, 0 to the new process, | |
62 | and the process ID of the new process to the old process. */ | |
63 | pid_t | |
64 | __fork (void) | |
65 | { | |
66 | jmp_buf env; | |
67 | pid_t pid; | |
68 | size_t i; | |
69 | error_t err; | |
28f540f4 | 70 | struct hurd_sigstate *volatile ss; |
28f540f4 | 71 | |
7b7bab13 ST |
72 | RUN_HOOK (_hurd_atfork_prepare_hook, ()); |
73 | ||
28f540f4 | 74 | ss = _hurd_self_sigstate (); |
8f0c527e | 75 | __spin_lock (&ss->critical_section_lock); |
11872325 RM |
76 | |
77 | #undef LOSE | |
221dc560 | 78 | #define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */ |
28f540f4 RM |
79 | |
80 | if (! setjmp (env)) | |
81 | { | |
82 | process_t newproc; | |
83 | task_t newtask; | |
84 | thread_t thread, sigthread; | |
85 | mach_port_urefs_t thread_refs, sigthread_refs; | |
86 | struct machine_thread_state state; | |
87 | mach_msg_type_number_t statecount; | |
88 | mach_port_t *portnames = NULL; | |
89 | mach_msg_type_number_t nportnames = 0; | |
90 | mach_port_type_t *porttypes = NULL; | |
91 | mach_msg_type_number_t nporttypes = 0; | |
92 | thread_t *threads = NULL; | |
93 | mach_msg_type_number_t nthreads = 0; | |
3fe9de0d RM |
94 | int ports_locked = 0, stopped = 0; |
95 | ||
96 | void resume_threads (void) | |
97 | { | |
98 | if (! stopped) | |
99 | return; | |
100 | ||
101 | assert (threads); | |
102 | ||
103 | for (i = 0; i < nthreads; ++i) | |
104 | if (threads[i] != ss->thread) | |
105 | __thread_resume (threads[i]); | |
106 | stopped = 0; | |
107 | } | |
28f540f4 RM |
108 | |
109 | /* Run things that prepare for forking before we create the task. */ | |
110 | RUN_HOOK (_hurd_fork_prepare_hook, ()); | |
111 | ||
112 | /* Lock things that want to be locked before we fork. */ | |
3fe9de0d RM |
113 | { |
114 | void *const *p; | |
115 | for (p = symbol_set_first_element (_hurd_fork_locks); | |
116 | ! symbol_set_end_p (_hurd_fork_locks, p); | |
117 | ++p) | |
118 | __mutex_lock (*p); | |
119 | } | |
11872325 | 120 | __mutex_lock (&_hurd_siglock); |
e3fa2641 | 121 | |
e67f54ab ST |
122 | /* Acquire malloc locks. This needs to come last because fork |
123 | handlers may use malloc, and the libio list lock has an | |
124 | indirect malloc dependency as well (via the getdelim | |
125 | function). */ | |
126 | call_function_static_weak (__malloc_fork_lock_parent); | |
127 | _hurd_malloc_fork_prepare (); | |
128 | ||
28f540f4 RM |
129 | newtask = MACH_PORT_NULL; |
130 | thread = sigthread = MACH_PORT_NULL; | |
131 | newproc = MACH_PORT_NULL; | |
132 | ||
133 | /* Lock all the port cells for the standard ports while we copy the | |
134 | address space. We want to insert all the send rights into the | |
135 | child with the same names. */ | |
136 | for (i = 0; i < _hurd_nports; ++i) | |
137 | __spin_lock (&_hurd_ports[i].lock); | |
138 | ports_locked = 1; | |
139 | ||
e3fa2641 | 140 | |
17a62de1 ST |
141 | /* Keep our SS locked while stopping other threads, so they don't get a |
142 | chance to have it locked in the copied space. */ | |
143 | __spin_lock (&ss->lock); | |
3fe9de0d RM |
144 | /* Stop all other threads while copying the address space, |
145 | so nothing changes. */ | |
146 | err = __proc_dostop (_hurd_ports[INIT_PORT_PROC].port, ss->thread); | |
17a62de1 | 147 | __spin_unlock (&ss->lock); |
3fe9de0d | 148 | if (!err) |
478b92f0 UD |
149 | { |
150 | stopped = 1; | |
3fe9de0d | 151 | |
6df1b247 RM |
152 | #define XXX_KERNEL_PAGE_FAULT_BUG /* XXX work around page fault bug in mk */ |
153 | ||
154 | #ifdef XXX_KERNEL_PAGE_FAULT_BUG | |
155 | /* Gag me with a pitchfork. | |
156 | The bug scenario is this: | |
157 | ||
158 | - The page containing __mach_task_self_ is paged out. | |
159 | - The signal thread was faulting on that page when we | |
160 | suspended it via proc_dostop. It holds some lock, or set | |
161 | some busy bit, or somesuch. | |
162 | - Now this thread faults on that same page. | |
163 | - GRATUIOUS DEADLOCK | |
164 | ||
165 | We can break the deadlock by aborting the thread that faulted | |
166 | first, which if the bug happened was the signal thread because | |
167 | it is the only other thread and we just suspended it. | |
168 | */ | |
169 | __thread_abort (_hurd_msgport_thread); | |
170 | #endif | |
3fe9de0d | 171 | /* Create the child task. It will inherit a copy of our memory. */ |
7595ddb8 RM |
172 | err = __task_create (__mach_task_self (), |
173 | #ifdef KERN_INVALID_LEDGER | |
174 | NULL, 0, /* OSF Mach */ | |
175 | #endif | |
176 | 1, &newtask); | |
478b92f0 | 177 | } |
11872325 RM |
178 | |
179 | /* Unlock the global signal state lock, so we do not | |
180 | block the signal thread any longer than necessary. */ | |
181 | __mutex_unlock (&_hurd_siglock); | |
182 | ||
183 | if (err) | |
184 | LOSE; | |
28f540f4 RM |
185 | |
186 | /* Fetch the names of all ports used in this task. */ | |
187 | if (err = __mach_port_names (__mach_task_self (), | |
188 | &portnames, &nportnames, | |
189 | &porttypes, &nporttypes)) | |
11872325 | 190 | LOSE; |
28f540f4 RM |
191 | if (nportnames != nporttypes) |
192 | { | |
193 | err = EGRATUITOUS; | |
11872325 | 194 | LOSE; |
28f540f4 RM |
195 | } |
196 | ||
197 | /* Get send rights for all the threads in this task. | |
198 | We want to avoid giving these rights to the child. */ | |
199 | if (err = __task_threads (__mach_task_self (), &threads, &nthreads)) | |
11872325 | 200 | LOSE; |
28f540f4 RM |
201 | |
202 | /* Get the child process's proc server port. We will insert it into | |
203 | the child with the same name as we use for our own proc server | |
204 | port; and we will need it to set the child's message port. */ | |
205 | if (err = __proc_task2proc (_hurd_ports[INIT_PORT_PROC].port, | |
206 | newtask, &newproc)) | |
11872325 | 207 | LOSE; |
28f540f4 RM |
208 | |
209 | /* Insert all our port rights into the child task. */ | |
210 | thread_refs = sigthread_refs = 0; | |
211 | for (i = 0; i < nportnames; ++i) | |
212 | { | |
213 | if (porttypes[i] & MACH_PORT_TYPE_RECEIVE) | |
214 | { | |
215 | /* This is a receive right. We want to give the child task | |
216 | its own new receive right under the same name. */ | |
7dab2d00 ST |
217 | if (err = __mach_port_allocate_name (newtask, |
218 | MACH_PORT_RIGHT_RECEIVE, | |
219 | portnames[i])) | |
11872325 | 220 | LOSE; |
28f540f4 RM |
221 | if (porttypes[i] & MACH_PORT_TYPE_SEND) |
222 | { | |
223 | /* Give the child as many send rights for its receive | |
224 | right as we have for ours. */ | |
225 | mach_port_urefs_t refs; | |
226 | mach_port_t port; | |
227 | mach_msg_type_name_t poly; | |
228 | if (err = __mach_port_get_refs (__mach_task_self (), | |
229 | portnames[i], | |
230 | MACH_PORT_RIGHT_SEND, | |
231 | &refs)) | |
11872325 | 232 | LOSE; |
28f540f4 RM |
233 | if (err = __mach_port_extract_right (newtask, |
234 | portnames[i], | |
235 | MACH_MSG_TYPE_MAKE_SEND, | |
236 | &port, &poly)) | |
11872325 | 237 | LOSE; |
28f540f4 RM |
238 | if (portnames[i] == _hurd_msgport) |
239 | { | |
240 | /* We just created a receive right for the child's | |
241 | message port and are about to insert send rights | |
242 | for it. Now, while we happen to have a send right | |
243 | for it, give it to the proc server. */ | |
244 | mach_port_t old; | |
245 | if (err = __proc_setmsgport (newproc, port, &old)) | |
11872325 | 246 | LOSE; |
28f540f4 RM |
247 | if (old != MACH_PORT_NULL) |
248 | /* XXX what to do here? */ | |
249 | __mach_port_deallocate (__mach_task_self (), old); | |
4ca84cff RM |
250 | /* The new task will receive its own exceptions |
251 | on its message port. */ | |
7595ddb8 RM |
252 | if (err = |
253 | #ifdef TASK_EXCEPTION_PORT | |
254 | __task_set_special_port (newtask, | |
255 | TASK_EXCEPTION_PORT, | |
256 | port) | |
257 | #elif defined (EXC_MASK_ALL) | |
258 | __task_set_exception_ports | |
259 | (newtask, EXC_MASK_ALL & ~(EXC_MASK_SYSCALL | |
260 | | EXC_MASK_MACH_SYSCALL | |
261 | | EXC_MASK_RPC_ALERT), | |
262 | port, EXCEPTION_DEFAULT, MACHINE_THREAD_STATE) | |
263 | #else | |
264 | # error task_set_exception_port? | |
265 | #endif | |
266 | ) | |
4ca84cff | 267 | LOSE; |
28f540f4 RM |
268 | } |
269 | if (err = __mach_port_insert_right (newtask, | |
270 | portnames[i], | |
271 | port, | |
272 | MACH_MSG_TYPE_MOVE_SEND)) | |
11872325 | 273 | LOSE; |
28f540f4 RM |
274 | if (refs > 1 && |
275 | (err = __mach_port_mod_refs (newtask, | |
276 | portnames[i], | |
277 | MACH_PORT_RIGHT_SEND, | |
278 | refs - 1))) | |
11872325 | 279 | LOSE; |
28f540f4 RM |
280 | } |
281 | if (porttypes[i] & MACH_PORT_TYPE_SEND_ONCE) | |
282 | { | |
283 | /* Give the child a send-once right for its receive right, | |
284 | since we have one for ours. */ | |
285 | mach_port_t port; | |
286 | mach_msg_type_name_t poly; | |
287 | if (err = __mach_port_extract_right | |
288 | (newtask, | |
289 | portnames[i], | |
290 | MACH_MSG_TYPE_MAKE_SEND_ONCE, | |
291 | &port, &poly)) | |
11872325 | 292 | LOSE; |
28f540f4 RM |
293 | if (err = __mach_port_insert_right |
294 | (newtask, | |
295 | portnames[i], port, | |
296 | MACH_MSG_TYPE_MOVE_SEND_ONCE)) | |
11872325 | 297 | LOSE; |
28f540f4 RM |
298 | } |
299 | } | |
11872325 RM |
300 | else if (porttypes[i] & |
301 | (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_DEAD_NAME)) | |
28f540f4 RM |
302 | { |
303 | /* This is a send right or a dead name. | |
304 | Give the child as many references for it as we have. */ | |
7f513ec8 | 305 | mach_port_urefs_t refs = 0, *record_refs = NULL; |
28f540f4 | 306 | mach_port_t insert; |
3125073e | 307 | mach_msg_type_name_t insert_type = MACH_MSG_TYPE_COPY_SEND; |
7119ea6d RM |
308 | if (portnames[i] == newtask || portnames[i] == newproc) |
309 | /* Skip the name we use for the child's task or proc ports. */ | |
28f540f4 RM |
310 | continue; |
311 | if (portnames[i] == __mach_task_self ()) | |
312 | /* For the name we use for our own task port, | |
313 | insert the child's task port instead. */ | |
314 | insert = newtask; | |
315 | else if (portnames[i] == _hurd_ports[INIT_PORT_PROC].port) | |
316 | { | |
7119ea6d RM |
317 | /* Use the proc server port for the new task. */ |
318 | insert = newproc; | |
319 | insert_type = MACH_MSG_TYPE_COPY_SEND; | |
28f540f4 | 320 | } |
3fe9de0d | 321 | else if (portnames[i] == ss->thread) |
28f540f4 RM |
322 | { |
323 | /* For the name we use for our own thread port, we will | |
324 | insert the thread port for the child main user thread | |
325 | after we create it. */ | |
326 | insert = MACH_PORT_NULL; | |
327 | record_refs = &thread_refs; | |
328 | /* Allocate a dead name right for this name as a | |
478b92f0 UD |
329 | placeholder, so the kernel will not chose this name |
330 | for any other new port (it might use it for one of the | |
331 | rights created when a thread is created). */ | |
28f540f4 RM |
332 | if (err = __mach_port_allocate_name |
333 | (newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i])) | |
11872325 | 334 | LOSE; |
28f540f4 RM |
335 | } |
336 | else if (portnames[i] == _hurd_msgport_thread) | |
337 | /* For the name we use for our signal thread's thread port, | |
338 | we will insert the thread port for the child's signal | |
339 | thread after we create it. */ | |
340 | { | |
341 | insert = MACH_PORT_NULL; | |
342 | record_refs = &sigthread_refs; | |
343 | /* Allocate a dead name right as a placeholder. */ | |
344 | if (err = __mach_port_allocate_name | |
345 | (newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i])) | |
11872325 | 346 | LOSE; |
28f540f4 RM |
347 | } |
348 | else | |
349 | { | |
350 | /* Skip the name we use for any of our own thread ports. */ | |
351 | mach_msg_type_number_t j; | |
352 | for (j = 0; j < nthreads; ++j) | |
353 | if (portnames[i] == threads[j]) | |
354 | break; | |
355 | if (j < nthreads) | |
356 | continue; | |
357 | ||
3125073e | 358 | /* Copy our own send right. */ |
28f540f4 RM |
359 | insert = portnames[i]; |
360 | } | |
361 | /* Find out how many user references we have for | |
362 | the send right with this name. */ | |
363 | if (err = __mach_port_get_refs (__mach_task_self (), | |
364 | portnames[i], | |
365 | MACH_PORT_RIGHT_SEND, | |
366 | record_refs ?: &refs)) | |
11872325 | 367 | LOSE; |
28f540f4 RM |
368 | if (insert == MACH_PORT_NULL) |
369 | continue; | |
11872325 RM |
370 | if (insert == portnames[i] && |
371 | (porttypes[i] & MACH_PORT_TYPE_DEAD_NAME)) | |
372 | /* This is a dead name; allocate another dead name | |
373 | with the same name in the child. */ | |
374 | allocate_dead_name: | |
375 | err = __mach_port_allocate_name (newtask, | |
376 | MACH_PORT_RIGHT_DEAD_NAME, | |
377 | portnames[i]); | |
378 | else | |
379 | /* Insert the chosen send right into the child. */ | |
380 | err = __mach_port_insert_right (newtask, | |
381 | portnames[i], | |
a1c93156 | 382 | insert, insert_type); |
11872325 | 383 | switch (err) |
28f540f4 | 384 | { |
11872325 RM |
385 | case KERN_NAME_EXISTS: |
386 | { | |
387 | /* It already has a send right under this name (?!). | |
388 | Well, it starts out with a send right for its task | |
389 | port, and inherits the bootstrap and exception ports | |
390 | from us. */ | |
391 | mach_port_t childport; | |
392 | mach_msg_type_name_t poly; | |
393 | assert (__mach_port_extract_right (newtask, portnames[i], | |
394 | MACH_MSG_TYPE_COPY_SEND, | |
395 | &childport, | |
396 | &poly) == 0 && | |
397 | childport == insert && | |
398 | __mach_port_deallocate (__mach_task_self (), | |
399 | childport) == 0); | |
400 | break; | |
401 | } | |
402 | ||
403 | case KERN_INVALID_CAPABILITY: | |
404 | /* The port just died. It was a send right, | |
405 | and now it's a dead name. */ | |
406 | goto allocate_dead_name; | |
407 | ||
408 | default: | |
409 | LOSE; | |
410 | break; | |
411 | ||
412 | case KERN_SUCCESS: | |
413 | /* Give the child as many user references as we have. */ | |
414 | if (refs > 1 && | |
415 | (err = __mach_port_mod_refs (newtask, | |
416 | portnames[i], | |
417 | MACH_PORT_RIGHT_SEND, | |
418 | refs - 1))) | |
419 | LOSE; | |
28f540f4 | 420 | } |
28f540f4 RM |
421 | } |
422 | } | |
423 | ||
424 | /* Unlock the standard port cells. The child must unlock its own | |
425 | copies too. */ | |
426 | for (i = 0; i < _hurd_nports; ++i) | |
427 | __spin_unlock (&_hurd_ports[i].lock); | |
428 | ports_locked = 0; | |
429 | ||
3fe9de0d | 430 | /* All state has now been copied from the parent. It is safe to |
478b92f0 | 431 | resume other parent threads. */ |
3fe9de0d RM |
432 | resume_threads (); |
433 | ||
28f540f4 RM |
434 | /* Create the child main user thread and signal thread. */ |
435 | if ((err = __thread_create (newtask, &thread)) || | |
436 | (err = __thread_create (newtask, &sigthread))) | |
11872325 | 437 | LOSE; |
28f540f4 RM |
438 | |
439 | /* Insert send rights for those threads. We previously allocated | |
478b92f0 UD |
440 | dead name rights with the names we want to give the thread ports |
441 | in the child as placeholders. Now deallocate them so we can use | |
442 | the names. */ | |
3fe9de0d RM |
443 | if ((err = __mach_port_deallocate (newtask, ss->thread)) || |
444 | (err = __mach_port_insert_right (newtask, ss->thread, | |
28f540f4 | 445 | thread, MACH_MSG_TYPE_COPY_SEND))) |
11872325 | 446 | LOSE; |
28f540f4 | 447 | if (thread_refs > 1 && |
3fe9de0d | 448 | (err = __mach_port_mod_refs (newtask, ss->thread, |
28f540f4 | 449 | MACH_PORT_RIGHT_SEND, |
894f3e13 | 450 | thread_refs - 1))) |
11872325 | 451 | LOSE; |
28f540f4 RM |
452 | if ((_hurd_msgport_thread != MACH_PORT_NULL) /* Let user have none. */ |
453 | && ((err = __mach_port_deallocate (newtask, _hurd_msgport_thread)) || | |
454 | (err = __mach_port_insert_right (newtask, _hurd_msgport_thread, | |
455 | sigthread, | |
456 | MACH_MSG_TYPE_COPY_SEND)))) | |
11872325 | 457 | LOSE; |
28f540f4 RM |
458 | if (sigthread_refs > 1 && |
459 | (err = __mach_port_mod_refs (newtask, _hurd_msgport_thread, | |
460 | MACH_PORT_RIGHT_SEND, | |
461 | sigthread_refs - 1))) | |
11872325 | 462 | LOSE; |
28f540f4 RM |
463 | |
464 | /* This seems like a convenient juncture to copy the proc server's | |
465 | idea of what addresses our argv and envp are found at from the | |
466 | parent into the child. Since we happen to know that the child | |
467 | shares our memory image, it is we who should do this copying. */ | |
468 | { | |
469 | vm_address_t argv, envp; | |
470 | err = (__USEPORT (PROC, __proc_get_arg_locations (port, &argv, &envp)) | |
471 | ?: __proc_set_arg_locations (newproc, argv, envp)); | |
472 | if (err) | |
11872325 | 473 | LOSE; |
28f540f4 | 474 | } |
e3fa2641 | 475 | |
28f540f4 RM |
476 | /* Set the child signal thread up to run the msgport server function |
477 | using the same signal thread stack copied from our address space. | |
478 | We fetch the state before longjmp'ing it so that miscellaneous | |
479 | registers not affected by longjmp (such as i386 segment registers) | |
480 | are in their normal default state. */ | |
481 | statecount = MACHINE_THREAD_STATE_COUNT; | |
482 | if (err = __thread_get_state (_hurd_msgport_thread, | |
483 | MACHINE_THREAD_STATE_FLAVOR, | |
484 | (natural_t *) &state, &statecount)) | |
11872325 | 485 | LOSE; |
868df0f9 | 486 | #ifdef STACK_GROWTH_UP |
72e1a750 RM |
487 | if (__hurd_sigthread_stack_base == 0) |
488 | { | |
489 | state.SP &= __hurd_threadvar_stack_mask; | |
dc33bef3 | 490 | state.SP += __hurd_threadvar_stack_offset; |
72e1a750 RM |
491 | } |
492 | else | |
493 | state.SP = __hurd_sigthread_stack_base; | |
28f540f4 | 494 | #else |
72e1a750 RM |
495 | if (__hurd_sigthread_stack_end == 0) |
496 | { | |
dc33bef3 | 497 | /* The signal thread has a stack assigned by cthreads. |
72e1a750 RM |
498 | The threadvar_stack variables conveniently tell us how |
499 | to get to the highest address in the stack, just below | |
500 | the per-thread variables. */ | |
501 | state.SP &= __hurd_threadvar_stack_mask; | |
502 | state.SP += __hurd_threadvar_stack_offset; | |
503 | } | |
504 | else | |
505 | state.SP = __hurd_sigthread_stack_end; | |
e3fa2641 | 506 | #endif |
28f540f4 RM |
507 | MACHINE_THREAD_STATE_SET_PC (&state, |
508 | (unsigned long int) _hurd_msgport_receive); | |
f8baf2a2 ST |
509 | |
510 | /* Do special signal thread setup for TLS if needed. */ | |
511 | if (err = _hurd_tls_fork (sigthread, _hurd_msgport_thread, &state)) | |
512 | LOSE; | |
513 | ||
28f540f4 RM |
514 | if (err = __thread_set_state (sigthread, MACHINE_THREAD_STATE_FLAVOR, |
515 | (natural_t *) &state, statecount)) | |
11872325 | 516 | LOSE; |
28f540f4 RM |
517 | /* We do not thread_resume SIGTHREAD here because the child |
518 | fork needs to do more setup before it can take signals. */ | |
519 | ||
520 | /* Set the child user thread up to return 1 from the setjmp above. */ | |
521 | _hurd_longjmp_thread_state (&state, env, 1); | |
3ba7b383 | 522 | |
3ba7b383 | 523 | /* Do special thread setup for TLS if needed. */ |
f8baf2a2 | 524 | if (err = _hurd_tls_fork (thread, ss->thread, &state)) |
3ba7b383 | 525 | LOSE; |
3ba7b383 | 526 | |
28f540f4 RM |
527 | if (err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR, |
528 | (natural_t *) &state, statecount)) | |
11872325 | 529 | LOSE; |
28f540f4 RM |
530 | |
531 | /* Get the PID of the child from the proc server. We must do this | |
532 | before calling proc_child below, because at that point any | |
533 | authorized POSIX.1 process may kill the child task with SIGKILL. */ | |
534 | if (err = __USEPORT (PROC, __proc_task2pid (port, newtask, &pid))) | |
11872325 | 535 | LOSE; |
28f540f4 RM |
536 | |
537 | /* Register the child with the proc server. It is important that | |
538 | this be that last thing we do before starting the child thread | |
539 | running. Once proc_child has been done for the task, it appears | |
540 | as a POSIX.1 process. Any errors we get must be detected before | |
541 | this point, and the child must have a message port so it responds | |
542 | to POSIX.1 signals. */ | |
543 | if (err = __USEPORT (PROC, __proc_child (port, newtask))) | |
11872325 | 544 | LOSE; |
28f540f4 RM |
545 | |
546 | /* This must be the absolutely last thing we do; we can't assume that | |
547 | the child will remain alive for even a moment once we do this. We | |
548 | ignore errors because we have committed to the fork and are not | |
549 | allowed to return them after the process becomes visible to | |
550 | POSIX.1 (which happened right above when we called proc_child). */ | |
551 | (void) __thread_resume (thread); | |
552 | ||
553 | lose: | |
554 | if (ports_locked) | |
555 | for (i = 0; i < _hurd_nports; ++i) | |
556 | __spin_unlock (&_hurd_ports[i].lock); | |
557 | ||
3fe9de0d RM |
558 | resume_threads (); |
559 | ||
28f540f4 RM |
560 | if (newtask != MACH_PORT_NULL) |
561 | { | |
562 | if (err) | |
563 | __task_terminate (newtask); | |
564 | __mach_port_deallocate (__mach_task_self (), newtask); | |
565 | } | |
566 | if (thread != MACH_PORT_NULL) | |
567 | __mach_port_deallocate (__mach_task_self (), thread); | |
568 | if (sigthread != MACH_PORT_NULL) | |
569 | __mach_port_deallocate (__mach_task_self (), sigthread); | |
570 | if (newproc != MACH_PORT_NULL) | |
571 | __mach_port_deallocate (__mach_task_self (), newproc); | |
28f540f4 RM |
572 | |
573 | if (portnames) | |
574 | __vm_deallocate (__mach_task_self (), | |
575 | (vm_address_t) portnames, | |
576 | nportnames * sizeof (*portnames)); | |
577 | if (porttypes) | |
578 | __vm_deallocate (__mach_task_self (), | |
579 | (vm_address_t) porttypes, | |
580 | nporttypes * sizeof (*porttypes)); | |
581 | if (threads) | |
582 | { | |
583 | for (i = 0; i < nthreads; ++i) | |
584 | __mach_port_deallocate (__mach_task_self (), threads[i]); | |
585 | __vm_deallocate (__mach_task_self (), | |
586 | (vm_address_t) threads, | |
587 | nthreads * sizeof (*threads)); | |
588 | } | |
589 | ||
29d79486 | 590 | /* Release malloc locks. */ |
e67f54ab | 591 | _hurd_malloc_fork_parent (); |
ef4f9764 | 592 | call_function_static_weak (__malloc_fork_unlock_parent); |
29d79486 | 593 | |
28f540f4 RM |
594 | /* Run things that want to run in the parent to restore it to |
595 | normality. Usually prepare hooks and parent hooks are | |
596 | symmetrical: the prepare hook arrests state in some way for the | |
597 | fork, and the parent hook restores the state for the parent to | |
598 | continue executing normally. */ | |
599 | RUN_HOOK (_hurd_fork_parent_hook, ()); | |
600 | } | |
601 | else | |
602 | { | |
603 | struct hurd_sigstate *oldstates; | |
604 | ||
605 | /* We are the child task. Unlock the standard port cells, which were | |
478b92f0 UD |
606 | locked in the parent when we copied its memory. The parent has |
607 | inserted send rights with the names that were in the cells then. */ | |
28f540f4 RM |
608 | for (i = 0; i < _hurd_nports; ++i) |
609 | __spin_unlock (&_hurd_ports[i].lock); | |
610 | ||
4ac9bb2e RM |
611 | /* We are one of the (exactly) two threads in this new task, we |
612 | will take the task-global signals. */ | |
3fe9de0d | 613 | _hurd_sigthread = ss->thread; |
28f540f4 | 614 | |
4ac9bb2e RM |
615 | /* Claim our sigstate structure and unchain the rest: the |
616 | threads existed in the parent task but don't exist in this | |
617 | task (the child process). Delay freeing them until later | |
618 | because some of the further setup and unlocking might be | |
619 | required for free to work. Before we finish cleaning up, | |
620 | we will reclaim the signal thread's sigstate structure (if | |
621 | it had one). */ | |
28f540f4 RM |
622 | oldstates = _hurd_sigstates; |
623 | if (oldstates == ss) | |
624 | oldstates = ss->next; | |
625 | else | |
626 | { | |
627 | while (_hurd_sigstates->next != ss) | |
628 | _hurd_sigstates = _hurd_sigstates->next; | |
629 | _hurd_sigstates->next = ss->next; | |
630 | } | |
631 | ss->next = NULL; | |
632 | _hurd_sigstates = ss; | |
11872325 | 633 | __mutex_unlock (&_hurd_siglock); |
28f540f4 RM |
634 | |
635 | /* Fetch our new process IDs from the proc server. No need to | |
636 | refetch our pgrp; it is always inherited from the parent (so | |
637 | _hurd_pgrp is already correct), and the proc server will send us a | |
638 | proc_newids notification when it changes. */ | |
639 | err = __USEPORT (PROC, __proc_getpids (port, &_hurd_pid, &_hurd_ppid, | |
640 | &_hurd_orphaned)); | |
641 | ||
b07c5668 | 642 | /* Forking clears the trace flag. */ |
8f0c527e | 643 | __sigemptyset (&_hurdsig_traced); |
b07c5668 | 644 | |
29d79486 | 645 | /* Release malloc locks. */ |
e67f54ab | 646 | _hurd_malloc_fork_child (); |
ef4f9764 | 647 | call_function_static_weak (__malloc_fork_unlock_child); |
29d79486 | 648 | |
28f540f4 RM |
649 | /* Run things that want to run in the child task to set up. */ |
650 | RUN_HOOK (_hurd_fork_child_hook, ()); | |
651 | ||
652 | /* Set up proc server-assisted fault recovery for the signal thread. */ | |
653 | _hurdsig_fault_init (); | |
654 | ||
655 | /* Start the signal thread listening on the message port. */ | |
656 | if (!err) | |
657 | err = __thread_resume (_hurd_msgport_thread); | |
658 | ||
4ac9bb2e RM |
659 | /* Reclaim the signal thread's sigstate structure and free the |
660 | other old sigstate structures. */ | |
28f540f4 RM |
661 | while (oldstates != NULL) |
662 | { | |
663 | struct hurd_sigstate *next = oldstates->next; | |
4ac9bb2e RM |
664 | |
665 | if (oldstates->thread == _hurd_msgport_thread) | |
666 | { | |
667 | /* If we have a second signal state structure then we | |
668 | must have been through here before--not good. */ | |
669 | assert (_hurd_sigstates->next == 0); | |
670 | _hurd_sigstates->next = oldstates; | |
671 | oldstates->next = 0; | |
672 | } | |
673 | else | |
674 | free (oldstates); | |
675 | ||
28f540f4 RM |
676 | oldstates = next; |
677 | } | |
4ac9bb2e | 678 | |
28f540f4 RM |
679 | /* XXX what to do if we have any errors here? */ |
680 | ||
681 | pid = 0; | |
682 | } | |
683 | ||
684 | /* Unlock things we locked before creating the child task. | |
685 | They are locked in both the parent and child tasks. */ | |
3fe9de0d RM |
686 | { |
687 | void *const *p; | |
688 | for (p = symbol_set_first_element (_hurd_fork_locks); | |
689 | ! symbol_set_end_p (_hurd_fork_locks, p); | |
690 | ++p) | |
691 | __mutex_unlock (*p); | |
692 | } | |
28f540f4 | 693 | |
11872325 | 694 | _hurd_critical_section_unlock (ss); |
28f540f4 | 695 | |
7b7bab13 ST |
696 | if (!err) |
697 | { | |
698 | if (pid != 0) | |
699 | RUN_HOOK (_hurd_atfork_parent_hook, ()); | |
700 | else | |
701 | RUN_HOOK (_hurd_atfork_child_hook, ()); | |
702 | } | |
703 | ||
28f540f4 RM |
704 | return err ? __hurd_fail (err) : pid; |
705 | } | |
d1436edc | 706 | libc_hidden_def (__fork) |
28f540f4 RM |
707 | |
708 | weak_alias (__fork, fork) |