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