2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94
35 * $FreeBSD: src/sys/kern/kern_fork.c,v 1.72.2.14 2003/06/26 04:15:10 silby Exp $
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <sys/malloc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/vnode.h>
51 #include <sys/ktrace.h>
52 #include <sys/unistd.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_extern.h>
62 #include <sys/vmmeter.h>
63 #include <sys/refcount.h>
64 #include <sys/thread2.h>
65 #include <sys/signal2.h>
66 #include <sys/spinlock2.h>
68 #include <sys/dsched.h>
70 static MALLOC_DEFINE(M_ATFORK
, "atfork", "atfork callback");
71 static MALLOC_DEFINE(M_REAPER
, "reaper", "process reapers");
74 * These are the stuctures used to create a callout list for things to do
75 * when forking a process
79 TAILQ_ENTRY(forklist
) next
;
82 TAILQ_HEAD(forklist_head
, forklist
);
83 static struct forklist_head fork_list
= TAILQ_HEAD_INITIALIZER(fork_list
);
85 static struct lwp
*lwp_fork(struct lwp
*, struct proc
*, int flags
,
86 const cpumask_t
*mask
);
87 static int lwp_create1(struct lwp_params
*params
,
88 const cpumask_t
*mask
);
89 static struct lock reaper_lock
= LOCK_INITIALIZER("reapgl", 0, 0);
91 int forksleep
; /* Place for fork1() to sleep on. */
94 * Red-Black tree support for LWPs
98 rb_lwp_compare(struct lwp
*lp1
, struct lwp
*lp2
)
100 if (lp1
->lwp_tid
< lp2
->lwp_tid
)
102 if (lp1
->lwp_tid
> lp2
->lwp_tid
)
107 RB_GENERATE2(lwp_rb_tree
, lwp
, u
.lwp_rbnode
, rb_lwp_compare
, lwpid_t
, lwp_tid
);
110 * When forking, memory underpinning umtx-supported mutexes may be set
111 * COW causing the physical address to change. We must wakeup any threads
112 * blocked on the physical address to allow them to re-resolve their VM.
115 wake_umtx_threads(struct proc
*p1
)
120 RB_FOREACH(lp
, lwp_rb_tree
, &p1
->p_lwp_tree
) {
122 if (td
&& (td
->td_flags
& TDF_TSLEEPQ
) &&
123 (td
->td_wdomain
& PDOMAIN_MASK
) == PDOMAIN_UMTX
) {
124 wakeup_domain(td
->td_wchan
, PDOMAIN_UMTX
);
133 sys_fork(struct fork_args
*uap
)
135 struct lwp
*lp
= curthread
->td_lwp
;
139 error
= fork1(lp
, RFFDG
| RFPROC
| RFPGLOCK
, &p2
);
142 start_forked_proc(lp
, p2
);
143 uap
->sysmsg_fds
[0] = p2
->p_pid
;
144 uap
->sysmsg_fds
[1] = 0;
151 * vfork() system call
154 sys_vfork(struct vfork_args
*uap
)
156 struct lwp
*lp
= curthread
->td_lwp
;
160 error
= fork1(lp
, RFFDG
| RFPROC
| RFPPWAIT
| RFMEM
| RFPGLOCK
, &p2
);
163 start_forked_proc(lp
, p2
);
164 uap
->sysmsg_fds
[0] = p2
->p_pid
;
165 uap
->sysmsg_fds
[1] = 0;
172 * Handle rforks. An rfork may (1) operate on the current process without
173 * creating a new, (2) create a new process that shared the current process's
174 * vmspace, signals, and/or descriptors, or (3) create a new process that does
175 * not share these things (normal fork).
177 * Note that we only call start_forked_proc() if a new process is actually
180 * rfork { int flags }
183 sys_rfork(struct rfork_args
*uap
)
185 struct lwp
*lp
= curthread
->td_lwp
;
189 if ((uap
->flags
& RFKERNELONLY
) != 0)
192 error
= fork1(lp
, uap
->flags
| RFPGLOCK
, &p2
);
196 start_forked_proc(lp
, p2
);
197 uap
->sysmsg_fds
[0] = p2
->p_pid
;
198 uap
->sysmsg_fds
[1] = 0;
201 uap
->sysmsg_fds
[0] = 0;
202 uap
->sysmsg_fds
[1] = 0;
209 lwp_create1(struct lwp_params
*uprm
, const cpumask_t
*umask
)
211 struct proc
*p
= curproc
;
213 struct lwp_params params
;
214 cpumask_t
*mask
= NULL
, mask0
;
217 error
= copyin(uprm
, ¶ms
, sizeof(params
));
222 error
= copyin(umask
, &mask0
, sizeof(mask0
));
225 CPUMASK_ANDMASK(mask0
, smp_active_mask
);
226 if (CPUMASK_TESTNZERO(mask0
))
230 lwkt_gettoken(&p
->p_token
);
231 plimit_lwp_fork(p
); /* force exclusive access */
232 lp
= lwp_fork(curthread
->td_lwp
, p
, RFPROC
| RFMEM
, mask
);
233 error
= cpu_prepare_lwp(lp
, ¶ms
);
236 if (params
.lwp_tid1
!= NULL
&&
237 (error
= copyout(&lp
->lwp_tid
, params
.lwp_tid1
, sizeof(lp
->lwp_tid
))))
239 if (params
.lwp_tid2
!= NULL
&&
240 (error
= copyout(&lp
->lwp_tid
, params
.lwp_tid2
, sizeof(lp
->lwp_tid
))))
244 * Now schedule the new lwp.
246 p
->p_usched
->resetpriority(lp
);
248 lp
->lwp_stat
= LSRUN
;
249 p
->p_usched
->setrunqueue(lp
);
251 lwkt_reltoken(&p
->p_token
);
257 * Make sure no one is using this lwp, before it is removed from
258 * the tree. If we didn't wait it here, lwp tree iteration with
259 * blocking operation would be broken.
261 while (lp
->lwp_lock
> 0)
262 tsleep(lp
, 0, "lwpfail", 1);
263 lwp_rb_tree_RB_REMOVE(&p
->p_lwp_tree
, lp
);
265 /* lwp_dispose expects an exited lwp, and a held proc */
266 atomic_set_int(&lp
->lwp_mpflags
, LWP_MP_WEXIT
);
267 lp
->lwp_thread
->td_flags
|= TDF_EXITING
;
268 lwkt_remove_tdallq(lp
->lwp_thread
);
270 biosched_done(lp
->lwp_thread
);
271 dsched_exit_thread(lp
->lwp_thread
);
273 lwkt_reltoken(&p
->p_token
);
279 * Low level thread create used by pthreads.
282 sys_lwp_create(struct lwp_create_args
*uap
)
285 return (lwp_create1(uap
->params
, NULL
));
289 sys_lwp_create2(struct lwp_create2_args
*uap
)
292 return (lwp_create1(uap
->params
, uap
->mask
));
295 int nprocs
= 1; /* process 0 */
298 fork1(struct lwp
*lp1
, int flags
, struct proc
**procp
)
300 struct proc
*p1
= lp1
->lwp_proc
;
305 struct sysreaper
*reap
;
308 static int curfail
= 0;
309 static struct timeval lastfail
;
311 struct filedesc_to_leader
*fdtol
;
313 if ((flags
& (RFFDG
|RFCFDG
)) == (RFFDG
|RFCFDG
))
316 lwkt_gettoken(&p1
->p_token
);
321 * Here we don't create a new process, but we divorce
322 * certain parts of a process from itself.
324 if ((flags
& RFPROC
) == 0) {
326 * This kind of stunt does not work anymore if
327 * there are native threads (lwps) running
329 if (p1
->p_nthreads
!= 1) {
334 vm_fork(p1
, 0, flags
);
335 wake_umtx_threads(p1
);
338 * Close all file descriptors.
340 if (flags
& RFCFDG
) {
341 struct filedesc
*fdtmp
;
347 * Unshare file descriptors (from parent.)
350 if (p1
->p_fd
->fd_refcnt
> 1) {
351 struct filedesc
*newfd
;
352 error
= fdcopy(p1
, &newfd
);
366 * Interlock against process group signal delivery. If signals
367 * are pending after the interlock is obtained we have to restart
368 * the system call to process the signals. If we don't the child
369 * can miss a pgsignal (such as ^C) sent during the fork.
371 * We can't use CURSIG() here because it will process any STOPs
372 * and cause the process group lock to be held indefinitely. If
373 * a STOP occurs, the fork will be restarted after the CONT.
376 if ((flags
& RFPGLOCK
) && (plkgrp
= p1
->p_pgrp
) != NULL
) {
378 lockmgr(&plkgrp
->pg_lock
, LK_SHARED
);
379 if (CURSIG_NOBLOCK(lp1
)) {
386 * Although process entries are dynamically created, we still keep
387 * a global limit on the maximum number we will create. Don't allow
388 * a nonprivileged user to use the last ten processes; don't let root
389 * exceed the limit. The variable nprocs is the current number of
390 * processes, maxproc is the limit.
392 uid
= lp1
->lwp_thread
->td_ucred
->cr_ruid
;
393 if ((nprocs
>= maxproc
- 10 && uid
!= 0) || nprocs
>= maxproc
) {
394 if (ppsratecheck(&lastfail
, &curfail
, 1))
395 kprintf("maxproc limit exceeded by uid %d, please "
396 "see tuning(7) and login.conf(5).\n", uid
);
397 tsleep(&forksleep
, 0, "fork", hz
/ 2);
403 * Increment the nprocs resource before blocking can occur. There
404 * are hard-limits as to the number of processes that can run.
406 atomic_add_int(&nprocs
, 1);
409 * Increment the count of procs running with this uid. This also
412 ok
= chgproccnt(lp1
->lwp_thread
->td_ucred
->cr_ruidinfo
, 1,
413 plimit_getadjvalue(RLIMIT_NPROC
));
416 * Back out the process count
418 atomic_add_int(&nprocs
, -1);
419 if (ppsratecheck(&lastfail
, &curfail
, 1)) {
420 kprintf("maxproc limit of %jd "
421 "exceeded by \"%s\" uid %d, "
422 "please see tuning(7) and login.conf(5).\n",
423 plimit_getadjvalue(RLIMIT_NPROC
),
427 tsleep(&forksleep
, 0, "fork", hz
/ 2);
433 * Allocate a new process, don't get fancy: zero the structure.
435 p2
= kmalloc(sizeof(struct proc
), M_PROC
, M_WAITOK
|M_ZERO
);
438 * Core initialization. SIDL is a safety state that protects the
439 * partially initialized process once it starts getting hooked
440 * into system structures and becomes addressable.
442 * We must be sure to acquire p2->p_token as well, we must hold it
443 * once the process is on the allproc list to avoid things such
444 * as competing modifications to p_flags.
446 mycpu
->gd_forkid
+= ncpus
;
447 p2
->p_forkid
= mycpu
->gd_forkid
+ mycpu
->gd_cpuid
;
448 p2
->p_lasttid
= 0; /* first tid will be 1 */
452 * NOTE: Process 0 will not have a reaper, but process 1 (init) and
453 * all other processes always will.
455 if ((reap
= p1
->p_reaper
) != NULL
) {
462 RB_INIT(&p2
->p_lwp_tree
);
463 spin_init(&p2
->p_spin
, "procfork1");
464 lwkt_token_init(&p2
->p_token
, "proc");
465 lwkt_gettoken(&p2
->p_token
);
468 * Setup linkage for kernel based threading XXX lwp. Also add the
469 * process to the allproclist.
471 * The process structure is addressable after this point.
473 if (flags
& RFTHREAD
) {
474 p2
->p_peers
= p1
->p_peers
;
476 p2
->p_leader
= p1
->p_leader
;
480 proc_add_allproc(p2
);
483 * Initialize the section which is copied verbatim from the parent.
485 bcopy(&p1
->p_startcopy
, &p2
->p_startcopy
,
486 ((caddr_t
)&p2
->p_endcopy
- (caddr_t
)&p2
->p_startcopy
));
489 * Duplicate sub-structures as needed. Increase reference counts
492 * NOTE: because we are now on the allproc list it is possible for
493 * other consumers to gain temporary references to p2
494 * (p2->p_lock can change).
496 if (p1
->p_flags
& P_PROFIL
)
498 p2
->p_ucred
= crhold(lp1
->lwp_thread
->td_ucred
);
500 if (jailed(p2
->p_ucred
))
501 p2
->p_flags
|= P_JAILED
;
504 refcount_acquire(&p2
->p_args
->ar_ref
);
506 p2
->p_usched
= p1
->p_usched
;
507 /* XXX: verify copy of the secondary iosched stuff */
508 dsched_enter_proc(p2
);
510 if (flags
& RFSIGSHARE
) {
511 p2
->p_sigacts
= p1
->p_sigacts
;
512 refcount_acquire(&p2
->p_sigacts
->ps_refcnt
);
514 p2
->p_sigacts
= kmalloc(sizeof(*p2
->p_sigacts
),
515 M_SUBPROC
, M_WAITOK
);
516 bcopy(p1
->p_sigacts
, p2
->p_sigacts
, sizeof(*p2
->p_sigacts
));
517 refcount_init(&p2
->p_sigacts
->ps_refcnt
, 1);
519 if (flags
& RFLINUXTHPN
)
520 p2
->p_sigparent
= SIGUSR1
;
522 p2
->p_sigparent
= SIGCHLD
;
524 /* bump references to the text vnode (for procfs) */
525 p2
->p_textvp
= p1
->p_textvp
;
529 /* copy namecache handle to the text file */
530 if (p1
->p_textnch
.mount
)
531 cache_copy(&p1
->p_textnch
, &p2
->p_textnch
);
534 * Handle file descriptors
536 if (flags
& RFCFDG
) {
537 p2
->p_fd
= fdinit(p1
);
539 } else if (flags
& RFFDG
) {
540 error
= fdcopy(p1
, &p2
->p_fd
);
547 p2
->p_fd
= fdshare(p1
);
548 if (p1
->p_fdtol
== NULL
) {
549 p1
->p_fdtol
= filedesc_to_leader_alloc(NULL
,
552 if ((flags
& RFTHREAD
) != 0) {
554 * Shared file descriptor table and
555 * shared process leaders.
558 fdtol
->fdl_refcount
++;
561 * Shared file descriptor table, and
562 * different process leaders
564 fdtol
= filedesc_to_leader_alloc(p1
->p_fdtol
, p2
);
568 p2
->p_limit
= plimit_fork(p1
);
571 * Adjust depth for resource downscaling
573 if ((p2
->p_depth
& 31) != 31)
577 * Preserve some more flags in subprocess. P_PROFIL has already
580 p2
->p_flags
|= p1
->p_flags
& P_SUGID
;
581 if (p1
->p_session
->s_ttyvp
!= NULL
&& (p1
->p_flags
& P_CONTROLT
))
582 p2
->p_flags
|= P_CONTROLT
;
583 if (flags
& RFPPWAIT
) {
584 p2
->p_flags
|= P_PPWAIT
;
586 atomic_add_int(&p1
->p_upmap
->invfork
, 1);
590 * Inherit the virtual kernel structure (allows a virtual kernel
591 * to fork to simulate multiple cpus).
594 vkernel_inherit(p1
, p2
);
597 * Once we are on a pglist we may receive signals. XXX we might
598 * race a ^C being sent to the process group by not receiving it
599 * at all prior to this line.
602 lwkt_gettoken(&p1grp
->pg_token
);
603 LIST_INSERT_AFTER(p1
, p2
, p_pglist
);
604 lwkt_reltoken(&p1grp
->pg_token
);
607 * Attach the new process to its parent.
609 * If RFNOWAIT is set, the newly created process becomes a child
610 * of the reaper (typically init). This effectively disassociates
611 * the child from the parent.
613 * Temporarily hold pptr for the RFNOWAIT case to avoid ripouts.
615 if (flags
& RFNOWAIT
) {
616 pptr
= reaper_get(reap
);
625 LIST_INIT(&p2
->p_children
);
627 lwkt_gettoken(&pptr
->p_token
);
628 LIST_INSERT_HEAD(&pptr
->p_children
, p2
, p_sibling
);
629 lwkt_reltoken(&pptr
->p_token
);
631 if (flags
& RFNOWAIT
)
634 varsymset_init(&p2
->p_varsymset
, &p1
->p_varsymset
);
635 callout_init_mp(&p2
->p_ithandle
);
639 * Copy traceflag and tracefile if enabled. If not inherited,
640 * these were zeroed above but we still could have a trace race
641 * so make sure p2's p_tracenode is NULL.
643 if ((p1
->p_traceflag
& KTRFAC_INHERIT
) && p2
->p_tracenode
== NULL
) {
644 p2
->p_traceflag
= p1
->p_traceflag
;
645 p2
->p_tracenode
= ktrinherit(p1
->p_tracenode
);
650 * This begins the section where we must prevent the parent
651 * from being swapped.
653 * Gets PRELE'd in the caller in start_forked_proc().
657 vm_fork(p1
, p2
, flags
);
658 wake_umtx_threads(p1
);
661 * Create the first lwp associated with the new proc.
662 * It will return via a different execution path later, directly
663 * into userland, after it was put on the runq by
664 * start_forked_proc().
666 lwp_fork(lp1
, p2
, flags
, NULL
);
668 if (flags
== (RFFDG
| RFPROC
| RFPGLOCK
)) {
669 mycpu
->gd_cnt
.v_forks
++;
670 mycpu
->gd_cnt
.v_forkpages
+= p2
->p_vmspace
->vm_dsize
+
671 p2
->p_vmspace
->vm_ssize
;
672 } else if (flags
== (RFFDG
| RFPROC
| RFPPWAIT
| RFMEM
| RFPGLOCK
)) {
673 mycpu
->gd_cnt
.v_vforks
++;
674 mycpu
->gd_cnt
.v_vforkpages
+= p2
->p_vmspace
->vm_dsize
+
675 p2
->p_vmspace
->vm_ssize
;
676 } else if (p1
== &proc0
) {
677 mycpu
->gd_cnt
.v_kthreads
++;
678 mycpu
->gd_cnt
.v_kthreadpages
+= p2
->p_vmspace
->vm_dsize
+
679 p2
->p_vmspace
->vm_ssize
;
681 mycpu
->gd_cnt
.v_rforks
++;
682 mycpu
->gd_cnt
.v_rforkpages
+= p2
->p_vmspace
->vm_dsize
+
683 p2
->p_vmspace
->vm_ssize
;
687 * Both processes are set up, now check if any loadable modules want
688 * to adjust anything.
689 * What if they have an error? XXX
691 TAILQ_FOREACH(ep
, &fork_list
, next
) {
692 (*ep
->function
)(p1
, p2
, flags
);
696 * Set the start time. Note that the process is not runnable. The
697 * caller is responsible for making it runnable.
699 microtime(&p2
->p_start
);
700 p2
->p_acflag
= AFORK
;
703 * tell any interested parties about the new process
705 KNOTE(&p1
->p_klist
, NOTE_FORK
| p2
->p_pid
);
708 * Return child proc pointer to parent.
714 lwkt_reltoken(&p2
->p_token
);
715 lwkt_reltoken(&p1
->p_token
);
717 lockmgr(&plkgrp
->pg_lock
, LK_RELEASE
);
724 lwp_fork(struct lwp
*origlp
, struct proc
*destproc
, int flags
,
725 const cpumask_t
*mask
)
727 globaldata_t gd
= mycpu
;
731 lp
= kmalloc(sizeof(struct lwp
), M_LWP
, M_WAITOK
|M_ZERO
);
733 lp
->lwp_proc
= destproc
;
734 lp
->lwp_vmspace
= destproc
->p_vmspace
;
735 lp
->lwp_stat
= LSRUN
;
736 bcopy(&origlp
->lwp_startcopy
, &lp
->lwp_startcopy
,
737 (unsigned) ((caddr_t
)&lp
->lwp_endcopy
-
738 (caddr_t
)&lp
->lwp_startcopy
));
740 lp
->lwp_cpumask
= *mask
;
743 * Reset the sigaltstack if memory is shared, otherwise inherit
747 lp
->lwp_sigstk
.ss_flags
= SS_DISABLE
;
748 lp
->lwp_sigstk
.ss_size
= 0;
749 lp
->lwp_sigstk
.ss_sp
= NULL
;
750 lp
->lwp_flags
&= ~LWP_ALTSTACK
;
752 lp
->lwp_flags
|= origlp
->lwp_flags
& LWP_ALTSTACK
;
756 * Set cpbase to the last timeout that occured (not the upcoming
759 * A critical section is required since a timer IPI can update
760 * scheduler specific data.
763 lp
->lwp_cpbase
= gd
->gd_schedclock
.time
- gd
->gd_schedclock
.periodic
;
764 destproc
->p_usched
->heuristic_forking(origlp
, lp
);
766 CPUMASK_ANDMASK(lp
->lwp_cpumask
, usched_mastermask
);
767 lwkt_token_init(&lp
->lwp_token
, "lwp_token");
768 spin_init(&lp
->lwp_spin
, "lwptoken");
771 * Assign the thread to the current cpu to begin with so we
774 td
= lwkt_alloc_thread(NULL
, LWKT_THREAD_STACK
, gd
->gd_cpuid
, 0);
776 td
->td_ucred
= crhold(destproc
->p_ucred
);
777 td
->td_proc
= destproc
;
779 td
->td_switch
= cpu_heavy_switch
;
780 #ifdef NO_LWKT_SPLIT_USERPRI
781 lwkt_setpri(td
, TDPRI_USER_NORM
);
783 lwkt_setpri(td
, TDPRI_KERN_USER
);
785 lwkt_set_comm(td
, "%s", destproc
->p_comm
);
788 * cpu_fork will copy and update the pcb, set up the kernel stack,
789 * and make the child ready to run.
791 cpu_fork(origlp
, lp
, flags
);
792 kqueue_init(&lp
->lwp_kqueue
, destproc
->p_fd
);
795 * Assign a TID to the lp. Loop until the insert succeeds (returns
798 * If we are in a vfork assign the same TID as the lwp that did the
799 * vfork(). This way if the user program messes around with
800 * pthread calls inside the vfork(), it will operate like an
801 * extension of the (blocked) parent. Also note that since the
802 * address space is being shared, insofar as pthreads is concerned,
803 * the code running in the vfork() is part of the original process.
805 if (flags
& RFPPWAIT
) {
806 lp
->lwp_tid
= origlp
->lwp_tid
- 1;
808 lp
->lwp_tid
= destproc
->p_lasttid
;
812 if (++lp
->lwp_tid
<= 0)
814 } while (lwp_rb_tree_RB_INSERT(&destproc
->p_lwp_tree
, lp
) != NULL
);
816 destproc
->p_lasttid
= lp
->lwp_tid
;
817 destproc
->p_nthreads
++;
820 * This flag is set and never cleared. It means that the process
821 * was threaded at some point. Used to improve exit performance.
823 destproc
->p_flags
|= P_MAYBETHREADED
;
829 * The next two functionms are general routines to handle adding/deleting
830 * items on the fork callout list.
833 * Take the arguments given and put them onto the fork callout list,
834 * However first make sure that it's not already there.
835 * Returns 0 on success or a standard error number.
838 at_fork(forklist_fn function
)
843 /* let the programmer know if he's been stupid */
844 if (rm_at_fork(function
)) {
845 kprintf("WARNING: fork callout entry (%p) already present\n",
849 ep
= kmalloc(sizeof(*ep
), M_ATFORK
, M_WAITOK
|M_ZERO
);
850 ep
->function
= function
;
851 TAILQ_INSERT_TAIL(&fork_list
, ep
, next
);
856 * Scan the exit callout list for the given item and remove it..
857 * Returns the number of items removed (0 or 1)
860 rm_at_fork(forklist_fn function
)
864 TAILQ_FOREACH(ep
, &fork_list
, next
) {
865 if (ep
->function
== function
) {
866 TAILQ_REMOVE(&fork_list
, ep
, next
);
875 * Add a forked process to the run queue after any remaining setup, such
876 * as setting the fork handler, has been completed.
878 * p2 is held by the caller.
881 start_forked_proc(struct lwp
*lp1
, struct proc
*p2
)
883 struct lwp
*lp2
= ONLY_LWP_IN_PROC(p2
);
887 * Move from SIDL to RUN queue, and activate the process's thread.
888 * Activation of the thread effectively makes the process "a"
889 * current process, so we do not setrunqueue().
891 * YYY setrunqueue works here but we should clean up the trampoline
892 * code so we just schedule the LWKT thread and let the trampoline
893 * deal with the userland scheduler on return to userland.
895 KASSERT(p2
->p_stat
== SIDL
,
896 ("cannot start forked process, bad status: %p", p2
));
897 p2
->p_usched
->resetpriority(lp2
);
899 p2
->p_stat
= SACTIVE
;
900 lp2
->lwp_stat
= LSRUN
;
901 p2
->p_usched
->setrunqueue(lp2
);
905 * Now can be swapped.
907 PRELE(lp1
->lwp_proc
);
910 * Preserve synchronization semantics of vfork. P_PPWAIT is set in
911 * the child until it has retired the parent's resources. The parent
912 * must wait for the flag to be cleared by the child.
914 * Interlock the flag/tsleep with atomic ops to avoid unnecessary
917 * XXX Is this use of an atomic op on a field that is not normally
918 * manipulated with atomic ops ok?
920 while ((pflags
= p2
->p_flags
) & P_PPWAIT
) {
922 tsleep_interlock(lp1
->lwp_proc
, 0);
923 if (atomic_cmpset_int(&p2
->p_flags
, pflags
, pflags
))
924 tsleep(lp1
->lwp_proc
, PINTERLOCKED
, "ppwait", 0);
929 * procctl (idtype_t idtype, id_t id, int cmd, void *arg)
932 sys_procctl(struct procctl_args
*uap
)
934 struct proc
*p
= curproc
;
936 struct sysreaper
*reap
;
937 union reaper_info udata
;
940 if (uap
->idtype
!= P_PID
|| uap
->id
!= (id_t
)p
->p_pid
)
944 case PROC_REAP_ACQUIRE
:
945 lwkt_gettoken(&p
->p_token
);
946 reap
= kmalloc(sizeof(*reap
), M_REAPER
, M_WAITOK
|M_ZERO
);
947 if (p
->p_reaper
== NULL
|| p
->p_reaper
->p
!= p
) {
948 reaper_init(p
, reap
);
951 kfree(reap
, M_REAPER
);
954 lwkt_reltoken(&p
->p_token
);
956 case PROC_REAP_RELEASE
:
957 lwkt_gettoken(&p
->p_token
);
960 KKASSERT(reap
!= NULL
);
962 reaper_hold(reap
); /* in case of thread race */
963 lockmgr(&reap
->lock
, LK_EXCLUSIVE
);
965 lockmgr(&reap
->lock
, LK_RELEASE
);
970 p
->p_reaper
= reap
->parent
;
972 reaper_hold(p
->p_reaper
);
973 lockmgr(&reap
->lock
, LK_RELEASE
);
974 reaper_drop(reap
); /* our ref */
975 reaper_drop(reap
); /* old p_reaper ref */
980 lwkt_reltoken(&p
->p_token
);
982 case PROC_REAP_STATUS
:
983 bzero(&udata
, sizeof(udata
));
984 lwkt_gettoken_shared(&p
->p_token
);
985 if ((reap
= p
->p_reaper
) != NULL
&& reap
->p
== p
) {
986 udata
.status
.flags
= reap
->flags
;
987 udata
.status
.refs
= reap
->refs
- 1; /* minus ours */
989 p2
= LIST_FIRST(&p
->p_children
);
990 udata
.status
.pid_head
= p2
? p2
->p_pid
: -1;
991 lwkt_reltoken(&p
->p_token
);
994 error
= copyout(&udata
, uap
->data
,
995 sizeof(udata
.status
));
1008 * Bump ref on reaper, preventing destruction
1011 reaper_hold(struct sysreaper
*reap
)
1013 KKASSERT(reap
->refs
> 0);
1014 refcount_acquire(&reap
->refs
);
1018 * Drop ref on reaper, destroy the structure on the 1->0
1019 * transition and loop on the parent.
1022 reaper_drop(struct sysreaper
*next
)
1024 struct sysreaper
*reap
;
1026 while ((reap
= next
) != NULL
) {
1027 if (refcount_release(&reap
->refs
)) {
1028 next
= reap
->parent
;
1029 KKASSERT(reap
->p
== NULL
);
1030 lockmgr(&reaper_lock
, LK_EXCLUSIVE
);
1031 reap
->parent
= NULL
;
1032 kfree(reap
, M_REAPER
);
1033 lockmgr(&reaper_lock
, LK_RELEASE
);
1041 * Initialize a static or newly allocated reaper structure
1044 reaper_init(struct proc
*p
, struct sysreaper
*reap
)
1046 reap
->parent
= p
->p_reaper
;
1048 if (p
== initproc
) {
1049 reap
->flags
= REAPER_STAT_OWNED
| REAPER_STAT_REALINIT
;
1052 reap
->flags
= REAPER_STAT_OWNED
;
1055 lockinit(&reap
->lock
, "subrp", 0, 0);
1061 * Called with p->p_token held during exit.
1063 * This is a bit simpler than RELEASE because there are no threads remaining
1064 * to race. We only release if we own the reaper, the exit code will handle
1065 * the final p_reaper release.
1068 reaper_exit(struct proc
*p
)
1070 struct sysreaper
*reap
;
1073 * Release acquired reaper
1075 if ((reap
= p
->p_reaper
) != NULL
&& reap
->p
== p
) {
1076 lockmgr(&reap
->lock
, LK_EXCLUSIVE
);
1077 p
->p_reaper
= reap
->parent
;
1079 reaper_hold(p
->p_reaper
);
1081 lockmgr(&reap
->lock
, LK_RELEASE
);
1086 * Return and clear reaper (caller is holding p_token for us)
1087 * (reap->p does not equal p). Caller must drop it.
1089 if ((reap
= p
->p_reaper
) != NULL
) {
1096 * Return a held (PHOLD) process representing the reaper for process (p).
1097 * NULL should not normally be returned. Caller should PRELE() the returned
1098 * reaper process when finished.
1100 * Remove dead internal nodes while we are at it.
1102 * Process (p)'s token must be held on call.
1103 * The returned process's token is NOT acquired by this routine.
1106 reaper_get(struct sysreaper
*reap
)
1108 struct sysreaper
*next
;
1109 struct proc
*reproc
;
1115 * Extra hold for loop
1120 lockmgr(&reap
->lock
, LK_SHARED
);
1128 lockmgr(&reap
->lock
, LK_RELEASE
);
1136 lockmgr(&reap
->lock
, LK_RELEASE
);
1141 * Traverse upwards in the reaper topology, destroy
1142 * dead internal nodes when possible.
1144 * NOTE: Our ref on next means that a dead node should
1145 * have 2 (ours and reap->parent's).
1147 next
= reap
->parent
;
1150 if (next
->refs
== 2 && next
->p
== NULL
) {
1151 lockmgr(&reap
->lock
, LK_RELEASE
);
1152 lockmgr(&reap
->lock
, LK_EXCLUSIVE
);
1153 if (next
->refs
== 2 &&
1154 reap
->parent
== next
&&
1157 * reap->parent inherits ref from next.
1159 reap
->parent
= next
->parent
;
1160 next
->parent
= NULL
;
1161 reaper_drop(next
); /* ours */
1162 reaper_drop(next
); /* old parent */
1163 next
= reap
->parent
;
1164 continue; /* possible chain */
1169 lockmgr(&reap
->lock
, LK_RELEASE
);
1177 * Test that the sender is allowed to send a signal to the target.
1178 * The sender process is assumed to have a stable reaper. The
1179 * target can be e.g. from a scan callback.
1181 * Target cannot be the reaper process itself unless reaper_ok is specified,
1182 * or sender == target.
1185 reaper_sigtest(struct proc
*sender
, struct proc
*target
, int reaper_ok
)
1187 struct sysreaper
*sreap
;
1188 struct sysreaper
*reap
;
1191 sreap
= sender
->p_reaper
;
1195 if (sreap
== target
->p_reaper
) {
1196 if (sreap
->p
== target
&& sreap
->p
!= sender
&& reaper_ok
== 0)
1200 lockmgr(&reaper_lock
, LK_SHARED
);
1202 for (reap
= target
->p_reaper
; reap
; reap
= reap
->parent
) {
1203 if (sreap
== reap
) {
1204 if (sreap
->p
!= target
|| reaper_ok
)
1209 lockmgr(&reaper_lock
, LK_RELEASE
);