1 /* Copyright (C) 1994-2020 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 <https://www.gnu.org/licenses/>. */
21 #include <hurd/signal.h>
22 #include <hurd/threadvar.h>
24 #include <thread_state.h>
25 #include <sysdep.h> /* For stack growth direction. */
26 #include "set-hooks.h"
28 #include "hurdmalloc.h" /* XXX */
30 #include <malloc/malloc-internal.h>
35 /* Things that want to be locked while forking. */
36 symbol_set_declare (_hurd_fork_locks
)
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));
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));
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));
53 /* Things to be run in the child fork. */
54 DEFINE_HOOK (_hurd_fork_child_hook
, (void));
56 /* Things to be run in the parent fork. */
57 DEFINE_HOOK (_hurd_fork_parent_hook
, (void));
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. */
70 struct hurd_sigstate
*volatile ss
;
72 RUN_HOOK (_hurd_atfork_prepare_hook
, ());
74 ss
= _hurd_self_sigstate ();
75 __spin_lock (&ss
->critical_section_lock
);
78 #define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */
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;
96 void resume_threads (void)
103 for (i
= 0; i
< nthreads
; ++i
)
104 if (threads
[i
] != ss
->thread
)
105 __thread_resume (threads
[i
]);
109 /* Run things that prepare for forking before we create the task. */
110 RUN_HOOK (_hurd_fork_prepare_hook
, ());
112 /* Lock things that want to be locked before we fork. */
115 for (p
= symbol_set_first_element (_hurd_fork_locks
);
116 ! symbol_set_end_p (_hurd_fork_locks
, p
);
120 __mutex_lock (&_hurd_siglock
);
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
126 call_function_static_weak (__malloc_fork_lock_parent
);
127 _hurd_malloc_fork_prepare ();
129 newtask
= MACH_PORT_NULL
;
130 thread
= sigthread
= MACH_PORT_NULL
;
131 newproc
= MACH_PORT_NULL
;
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
);
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
);
152 #define XXX_KERNEL_PAGE_FAULT_BUG /* XXX work around page fault bug in mk */
154 #ifdef XXX_KERNEL_PAGE_FAULT_BUG
155 /* Gag me with a pitchfork.
156 The bug scenario is this:
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.
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.
169 __thread_abort (_hurd_msgport_thread
);
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 */
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
);
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
))
191 if (nportnames
!= nporttypes
)
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
))
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
,
209 /* Insert all our port rights into the child task. */
210 thread_refs
= sigthread_refs
= 0;
211 for (i
= 0; i
< nportnames
; ++i
)
213 if (porttypes
[i
] & MACH_PORT_TYPE_RECEIVE
)
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
,
221 if (porttypes
[i
] & MACH_PORT_TYPE_SEND
)
223 /* Give the child as many send rights for its receive
224 right as we have for ours. */
225 mach_port_urefs_t refs
;
227 mach_msg_type_name_t poly
;
228 if (err
= __mach_port_get_refs (__mach_task_self (),
230 MACH_PORT_RIGHT_SEND
,
233 if (err
= __mach_port_extract_right (newtask
,
235 MACH_MSG_TYPE_MAKE_SEND
,
238 if (portnames
[i
] == _hurd_msgport
)
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. */
245 if (err
= __proc_setmsgport (newproc
, port
, &old
))
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. */
253 #ifdef TASK_EXCEPTION_PORT
254 __task_set_special_port (newtask
,
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
)
264 # error task_set_exception_port?
269 if (err
= __mach_port_insert_right (newtask
,
272 MACH_MSG_TYPE_MOVE_SEND
))
275 && (err
= __mach_port_mod_refs (newtask
,
277 MACH_PORT_RIGHT_SEND
,
281 if (porttypes
[i
] & MACH_PORT_TYPE_SEND_ONCE
)
283 /* Give the child a send-once right for its receive right,
284 since we have one for ours. */
286 mach_msg_type_name_t poly
;
287 if (err
= __mach_port_extract_right
290 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
293 if (err
= __mach_port_insert_right
296 MACH_MSG_TYPE_MOVE_SEND_ONCE
))
300 else if (porttypes
[i
]
301 & (MACH_PORT_TYPE_SEND
|MACH_PORT_TYPE_DEAD_NAME
))
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
;
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. */
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. */
315 else if (portnames
[i
] == _hurd_ports
[INIT_PORT_PROC
].port
)
317 /* Use the proc server port for the new task. */
319 insert_type
= MACH_MSG_TYPE_COPY_SEND
;
321 else if (portnames
[i
] == ss
->thread
)
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
]))
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. */
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
]))
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
])
358 /* Copy our own send right. */
359 insert
= portnames
[i
];
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 (),
365 MACH_PORT_RIGHT_SEND
,
366 record_refs
?: &refs
))
368 if (insert
== MACH_PORT_NULL
)
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. */
375 err
= __mach_port_allocate_name (newtask
,
376 MACH_PORT_RIGHT_DEAD_NAME
,
379 /* Insert the chosen send right into the child. */
380 err
= __mach_port_insert_right (newtask
,
382 insert
, insert_type
);
385 case KERN_NAME_EXISTS
:
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
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
,
397 && childport
== insert
398 && __mach_port_deallocate (__mach_task_self (),
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
;
413 /* Give the child as many user references as we have. */
415 && (err
= __mach_port_mod_refs (newtask
,
417 MACH_PORT_RIGHT_SEND
,
424 /* Unlock the standard port cells. The child must unlock its own
426 for (i
= 0; i
< _hurd_nports
; ++i
)
427 __spin_unlock (&_hurd_ports
[i
].lock
);
430 /* All state has now been copied from the parent. It is safe to
431 resume other parent threads. */
434 /* Create the child main user thread and signal thread. */
435 if ((err
= __thread_create (newtask
, &thread
))
436 || (err
= __thread_create (newtask
, &sigthread
)))
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
443 if ((err
= __mach_port_deallocate (newtask
, ss
->thread
))
444 || (err
= __mach_port_insert_right (newtask
, ss
->thread
,
446 MACH_MSG_TYPE_COPY_SEND
)))
448 /* XXX consumed? (_hurd_sigthread is no more) */
450 && (err
= __mach_port_mod_refs (newtask
, ss
->thread
,
451 MACH_PORT_RIGHT_SEND
,
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
,
459 MACH_MSG_TYPE_COPY_SEND
))))
461 if (sigthread_refs
> 1
462 && (err
= __mach_port_mod_refs (newtask
, _hurd_msgport_thread
,
463 MACH_PORT_RIGHT_SEND
,
464 sigthread_refs
- 1)))
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. */
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
));
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
))
489 #ifdef STACK_GROWTH_UP
490 if (__hurd_sigthread_stack_base
== 0)
492 state
.SP
&= __hurd_threadvar_stack_mask
;
493 state
.SP
+= __hurd_threadvar_stack_offset
;
496 state
.SP
= __hurd_sigthread_stack_base
;
498 if (__hurd_sigthread_stack_end
== 0)
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
;
508 state
.SP
= __hurd_sigthread_stack_end
;
510 MACHINE_THREAD_STATE_SET_PC (&state
,
511 (unsigned long int) _hurd_msgport_receive
);
513 /* Do special signal thread setup for TLS if needed. */
514 if (err
= _hurd_tls_fork (sigthread
, _hurd_msgport_thread
, &state
))
517 if (err
= __thread_set_state (sigthread
, MACHINE_THREAD_STATE_FLAVOR
,
518 (natural_t
*) &state
, statecount
))
520 /* We do not thread_resume SIGTHREAD here because the child
521 fork needs to do more setup before it can take signals. */
523 /* Set the child user thread up to return 1 from the setjmp above. */
524 _hurd_longjmp_thread_state (&state
, env
, 1);
526 /* Do special thread setup for TLS if needed. */
527 if (err
= _hurd_tls_fork (thread
, ss
->thread
, &state
))
530 if (err
= __thread_set_state (thread
, MACHINE_THREAD_STATE_FLAVOR
,
531 (natural_t
*) &state
, statecount
))
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
)))
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
)))
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
);
558 for (i
= 0; i
< _hurd_nports
; ++i
)
559 __spin_unlock (&_hurd_ports
[i
].lock
);
563 if (newtask
!= MACH_PORT_NULL
)
566 __task_terminate (newtask
);
567 __mach_port_deallocate (__mach_task_self (), newtask
);
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
);
577 __vm_deallocate (__mach_task_self (),
578 (vm_address_t
) portnames
,
579 nportnames
* sizeof (*portnames
));
581 __vm_deallocate (__mach_task_self (),
582 (vm_address_t
) porttypes
,
583 nporttypes
* sizeof (*porttypes
));
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
));
593 /* Release malloc locks. */
594 _hurd_malloc_fork_parent ();
595 call_function_static_weak (__malloc_fork_unlock_parent
);
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
, ());
606 struct hurd_sigstate
*oldstates
;
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
);
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
621 oldstates
= _hurd_sigstates
;
623 oldstates
= ss
->next
;
626 while (_hurd_sigstates
->next
!= ss
)
627 _hurd_sigstates
= _hurd_sigstates
->next
;
628 _hurd_sigstates
->next
= ss
->next
;
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
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. */
642 _hurd_thread_sigstate_init (_hurd_global_sigstate
, MACH_PORT_NULL
);
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
);
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
);
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
,
660 /* Forking clears the trace flag and pending masks. */
661 __sigemptyset (&_hurdsig_traced
);
662 __sigemptyset (&_hurd_global_sigstate
->pending
);
663 __sigemptyset (&ss
->pending
);
665 /* Release malloc locks. */
666 _hurd_malloc_fork_child ();
667 call_function_static_weak (__malloc_fork_unlock_child
);
669 /* Run things that want to run in the child task to set up. */
670 RUN_HOOK (_hurd_fork_child_hook
, ());
672 /* Set up proc server-assisted fault recovery for the signal thread. */
673 _hurdsig_fault_init ();
675 /* Start the signal thread listening on the message port. */
677 err
= __thread_resume (_hurd_msgport_thread
);
679 /* Reclaim the signal thread's sigstate structure and free the
680 other old sigstate structures. */
681 while (oldstates
!= NULL
)
683 struct hurd_sigstate
*next
= oldstates
->next
;
685 if (oldstates
->thread
== _hurd_msgport_thread
)
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
;
699 /* XXX what to do if we have any errors here? */
704 /* Unlock things we locked before creating the child task.
705 They are locked in both the parent and child tasks. */
708 for (p
= symbol_set_first_element (_hurd_fork_locks
);
709 ! symbol_set_end_p (_hurd_fork_locks
, p
);
714 _hurd_critical_section_unlock (ss
);
719 RUN_HOOK (_hurd_atfork_parent_hook
, ());
721 RUN_HOOK (_hurd_atfork_child_hook
, ());
724 return err
? __hurd_fail (err
) : pid
;
726 libc_hidden_def (__fork
)
728 weak_alias (__fork
, fork
)