kern_fork.c revision 1.236 1 /* $NetBSD: kern_fork.c,v 1.236 2026/08/28 13:58:27 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008, 2019
5 * The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 1982, 1986, 1989, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.236 2026/08/28 13:58:27 riastradh Exp $");
72
73 #include "opt_ktrace.h"
74 #include "opt_dtrace.h"
75
76 #include <sys/param.h>
77 #include <sys/types.h>
78
79 #include <sys/acct.h>
80 #include <sys/atomic.h>
81 #include <sys/file.h>
82 #include <sys/filedesc.h>
83 #include <sys/kauth.h>
84 #include <sys/kernel.h>
85 #include <sys/ktrace.h>
86 #include <sys/mount.h>
87 #include <sys/pool.h>
88 #include <sys/proc.h>
89 #include <sys/ptrace.h>
90 #include <sys/ras.h>
91 #include <sys/resourcevar.h>
92 #include <sys/sched.h>
93 #include <sys/sdt.h>
94 #include <sys/signalvar.h>
95 #include <sys/syscall.h>
96 #include <sys/syscallargs.h>
97 #include <sys/systm.h>
98 #include <sys/uidinfo.h>
99 #include <sys/vnode.h>
100
101 /*
102 * DTrace SDT provider definitions
103 */
104 SDT_PROVIDER_DECLARE(proc);
105 SDT_PROBE_DEFINE3(proc, kernel, , create,
106 "struct proc *", /* new process */
107 "struct proc *", /* parent process */
108 "int" /* flags */);
109
110 u_int nprocs __cacheline_aligned = 1; /* process 0 */
111
112 /*
113 * Number of ticks to sleep if fork() would fail due to process hitting
114 * limits. Exported in milliseconds to userland via sysctl.
115 */
116 int forkfsleep = 0;
117
118 int
119 sys_fork(struct lwp *l, const void *v, register_t *retval)
120 {
121
122 return fork1(l, 0, SIGCHLD, NULL, 0, NULL, NULL, retval);
123 }
124
125 /*
126 * vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
127 * Address space is not shared, but parent is blocked until child exit.
128 */
129 int
130 sys_vfork(struct lwp *l, const void *v, register_t *retval)
131 {
132
133 return fork1(l, FORK_PPWAIT, SIGCHLD, NULL, 0, NULL, NULL,
134 retval);
135 }
136
137 /*
138 * New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
139 * semantics. Address space is shared, and parent is blocked until child exit.
140 */
141 int
142 sys___vfork14(struct lwp *l, const void *v, register_t *retval)
143 {
144
145 return fork1(l, FORK_PPWAIT|FORK_SHAREVM, SIGCHLD, NULL, 0,
146 NULL, NULL, retval);
147 }
148
149 /*
150 * Linux-compatible __clone(2) system call.
151 */
152 int
153 sys___clone(struct lwp *l, const struct sys___clone_args *uap,
154 register_t *retval)
155 {
156 /* {
157 syscallarg(int) flags;
158 syscallarg(void *) stack;
159 } */
160 int flags, sig;
161
162 /*
163 * We don't support the CLONE_PTRACE flag.
164 */
165 if (SCARG(uap, flags) & CLONE_PTRACE)
166 return SET_ERROR(EINVAL);
167
168 /*
169 * Linux enforces CLONE_VM with CLONE_SIGHAND, do same.
170 */
171 if (SCARG(uap, flags) & CLONE_SIGHAND
172 && (SCARG(uap, flags) & CLONE_VM) == 0)
173 return SET_ERROR(EINVAL);
174
175 flags = 0;
176
177 if (SCARG(uap, flags) & CLONE_VM)
178 flags |= FORK_SHAREVM;
179 if (SCARG(uap, flags) & CLONE_FS)
180 flags |= FORK_SHARECWD;
181 if (SCARG(uap, flags) & CLONE_FILES)
182 flags |= FORK_SHAREFILES;
183 if (SCARG(uap, flags) & CLONE_SIGHAND)
184 flags |= FORK_SHARESIGS;
185 if (SCARG(uap, flags) & CLONE_VFORK)
186 flags |= FORK_PPWAIT;
187
188 sig = SCARG(uap, flags) & CLONE_CSIGNAL;
189 if (sig < 0 || sig >= _NSIG)
190 return SET_ERROR(EINVAL);
191
192 /*
193 * Linux doesn't have close-on-fork yet, so we don't
194 * know what they will do combining CLONE_FILES with
195 * close-on-fork (which are not really compatible).
196 * This might need to be changed in the future (another
197 * option would be to just disable FORK_SHAREFILES)
198 */
199 if ((flags & FORK_SHAREFILES) != 0) {
200 if (l->l_fd != NULL && l->l_fd->fd_foclose)
201 return SET_ERROR(EINVAL);
202 }
203
204 /*
205 * Note that the Linux API does not provide a portable way of
206 * specifying the stack area; the caller must know if the stack
207 * grows up or down. So, we pass a stack size of 0, so that the
208 * code that makes this adjustment is a noop.
209 */
210 return fork1(l, flags, sig, SCARG(uap, stack), 0,
211 NULL, NULL, retval);
212 }
213
214 /*
215 * Print the 'table full' message once per 10 seconds.
216 */
217 static struct timeval fork_tfmrate = { 10, 0 };
218
219 /*
220 * Check if a process is traced and shall inform about FORK events.
221 */
222 static inline bool
223 tracefork(struct proc *p, int flags)
224 {
225
226 return (p->p_slflag & (PSL_TRACEFORK|PSL_TRACED)) ==
227 (PSL_TRACEFORK|PSL_TRACED) && (flags & FORK_PPWAIT) == 0;
228 }
229
230 /*
231 * Check if a process is traced and shall inform about VFORK events.
232 */
233 static inline bool
234 tracevfork(struct proc *p, int flags)
235 {
236
237 return (p->p_slflag & (PSL_TRACEVFORK|PSL_TRACED)) ==
238 (PSL_TRACEVFORK|PSL_TRACED) && (flags & FORK_PPWAIT) != 0;
239 }
240
241 /*
242 * Check if a process is traced and shall inform about VFORK_DONE events.
243 */
244 static inline bool
245 tracevforkdone(struct proc *p, int flags)
246 {
247
248 return (p->p_slflag & (PSL_TRACEVFORK_DONE|PSL_TRACED)) ==
249 (PSL_TRACEVFORK_DONE|PSL_TRACED) && (flags & FORK_PPWAIT);
250 }
251
252 /*
253 * General fork call. Note that another LWP in the process may call exec()
254 * or exit() while we are forking. It's safe to continue here, because
255 * neither operation will complete until all LWPs have exited the process.
256 */
257 int
258 fork1(struct lwp *l1, int flags, int exitsig, void *stack, size_t stacksize,
259 void (*func)(void *), void *arg, register_t *retval)
260 {
261 struct proc *p1, *p2, *parent;
262 struct plimit *p1_lim;
263 uid_t uid;
264 struct lwp *l2;
265 int count;
266 vaddr_t uaddr;
267 int tnprocs;
268 int error = 0;
269
270 p1 = l1->l_proc;
271 uid = kauth_cred_getuid(l1->l_cred);
272 tnprocs = atomic_inc_uint_nv(&nprocs);
273
274 /*
275 * Although process entries are dynamically created, we still keep
276 * a global limit on the maximum number we will create.
277 */
278 if (__predict_false(tnprocs >= maxproc))
279 error = -1;
280 else
281 error = kauth_authorize_process(l1->l_cred,
282 KAUTH_PROCESS_FORK, p1, KAUTH_ARG(tnprocs), NULL, NULL);
283
284 if (error) {
285 static struct timeval lasttfm;
286 atomic_dec_uint(&nprocs);
287 if (ratecheck(&lasttfm, &fork_tfmrate))
288 tablefull("proc", "increase kern.maxproc or NPROC");
289 if (forkfsleep)
290 kpause("forkmx", false, forkfsleep, NULL);
291 return SET_ERROR(EAGAIN);
292 }
293
294 /*
295 * Enforce limits.
296 */
297 count = chgproccnt(uid, 1);
298 if (__predict_false(count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)) {
299 if (kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_RLIMIT,
300 p1, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS),
301 &p1->p_rlimit[RLIMIT_NPROC], KAUTH_ARG(RLIMIT_NPROC)) != 0) {
302 (void)chgproccnt(uid, -1);
303 atomic_dec_uint(&nprocs);
304 if (forkfsleep)
305 kpause("forkulim", false, forkfsleep, NULL);
306 return SET_ERROR(EAGAIN);
307 }
308 }
309
310 /*
311 * Allocate virtual address space for the U-area now, while it
312 * is still easy to abort the fork operation if we're out of
313 * kernel virtual address space.
314 */
315 uaddr = uvm_uarea_alloc();
316 if (__predict_false(uaddr == 0)) {
317 (void)chgproccnt(uid, -1);
318 atomic_dec_uint(&nprocs);
319 return SET_ERROR(ENOMEM);
320 }
321
322 /* Allocate new proc. */
323 p2 = proc_alloc();
324 if (p2 == NULL) {
325 /* We were unable to allocate a process ID. */
326 uvm_uarea_free(uaddr);
327 mutex_enter(p1->p_lock);
328 uid = kauth_cred_getuid(p1->p_cred);
329 (void)chgproccnt(uid, -1);
330 mutex_exit(p1->p_lock);
331 atomic_dec_uint(&nprocs);
332 return SET_ERROR(EAGAIN);
333 }
334
335 /*
336 * We are now committed to the fork. From here on, we may
337 * block on resources, but resource allocation may NOT fail.
338 */
339
340 /*
341 * Make a proc table entry for the new process.
342 * Start by zeroing the section of proc that is zero-initialized,
343 * then copy the section that is copied directly from the parent.
344 */
345 memset(&p2->p_startzero, 0,
346 (unsigned) ((char *)&p2->p_endzero - (char *)&p2->p_startzero));
347 memcpy(&p2->p_startcopy, &p1->p_startcopy,
348 (unsigned) ((char *)&p2->p_endcopy - (char *)&p2->p_startcopy));
349
350 TAILQ_INIT(&p2->p_sigpend.sp_info);
351
352 LIST_INIT(&p2->p_lwps);
353 LIST_INIT(&p2->p_sigwaiters);
354
355 /*
356 * Duplicate sub-structures as needed.
357 * Increase reference counts on shared objects.
358 * Inherit flags we want to keep. The flags related to SIGCHLD
359 * handling are important in order to keep a consistent behaviour
360 * for the child after the fork. If we are a 32-bit process, the
361 * child will be too.
362 */
363 p2->p_flag =
364 p1->p_flag & (PK_SUGID | PK_NOCLDWAIT | PK_CLDSIGIGN | PK_32);
365 p2->p_emul = p1->p_emul;
366 p2->p_execsw = p1->p_execsw;
367
368 if (flags & FORK_SYSTEM) {
369 /*
370 * Mark it as a system process. Set P_NOCLDWAIT so that
371 * children are reparented to init(8) when they exit.
372 * init(8) can easily wait them out for us.
373 */
374 p2->p_flag |= (PK_SYSTEM | PK_NOCLDWAIT);
375 }
376
377 mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
378 mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
379 rw_init(&p2->p_reflock);
380 cv_init(&p2->p_waitcv, "wait");
381 cv_init(&p2->p_lwpcv, "lwpwait");
382
383 /*
384 * Share a lock between the processes if they are to share signal
385 * state: we must synchronize access to it.
386 */
387 if (flags & FORK_SHARESIGS) {
388 p2->p_lock = p1->p_lock;
389 mutex_obj_hold(p1->p_lock);
390 } else
391 p2->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
392
393 kauth_proc_fork(p1, p2);
394
395 p2->p_raslist = NULL;
396 #if defined(__HAVE_RAS)
397 ras_fork(p1, p2);
398 #endif
399
400 /* bump references to the text vnode (for procfs) */
401 p2->p_textvp = p1->p_textvp;
402 if (p2->p_textvp)
403 vref(p2->p_textvp);
404 if (p1->p_path)
405 p2->p_path = kmem_strdupsize(p1->p_path, NULL, KM_SLEEP);
406 else
407 p2->p_path = NULL;
408
409 if (flags & FORK_SHAREFILES)
410 fd_share(p2);
411 else if (flags & FORK_CLEANFILES)
412 p2->p_fd = fd_init(NULL);
413 else
414 p2->p_fd = fd_copy();
415
416 /* XXX racy */
417 p2->p_mqueue_cnt = p1->p_mqueue_cnt;
418
419 if (flags & FORK_SHARECWD)
420 cwdshare(p2);
421 else
422 p2->p_cwdi = cwdinit();
423
424 /*
425 * Note: p_limit (rlimit stuff) is copy-on-write, so normally
426 * we just need increase pl_refcnt.
427 */
428 p1_lim = p1->p_limit;
429 if (!p1_lim->pl_writeable) {
430 lim_addref(p1_lim);
431 p2->p_limit = p1_lim;
432 } else {
433 p2->p_limit = lim_copy(p1_lim);
434 }
435
436 if (flags & FORK_PPWAIT) {
437 /* Mark ourselves as waiting for a child. */
438 p2->p_lflag = PL_PPWAIT;
439 l1->l_vforkwaiting = true;
440 p2->p_vforklwp = l1;
441 } else {
442 p2->p_lflag = 0;
443 l1->l_vforkwaiting = false;
444 }
445 p2->p_sflag = 0;
446 p2->p_slflag = 0;
447 parent = (flags & FORK_NOWAIT) ? initproc : p1;
448 p2->p_pptr = parent;
449 p2->p_ppid = parent->p_pid;
450 LIST_INIT(&p2->p_children);
451
452 p2->p_aio = NULL;
453
454 #ifdef KTRACE
455 /*
456 * Copy traceflag and tracefile if enabled.
457 * If not inherited, these were zeroed above.
458 */
459 if (p1->p_traceflag & KTRFAC_INHERIT) {
460 mutex_enter(&ktrace_lock);
461 p2->p_traceflag = p1->p_traceflag;
462 if ((p2->p_tracep = p1->p_tracep) != NULL)
463 ktradref(p2);
464 mutex_exit(&ktrace_lock);
465 }
466 #endif
467
468 /*
469 * Create signal actions for the child process.
470 */
471 p2->p_sigacts = sigactsinit(p1, flags & FORK_SHARESIGS);
472 mutex_enter(p1->p_lock);
473 p2->p_sflag |=
474 (p1->p_sflag & (PS_STOPFORK | PS_STOPEXEC | PS_NOCLDSTOP));
475 sched_proc_fork(p1, p2);
476 mutex_exit(p1->p_lock);
477
478 p2->p_stflag = p1->p_stflag;
479
480 /*
481 * p_stats.
482 * Copy parts of p_stats, and zero out the rest.
483 */
484 p2->p_stats = pstatscopy(p1->p_stats);
485
486 /*
487 * Set up the new process address space.
488 */
489 uvm_proc_fork(p1, p2, (flags & FORK_SHAREVM) ? true : false);
490
491 /*
492 * Finish creating the child process.
493 * It will return through a different path later.
494 */
495 lwp_create(l1, p2, uaddr, (flags & FORK_PPWAIT) ? LWP_VFORK : 0,
496 stack, stacksize, (func != NULL) ? func : child_return, arg, &l2,
497 l1->l_class, &l1->l_sigmask, &l1->l_sigstk);
498
499 /*
500 * Inherit l_private from the parent.
501 * Note that we cannot use lwp_setprivate() here since that
502 * also sets the CPU TLS register, which is incorrect if the
503 * process has changed that without letting the kernel know.
504 */
505 l2->l_private = l1->l_private;
506
507 /*
508 * If emulation has a process fork hook, call it now.
509 */
510 if (p2->p_emul->e_proc_fork)
511 (*p2->p_emul->e_proc_fork)(p2, l1, flags);
512
513 /*
514 * ...and finally, any other random fork hooks that subsystems
515 * might have registered.
516 */
517 doforkhooks(p2, p1);
518
519 SDT_PROBE(proc, kernel, , create, p2, p1, flags, 0, 0);
520
521 /*
522 * It's now safe for the scheduler and other processes to see the
523 * child process.
524 */
525 mutex_enter(&proc_lock);
526
527 if (p1->p_session->s_ttyvp != NULL && p1->p_lflag & PL_CONTROLT)
528 p2->p_lflag |= PL_CONTROLT;
529
530 LIST_INSERT_HEAD(&parent->p_children, p2, p_sibling);
531 p2->p_exitsig = exitsig; /* signal for parent on exit */
532
533 /*
534 * Trace fork(2) and vfork(2)-like events on demand in a debugger.
535 */
536 if (tracefork(p1, flags) || tracevfork(p1, flags)) {
537 proc_changeparent(p2, p1->p_pptr);
538 SET(p2->p_slflag, PSL_TRACEDCHILD);
539 }
540
541 p2->p_oppid = p1->p_pid; /* Remember the original parent id. */
542
543 LIST_INSERT_AFTER(p1, p2, p_pglist);
544 LIST_INSERT_HEAD(&allproc, p2, p_list);
545
546 p2->p_trace_enabled = trace_is_enabled(p2);
547 #ifdef __HAVE_SYSCALL_INTERN
548 (*p2->p_emul->e_syscall_intern)(p2);
549 #endif
550
551 /*
552 * Update stats now that we know the fork was successful.
553 */
554 KPREEMPT_DISABLE(l1);
555 CPU_COUNT(CPU_COUNT_FORKS, 1);
556 if (flags & FORK_PPWAIT)
557 CPU_COUNT(CPU_COUNT_FORKS_PPWAIT, 1);
558 if (flags & FORK_SHAREVM)
559 CPU_COUNT(CPU_COUNT_FORKS_SHAREVM, 1);
560 KPREEMPT_ENABLE(l1);
561
562 if (ktrpoint(KTR_EMUL))
563 p2->p_traceflag |= KTRFAC_TRC_EMUL;
564
565 /*
566 * Notify any interested parties about the new process.
567 */
568 if (!SLIST_EMPTY(&p1->p_klist)) {
569 mutex_exit(&proc_lock);
570 knote_proc_fork(p1, p2);
571 mutex_enter(&proc_lock);
572 }
573
574 /*
575 * Make child runnable, set start time, and add to run queue except
576 * if the parent requested the child to start in SSTOP state.
577 */
578 mutex_enter(p2->p_lock);
579
580 /*
581 * Start profiling.
582 */
583 if ((p2->p_stflag & PST_PROFIL) != 0) {
584 mutex_spin_enter(&p2->p_stmutex);
585 startprofclock(p2);
586 mutex_spin_exit(&p2->p_stmutex);
587 }
588
589 getmicrotime(&p2->p_stats->p_start);
590 p2->p_acflag = AFORK;
591 lwp_lock(l2);
592 KASSERT(p2->p_nrlwps == 1);
593 KASSERT(l2->l_stat == LSIDL);
594 if (p2->p_sflag & PS_STOPFORK) {
595 p2->p_nrlwps = 0;
596 p2->p_stat = SSTOP;
597 p2->p_waited = 0;
598 p1->p_nstopchild++;
599 l2->l_stat = LSSTOP;
600 KASSERT(l2->l_wchan == NULL);
601 lwp_unlock(l2);
602 } else {
603 p2->p_nrlwps = 1;
604 p2->p_stat = SACTIVE;
605 setrunnable(l2);
606 /* LWP now unlocked */
607 }
608
609 /*
610 * Return child pid to parent process,
611 * marking us as parent via retval[1].
612 */
613 if (retval != NULL) {
614 retval[0] = p2->p_pid;
615 retval[1] = 0;
616 }
617
618 mutex_exit(p2->p_lock);
619
620 /*
621 * Let the parent know that we are tracing its child.
622 */
623 if (tracefork(p1, flags) || tracevfork(p1, flags)) {
624 mutex_enter(p1->p_lock);
625 eventswitch(TRAP_CHLD,
626 tracefork(p1, flags) ? PTRACE_FORK : PTRACE_VFORK,
627 retval[0]);
628 mutex_enter(&proc_lock);
629 }
630
631 /*
632 * Preserve synchronization semantics of vfork. If waiting for
633 * child to exec or exit, sleep until it clears p_vforkwaiting.
634 */
635 while (l1->l_vforkwaiting)
636 cv_wait(&l1->l_waitcv, &proc_lock);
637
638 /*
639 * Let the parent know that we are tracing its child.
640 */
641 if (tracevforkdone(p1, flags)) {
642 mutex_enter(p1->p_lock);
643 eventswitch(TRAP_CHLD, PTRACE_VFORK_DONE, retval[0]);
644 } else
645 mutex_exit(&proc_lock);
646
647 return 0;
648 }
649
650 /*
651 * MI code executed in each newly spawned process before returning to userland.
652 */
653 void
654 child_return(void *arg)
655 {
656 struct lwp *l = curlwp;
657 struct proc *p = l->l_proc;
658
659 if ((p->p_slflag & (PSL_TRACED|PSL_TRACEDCHILD)) ==
660 (PSL_TRACED|PSL_TRACEDCHILD)) {
661 eventswitchchild(p, TRAP_CHLD,
662 ISSET(p->p_lflag, PL_PPWAIT) ? PTRACE_VFORK : PTRACE_FORK);
663 }
664
665 md_child_return(l);
666
667 /*
668 * Return SYS_fork for all fork types, including vfork(2) and clone(2).
669 *
670 * This approach simplifies the code and avoids extra locking.
671 */
672 ktrsysret(SYS_fork, 0, 0);
673 }
674