1 /* Copyright (C) 1994,1995,1996,1997,1999,2001,2002,2004,2005,2006,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 #include <hurd/signal.h>
24 #include <thread_state.h>
25 #include <sysdep.h> /* For stack growth direction. */
26 #include "set-hooks.h"
28 #include "hurdmalloc.h" /* XXX */
34 /* Things that want to be locked while forking. */
35 symbol_set_declare (_hurd_fork_locks
)
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. */
40 DEFINE_HOOK (_hurd_fork_prepare_hook
, (void));
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. */
45 DEFINE_HOOK (_hurd_fork_setup_hook
, (void));
47 /* Things to be run in the child fork. */
48 DEFINE_HOOK (_hurd_fork_child_hook
, (void));
50 /* Things to be run in the parent fork. */
51 DEFINE_HOOK (_hurd_fork_parent_hook
, (void));
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. */
64 struct hurd_sigstate
*volatile ss
;
66 ss
= _hurd_self_sigstate ();
67 __spin_lock (&ss
->critical_section_lock
);
70 #define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */
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;
86 int ports_locked
= 0, stopped
= 0;
88 void resume_threads (void)
95 for (i
= 0; i
< nthreads
; ++i
)
96 if (threads
[i
] != ss
->thread
)
97 __thread_resume (threads
[i
]);
101 /* Run things that prepare for forking before we create the task. */
102 RUN_HOOK (_hurd_fork_prepare_hook
, ());
104 /* Lock things that want to be locked before we fork. */
107 for (p
= symbol_set_first_element (_hurd_fork_locks
);
108 ! symbol_set_end_p (_hurd_fork_locks
, p
);
112 __mutex_lock (&_hurd_siglock
);
114 newtask
= MACH_PORT_NULL
;
115 thread
= sigthread
= MACH_PORT_NULL
;
116 newproc
= MACH_PORT_NULL
;
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
);
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
);
133 #define XXX_KERNEL_PAGE_FAULT_BUG /* XXX work around page fault bug in mk */
135 #ifdef XXX_KERNEL_PAGE_FAULT_BUG
136 /* Gag me with a pitchfork.
137 The bug scenario is this:
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.
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.
150 __thread_abort (_hurd_msgport_thread
);
152 /* Create the child task. It will inherit a copy of our memory. */
153 err
= __task_create (__mach_task_self (),
154 #ifdef KERN_INVALID_LEDGER
155 NULL
, 0, /* OSF Mach */
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
);
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
))
172 if (nportnames
!= nporttypes
)
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
))
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
,
190 /* Insert all our port rights into the child task. */
191 thread_refs
= sigthread_refs
= 0;
192 for (i
= 0; i
< nportnames
; ++i
)
194 if (porttypes
[i
] & MACH_PORT_TYPE_RECEIVE
)
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
,
201 if (err
== KERN_NAME_EXISTS
)
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. */
210 #define TASK_NOTIFY_PORT 2
212 assert (({ mach_port_t thisport
, notify_port
;
213 mach_msg_type_name_t poly
;
214 (__task_get_special_port (newtask
,
216 ¬ify_port
) == 0 &&
217 __mach_port_extract_right
220 MACH_MSG_TYPE_MAKE_SEND
,
221 &thisport
, &poly
) == 0 &&
222 (thisport
== notify_port
) &&
223 __mach_port_deallocate (__mach_task_self (),
225 __mach_port_deallocate (__mach_task_self (),
231 if (porttypes
[i
] & MACH_PORT_TYPE_SEND
)
233 /* Give the child as many send rights for its receive
234 right as we have for ours. */
235 mach_port_urefs_t refs
;
237 mach_msg_type_name_t poly
;
238 if (err
= __mach_port_get_refs (__mach_task_self (),
240 MACH_PORT_RIGHT_SEND
,
243 if (err
= __mach_port_extract_right (newtask
,
245 MACH_MSG_TYPE_MAKE_SEND
,
248 if (portnames
[i
] == _hurd_msgport
)
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. */
255 if (err
= __proc_setmsgport (newproc
, port
, &old
))
257 if (old
!= MACH_PORT_NULL
)
258 /* XXX what to do here? */
259 __mach_port_deallocate (__mach_task_self (), old
);
260 /* The new task will receive its own exceptions
261 on its message port. */
263 #ifdef TASK_EXCEPTION_PORT
264 __task_set_special_port (newtask
,
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
)
274 # error task_set_exception_port?
279 if (err
= __mach_port_insert_right (newtask
,
282 MACH_MSG_TYPE_MOVE_SEND
))
285 (err
= __mach_port_mod_refs (newtask
,
287 MACH_PORT_RIGHT_SEND
,
291 if (porttypes
[i
] & MACH_PORT_TYPE_SEND_ONCE
)
293 /* Give the child a send-once right for its receive right,
294 since we have one for ours. */
296 mach_msg_type_name_t poly
;
297 if (err
= __mach_port_extract_right
300 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
303 if (err
= __mach_port_insert_right
306 MACH_MSG_TYPE_MOVE_SEND_ONCE
))
310 else if (porttypes
[i
] &
311 (MACH_PORT_TYPE_SEND
|MACH_PORT_TYPE_DEAD_NAME
))
313 /* This is a send right or a dead name.
314 Give the child as many references for it as we have. */
315 mach_port_urefs_t refs
= 0, *record_refs
= NULL
;
317 mach_msg_type_name_t insert_type
= MACH_MSG_TYPE_COPY_SEND
;
318 if (portnames
[i
] == newtask
|| portnames
[i
] == newproc
)
319 /* Skip the name we use for the child's task or proc ports. */
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. */
325 else if (portnames
[i
] == _hurd_ports
[INIT_PORT_PROC
].port
)
327 /* Use the proc server port for the new task. */
329 insert_type
= MACH_MSG_TYPE_COPY_SEND
;
331 else if (portnames
[i
] == ss
->thread
)
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
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). */
342 if (err
= __mach_port_allocate_name
343 (newtask
, MACH_PORT_RIGHT_DEAD_NAME
, portnames
[i
]))
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. */
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
]))
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
])
368 /* Copy our own send right. */
369 insert
= portnames
[i
];
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 (),
375 MACH_PORT_RIGHT_SEND
,
376 record_refs
?: &refs
))
378 if (insert
== MACH_PORT_NULL
)
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. */
385 err
= __mach_port_allocate_name (newtask
,
386 MACH_PORT_RIGHT_DEAD_NAME
,
389 /* Insert the chosen send right into the child. */
390 err
= __mach_port_insert_right (newtask
,
392 insert
, insert_type
);
395 case KERN_NAME_EXISTS
:
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
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
,
407 childport
== insert
&&
408 __mach_port_deallocate (__mach_task_self (),
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
;
423 /* Give the child as many user references as we have. */
425 (err
= __mach_port_mod_refs (newtask
,
427 MACH_PORT_RIGHT_SEND
,
434 /* Unlock the standard port cells. The child must unlock its own
436 for (i
= 0; i
< _hurd_nports
; ++i
)
437 __spin_unlock (&_hurd_ports
[i
].lock
);
440 /* All state has now been copied from the parent. It is safe to
441 resume other parent threads. */
444 /* Create the child main user thread and signal thread. */
445 if ((err
= __thread_create (newtask
, &thread
)) ||
446 (err
= __thread_create (newtask
, &sigthread
)))
449 /* Insert send rights for those threads. We previously allocated
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
453 if ((err
= __mach_port_deallocate (newtask
, ss
->thread
)) ||
454 (err
= __mach_port_insert_right (newtask
, ss
->thread
,
455 thread
, MACH_MSG_TYPE_COPY_SEND
)))
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 &&
462 (err
= __mach_port_mod_refs (newtask
, ss
->thread
,
463 MACH_PORT_RIGHT_SEND
,
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
,
470 MACH_MSG_TYPE_COPY_SEND
))))
472 if (sigthread_refs
> 1 &&
473 (err
= __mach_port_mod_refs (newtask
, _hurd_msgport_thread
,
474 MACH_PORT_RIGHT_SEND
,
475 sigthread_refs
- 1)))
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. */
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
));
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
))
501 #define THREADVAR_SPACE (__hurd_threadvar_max \
502 * sizeof *__hurd_sightread_variables)
503 if (__hurd_sigthread_stack_base
== 0)
505 state
.SP
&= __hurd_threadvar_stack_mask
;
506 state
.SP
+= __hurd_threadvar_stack_offset
+ THREADVAR_SPACE
;
509 state
.SP
= __hurd_sigthread_stack_base
;
511 if (__hurd_sigthread_stack_end
== 0)
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
;
521 state
.SP
= __hurd_sigthread_stack_end
;
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
))
528 /* We do not thread_resume SIGTHREAD here because the child
529 fork needs to do more setup before it can take signals. */
531 /* Set the child user thread up to return 1 from the setjmp above. */
532 _hurd_longjmp_thread_state (&state
, env
, 1);
534 /* Do special thread setup for TLS if needed. */
535 if (err
= _hurd_tls_fork (thread
, &state
))
538 if (err
= __thread_set_state (thread
, MACHINE_THREAD_STATE_FLAVOR
,
539 (natural_t
*) &state
, statecount
))
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
)))
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
)))
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
);
566 for (i
= 0; i
< _hurd_nports
; ++i
)
567 __spin_unlock (&_hurd_ports
[i
].lock
);
571 if (newtask
!= MACH_PORT_NULL
)
574 __task_terminate (newtask
);
575 __mach_port_deallocate (__mach_task_self (), newtask
);
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
);
585 __vm_deallocate (__mach_task_self (),
586 (vm_address_t
) portnames
,
587 nportnames
* sizeof (*portnames
));
589 __vm_deallocate (__mach_task_self (),
590 (vm_address_t
) porttypes
,
591 nporttypes
* sizeof (*porttypes
));
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
));
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
, ());
610 struct hurd_sigstate
*oldstates
;
612 /* We are the child task. Unlock the standard port cells, which were
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. */
615 for (i
= 0; i
< _hurd_nports
; ++i
)
616 __spin_unlock (&_hurd_ports
[i
].lock
);
618 /* We are one of the (exactly) two threads in this new task, we
619 will take the task-global signals. */
620 _hurd_sigthread
= ss
->thread
;
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
629 oldstates
= _hurd_sigstates
;
631 oldstates
= ss
->next
;
634 while (_hurd_sigstates
->next
!= ss
)
635 _hurd_sigstates
= _hurd_sigstates
->next
;
636 _hurd_sigstates
->next
= ss
->next
;
639 _hurd_sigstates
= ss
;
640 __mutex_unlock (&_hurd_siglock
);
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
,
649 /* Forking clears the trace flag. */
650 __sigemptyset (&_hurdsig_traced
);
652 /* Run things that want to run in the child task to set up. */
653 RUN_HOOK (_hurd_fork_child_hook
, ());
655 /* Set up proc server-assisted fault recovery for the signal thread. */
656 _hurdsig_fault_init ();
658 /* Start the signal thread listening on the message port. */
660 err
= __thread_resume (_hurd_msgport_thread
);
662 /* Reclaim the signal thread's sigstate structure and free the
663 other old sigstate structures. */
664 while (oldstates
!= NULL
)
666 struct hurd_sigstate
*next
= oldstates
->next
;
668 if (oldstates
->thread
== _hurd_msgport_thread
)
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
;
682 /* XXX what to do if we have any errors here? */
687 /* Unlock things we locked before creating the child task.
688 They are locked in both the parent and child tasks. */
691 for (p
= symbol_set_first_element (_hurd_fork_locks
);
692 ! symbol_set_end_p (_hurd_fork_locks
, p
);
697 _hurd_critical_section_unlock (ss
);
699 return err
? __hurd_fail (err
) : pid
;
701 libc_hidden_def (__fork
)
703 weak_alias (__fork
, fork
)