1 /* Copyright (C) 1994-2019 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>
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
,
445 thread
, MACH_MSG_TYPE_COPY_SEND
)))
447 if (thread_refs
> 1 &&
448 (err
= __mach_port_mod_refs (newtask
, ss
->thread
,
449 MACH_PORT_RIGHT_SEND
,
452 if ((_hurd_msgport_thread
!= MACH_PORT_NULL
) /* Let user have none. */
453 && ((err
= __mach_port_deallocate (newtask
, _hurd_msgport_thread
)) ||
454 (err
= __mach_port_insert_right (newtask
, _hurd_msgport_thread
,
456 MACH_MSG_TYPE_COPY_SEND
))))
458 if (sigthread_refs
> 1 &&
459 (err
= __mach_port_mod_refs (newtask
, _hurd_msgport_thread
,
460 MACH_PORT_RIGHT_SEND
,
461 sigthread_refs
- 1)))
464 /* This seems like a convenient juncture to copy the proc server's
465 idea of what addresses our argv and envp are found at from the
466 parent into the child. Since we happen to know that the child
467 shares our memory image, it is we who should do this copying. */
469 vm_address_t argv
, envp
;
470 err
= (__USEPORT (PROC
, __proc_get_arg_locations (port
, &argv
, &envp
))
471 ?: __proc_set_arg_locations (newproc
, argv
, envp
));
476 /* Set the child signal thread up to run the msgport server function
477 using the same signal thread stack copied from our address space.
478 We fetch the state before longjmp'ing it so that miscellaneous
479 registers not affected by longjmp (such as i386 segment registers)
480 are in their normal default state. */
481 statecount
= MACHINE_THREAD_STATE_COUNT
;
482 if (err
= __thread_get_state (_hurd_msgport_thread
,
483 MACHINE_THREAD_STATE_FLAVOR
,
484 (natural_t
*) &state
, &statecount
))
486 #ifdef STACK_GROWTH_UP
487 if (__hurd_sigthread_stack_base
== 0)
489 state
.SP
&= __hurd_threadvar_stack_mask
;
490 state
.SP
+= __hurd_threadvar_stack_offset
;
493 state
.SP
= __hurd_sigthread_stack_base
;
495 if (__hurd_sigthread_stack_end
== 0)
497 /* The signal thread has a stack assigned by cthreads.
498 The threadvar_stack variables conveniently tell us how
499 to get to the highest address in the stack, just below
500 the per-thread variables. */
501 state
.SP
&= __hurd_threadvar_stack_mask
;
502 state
.SP
+= __hurd_threadvar_stack_offset
;
505 state
.SP
= __hurd_sigthread_stack_end
;
507 MACHINE_THREAD_STATE_SET_PC (&state
,
508 (unsigned long int) _hurd_msgport_receive
);
510 /* Do special signal thread setup for TLS if needed. */
511 if (err
= _hurd_tls_fork (sigthread
, _hurd_msgport_thread
, &state
))
514 if (err
= __thread_set_state (sigthread
, MACHINE_THREAD_STATE_FLAVOR
,
515 (natural_t
*) &state
, statecount
))
517 /* We do not thread_resume SIGTHREAD here because the child
518 fork needs to do more setup before it can take signals. */
520 /* Set the child user thread up to return 1 from the setjmp above. */
521 _hurd_longjmp_thread_state (&state
, env
, 1);
523 /* Do special thread setup for TLS if needed. */
524 if (err
= _hurd_tls_fork (thread
, ss
->thread
, &state
))
527 if (err
= __thread_set_state (thread
, MACHINE_THREAD_STATE_FLAVOR
,
528 (natural_t
*) &state
, statecount
))
531 /* Get the PID of the child from the proc server. We must do this
532 before calling proc_child below, because at that point any
533 authorized POSIX.1 process may kill the child task with SIGKILL. */
534 if (err
= __USEPORT (PROC
, __proc_task2pid (port
, newtask
, &pid
)))
537 /* Register the child with the proc server. It is important that
538 this be that last thing we do before starting the child thread
539 running. Once proc_child has been done for the task, it appears
540 as a POSIX.1 process. Any errors we get must be detected before
541 this point, and the child must have a message port so it responds
542 to POSIX.1 signals. */
543 if (err
= __USEPORT (PROC
, __proc_child (port
, newtask
)))
546 /* This must be the absolutely last thing we do; we can't assume that
547 the child will remain alive for even a moment once we do this. We
548 ignore errors because we have committed to the fork and are not
549 allowed to return them after the process becomes visible to
550 POSIX.1 (which happened right above when we called proc_child). */
551 (void) __thread_resume (thread
);
555 for (i
= 0; i
< _hurd_nports
; ++i
)
556 __spin_unlock (&_hurd_ports
[i
].lock
);
560 if (newtask
!= MACH_PORT_NULL
)
563 __task_terminate (newtask
);
564 __mach_port_deallocate (__mach_task_self (), newtask
);
566 if (thread
!= MACH_PORT_NULL
)
567 __mach_port_deallocate (__mach_task_self (), thread
);
568 if (sigthread
!= MACH_PORT_NULL
)
569 __mach_port_deallocate (__mach_task_self (), sigthread
);
570 if (newproc
!= MACH_PORT_NULL
)
571 __mach_port_deallocate (__mach_task_self (), newproc
);
574 __vm_deallocate (__mach_task_self (),
575 (vm_address_t
) portnames
,
576 nportnames
* sizeof (*portnames
));
578 __vm_deallocate (__mach_task_self (),
579 (vm_address_t
) porttypes
,
580 nporttypes
* sizeof (*porttypes
));
583 for (i
= 0; i
< nthreads
; ++i
)
584 __mach_port_deallocate (__mach_task_self (), threads
[i
]);
585 __vm_deallocate (__mach_task_self (),
586 (vm_address_t
) threads
,
587 nthreads
* sizeof (*threads
));
590 /* Release malloc locks. */
591 _hurd_malloc_fork_parent ();
592 call_function_static_weak (__malloc_fork_unlock_parent
);
594 /* Run things that want to run in the parent to restore it to
595 normality. Usually prepare hooks and parent hooks are
596 symmetrical: the prepare hook arrests state in some way for the
597 fork, and the parent hook restores the state for the parent to
598 continue executing normally. */
599 RUN_HOOK (_hurd_fork_parent_hook
, ());
603 struct hurd_sigstate
*oldstates
;
605 /* We are the child task. Unlock the standard port cells, which were
606 locked in the parent when we copied its memory. The parent has
607 inserted send rights with the names that were in the cells then. */
608 for (i
= 0; i
< _hurd_nports
; ++i
)
609 __spin_unlock (&_hurd_ports
[i
].lock
);
611 /* We are one of the (exactly) two threads in this new task, we
612 will take the task-global signals. */
613 _hurd_sigthread
= ss
->thread
;
615 /* Claim our sigstate structure and unchain the rest: the
616 threads existed in the parent task but don't exist in this
617 task (the child process). Delay freeing them until later
618 because some of the further setup and unlocking might be
619 required for free to work. Before we finish cleaning up,
620 we will reclaim the signal thread's sigstate structure (if
622 oldstates
= _hurd_sigstates
;
624 oldstates
= ss
->next
;
627 while (_hurd_sigstates
->next
!= ss
)
628 _hurd_sigstates
= _hurd_sigstates
->next
;
629 _hurd_sigstates
->next
= ss
->next
;
632 _hurd_sigstates
= ss
;
633 __mutex_unlock (&_hurd_siglock
);
635 /* Fetch our new process IDs from the proc server. No need to
636 refetch our pgrp; it is always inherited from the parent (so
637 _hurd_pgrp is already correct), and the proc server will send us a
638 proc_newids notification when it changes. */
639 err
= __USEPORT (PROC
, __proc_getpids (port
, &_hurd_pid
, &_hurd_ppid
,
642 /* Forking clears the trace flag. */
643 __sigemptyset (&_hurdsig_traced
);
645 /* Release malloc locks. */
646 _hurd_malloc_fork_child ();
647 call_function_static_weak (__malloc_fork_unlock_child
);
649 /* Run things that want to run in the child task to set up. */
650 RUN_HOOK (_hurd_fork_child_hook
, ());
652 /* Set up proc server-assisted fault recovery for the signal thread. */
653 _hurdsig_fault_init ();
655 /* Start the signal thread listening on the message port. */
657 err
= __thread_resume (_hurd_msgport_thread
);
659 /* Reclaim the signal thread's sigstate structure and free the
660 other old sigstate structures. */
661 while (oldstates
!= NULL
)
663 struct hurd_sigstate
*next
= oldstates
->next
;
665 if (oldstates
->thread
== _hurd_msgport_thread
)
667 /* If we have a second signal state structure then we
668 must have been through here before--not good. */
669 assert (_hurd_sigstates
->next
== 0);
670 _hurd_sigstates
->next
= oldstates
;
679 /* XXX what to do if we have any errors here? */
684 /* Unlock things we locked before creating the child task.
685 They are locked in both the parent and child tasks. */
688 for (p
= symbol_set_first_element (_hurd_fork_locks
);
689 ! symbol_set_end_p (_hurd_fork_locks
, p
);
694 _hurd_critical_section_unlock (ss
);
699 RUN_HOOK (_hurd_atfork_parent_hook
, ());
701 RUN_HOOK (_hurd_atfork_child_hook
, ());
704 return err
? __hurd_fail (err
) : pid
;
706 libc_hidden_def (__fork
)
708 weak_alias (__fork
, fork
)