]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_fork.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
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.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
60 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
63 #include <kern/assert.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/filedesc.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
71 #include <sys/resourcevar.h>
72 #include <sys/vnode.h>
77 #include <bsm/audit_kernel.h>
80 #include <sys/ktrace.h>
84 #include <mach/mach_types.h>
85 #include <kern/mach_param.h>
87 #include <machine/spl.h>
89 thread_act_t
cloneproc(struct proc
*, int);
90 struct proc
* forkproc(struct proc
*, int);
91 thread_act_t
procdup();
93 #define DOFORK 0x1 /* fork() system call */
94 #define DOVFORK 0x2 /* vfork() system call */
95 static int fork1(struct proc
*, long, register_t
*);
106 return (fork1(p
, (long)DOFORK
, retval
));
113 vfork(p
, uap
, retval
)
118 register struct proc
* newproc
;
120 thread_act_t cur_act
= (thread_act_t
)current_act();
126 * Although process entries are dynamically created, we still keep
127 * a global limit on the maximum number we will create. Don't allow
128 * a nonprivileged user to use the last process; don't let root
129 * exceed the limit. The variable nprocs is the current number of
130 * processes, maxproc is the limit.
132 uid
= p
->p_cred
->p_ruid
;
133 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
140 * Increment the count of procs running with this uid. Don't allow
141 * a nonprivileged user to exceed their current limit.
143 count
= chgproccnt(uid
, 1);
144 if (uid
!= 0 && count
> p
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
145 (void)chgproccnt(uid
, -1);
149 ut
= (struct uthread
*)get_bsdthread_info(cur_act
);
150 if (ut
->uu_flag
& P_VFORK
) {
151 printf("vfork called recursively by %s\n", p
->p_comm
);
152 (void)chgproccnt(uid
, -1);
155 p
->p_flag
|= P_VFORK
;
158 /* The newly created process comes with signal lock held */
159 newproc
= (struct proc
*)forkproc(p
,1);
161 AUDIT_ARG(pid
, newproc
->p_pid
);
163 LIST_INSERT_AFTER(p
, newproc
, p_pglist
);
165 newproc
->task
= p
->task
;
166 LIST_INSERT_HEAD(&p
->p_children
, newproc
, p_sibling
);
167 LIST_INIT(&newproc
->p_children
);
168 LIST_INSERT_HEAD(&allproc
, newproc
, p_list
);
169 LIST_INSERT_HEAD(PIDHASH(newproc
->p_pid
), newproc
, p_hash
);
170 TAILQ_INIT(& newproc
->p_evlist
);
171 newproc
->p_stat
= SRUN
;
172 newproc
->p_flag
|= P_INVFORK
;
173 newproc
->p_vforkact
= cur_act
;
175 ut
->uu_flag
|= P_VFORK
;
176 ut
->uu_proc
= newproc
;
177 ut
->uu_userstate
= (void *)act_thread_csave();
178 ut
->uu_vforkmask
= ut
->uu_sigmask
;
180 thread_set_child(cur_act
, newproc
->p_pid
);
182 newproc
->p_stats
->p_start
= time
;
183 newproc
->p_acflag
= AFORK
;
186 * Preserve synchronization semantics of vfork. If waiting for
187 * child to exec or exit, set P_PPWAIT on child, and sleep on our
188 * proc (in case of exit).
190 newproc
->p_flag
|= P_PPWAIT
;
192 /* drop the signal lock on the child */
193 signal_unlock(newproc
);
195 retval
[0] = newproc
->p_pid
;
196 retval
[1] = 1; /* mark child */
202 * Return to parent vfork ehread()
205 vfork_return(th_act
, p
, p2
, retval
)
217 ut
= (struct uthread
*)get_bsdthread_info(th_act
);
219 act_thread_catt(ut
->uu_userstate
);
221 /* Make sure only one at this time */
224 if (p
->p_vforkcnt
<0)
225 panic("vfork cnt is -ve");
226 if (p
->p_vforkcnt
<=0)
227 p
->p_flag
&= ~P_VFORK
;
229 ut
->uu_userstate
= 0;
230 ut
->uu_flag
&= ~P_VFORK
;
232 ut
->uu_sigmask
= ut
->uu_vforkmask
;
233 p2
->p_flag
&= ~P_INVFORK
;
234 p2
->p_vforkact
= (void *)0;
236 thread_set_parent(th_act
, p2
->p_pid
);
239 retval
[0] = p2
->p_pid
;
240 retval
[1] = 0; /* mark parent */
253 kern_return_t result
;
255 extern task_t kernel_task
;
257 if (parent
->task
== kernel_task
)
258 result
= task_create_internal(TASK_NULL
, FALSE
, &task
);
260 result
= task_create_internal(parent
->task
, TRUE
, &task
);
261 if (result
!= KERN_SUCCESS
)
262 printf("fork/procdup: task_create failed. Code: 0x%x\n", result
);
264 /* task->proc = child; */
265 set_bsdtask_info(task
, child
);
266 if (child
->p_nice
!= 0)
267 resetpriority(child
);
269 result
= thread_create(task
, &thread
);
270 if (result
!= KERN_SUCCESS
)
271 printf("fork/procdup: thread_create failed. Code: 0x%x\n", result
);
278 fork1(p1
, flags
, retval
)
283 register struct proc
*p2
;
290 * Although process entries are dynamically created, we still keep
291 * a global limit on the maximum number we will create. Don't allow
292 * a nonprivileged user to use the last process; don't let root
293 * exceed the limit. The variable nprocs is the current number of
294 * processes, maxproc is the limit.
296 uid
= p1
->p_cred
->p_ruid
;
297 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
304 * Increment the count of procs running with this uid. Don't allow
305 * a nonprivileged user to exceed their current limit.
307 count
= chgproccnt(uid
, 1);
308 if (uid
!= 0 && count
> p1
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
309 (void)chgproccnt(uid
, -1);
313 /* The newly created process comes with signal lock held */
314 newth
= cloneproc(p1
, 1);
316 /* p2 = newth->task->proc; */
317 p2
= (struct proc
*)(get_bsdtask_info(get_threadtask(newth
)));
319 AUDIT_ARG(pid
, p2
->p_pid
);
321 thread_set_child(newth
, p2
->p_pid
);
324 p2
->p_stats
->p_start
= time
;
326 p2
->p_acflag
= AFORK
;
329 * Preserve synchronization semantics of vfork. If waiting for
330 * child to exec or exit, set P_PPWAIT on child, and sleep on our
331 * proc (in case of exit).
333 if (flags
== DOVFORK
)
334 p2
->p_flag
|= P_PPWAIT
;
335 /* drop the signal lock on the child */
338 (void) thread_resume(newth
);
340 /* drop the extra references we got during the creation */
341 if (t
= (task_t
)get_threadtask(newth
)) {
344 act_deallocate(newth
);
346 KNOTE(&p1
->p_klist
, NOTE_FORK
| p2
->p_pid
);
348 while (p2
->p_flag
& P_PPWAIT
)
349 tsleep(p1
, PWAIT
, "ppwait", 0);
351 retval
[0] = p2
->p_pid
;
352 retval
[1] = 0; /* mark parent */
360 * Create a new process from a specified process.
361 * On return newly created child process has signal
362 * lock held to block delivery of signal to it if called with
363 * lock set. fork() code needs to explicity remove this lock
364 * before signals can be delivered
368 register struct proc
*p1
;
371 register struct proc
*p2
;
374 p2
= (struct proc
*)forkproc(p1
,lock
);
377 th
= procdup(p2
, p1
); /* child, parent */
379 LIST_INSERT_AFTER(p1
, p2
, p_pglist
);
381 LIST_INSERT_HEAD(&p1
->p_children
, p2
, p_sibling
);
382 LIST_INIT(&p2
->p_children
);
383 LIST_INSERT_HEAD(&allproc
, p2
, p_list
);
384 LIST_INSERT_HEAD(PIDHASH(p2
->p_pid
), p2
, p_hash
);
385 TAILQ_INIT(&p2
->p_evlist
);
387 * Make child runnable, set start time.
396 register struct proc
*p1
;
399 register struct proc
*p2
, *newproc
;
400 static int nextpid
= 0, pidchecked
= 0;
403 /* Allocate new proc. */
404 MALLOC_ZONE(newproc
, struct proc
*,
405 sizeof *newproc
, M_PROC
, M_WAITOK
);
406 MALLOC_ZONE(newproc
->p_cred
, struct pcred
*,
407 sizeof *newproc
->p_cred
, M_SUBPROC
, M_WAITOK
);
408 MALLOC_ZONE(newproc
->p_stats
, struct pstats
*,
409 sizeof *newproc
->p_stats
, M_SUBPROC
, M_WAITOK
);
410 MALLOC_ZONE(newproc
->p_sigacts
, struct sigacts
*,
411 sizeof *newproc
->p_sigacts
, M_SUBPROC
, M_WAITOK
);
414 * Find an unused process ID. We remember a range of unused IDs
415 * ready to use (from nextpid+1 through pidchecked-1).
420 * If the process ID prototype has wrapped around,
421 * restart somewhat above 0, as the low-numbered procs
422 * tend to include daemons that don't exit.
424 if (nextpid
>= PID_MAX
) {
428 if (nextpid
>= pidchecked
) {
431 pidchecked
= PID_MAX
;
433 * Scan the active and zombie procs to check whether this pid
434 * is in use. Remember the lowest pid that's greater
435 * than nextpid, so we can avoid checking for a while.
437 p2
= allproc
.lh_first
;
439 for (; p2
!= 0; p2
= p2
->p_list
.le_next
) {
440 while (p2
->p_pid
== nextpid
||
441 p2
->p_pgrp
->pg_id
== nextpid
||
442 p2
->p_session
->s_sid
== nextpid
) {
444 if (nextpid
>= pidchecked
)
447 if (p2
->p_pid
> nextpid
&& pidchecked
> p2
->p_pid
)
448 pidchecked
= p2
->p_pid
;
449 if (p2
->p_pgrp
&& p2
->p_pgrp
->pg_id
> nextpid
&&
450 pidchecked
> p2
->p_pgrp
->pg_id
)
451 pidchecked
= p2
->p_pgrp
->pg_id
;
452 if (p2
->p_session
->s_sid
> nextpid
&&
453 pidchecked
> p2
->p_session
->s_sid
)
454 pidchecked
= p2
->p_session
->s_sid
;
458 p2
= zombproc
.lh_first
;
468 p2
->p_shutdownstate
= 0;
470 * Make a proc table entry for the new process.
471 * Start by zeroing the section of proc that is zero-initialized,
472 * then copy the section that is copied directly from the parent.
474 bzero(&p2
->p_startzero
,
475 (unsigned) ((caddr_t
)&p2
->p_endzero
- (caddr_t
)&p2
->p_startzero
));
476 bcopy(&p1
->p_startcopy
, &p2
->p_startcopy
,
477 (unsigned) ((caddr_t
)&p2
->p_endcopy
- (caddr_t
)&p2
->p_startcopy
));
478 p2
->vm_shm
= (void *)NULL
; /* Make sure it is zero */
481 * Copy the audit info.
483 audit_proc_fork(p1
, p2
);
486 * Duplicate sub-structures as needed.
487 * Increase reference counts on shared objects.
488 * The p_stats and p_sigacts substructs are set in vm_fork.
490 p2
->p_flag
= P_INMEM
;
491 p2
->p_flag
|= (p1
->p_flag
& P_CLASSIC
); // copy from parent
492 p2
->p_flag
|= (p1
->p_flag
& P_AFFINITY
); // copy from parent
493 if (p1
->p_flag
& P_PROFIL
)
495 bcopy(p1
->p_cred
, p2
->p_cred
, sizeof(*p2
->p_cred
));
496 p2
->p_cred
->p_refcnt
= 1;
498 lockinit(&p2
->p_cred
->pc_lock
, PLOCK
, "proc cred", 0, 0);
499 klist_init(&p2
->p_klist
);
501 /* bump references to the text vnode */
502 p2
->p_textvp
= p1
->p_textvp
;
506 p2
->p_fd
= fdcopy(p1
);
511 * If p_limit is still copy-on-write, bump refcnt,
512 * otherwise get a copy that won't be modified.
513 * (If PL_SHAREMOD is clear, the structure is shared
516 if (p1
->p_limit
->p_lflags
& PL_SHAREMOD
)
517 p2
->p_limit
= limcopy(p1
->p_limit
);
519 p2
->p_limit
= p1
->p_limit
;
520 p2
->p_limit
->p_refcnt
++;
523 bzero(&p2
->p_stats
->pstat_startzero
,
524 (unsigned) ((caddr_t
)&p2
->p_stats
->pstat_endzero
-
525 (caddr_t
)&p2
->p_stats
->pstat_startzero
));
526 bcopy(&p1
->p_stats
->pstat_startcopy
, &p2
->p_stats
->pstat_startcopy
,
527 ((caddr_t
)&p2
->p_stats
->pstat_endcopy
-
528 (caddr_t
)&p2
->p_stats
->pstat_startcopy
));
530 if (p1
->p_sigacts
!= NULL
)
531 (void)memcpy(p2
->p_sigacts
,
532 p1
->p_sigacts
, sizeof *p2
->p_sigacts
);
534 (void)memset(p2
->p_sigacts
, 0, sizeof *p2
->p_sigacts
);
536 if (p1
->p_session
->s_ttyvp
!= NULL
&& p1
->p_flag
& P_CONTROLT
)
537 p2
->p_flag
|= P_CONTROLT
;
539 p2
->p_argslen
= p1
->p_argslen
;
540 p2
->p_argc
= p1
->p_argc
;
544 p2
->p_debugger
= 0; /* don't inherit */
545 lockinit(&p2
->signal_lock
, PVM
, "signal", 0, 0);
546 /* block all signals to reach the process */
550 p2
->sigwait_thread
= NULL
;
551 p2
->exit_thread
= NULL
;
552 p2
->user_stack
= p1
->user_stack
;
555 TAILQ_INIT(&p2
->p_uthlist
);
556 TAILQ_INIT(&p2
->aio_activeq
);
557 TAILQ_INIT(&p2
->aio_doneq
);
558 p2
->aio_active_count
= 0;
559 p2
->aio_done_count
= 0;
563 * Copy traceflag and tracefile if enabled.
564 * If not inherited, these were zeroed above.
566 if (p1
->p_traceflag
&KTRFAC_INHERIT
) {
567 p2
->p_traceflag
= p1
->p_traceflag
;
568 if ((p2
->p_tracep
= p1
->p_tracep
) != NULL
) {
569 if (UBCINFOEXISTS(p2
->p_tracep
))
570 ubc_hold(p2
->p_tracep
);
579 #include <kern/zalloc.h>
581 struct zone
*uthread_zone
;
582 int uthread_zone_inited
= 0;
587 if (!uthread_zone_inited
) {
588 uthread_zone
= zinit(sizeof(struct uthread
),
589 THREAD_MAX
* sizeof(struct uthread
),
590 THREAD_CHUNK
* sizeof(struct uthread
),
592 uthread_zone_inited
= 1;
597 uthread_alloc(task_t task
, thread_act_t thr_act
)
600 struct uthread
*uth
, *uth_parent
;
602 extern task_t kernel_task
;
603 boolean_t funnel_state
;
605 if (!uthread_zone_inited
)
608 ut
= (void *)zalloc(uthread_zone
);
609 bzero(ut
, sizeof(struct uthread
));
611 if (task
!= kernel_task
) {
612 uth
= (struct uthread
*)ut
;
613 p
= (struct proc
*) get_bsdtask_info(task
);
615 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
616 uth_parent
= (struct uthread
*)get_bsdthread_info(current_act());
618 if (uth_parent
->uu_flag
& USAS_OLDMASK
)
619 uth
->uu_sigmask
= uth_parent
->uu_oldmask
;
621 uth
->uu_sigmask
= uth_parent
->uu_sigmask
;
623 uth
->uu_act
= thr_act
;
626 TAILQ_INSERT_TAIL(&p
->p_uthlist
, uth
, uu_list
);
628 (void)thread_funnel_set(kernel_flock
, funnel_state
);
636 uthread_free(task_t task
, thread_t act
, void *uthread
, void * bsd_info
)
639 struct uthread
*uth
= (struct uthread
*)uthread
;
640 struct proc
* p
= (struct proc
*)bsd_info
;
641 extern task_t kernel_task
;
643 boolean_t funnel_state
;
644 struct nlminfo
*nlmp
;
648 * Per-thread audit state should never last beyond system
649 * call return. Since we don't audit the thread creation/
650 * removal, the thread state pointer should never be
651 * non-NULL when we get here.
653 assert(uth
->uu_ar
== NULL
);
655 sel
= &uth
->uu_state
.ss_select
;
656 /* cleanup the select bit space */
658 FREE(sel
->ibits
, M_TEMP
);
659 FREE(sel
->obits
, M_TEMP
);
662 if (sel
->allocsize
&& uth
->uu_wqsub
){
663 kfree(uth
->uu_wqsub
, sel
->allocsize
);
664 sel
->count
= sel
->nfcount
= 0;
670 if ((nlmp
= uth
->uu_nlminfo
)) {
675 if ((task
!= kernel_task
) ) {
676 int vfork_exit(struct proc
*, int);
678 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
680 TAILQ_REMOVE(&p
->p_uthlist
, uth
, uu_list
);
681 if ((uth
->uu_flag
& P_VFORK
) && (vproc
= uth
->uu_proc
)
682 && (vproc
->p_flag
& P_INVFORK
)) {
683 if (!vfork_exit(vproc
, W_EXITCODE(0, SIGKILL
)))
684 vfork_return(act
, p
, vproc
, NULL
);
687 (void)thread_funnel_set(kernel_flock
, funnel_state
);
689 /* and free the uthread itself */
690 zfree(uthread_zone
, (vm_offset_t
)uthread
);