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