]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/fork.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / mach / hurd / fork.c
1 /* Copyright (C) 1994-2020 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 <https://www.gnu.org/licenses/>. */
17
18 #include <errno.h>
19 #include <unistd.h>
20 #include <hurd.h>
21 #include <hurd/signal.h>
22 #include <hurd/threadvar.h>
23 #include <setjmp.h>
24 #include <thread_state.h>
25 #include <sysdep.h> /* For stack growth direction. */
26 #include "set-hooks.h"
27 #include <assert.h>
28 #include "hurdmalloc.h" /* XXX */
29 #include <tls.h>
30 #include <malloc/malloc-internal.h>
31
32 #undef __fork
33
34
35 /* Things that want to be locked while forking. */
36 symbol_set_declare (_hurd_fork_locks)
37
38
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
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;
70 struct hurd_sigstate *volatile ss;
71
72 RUN_HOOK (_hurd_atfork_prepare_hook, ());
73
74 ss = _hurd_self_sigstate ();
75 __spin_lock (&ss->critical_section_lock);
76
77 #undef LOSE
78 #define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */
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;
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 }
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. */
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 }
120 __mutex_lock (&_hurd_siglock);
121
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
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
140
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);
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);
147 __spin_unlock (&ss->lock);
148 if (!err)
149 {
150 stopped = 1;
151
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
171 /* Create the child task. It will inherit a copy of our memory. */
172 err = __task_create (__mach_task_self (),
173 #ifdef KERN_INVALID_LEDGER
174 NULL, 0, /* OSF Mach */
175 #endif
176 1, &newtask);
177 }
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;
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))
190 LOSE;
191 if (nportnames != nporttypes)
192 {
193 err = EGRATUITOUS;
194 LOSE;
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))
200 LOSE;
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))
207 LOSE;
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. */
217 if (err = __mach_port_allocate_name (newtask,
218 MACH_PORT_RIGHT_RECEIVE,
219 portnames[i]))
220 LOSE;
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))
232 LOSE;
233 if (err = __mach_port_extract_right (newtask,
234 portnames[i],
235 MACH_MSG_TYPE_MAKE_SEND,
236 &port, &poly))
237 LOSE;
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))
246 LOSE;
247 if (old != MACH_PORT_NULL)
248 /* XXX what to do here? */
249 __mach_port_deallocate (__mach_task_self (), old);
250 /* The new task will receive its own exceptions
251 on its message port. */
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 )
267 LOSE;
268 }
269 if (err = __mach_port_insert_right (newtask,
270 portnames[i],
271 port,
272 MACH_MSG_TYPE_MOVE_SEND))
273 LOSE;
274 if (refs > 1
275 && (err = __mach_port_mod_refs (newtask,
276 portnames[i],
277 MACH_PORT_RIGHT_SEND,
278 refs - 1)))
279 LOSE;
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))
292 LOSE;
293 if (err = __mach_port_insert_right
294 (newtask,
295 portnames[i], port,
296 MACH_MSG_TYPE_MOVE_SEND_ONCE))
297 LOSE;
298 }
299 }
300 else if (porttypes[i]
301 & (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_DEAD_NAME))
302 {
303 /* This is a send right or a dead name.
304 Give the child as many references for it as we have. */
305 mach_port_urefs_t refs = 0, *record_refs = NULL;
306 mach_port_t insert;
307 mach_msg_type_name_t insert_type = MACH_MSG_TYPE_COPY_SEND;
308 if (portnames[i] == newtask || portnames[i] == newproc)
309 /* Skip the name we use for the child's task or proc ports. */
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 {
317 /* Use the proc server port for the new task. */
318 insert = newproc;
319 insert_type = MACH_MSG_TYPE_COPY_SEND;
320 }
321 else if (portnames[i] == ss->thread)
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
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). */
332 if (err = __mach_port_allocate_name
333 (newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i]))
334 LOSE;
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]))
346 LOSE;
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
358 /* Copy our own send right. */
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))
367 LOSE;
368 if (insert == MACH_PORT_NULL)
369 continue;
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],
382 insert, insert_type);
383 switch (err)
384 {
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;
420 }
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
430 /* All state has now been copied from the parent. It is safe to
431 resume other parent threads. */
432 resume_threads ();
433
434 /* Create the child main user thread and signal thread. */
435 if ((err = __thread_create (newtask, &thread))
436 || (err = __thread_create (newtask, &sigthread)))
437 LOSE;
438
439 /* Insert send rights for those threads. We previously allocated
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. */
443 if ((err = __mach_port_deallocate (newtask, ss->thread))
444 || (err = __mach_port_insert_right (newtask, ss->thread,
445 thread,
446 MACH_MSG_TYPE_COPY_SEND)))
447 LOSE;
448 /* XXX consumed? (_hurd_sigthread is no more) */
449 if (thread_refs > 1
450 && (err = __mach_port_mod_refs (newtask, ss->thread,
451 MACH_PORT_RIGHT_SEND,
452 thread_refs - 1)))
453 LOSE;
454 if ((_hurd_msgport_thread != MACH_PORT_NULL) /* Let user have none. */
455 && ((err = __mach_port_deallocate (newtask, _hurd_msgport_thread))
456 || (err = __mach_port_insert_right (newtask,
457 _hurd_msgport_thread,
458 sigthread,
459 MACH_MSG_TYPE_COPY_SEND))))
460 LOSE;
461 if (sigthread_refs > 1
462 && (err = __mach_port_mod_refs (newtask, _hurd_msgport_thread,
463 MACH_PORT_RIGHT_SEND,
464 sigthread_refs - 1)))
465 LOSE;
466
467 /* This seems like a convenient juncture to copy the proc server's
468 idea of what addresses our argv and envp are found at from the
469 parent into the child. Since we happen to know that the child
470 shares our memory image, it is we who should do this copying. */
471 {
472 vm_address_t argv, envp;
473 err = (__USEPORT (PROC, __proc_get_arg_locations (port, &argv, &envp))
474 ?: __proc_set_arg_locations (newproc, argv, envp));
475 if (err)
476 LOSE;
477 }
478
479 /* Set the child signal thread up to run the msgport server function
480 using the same signal thread stack copied from our address space.
481 We fetch the state before longjmp'ing it so that miscellaneous
482 registers not affected by longjmp (such as i386 segment registers)
483 are in their normal default state. */
484 statecount = MACHINE_THREAD_STATE_COUNT;
485 if (err = __thread_get_state (_hurd_msgport_thread,
486 MACHINE_THREAD_STATE_FLAVOR,
487 (natural_t *) &state, &statecount))
488 LOSE;
489 #ifdef STACK_GROWTH_UP
490 if (__hurd_sigthread_stack_base == 0)
491 {
492 state.SP &= __hurd_threadvar_stack_mask;
493 state.SP += __hurd_threadvar_stack_offset;
494 }
495 else
496 state.SP = __hurd_sigthread_stack_base;
497 #else
498 if (__hurd_sigthread_stack_end == 0)
499 {
500 /* The signal thread has a stack assigned by cthreads.
501 The threadvar_stack variables conveniently tell us how
502 to get to the highest address in the stack, just below
503 the per-thread variables. */
504 state.SP &= __hurd_threadvar_stack_mask;
505 state.SP += __hurd_threadvar_stack_offset;
506 }
507 else
508 state.SP = __hurd_sigthread_stack_end;
509 #endif
510 MACHINE_THREAD_STATE_SET_PC (&state,
511 (unsigned long int) _hurd_msgport_receive);
512
513 /* Do special signal thread setup for TLS if needed. */
514 if (err = _hurd_tls_fork (sigthread, _hurd_msgport_thread, &state))
515 LOSE;
516
517 if (err = __thread_set_state (sigthread, MACHINE_THREAD_STATE_FLAVOR,
518 (natural_t *) &state, statecount))
519 LOSE;
520 /* We do not thread_resume SIGTHREAD here because the child
521 fork needs to do more setup before it can take signals. */
522
523 /* Set the child user thread up to return 1 from the setjmp above. */
524 _hurd_longjmp_thread_state (&state, env, 1);
525
526 /* Do special thread setup for TLS if needed. */
527 if (err = _hurd_tls_fork (thread, ss->thread, &state))
528 LOSE;
529
530 if (err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
531 (natural_t *) &state, statecount))
532 LOSE;
533
534 /* Get the PID of the child from the proc server. We must do this
535 before calling proc_child below, because at that point any
536 authorized POSIX.1 process may kill the child task with SIGKILL. */
537 if (err = __USEPORT (PROC, __proc_task2pid (port, newtask, &pid)))
538 LOSE;
539
540 /* Register the child with the proc server. It is important that
541 this be that last thing we do before starting the child thread
542 running. Once proc_child has been done for the task, it appears
543 as a POSIX.1 process. Any errors we get must be detected before
544 this point, and the child must have a message port so it responds
545 to POSIX.1 signals. */
546 if (err = __USEPORT (PROC, __proc_child (port, newtask)))
547 LOSE;
548
549 /* This must be the absolutely last thing we do; we can't assume that
550 the child will remain alive for even a moment once we do this. We
551 ignore errors because we have committed to the fork and are not
552 allowed to return them after the process becomes visible to
553 POSIX.1 (which happened right above when we called proc_child). */
554 (void) __thread_resume (thread);
555
556 lose:
557 if (ports_locked)
558 for (i = 0; i < _hurd_nports; ++i)
559 __spin_unlock (&_hurd_ports[i].lock);
560
561 resume_threads ();
562
563 if (newtask != MACH_PORT_NULL)
564 {
565 if (err)
566 __task_terminate (newtask);
567 __mach_port_deallocate (__mach_task_self (), newtask);
568 }
569 if (thread != MACH_PORT_NULL)
570 __mach_port_deallocate (__mach_task_self (), thread);
571 if (sigthread != MACH_PORT_NULL)
572 __mach_port_deallocate (__mach_task_self (), sigthread);
573 if (newproc != MACH_PORT_NULL)
574 __mach_port_deallocate (__mach_task_self (), newproc);
575
576 if (portnames)
577 __vm_deallocate (__mach_task_self (),
578 (vm_address_t) portnames,
579 nportnames * sizeof (*portnames));
580 if (porttypes)
581 __vm_deallocate (__mach_task_self (),
582 (vm_address_t) porttypes,
583 nporttypes * sizeof (*porttypes));
584 if (threads)
585 {
586 for (i = 0; i < nthreads; ++i)
587 __mach_port_deallocate (__mach_task_self (), threads[i]);
588 __vm_deallocate (__mach_task_self (),
589 (vm_address_t) threads,
590 nthreads * sizeof (*threads));
591 }
592
593 /* Release malloc locks. */
594 _hurd_malloc_fork_parent ();
595 call_function_static_weak (__malloc_fork_unlock_parent);
596
597 /* Run things that want to run in the parent to restore it to
598 normality. Usually prepare hooks and parent hooks are
599 symmetrical: the prepare hook arrests state in some way for the
600 fork, and the parent hook restores the state for the parent to
601 continue executing normally. */
602 RUN_HOOK (_hurd_fork_parent_hook, ());
603 }
604 else
605 {
606 struct hurd_sigstate *oldstates;
607
608 /* We are the child task. Unlock the standard port cells, which were
609 locked in the parent when we copied its memory. The parent has
610 inserted send rights with the names that were in the cells then. */
611 for (i = 0; i < _hurd_nports; ++i)
612 __spin_unlock (&_hurd_ports[i].lock);
613
614 /* Claim our sigstate structure and unchain the rest: the
615 threads existed in the parent task but don't exist in this
616 task (the child process). Delay freeing them until later
617 because some of the further setup and unlocking might be
618 required for free to work. Before we finish cleaning up,
619 we will reclaim the signal thread's sigstate structure (if
620 it had one). */
621 oldstates = _hurd_sigstates;
622 if (oldstates == ss)
623 oldstates = ss->next;
624 else
625 {
626 while (_hurd_sigstates->next != ss)
627 _hurd_sigstates = _hurd_sigstates->next;
628 _hurd_sigstates->next = ss->next;
629 }
630 ss->next = NULL;
631 _hurd_sigstates = ss;
632 __mutex_unlock (&_hurd_siglock);
633 /* Earlier on, the global sigstate may have been tainted and now needs to
634 be reinitialized. Nobody is interested in its present state anymore:
635 we're not, the signal thread will be restarted, and there are no other
636 threads.
637
638 We can't simply allocate a fresh global sigstate here, as
639 _hurd_thread_sigstate will call malloc and that will deadlock trying
640 to determine the current thread's sigstate. */
641 #if 0
642 _hurd_thread_sigstate_init (_hurd_global_sigstate, MACH_PORT_NULL);
643 #else
644 /* Only reinitialize the lock -- otherwise we might have to do additional
645 setup as done in hurdsig.c:_hurdsig_init. */
646 __spin_lock_init (&_hurd_global_sigstate->lock);
647 #endif
648
649 /* We are one of the (exactly) two threads in this new task, we
650 will take the task-global signals. */
651 _hurd_sigstate_set_global_rcv (ss);
652
653 /* Fetch our new process IDs from the proc server. No need to
654 refetch our pgrp; it is always inherited from the parent (so
655 _hurd_pgrp is already correct), and the proc server will send us a
656 proc_newids notification when it changes. */
657 err = __USEPORT (PROC, __proc_getpids (port, &_hurd_pid, &_hurd_ppid,
658 &_hurd_orphaned));
659
660 /* Forking clears the trace flag and pending masks. */
661 __sigemptyset (&_hurdsig_traced);
662 __sigemptyset (&_hurd_global_sigstate->pending);
663 __sigemptyset (&ss->pending);
664
665 /* Release malloc locks. */
666 _hurd_malloc_fork_child ();
667 call_function_static_weak (__malloc_fork_unlock_child);
668
669 /* Run things that want to run in the child task to set up. */
670 RUN_HOOK (_hurd_fork_child_hook, ());
671
672 /* Set up proc server-assisted fault recovery for the signal thread. */
673 _hurdsig_fault_init ();
674
675 /* Start the signal thread listening on the message port. */
676 if (!err)
677 err = __thread_resume (_hurd_msgport_thread);
678
679 /* Reclaim the signal thread's sigstate structure and free the
680 other old sigstate structures. */
681 while (oldstates != NULL)
682 {
683 struct hurd_sigstate *next = oldstates->next;
684
685 if (oldstates->thread == _hurd_msgport_thread)
686 {
687 /* If we have a second signal state structure then we
688 must have been through here before--not good. */
689 assert (_hurd_sigstates->next == 0);
690 _hurd_sigstates->next = oldstates;
691 oldstates->next = 0;
692 }
693 else
694 free (oldstates);
695
696 oldstates = next;
697 }
698
699 /* XXX what to do if we have any errors here? */
700
701 pid = 0;
702 }
703
704 /* Unlock things we locked before creating the child task.
705 They are locked in both the parent and child tasks. */
706 {
707 void *const *p;
708 for (p = symbol_set_first_element (_hurd_fork_locks);
709 ! symbol_set_end_p (_hurd_fork_locks, p);
710 ++p)
711 __mutex_unlock (*p);
712 }
713
714 _hurd_critical_section_unlock (ss);
715
716 if (!err)
717 {
718 if (pid != 0)
719 RUN_HOOK (_hurd_atfork_parent_hook, ());
720 else
721 RUN_HOOK (_hurd_atfork_child_hook, ());
722 }
723
724 return err ? __hurd_fail (err) : pid;
725 }
726 libc_hidden_def (__fork)
727
728 weak_alias (__fork, fork)