]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_fork.c
xnu-201.19.tar.gz
[apple/xnu.git] / bsd / kern / kern_fork.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
61 */
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/filedesc.h>
66#include <sys/kernel.h>
67#include <sys/malloc.h>
68#include <sys/proc.h>
69#include <sys/user.h>
70#include <sys/resourcevar.h>
71#include <sys/vnode.h>
72#include <sys/file.h>
73#include <sys/acct.h>
74#include <sys/ktrace.h>
75
76#include <mach/mach_types.h>
77#include <kern/mach_param.h>
78
79#include <machine/spl.h>
80
81thread_t cloneproc(struct proc *, int);
0b4e3aa0 82struct proc * forkproc(struct proc *, int);
1c79356b
A
83thread_t procdup();
84
85#define DOFORK 0x1 /* fork() system call */
86#define DOVFORK 0x2 /* vfork() system call */
87static int fork1(struct proc *, long, register_t *);
88
89/*
90 * fork system call.
91 */
92int
93fork(p, uap, retval)
94 struct proc *p;
95 void *uap;
96 register_t *retval;
97{
98 return (fork1(p, (long)DOFORK, retval));
99}
100
101/*
102 * vfork system call
103 */
104int
105vfork(p, uap, retval)
106 struct proc *p;
107 void *uap;
108 register_t *retval;
109{
0b4e3aa0
A
110 register struct proc * newproc;
111 register uid_t uid;
112 thread_act_t cur_act = (thread_act_t)current_act();
113 int count;
114 task_t t;
115 uthread_t ut;
116
117 /*
118 * Although process entries are dynamically created, we still keep
119 * a global limit on the maximum number we will create. Don't allow
120 * a nonprivileged user to use the last process; don't let root
121 * exceed the limit. The variable nprocs is the current number of
122 * processes, maxproc is the limit.
123 */
124 uid = p->p_cred->p_ruid;
125 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
126 tablefull("proc");
127 retval[1] = 0;
128 return (EAGAIN);
129 }
130
131 /*
132 * Increment the count of procs running with this uid. Don't allow
133 * a nonprivileged user to exceed their current limit.
134 */
135 count = chgproccnt(uid, 1);
136 if (uid != 0 && count > p->p_rlimit[RLIMIT_NPROC].rlim_cur) {
137 (void)chgproccnt(uid, -1);
138 return (EAGAIN);
139 }
140
141 ut = (struct uthread *)get_bsdthread_info(cur_act);
142 if (ut->uu_flag & P_VFORK) {
143 printf("vfork called recursively by %s\n", p->p_comm);
144 return (EINVAL);
145 }
146 p->p_flag |= P_VFORK;
147 p->p_vforkcnt++;
148
149 /* The newly created process comes with signal lock held */
150 newproc = (struct proc *)forkproc(p,1);
151
152 LIST_INSERT_AFTER(p, newproc, p_pglist);
153 newproc->p_pptr = p;
154 newproc->task = p->task;
155 LIST_INSERT_HEAD(&p->p_children, newproc, p_sibling);
156 LIST_INIT(&newproc->p_children);
157 LIST_INSERT_HEAD(&allproc, newproc, p_list);
158 LIST_INSERT_HEAD(PIDHASH(newproc->p_pid), newproc, p_hash);
159 TAILQ_INIT(& newproc->p_evlist);
160 newproc->p_stat = SRUN;
161 newproc->p_flag |= P_INVFORK;
162 newproc->p_vforkact = cur_act;
163
164 ut->uu_flag |= P_VFORK;
165 ut->uu_proc = newproc;
166 ut->uu_userstate = (void *)act_thread_csave();
167
168 thread_set_child(cur_act, newproc->p_pid);
169
170 newproc->p_stats->p_start = time;
171 newproc->p_acflag = AFORK;
172
173 /*
174 * Preserve synchronization semantics of vfork. If waiting for
175 * child to exec or exit, set P_PPWAIT on child, and sleep on our
176 * proc (in case of exit).
177 */
178 newproc->p_flag |= P_PPWAIT;
179
180 /* drop the signal lock on the child */
181 signal_unlock(newproc);
182
183 retval[0] = newproc->p_pid;
184 retval[1] = 1; /* mark child */
185
186 return (0);
1c79356b
A
187}
188
0b4e3aa0
A
189/*
190 * Return to parent vfork ehread()
191 */
192void
193vfork_return(th_act, p, p2, retval)
194 thread_act_t th_act;
195 struct proc * p;
196 struct proc *p2;
197 register_t *retval;
198{
199 long flags;
200 register uid_t uid;
201 thread_t newth, self = current_thread();
202 thread_act_t cur_act = (thread_act_t)current_act();
203 int s, count;
204 task_t t;
205 uthread_t ut;
206
207 ut = (struct uthread *)get_bsdthread_info(cur_act);
208
209 act_thread_catt(ut->uu_userstate);
210
211 /* Make sure only one at this time */
212 p->p_vforkcnt--;
213 if (p->p_vforkcnt <0)
214 panic("vfork cnt is -ve");
215 if (p->p_vforkcnt <=0)
216 p->p_flag &= ~P_VFORK;
217 ut->uu_userstate = 0;
218 ut->uu_flag &= ~P_VFORK;
219 ut->uu_proc = 0;
220 p2->p_flag &= ~P_INVFORK;
221 p2->p_vforkact = (void *)0;
222
223 thread_set_parent(cur_act, p2->p_pid);
224
225 if (retval) {
226 retval[0] = p2->p_pid;
227 retval[1] = 0; /* mark parent */
228 }
229
230 return;
231}
232
233thread_t
234procdup(
235 struct proc *child,
236 struct proc *parent)
237{
238 thread_t thread;
239 task_t task;
240 kern_return_t result;
241 extern task_t kernel_task;
242
243 if (parent->task == kernel_task)
244 result = task_create_local(TASK_NULL, FALSE, FALSE, &task);
245 else
246 result = task_create_local(parent->task, TRUE, FALSE, &task);
247 if (result != KERN_SUCCESS)
248 printf("fork/procdup: task_create failed. Code: 0x%x\n", result);
249 child->task = task;
250 /* task->proc = child; */
251 set_bsdtask_info(task, child);
252 if (child->p_nice != 0)
253 resetpriority(child);
254 result = thread_create(task, &thread);
255 if (result != KERN_SUCCESS)
256 printf("fork/procdup: thread_create failed. Code: 0x%x\n", result);
257
258 return(thread);
259}
260
261
1c79356b
A
262static int
263fork1(p1, flags, retval)
264 struct proc *p1;
265 long flags;
266 register_t *retval;
267{
268 register struct proc *p2;
269 register uid_t uid;
270 thread_t newth, self = current_thread();
271 int s, count;
272 task_t t;
273
274 /*
275 * Although process entries are dynamically created, we still keep
276 * a global limit on the maximum number we will create. Don't allow
277 * a nonprivileged user to use the last process; don't let root
278 * exceed the limit. The variable nprocs is the current number of
279 * processes, maxproc is the limit.
280 */
281 uid = p1->p_cred->p_ruid;
282 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) {
283 tablefull("proc");
284 retval[1] = 0;
285 return (EAGAIN);
286 }
287
288 /*
289 * Increment the count of procs running with this uid. Don't allow
290 * a nonprivileged user to exceed their current limit.
291 */
292 count = chgproccnt(uid, 1);
293 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) {
294 (void)chgproccnt(uid, -1);
295 return (EAGAIN);
296 }
297
298 /* The newly created process comes with signal lock held */
299 newth = cloneproc(p1, 1);
300 thread_dup(current_act(), newth);
301 /* p2 = newth->task->proc; */
302 p2 = (struct proc *)(get_bsdtask_info(get_threadtask(newth)));
303
304 thread_set_child(newth, p2->p_pid);
305
306 s = splhigh();
307 p2->p_stats->p_start = time;
308 splx(s);
309 p2->p_acflag = AFORK;
310
311 /*
312 * Preserve synchronization semantics of vfork. If waiting for
313 * child to exec or exit, set P_PPWAIT on child, and sleep on our
314 * proc (in case of exit).
315 */
316 if (flags == DOVFORK)
317 p2->p_flag |= P_PPWAIT;
318 /* drop the signal lock on the child */
319 signal_unlock(p2);
320
321 (void) thread_resume(newth);
322
323 /* drop the extra references we got during the creation */
0b4e3aa0 324 if (t = (task_t)get_threadtask(newth)) {
1c79356b
A
325 task_deallocate(t);
326 }
327 act_deallocate(newth);
328
329 while (p2->p_flag & P_PPWAIT)
330 tsleep(p1, PWAIT, "ppwait", 0);
331
332 retval[0] = p2->p_pid;
333 retval[1] = 0; /* mark parent */
334
335 return (0);
336}
337
338/*
339 * cloneproc()
340 *
341 * Create a new process from a specified process.
342 * On return newly created child process has signal
343 * lock held to block delivery of signal to it if called with
344 * lock set. fork() code needs to explicity remove this lock
345 * before signals can be delivered
346 */
347thread_t
348cloneproc(p1, lock)
349 register struct proc *p1;
350 register int lock;
0b4e3aa0
A
351{
352 register struct proc *p2;
353 thread_t th;
354
355 p2 = (struct proc *)forkproc(p1,lock);
356 th = procdup(p2, p1); /* child, parent */
357
358 LIST_INSERT_AFTER(p1, p2, p_pglist);
359 p2->p_pptr = p1;
360 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
361 LIST_INIT(&p2->p_children);
362 LIST_INSERT_HEAD(&allproc, p2, p_list);
363 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
364 TAILQ_INIT(&p2->p_evlist);
365 /*
366 * Make child runnable, set start time.
367 */
368 p2->p_stat = SRUN;
369
370 return(th);
371}
372
373struct proc *
374forkproc(p1, lock)
375 register struct proc *p1;
376 register int lock;
1c79356b
A
377{
378 register struct proc *p2, *newproc;
379 static int nextpid = 0, pidchecked = 0;
380 thread_t th;
381
382 /* Allocate new proc. */
383 MALLOC_ZONE(newproc, struct proc *,
384 sizeof *newproc, M_PROC, M_WAITOK);
385 MALLOC_ZONE(newproc->p_cred, struct pcred *,
386 sizeof *newproc->p_cred, M_SUBPROC, M_WAITOK);
387 MALLOC_ZONE(newproc->p_stats, struct pstats *,
388 sizeof *newproc->p_stats, M_SUBPROC, M_WAITOK);
389 MALLOC_ZONE(newproc->p_sigacts, struct sigacts *,
390 sizeof *newproc->p_sigacts, M_SUBPROC, M_WAITOK);
391
392 /*
393 * Find an unused process ID. We remember a range of unused IDs
394 * ready to use (from nextpid+1 through pidchecked-1).
395 */
396 nextpid++;
397retry:
398 /*
399 * If the process ID prototype has wrapped around,
400 * restart somewhat above 0, as the low-numbered procs
401 * tend to include daemons that don't exit.
402 */
403 if (nextpid >= PID_MAX) {
404 nextpid = 100;
405 pidchecked = 0;
406 }
407 if (nextpid >= pidchecked) {
408 int doingzomb = 0;
409
410 pidchecked = PID_MAX;
411 /*
412 * Scan the active and zombie procs to check whether this pid
413 * is in use. Remember the lowest pid that's greater
414 * than nextpid, so we can avoid checking for a while.
415 */
416 p2 = allproc.lh_first;
417again:
418 for (; p2 != 0; p2 = p2->p_list.le_next) {
419 while (p2->p_pid == nextpid ||
420 p2->p_pgrp->pg_id == nextpid) {
421 nextpid++;
422 if (nextpid >= pidchecked)
423 goto retry;
424 }
425 if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
426 pidchecked = p2->p_pid;
427 if (p2->p_pgrp && p2->p_pgrp->pg_id > nextpid &&
428 pidchecked > p2->p_pgrp->pg_id)
429 pidchecked = p2->p_pgrp->pg_id;
430 }
431 if (!doingzomb) {
432 doingzomb = 1;
433 p2 = zombproc.lh_first;
434 goto again;
435 }
436 }
437
438 nprocs++;
439 p2 = newproc;
440 p2->p_stat = SIDL;
441 p2->p_pid = nextpid;
442
443 /*
444 * Make a proc table entry for the new process.
445 * Start by zeroing the section of proc that is zero-initialized,
446 * then copy the section that is copied directly from the parent.
447 */
448 bzero(&p2->p_startzero,
449 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
450 bcopy(&p1->p_startcopy, &p2->p_startcopy,
451 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
452 p2->vm_shm = (void *)NULL; /* Make sure it is zero */
453
454 /*
455 * Duplicate sub-structures as needed.
456 * Increase reference counts on shared objects.
457 * The p_stats and p_sigacts substructs are set in vm_fork.
458 */
459 p2->p_flag = P_INMEM;
460 if (p1->p_flag & P_PROFIL)
461 startprofclock(p2);
462 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
463 p2->p_cred->p_refcnt = 1;
464 crhold(p1->p_ucred);
465 lockinit(&p2->p_cred->pc_lock, PLOCK, "proc cred", 0, 0);
466
467 /* bump references to the text vnode (for procfs) */
468 p2->p_textvp = p1->p_textvp;
469 if (p2->p_textvp)
470 VREF(p2->p_textvp);
471
472 p2->p_fd = fdcopy(p1);
473 if (p1->vm_shm) {
474 shmfork(p1,p2);
475 }
476 /*
477 * If p_limit is still copy-on-write, bump refcnt,
478 * otherwise get a copy that won't be modified.
479 * (If PL_SHAREMOD is clear, the structure is shared
480 * copy-on-write.)
481 */
482 if (p1->p_limit->p_lflags & PL_SHAREMOD)
483 p2->p_limit = limcopy(p1->p_limit);
484 else {
485 p2->p_limit = p1->p_limit;
486 p2->p_limit->p_refcnt++;
487 }
488
489 bzero(&p2->p_stats->pstat_startzero,
490 (unsigned) ((caddr_t)&p2->p_stats->pstat_endzero -
491 (caddr_t)&p2->p_stats->pstat_startzero));
492 bcopy(&p1->p_stats->pstat_startcopy, &p2->p_stats->pstat_startcopy,
493 ((caddr_t)&p2->p_stats->pstat_endcopy -
494 (caddr_t)&p2->p_stats->pstat_startcopy));
495
496 if (p1->p_sigacts != NULL)
497 (void)memcpy(p2->p_sigacts,
498 p1->p_sigacts, sizeof *p2->p_sigacts);
499 else
500 (void)memset(p2->p_sigacts, 0, sizeof *p2->p_sigacts);
501
502 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
503 p2->p_flag |= P_CONTROLT;
504
505 p2->p_xstat = 0;
506 p2->p_ru = NULL;
507
508 p2->p_debugger = 0; /* don't inherit */
509 lockinit(&p2->signal_lock, PVM, "signal", 0, 0);
510 /* block all signals to reach the process */
511 if (lock)
512 signal_lock(p2);
513 p2->sigwait = FALSE;
514 p2->sigwait_thread = NULL;
515 p2->exit_thread = NULL;
516 p2->user_stack = p1->user_stack;
517 p2->p_sigpending = 0;
0b4e3aa0
A
518 p2->p_vforkcnt = 0;
519 p2->p_vforkact = 0;
1c79356b
A
520
521#if KTRACE
522 /*
523 * Copy traceflag and tracefile if enabled.
524 * If not inherited, these were zeroed above.
525 */
526 if (p1->p_traceflag&KTRFAC_INHERIT) {
527 p2->p_traceflag = p1->p_traceflag;
528 if ((p2->p_tracep = p1->p_tracep) != NULL)
529 VREF(p2->p_tracep);
530 }
531#endif
0b4e3aa0 532 return(p2);
1c79356b 533
1c79356b
A
534}
535
536#include <kern/zalloc.h>
537
538struct zone *uthread_zone;
539int uthread_zone_inited = 0;
540
541void
542uthread_zone_init()
543{
544 if (!uthread_zone_inited) {
545 uthread_zone = zinit(sizeof(struct uthread),
546 THREAD_MAX * sizeof(struct uthread),
547 THREAD_CHUNK * sizeof(struct uthread),
548 "uthreads");
549 uthread_zone_inited = 1;
550 }
551}
552
553void *
554uthread_alloc(void)
555{
556 void *ut;
557
558 if (!uthread_zone_inited)
559 uthread_zone_init();
560
561 ut = (void *)zalloc(uthread_zone);
562 bzero(ut, sizeof(struct uthread));
563 return (ut);
564}
565
0b4e3aa0 566
1c79356b
A
567void
568uthread_free(void *uthread)
569{
570 struct _select *sel;
571 struct uthread *uth = (struct uthread *)uthread;
0b4e3aa0 572 int size;
1c79356b
A
573
574 sel = &uth->uu_state.ss_select;
575 /* cleanup the select bit space */
576 if (sel->nbytes) {
577 FREE(sel->ibits, M_TEMP);
578 FREE(sel->obits, M_TEMP);
579 }
580
0b4e3aa0
A
581 if (sel->allocsize && uth->uu_wqsub){
582 kfree(uth->uu_wqsub, sel->allocsize);
583 sel->count = sel->nfcount = 0;
584 sel->allocsize = 0;
585 uth->uu_wqsub = 0;
586 sel->wql = 0;
587 }
588
1c79356b
A
589 /* and free the uthread itself */
590 zfree(uthread_zone, (vm_offset_t)uthread);
591}