Signals have to be blocked when creating our LWPs or a LWP may receive a
[dragonfly.git] / sys / kern / kern_fork.c
CommitLineData
984263bc
MD
1/*
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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94
5bc7cd8d 39 * $FreeBSD: src/sys/kern/kern_fork.c,v 1.72.2.14 2003/06/26 04:15:10 silby Exp $
a5ae2446 40 * $DragonFly: src/sys/kern/kern_fork.c,v 1.70 2007/07/02 17:06:54 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/filedesc.h>
49#include <sys/kernel.h>
50#include <sys/sysctl.h>
51#include <sys/malloc.h>
52#include <sys/proc.h>
53#include <sys/resourcevar.h>
54#include <sys/vnode.h>
55#include <sys/acct.h>
56#include <sys/ktrace.h>
dfc1fc13
EN
57#include <sys/unistd.h>
58#include <sys/jail.h>
59#include <sys/caps.h>
984263bc
MD
60
61#include <vm/vm.h>
62#include <sys/lock.h>
63#include <vm/pmap.h>
64#include <vm/vm_map.h>
65#include <vm/vm_extern.h>
66#include <vm/vm_zone.h>
67
68#include <sys/vmmeter.h>
e43a034f 69#include <sys/thread2.h>
b1b4e5a6 70#include <sys/signal2.h>
984263bc
MD
71
72static MALLOC_DEFINE(M_ATFORK, "atfork", "atfork callback");
73
74/*
75 * These are the stuctures used to create a callout list for things to do
76 * when forking a process
77 */
78struct forklist {
79 forklist_fn function;
80 TAILQ_ENTRY(forklist) next;
81};
82
83TAILQ_HEAD(forklist_head, forklist);
84static struct forklist_head fork_list = TAILQ_HEAD_INITIALIZER(fork_list);
85
13d13d89
SS
86static struct lwp *lwp_fork(struct lwp *, struct proc *, int flags);
87
984263bc
MD
88int forksleep; /* Place for fork1() to sleep on. */
89
90/* ARGSUSED */
91int
753fd850 92sys_fork(struct fork_args *uap)
984263bc 93{
553ea3c8 94 struct lwp *lp = curthread->td_lwp;
984263bc 95 struct proc *p2;
41c20dac 96 int error;
984263bc 97
167e6ecb 98 error = fork1(lp, RFFDG | RFPROC | RFPGLOCK, &p2);
984263bc 99 if (error == 0) {
553ea3c8 100 start_forked_proc(lp, p2);
c7114eea
MD
101 uap->sysmsg_fds[0] = p2->p_pid;
102 uap->sysmsg_fds[1] = 0;
984263bc
MD
103 }
104 return error;
105}
106
107/* ARGSUSED */
108int
753fd850 109sys_vfork(struct vfork_args *uap)
984263bc 110{
553ea3c8 111 struct lwp *lp = curthread->td_lwp;
984263bc 112 struct proc *p2;
41c20dac 113 int error;
984263bc 114
167e6ecb 115 error = fork1(lp, RFFDG | RFPROC | RFPPWAIT | RFMEM | RFPGLOCK, &p2);
984263bc 116 if (error == 0) {
553ea3c8 117 start_forked_proc(lp, p2);
c7114eea
MD
118 uap->sysmsg_fds[0] = p2->p_pid;
119 uap->sysmsg_fds[1] = 0;
984263bc
MD
120 }
121 return error;
122}
123
f61c1ff1
MD
124/*
125 * Handle rforks. An rfork may (1) operate on the current process without
126 * creating a new, (2) create a new process that shared the current process's
127 * vmspace, signals, and/or descriptors, or (3) create a new process that does
128 * not share these things (normal fork).
129 *
130 * Note that we only call start_forked_proc() if a new process is actually
131 * created.
132 *
133 * rfork { int flags }
134 */
984263bc 135int
753fd850 136sys_rfork(struct rfork_args *uap)
984263bc 137{
553ea3c8 138 struct lwp *lp = curthread->td_lwp;
984263bc 139 struct proc *p2;
41c20dac 140 int error;
984263bc 141
6654fbcb
MD
142 if ((uap->flags & RFKERNELONLY) != 0)
143 return (EINVAL);
144
167e6ecb 145 error = fork1(lp, uap->flags | RFPGLOCK, &p2);
984263bc 146 if (error == 0) {
f61c1ff1 147 if (p2)
553ea3c8 148 start_forked_proc(lp, p2);
c7114eea
MD
149 uap->sysmsg_fds[0] = p2 ? p2->p_pid : 0;
150 uap->sysmsg_fds[1] = 0;
984263bc
MD
151 }
152 return error;
153}
154
91bd9c1e
SS
155int
156sys_lwp_create(struct lwp_create_args *uap)
157{
158 struct proc *p = curproc;
159 struct lwp *lp;
160 struct lwp_params params;
161 int error;
162
163 error = copyin(uap->params, &params, sizeof(params));
164 if (error)
165 goto fail2;
166
167 lp = lwp_fork(curthread->td_lwp, p, RFPROC);
168 error = cpu_prepare_lwp(lp, &params);
169 if (params.tid1 != NULL &&
170 (error = copyout(&lp->lwp_tid, params.tid1, sizeof(lp->lwp_tid))))
171 goto fail;
172 if (params.tid2 != NULL &&
173 (error = copyout(&lp->lwp_tid, params.tid2, sizeof(lp->lwp_tid))))
174 goto fail;
175
176 /*
177 * Now schedule the new lwp.
178 */
179 p->p_usched->resetpriority(lp);
180 crit_enter();
181 lp->lwp_stat = LSRUN;
182 p->p_usched->setrunqueue(lp);
183 crit_exit();
184
185 return (0);
186
187fail:
188 --p->p_nthreads;
189 LIST_REMOVE(lp, lwp_list);
e3161323
MD
190 /* lwp_dispose expects an exited lwp, and a held proc */
191 lp->lwp_flag |= LWP_WEXIT;
192 lp->lwp_thread->td_flags |= TDF_EXITING;
193 PHOLD(p);
91bd9c1e
SS
194 lwp_dispose(lp);
195fail2:
196 return (error);
197}
984263bc
MD
198
199int nprocs = 1; /* process 0 */
984263bc
MD
200
201int
553ea3c8 202fork1(struct lwp *lp1, int flags, struct proc **procp)
984263bc 203{
553ea3c8 204 struct proc *p1 = lp1->lwp_proc;
984263bc 205 struct proc *p2, *pptr;
167e6ecb 206 struct pgrp *pgrp;
984263bc 207 uid_t uid;
167e6ecb 208 int ok, error;
51e64ff2 209 static int curfail = 0;
5bc7cd8d 210 static struct timeval lastfail;
984263bc
MD
211 struct forklist *ep;
212 struct filedesc_to_leader *fdtol;
213
214 if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
215 return (EINVAL);
216
217 /*
218 * Here we don't create a new process, but we divorce
219 * certain parts of a process from itself.
220 */
221 if ((flags & RFPROC) == 0) {
13d13d89
SS
222 /*
223 * This kind of stunt does not work anymore if
224 * there are native threads (lwps) running
225 */
226 if (p1->p_nthreads != 1)
227 return (EINVAL);
228
229 vm_fork(p1, 0, flags);
984263bc
MD
230
231 /*
232 * Close all file descriptors.
233 */
234 if (flags & RFCFDG) {
235 struct filedesc *fdtmp;
236 fdtmp = fdinit(p1);
237 fdfree(p1);
238 p1->p_fd = fdtmp;
239 }
240
241 /*
242 * Unshare file descriptors (from parent.)
243 */
244 if (flags & RFFDG) {
245 if (p1->p_fd->fd_refcnt > 1) {
246 struct filedesc *newfd;
247 newfd = fdcopy(p1);
248 fdfree(p1);
249 p1->p_fd = newfd;
250 }
251 }
252 *procp = NULL;
253 return (0);
254 }
255
167e6ecb
MD
256 /*
257 * Interlock against process group signal delivery. If signals
258 * are pending after the interlock is obtained we have to restart
259 * the system call to process the signals. If we don't the child
260 * can miss a pgsignal (such as ^C) sent during the fork.
261 *
262 * We can't use CURSIG() here because it will process any STOPs
263 * and cause the process group lock to be held indefinitely. If
264 * a STOP occurs, the fork will be restarted after the CONT.
265 */
266 error = 0;
267 pgrp = NULL;
268 if ((flags & RFPGLOCK) && (pgrp = p1->p_pgrp) != NULL) {
269 lockmgr(&pgrp->pg_lock, LK_SHARED);
08f2f1bb 270 if (CURSIGNB(lp1)) {
167e6ecb
MD
271 error = ERESTART;
272 goto done;
273 }
274 }
275
984263bc
MD
276 /*
277 * Although process entries are dynamically created, we still keep
278 * a global limit on the maximum number we will create. Don't allow
279 * a nonprivileged user to use the last ten processes; don't let root
280 * exceed the limit. The variable nprocs is the current number of
281 * processes, maxproc is the limit.
282 */
41c20dac 283 uid = p1->p_ucred->cr_ruid;
984263bc 284 if ((nprocs >= maxproc - 10 && uid != 0) || nprocs >= maxproc) {
5bc7cd8d 285 if (ppsratecheck(&lastfail, &curfail, 1))
6ea70f76 286 kprintf("maxproc limit exceeded by uid %d, please "
5bc7cd8d 287 "see tuning(7) and login.conf(5).\n", uid);
377d4740 288 tsleep(&forksleep, 0, "fork", hz / 2);
167e6ecb
MD
289 error = EAGAIN;
290 goto done;
984263bc
MD
291 }
292 /*
293 * Increment the nprocs resource before blocking can occur. There
294 * are hard-limits as to the number of processes that can run.
295 */
296 nprocs++;
297
298 /*
299 * Increment the count of procs running with this uid. Don't allow
300 * a nonprivileged user to exceed their current limit.
301 */
41c20dac 302 ok = chgproccnt(p1->p_ucred->cr_ruidinfo, 1,
984263bc
MD
303 (uid != 0) ? p1->p_rlimit[RLIMIT_NPROC].rlim_cur : 0);
304 if (!ok) {
305 /*
306 * Back out the process count
307 */
308 nprocs--;
5bc7cd8d 309 if (ppsratecheck(&lastfail, &curfail, 1))
6ea70f76 310 kprintf("maxproc limit exceeded by uid %d, please "
5bc7cd8d 311 "see tuning(7) and login.conf(5).\n", uid);
377d4740 312 tsleep(&forksleep, 0, "fork", hz / 2);
167e6ecb
MD
313 error = EAGAIN;
314 goto done;
984263bc
MD
315 }
316
317 /* Allocate new proc. */
51e64ff2 318 p2 = zalloc(proc_zone);
f9fe34b6 319 bzero(p2, sizeof(*p2));
984263bc
MD
320
321 /*
ef09c3ed 322 * Setup linkage for kernel based threading XXX lwp
984263bc 323 */
cb74210d 324 if (flags & RFTHREAD) {
51e64ff2
MD
325 p2->p_peers = p1->p_peers;
326 p1->p_peers = p2;
327 p2->p_leader = p1->p_leader;
984263bc 328 } else {
51e64ff2 329 p2->p_leader = p2;
984263bc
MD
330 }
331
51e64ff2 332 LIST_INIT(&p2->p_lwps);
ef09c3ed 333
984263bc 334 /*
51e64ff2
MD
335 * Setting the state to SIDL protects the partially initialized
336 * process once it starts getting hooked into the rest of the system.
984263bc 337 */
51e64ff2
MD
338 p2->p_stat = SIDL;
339 proc_add_allproc(p2);
984263bc
MD
340
341 /*
342 * Make a proc table entry for the new process.
f9fe34b6
SS
343 * The whole structure was zeroed above, so copy the section that is
344 * copied directly from the parent.
984263bc 345 */
984263bc
MD
346 bcopy(&p1->p_startcopy, &p2->p_startcopy,
347 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
348
984263bc
MD
349 /*
350 * Duplicate sub-structures as needed.
351 * Increase reference counts on shared objects.
984263bc 352 */
984263bc
MD
353 if (p1->p_flag & P_PROFIL)
354 startprofclock(p2);
dadab5e9 355 p2->p_ucred = crhold(p1->p_ucred);
984263bc 356
b40e316c 357 if (jailed(p2->p_ucred))
984263bc 358 p2->p_flag |= P_JAILED;
984263bc
MD
359
360 if (p2->p_args)
361 p2->p_args->ar_ref++;
362
13d13d89
SS
363 p2->p_usched = p1->p_usched;
364
984263bc 365 if (flags & RFSIGSHARE) {
b1b4e5a6
SS
366 p2->p_sigacts = p1->p_sigacts;
367 p2->p_sigacts->ps_refcnt++;
984263bc 368 } else {
b1b4e5a6 369 p2->p_sigacts = (struct sigacts *)kmalloc(sizeof(*p2->p_sigacts),
984263bc 370 M_SUBPROC, M_WAITOK);
b1b4e5a6
SS
371 bcopy(p1->p_sigacts, p2->p_sigacts, sizeof(*p2->p_sigacts));
372 p2->p_sigacts->ps_refcnt = 1;
984263bc
MD
373 }
374 if (flags & RFLINUXTHPN)
375 p2->p_sigparent = SIGUSR1;
376 else
377 p2->p_sigparent = SIGCHLD;
378
379 /* bump references to the text vnode (for procfs) */
380 p2->p_textvp = p1->p_textvp;
381 if (p2->p_textvp)
597aea93 382 vref(p2->p_textvp);
984263bc 383
0daa37a5
MD
384 /*
385 * Handle file descriptors
386 */
984263bc
MD
387 if (flags & RFCFDG) {
388 p2->p_fd = fdinit(p1);
389 fdtol = NULL;
390 } else if (flags & RFFDG) {
391 p2->p_fd = fdcopy(p1);
392 fdtol = NULL;
393 } else {
394 p2->p_fd = fdshare(p1);
395 if (p1->p_fdtol == NULL)
396 p1->p_fdtol =
397 filedesc_to_leader_alloc(NULL,
398 p1->p_leader);
399 if ((flags & RFTHREAD) != 0) {
400 /*
401 * Shared file descriptor table and
402 * shared process leaders.
403 */
404 fdtol = p1->p_fdtol;
405 fdtol->fdl_refcount++;
406 } else {
407 /*
408 * Shared file descriptor table, and
409 * different process leaders
410 */
98a7f915 411 fdtol = filedesc_to_leader_alloc(p1->p_fdtol, p2);
984263bc
MD
412 }
413 }
414 p2->p_fdtol = fdtol;
c0b8a06d 415 p2->p_limit = plimit_fork(p1->p_limit);
984263bc
MD
416
417 /*
418 * Preserve some more flags in subprocess. P_PROFIL has already
419 * been preserved.
420 */
08f2f1bb 421 p2->p_flag |= p1->p_flag & P_SUGID;
984263bc
MD
422 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
423 p2->p_flag |= P_CONTROLT;
424 if (flags & RFPPWAIT)
425 p2->p_flag |= P_PPWAIT;
426
0daa37a5
MD
427 /*
428 * Inherit the virtual kernel structure (allows a virtual kernel
429 * to fork to simulate multiple cpus).
430 */
4a22e893
MD
431 if (p1->p_vkernel)
432 vkernel_inherit(p1, p2);
0daa37a5 433
5fd012e0
MD
434 /*
435 * Once we are on a pglist we may receive signals. XXX we might
436 * race a ^C being sent to the process group by not receiving it
437 * at all prior to this line.
438 */
984263bc
MD
439 LIST_INSERT_AFTER(p1, p2, p_pglist);
440
441 /*
442 * Attach the new process to its parent.
443 *
444 * If RFNOWAIT is set, the newly created process becomes a child
445 * of init. This effectively disassociates the child from the
446 * parent.
447 */
448 if (flags & RFNOWAIT)
449 pptr = initproc;
450 else
451 pptr = p1;
452 p2->p_pptr = pptr;
453 LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling);
454 LIST_INIT(&p2->p_children);
98a7f915 455 varsymset_init(&p2->p_varsymset, &p1->p_varsymset);
8fbf9130 456 callout_init(&p2->p_ithandle);
984263bc
MD
457
458#ifdef KTRACE
459 /*
460 * Copy traceflag and tracefile if enabled. If not inherited,
461 * these were zeroed above but we still could have a trace race
29f58392 462 * so make sure p2's p_tracenode is NULL.
984263bc 463 */
29f58392 464 if ((p1->p_traceflag & KTRFAC_INHERIT) && p2->p_tracenode == NULL) {
984263bc 465 p2->p_traceflag = p1->p_traceflag;
29f58392 466 p2->p_tracenode = ktrinherit(p1->p_tracenode);
984263bc
MD
467 }
468#endif
469
984263bc
MD
470 /*
471 * This begins the section where we must prevent the parent
472 * from being swapped.
13d13d89
SS
473 *
474 * Gets PRELE'd in the caller in start_forked_proc().
984263bc
MD
475 */
476 PHOLD(p1);
477
13d13d89
SS
478 vm_fork(p1, p2, flags);
479
984263bc 480 /*
13d13d89
SS
481 * Create the first lwp associated with the new proc.
482 * It will return via a different execution path later, directly
483 * into userland, after it was put on the runq by
484 * start_forked_proc().
984263bc 485 */
13d13d89 486 lwp_fork(lp1, p2, flags);
984263bc
MD
487
488 if (flags == (RFFDG | RFPROC)) {
12e4aaff
MD
489 mycpu->gd_cnt.v_forks++;
490 mycpu->gd_cnt.v_forkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
984263bc 491 } else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) {
12e4aaff
MD
492 mycpu->gd_cnt.v_vforks++;
493 mycpu->gd_cnt.v_vforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
984263bc 494 } else if (p1 == &proc0) {
12e4aaff
MD
495 mycpu->gd_cnt.v_kthreads++;
496 mycpu->gd_cnt.v_kthreadpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
984263bc 497 } else {
12e4aaff
MD
498 mycpu->gd_cnt.v_rforks++;
499 mycpu->gd_cnt.v_rforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize;
984263bc
MD
500 }
501
502 /*
503 * Both processes are set up, now check if any loadable modules want
504 * to adjust anything.
505 * What if they have an error? XXX
506 */
507 TAILQ_FOREACH(ep, &fork_list, next) {
508 (*ep->function)(p1, p2, flags);
509 }
510
511 /*
a77ac49d
MD
512 * Set the start time. Note that the process is not runnable. The
513 * caller is responsible for making it runnable.
984263bc 514 */
d9fa5f67 515 microtime(&p2->p_start);
984263bc 516 p2->p_acflag = AFORK;
984263bc
MD
517
518 /*
519 * tell any interested parties about the new process
520 */
521 KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
522
984263bc
MD
523 /*
524 * Return child proc pointer to parent.
525 */
526 *procp = p2;
167e6ecb
MD
527done:
528 if (pgrp)
529 lockmgr(&pgrp->pg_lock, LK_RELEASE);
530 return (error);
984263bc
MD
531}
532
13d13d89
SS
533static struct lwp *
534lwp_fork(struct lwp *origlp, struct proc *destproc, int flags)
535{
536 struct lwp *lp;
537 struct thread *td;
538 lwpid_t tid;
539
540 /*
541 * We need to prevent wrap-around collisions.
542 * Until we have a nice tid allocator, we need to
543 * start searching for free tids once we wrap around.
544 *
545 * XXX give me a nicer allocator
546 */
547 if (destproc->p_lasttid + 1 <= 0) {
548 tid = 0;
549restart:
550 FOREACH_LWP_IN_PROC(lp, destproc) {
551 if (lp->lwp_tid != tid)
552 continue;
553 /* tids match, search next. */
554 tid++;
555 /*
556 * Wait -- the whole tid space is depleted?
557 * Impossible.
558 */
559 if (tid <= 0)
560 panic("lwp_fork: All tids depleted?!");
561 goto restart;
562 }
563 /* When we come here, the tid is not occupied */
564 } else {
565 tid = destproc->p_lasttid++;
566 }
567
568 lp = zalloc(lwp_zone);
f9fe34b6 569 bzero(lp, sizeof(*lp));
13d13d89 570 lp->lwp_proc = destproc;
287ebb09 571 lp->lwp_vmspace = destproc->p_vmspace;
13d13d89
SS
572 lp->lwp_tid = tid;
573 LIST_INSERT_HEAD(&destproc->p_lwps, lp, lwp_list);
574 destproc->p_nthreads++;
575 lp->lwp_stat = LSRUN;
13d13d89
SS
576 bcopy(&origlp->lwp_startcopy, &lp->lwp_startcopy,
577 (unsigned) ((caddr_t)&lp->lwp_endcopy -
578 (caddr_t)&lp->lwp_startcopy));
13d13d89
SS
579 lp->lwp_flag |= origlp->lwp_flag & LWP_ALTSTACK;
580 /*
581 * Set cpbase to the last timeout that occured (not the upcoming
582 * timeout).
583 *
584 * A critical section is required since a timer IPI can update
585 * scheduler specific data.
586 */
587 crit_enter();
588 lp->lwp_cpbase = mycpu->gd_schedclock.time -
589 mycpu->gd_schedclock.periodic;
590 destproc->p_usched->heuristic_forking(origlp, lp);
591 crit_exit();
a5ae2446 592 lp->lwp_cpumask &= usched_mastermask;
13d13d89
SS
593
594 td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, -1, 0);
595 lp->lwp_thread = td;
596 td->td_proc = destproc;
597 td->td_lwp = lp;
598 td->td_switch = cpu_heavy_switch;
599#ifdef SMP
600 KKASSERT(td->td_mpcount == 1);
601#endif
602 lwkt_setpri(td, TDPRI_KERN_USER);
603 lwkt_set_comm(td, "%s", destproc->p_comm);
604
605 /*
606 * cpu_fork will copy and update the pcb, set up the kernel stack,
607 * and make the child ready to run.
608 */
609 cpu_fork(origlp, lp, flags);
610 caps_fork(origlp->lwp_thread, lp->lwp_thread);
611
612 return (lp);
613}
614
984263bc
MD
615/*
616 * The next two functionms are general routines to handle adding/deleting
617 * items on the fork callout list.
618 *
619 * at_fork():
620 * Take the arguments given and put them onto the fork callout list,
621 * However first make sure that it's not already there.
622 * Returns 0 on success or a standard error number.
623 */
984263bc 624int
303c76d5 625at_fork(forklist_fn function)
984263bc
MD
626{
627 struct forklist *ep;
628
629#ifdef INVARIANTS
630 /* let the programmer know if he's been stupid */
303c76d5 631 if (rm_at_fork(function)) {
6ea70f76 632 kprintf("WARNING: fork callout entry (%p) already present\n",
984263bc 633 function);
303c76d5 634 }
984263bc 635#endif
efda3bd0 636 ep = kmalloc(sizeof(*ep), M_ATFORK, M_WAITOK|M_ZERO);
984263bc
MD
637 ep->function = function;
638 TAILQ_INSERT_TAIL(&fork_list, ep, next);
639 return (0);
640}
641
642/*
643 * Scan the exit callout list for the given item and remove it..
644 * Returns the number of items removed (0 or 1)
645 */
984263bc 646int
303c76d5 647rm_at_fork(forklist_fn function)
984263bc
MD
648{
649 struct forklist *ep;
650
651 TAILQ_FOREACH(ep, &fork_list, next) {
652 if (ep->function == function) {
653 TAILQ_REMOVE(&fork_list, ep, next);
efda3bd0 654 kfree(ep, M_ATFORK);
984263bc
MD
655 return(1);
656 }
657 }
658 return (0);
659}
7d0bac62
MD
660
661/*
662 * Add a forked process to the run queue after any remaining setup, such
663 * as setting the fork handler, has been completed.
664 */
7d0bac62 665void
553ea3c8 666start_forked_proc(struct lwp *lp1, struct proc *p2)
7d0bac62 667{
08f2f1bb 668 struct lwp *lp2 = ONLY_LWP_IN_PROC(p2);
553ea3c8 669
7d0bac62 670 /*
26a0694b
MD
671 * Move from SIDL to RUN queue, and activate the process's thread.
672 * Activation of the thread effectively makes the process "a"
673 * current process, so we do not setrunqueue().
8ec60c3f
MD
674 *
675 * YYY setrunqueue works here but we should clean up the trampoline
676 * code so we just schedule the LWKT thread and let the trampoline
677 * deal with the userland scheduler on return to userland.
7d0bac62 678 */
553ea3c8 679 KASSERT(p2->p_stat == SIDL,
7d0bac62 680 ("cannot start forked process, bad status: %p", p2));
553ea3c8 681 p2->p_usched->resetpriority(lp2);
e43a034f 682 crit_enter();
164b8401
SS
683 p2->p_stat = SACTIVE;
684 lp2->lwp_stat = LSRUN;
553ea3c8 685 p2->p_usched->setrunqueue(lp2);
e43a034f 686 crit_exit();
7d0bac62
MD
687
688 /*
689 * Now can be swapped.
690 */
553ea3c8 691 PRELE(lp1->lwp_proc);
7d0bac62
MD
692
693 /*
694 * Preserve synchronization semantics of vfork. If waiting for
695 * child to exec or exit, set P_PPWAIT on child, and sleep on our
696 * proc (in case of exit).
697 */
698 while (p2->p_flag & P_PPWAIT)
553ea3c8 699 tsleep(lp1->lwp_proc, 0, "ppwait", 0);
7d0bac62 700}