1 /* Copyright (C) 1994-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <hurd/signal.h>
23 #include <thread_state.h>
24 #include <sysdep.h> /* For stack growth direction. */
25 #include "set-hooks.h"
27 #include "hurdmalloc.h" /* XXX */
29 #include <malloc/malloc-internal.h>
34 /* Things that want to be locked while forking. */
35 symbol_set_declare (_hurd_fork_locks
)
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));
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));
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));
52 /* Things to be run in the child fork. */
53 DEFINE_HOOK (_hurd_fork_child_hook
, (void));
55 /* Things to be run in the parent fork. */
56 DEFINE_HOOK (_hurd_fork_parent_hook
, (void));
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. */
69 struct hurd_sigstate
*volatile ss
;
71 RUN_HOOK (_hurd_atfork_prepare_hook
, ());
73 ss
= _hurd_self_sigstate ();
74 __spin_lock (&ss
->critical_section_lock
);
77 #define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */
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;
95 void resume_threads (void)
102 for (i
= 0; i
< nthreads
; ++i
)
103 if (threads
[i
] != ss
->thread
)
104 __thread_resume (threads
[i
]);
108 /* Run things that prepare for forking before we create the task. */
109 RUN_HOOK (_hurd_fork_prepare_hook
, ());
111 /* Lock things that want to be locked before we fork. */
114 for (p
= symbol_set_first_element (_hurd_fork_locks
);
115 ! symbol_set_end_p (_hurd_fork_locks
, p
);
119 __mutex_lock (&_hurd_siglock
);
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
125 call_function_static_weak (__malloc_fork_lock_parent
);
126 _hurd_malloc_fork_prepare ();
128 newtask
= MACH_PORT_NULL
;
129 thread
= sigthread
= MACH_PORT_NULL
;
130 newproc
= MACH_PORT_NULL
;
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
);
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
);
151 #define XXX_KERNEL_PAGE_FAULT_BUG /* XXX work around page fault bug in mk */
153 #ifdef XXX_KERNEL_PAGE_FAULT_BUG
154 /* Gag me with a pitchfork.
155 The bug scenario is this:
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.
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.
168 __thread_abort (_hurd_msgport_thread
);
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 */
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
);
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
))
190 if (nportnames
!= nporttypes
)
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
))
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
,
208 /* Insert all our port rights into the child task. */
209 thread_refs
= sigthread_refs
= 0;
210 for (i
= 0; i
< nportnames
; ++i
)
212 if (porttypes
[i
] & MACH_PORT_TYPE_RECEIVE
)
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
,
219 if (err
== KERN_NAME_EXISTS
)
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. */
228 #define TASK_NOTIFY_PORT 2
230 assert (({ mach_port_t thisport
, notify_port
;
231 mach_msg_type_name_t poly
;
232 (__task_get_special_port (newtask
,
234 ¬ify_port
) == 0 &&
235 __mach_port_extract_right
238 MACH_MSG_TYPE_MAKE_SEND
,
239 &thisport
, &poly
) == 0 &&
240 (thisport
== notify_port
) &&
241 __mach_port_deallocate (__mach_task_self (),
243 __mach_port_deallocate (__mach_task_self (),
249 if (porttypes
[i
] & MACH_PORT_TYPE_SEND
)
251 /* Give the child as many send rights for its receive
252 right as we have for ours. */
253 mach_port_urefs_t refs
;
255 mach_msg_type_name_t poly
;
256 if (err
= __mach_port_get_refs (__mach_task_self (),
258 MACH_PORT_RIGHT_SEND
,
261 if (err
= __mach_port_extract_right (newtask
,
263 MACH_MSG_TYPE_MAKE_SEND
,
266 if (portnames
[i
] == _hurd_msgport
)
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. */
273 if (err
= __proc_setmsgport (newproc
, port
, &old
))
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. */
281 #ifdef TASK_EXCEPTION_PORT
282 __task_set_special_port (newtask
,
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
)
292 # error task_set_exception_port?
297 if (err
= __mach_port_insert_right (newtask
,
300 MACH_MSG_TYPE_MOVE_SEND
))
303 (err
= __mach_port_mod_refs (newtask
,
305 MACH_PORT_RIGHT_SEND
,
309 if (porttypes
[i
] & MACH_PORT_TYPE_SEND_ONCE
)
311 /* Give the child a send-once right for its receive right,
312 since we have one for ours. */
314 mach_msg_type_name_t poly
;
315 if (err
= __mach_port_extract_right
318 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
321 if (err
= __mach_port_insert_right
324 MACH_MSG_TYPE_MOVE_SEND_ONCE
))
328 else if (porttypes
[i
] &
329 (MACH_PORT_TYPE_SEND
|MACH_PORT_TYPE_DEAD_NAME
))
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
;
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. */
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. */
343 else if (portnames
[i
] == _hurd_ports
[INIT_PORT_PROC
].port
)
345 /* Use the proc server port for the new task. */
347 insert_type
= MACH_MSG_TYPE_COPY_SEND
;
349 else if (portnames
[i
] == ss
->thread
)
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
]))
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. */
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
]))
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
])
386 /* Copy our own send right. */
387 insert
= portnames
[i
];
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 (),
393 MACH_PORT_RIGHT_SEND
,
394 record_refs
?: &refs
))
396 if (insert
== MACH_PORT_NULL
)
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. */
403 err
= __mach_port_allocate_name (newtask
,
404 MACH_PORT_RIGHT_DEAD_NAME
,
407 /* Insert the chosen send right into the child. */
408 err
= __mach_port_insert_right (newtask
,
410 insert
, insert_type
);
413 case KERN_NAME_EXISTS
:
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
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
,
425 childport
== insert
&&
426 __mach_port_deallocate (__mach_task_self (),
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
;
441 /* Give the child as many user references as we have. */
443 (err
= __mach_port_mod_refs (newtask
,
445 MACH_PORT_RIGHT_SEND
,
452 /* Unlock the standard port cells. The child must unlock its own
454 for (i
= 0; i
< _hurd_nports
; ++i
)
455 __spin_unlock (&_hurd_ports
[i
].lock
);
458 /* All state has now been copied from the parent. It is safe to
459 resume other parent threads. */
462 /* Create the child main user thread and signal thread. */
463 if ((err
= __thread_create (newtask
, &thread
)) ||
464 (err
= __thread_create (newtask
, &sigthread
)))
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
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
)))
475 if (thread_refs
> 1 &&
476 (err
= __mach_port_mod_refs (newtask
, ss
->thread
,
477 MACH_PORT_RIGHT_SEND
,
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
,
484 MACH_MSG_TYPE_COPY_SEND
))))
486 if (sigthread_refs
> 1 &&
487 (err
= __mach_port_mod_refs (newtask
, _hurd_msgport_thread
,
488 MACH_PORT_RIGHT_SEND
,
489 sigthread_refs
- 1)))
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. */
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
));
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
))
514 #ifdef STACK_GROWTH_UP
515 #define THREADVAR_SPACE (__hurd_threadvar_max \
516 * sizeof *__hurd_sightread_variables)
517 if (__hurd_sigthread_stack_base
== 0)
519 state
.SP
&= __hurd_threadvar_stack_mask
;
520 state
.SP
+= __hurd_threadvar_stack_offset
+ THREADVAR_SPACE
;
523 state
.SP
= __hurd_sigthread_stack_base
;
525 if (__hurd_sigthread_stack_end
== 0)
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
;
535 state
.SP
= __hurd_sigthread_stack_end
;
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
))
542 /* We do not thread_resume SIGTHREAD here because the child
543 fork needs to do more setup before it can take signals. */
545 /* Set the child user thread up to return 1 from the setjmp above. */
546 _hurd_longjmp_thread_state (&state
, env
, 1);
548 /* Do special thread setup for TLS if needed. */
549 if (err
= _hurd_tls_fork (thread
, &state
))
552 if (err
= __thread_set_state (thread
, MACHINE_THREAD_STATE_FLAVOR
,
553 (natural_t
*) &state
, statecount
))
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
)))
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
)))
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
);
580 for (i
= 0; i
< _hurd_nports
; ++i
)
581 __spin_unlock (&_hurd_ports
[i
].lock
);
585 if (newtask
!= MACH_PORT_NULL
)
588 __task_terminate (newtask
);
589 __mach_port_deallocate (__mach_task_self (), newtask
);
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
);
599 __vm_deallocate (__mach_task_self (),
600 (vm_address_t
) portnames
,
601 nportnames
* sizeof (*portnames
));
603 __vm_deallocate (__mach_task_self (),
604 (vm_address_t
) porttypes
,
605 nporttypes
* sizeof (*porttypes
));
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
));
615 /* Release malloc locks. */
616 _hurd_malloc_fork_parent ();
617 call_function_static_weak (__malloc_fork_unlock_parent
);
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
, ());
628 struct hurd_sigstate
*oldstates
;
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
);
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
;
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
647 oldstates
= _hurd_sigstates
;
649 oldstates
= ss
->next
;
652 while (_hurd_sigstates
->next
!= ss
)
653 _hurd_sigstates
= _hurd_sigstates
->next
;
654 _hurd_sigstates
->next
= ss
->next
;
657 _hurd_sigstates
= ss
;
658 __mutex_unlock (&_hurd_siglock
);
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
,
667 /* Forking clears the trace flag. */
668 __sigemptyset (&_hurdsig_traced
);
670 /* Release malloc locks. */
671 _hurd_malloc_fork_child ();
672 call_function_static_weak (__malloc_fork_unlock_child
);
674 /* Run things that want to run in the child task to set up. */
675 RUN_HOOK (_hurd_fork_child_hook
, ());
677 /* Set up proc server-assisted fault recovery for the signal thread. */
678 _hurdsig_fault_init ();
680 /* Start the signal thread listening on the message port. */
682 err
= __thread_resume (_hurd_msgport_thread
);
684 /* Reclaim the signal thread's sigstate structure and free the
685 other old sigstate structures. */
686 while (oldstates
!= NULL
)
688 struct hurd_sigstate
*next
= oldstates
->next
;
690 if (oldstates
->thread
== _hurd_msgport_thread
)
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
;
704 /* XXX what to do if we have any errors here? */
709 /* Unlock things we locked before creating the child task.
710 They are locked in both the parent and child tasks. */
713 for (p
= symbol_set_first_element (_hurd_fork_locks
);
714 ! symbol_set_end_p (_hurd_fork_locks
, p
);
719 _hurd_critical_section_unlock (ss
);
724 RUN_HOOK (_hurd_atfork_parent_hook
, ());
726 RUN_HOOK (_hurd_atfork_child_hook
, ());
729 return err
? __hurd_fail (err
) : pid
;
731 libc_hidden_def (__fork
)
733 weak_alias (__fork
, fork
)