]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_fork.c
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
71 #include <kern/assert.h>
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/filedesc.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/proc_internal.h>
78 #include <sys/kauth.h>
80 #include <sys/resourcevar.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/file_internal.h>
85 #include <sys/ktrace.h>
88 #include <bsm/audit_kernel.h>
90 #include <mach/mach_types.h>
91 #include <kern/kern_types.h>
92 #include <kern/kalloc.h>
93 #include <kern/mach_param.h>
94 #include <kern/task.h>
95 #include <kern/zalloc.h>
97 #include <machine/spl.h>
99 #include <vm/vm_protos.h> // for vm_map_commpage64
101 thread_t
cloneproc(struct proc
*, int);
102 struct proc
* forkproc(struct proc
*, int);
103 thread_t
procdup(struct proc
*child
, struct proc
*parent
);
105 #define DOFORK 0x1 /* fork() system call */
106 #define DOVFORK 0x2 /* vfork() system call */
107 static int fork1(struct proc
*, long, register_t
*);
113 fork(struct proc
*p
, __unused
void *uap
, register_t
*retval
)
115 return (fork1(p
, (long)DOFORK
, retval
));
122 vfork(struct proc
*p
, void *uap
, register_t
*retval
)
124 register struct proc
* newproc
;
126 thread_t cur_act
= (thread_t
)current_thread();
132 * Although process entries are dynamically created, we still keep
133 * a global limit on the maximum number we will create. Don't allow
134 * a nonprivileged user to use the last process; don't let root
135 * exceed the limit. The variable nprocs is the current number of
136 * processes, maxproc is the limit.
138 uid
= kauth_cred_get()->cr_ruid
;
139 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
146 * Increment the count of procs running with this uid. Don't allow
147 * a nonprivileged user to exceed their current limit.
149 count
= chgproccnt(uid
, 1);
150 if (uid
!= 0 && count
> p
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
151 (void)chgproccnt(uid
, -1);
155 ut
= (struct uthread
*)get_bsdthread_info(cur_act
);
156 if (ut
->uu_flag
& UT_VFORK
) {
157 printf("vfork called recursively by %s\n", p
->p_comm
);
158 (void)chgproccnt(uid
, -1);
161 p
->p_flag
|= P_VFORK
;
164 /* The newly created process comes with signal lock held */
165 newproc
= (struct proc
*)forkproc(p
,1);
167 AUDIT_ARG(pid
, newproc
->p_pid
);
169 LIST_INSERT_AFTER(p
, newproc
, p_pglist
);
171 newproc
->task
= p
->task
;
172 LIST_INSERT_HEAD(&p
->p_children
, newproc
, p_sibling
);
173 LIST_INIT(&newproc
->p_children
);
174 LIST_INSERT_HEAD(&allproc
, newproc
, p_list
);
175 LIST_INSERT_HEAD(PIDHASH(newproc
->p_pid
), newproc
, p_hash
);
176 TAILQ_INIT(& newproc
->p_evlist
);
177 newproc
->p_stat
= SRUN
;
178 newproc
->p_flag
|= P_INVFORK
;
179 newproc
->p_vforkact
= cur_act
;
181 ut
->uu_flag
|= UT_VFORK
;
182 ut
->uu_proc
= newproc
;
183 ut
->uu_userstate
= (void *)act_thread_csave();
184 ut
->uu_vforkmask
= ut
->uu_sigmask
;
186 /* temporarily drop thread-set-id state */
187 if (ut
->uu_flag
& UT_SETUID
) {
188 ut
->uu_flag
|= UT_WASSETUID
;
189 ut
->uu_flag
&= ~UT_SETUID
;
192 thread_set_child(cur_act
, newproc
->p_pid
);
194 microtime(&newproc
->p_stats
->p_start
);
195 newproc
->p_acflag
= AFORK
;
198 * Preserve synchronization semantics of vfork. If waiting for
199 * child to exec or exit, set P_PPWAIT on child, and sleep on our
200 * proc (in case of exit).
202 newproc
->p_flag
|= P_PPWAIT
;
204 /* drop the signal lock on the child */
205 signal_unlock(newproc
);
207 retval
[0] = newproc
->p_pid
;
208 retval
[1] = 1; /* mark child */
214 * Return to parent vfork ehread()
217 vfork_return(__unused thread_t th_act
, struct proc
*p
, struct proc
*p2
,
220 thread_t cur_act
= (thread_t
)current_thread();
223 ut
= (struct uthread
*)get_bsdthread_info(cur_act
);
225 act_thread_catt(ut
->uu_userstate
);
227 /* Make sure only one at this time */
229 if (p
->p_vforkcnt
<0)
230 panic("vfork cnt is -ve");
231 if (p
->p_vforkcnt
<=0)
232 p
->p_flag
&= ~P_VFORK
;
233 ut
->uu_userstate
= 0;
234 ut
->uu_flag
&= ~UT_VFORK
;
235 /* restore thread-set-id state */
236 if (ut
->uu_flag
& UT_WASSETUID
) {
237 ut
->uu_flag
|= UT_SETUID
;
238 ut
->uu_flag
&= UT_WASSETUID
;
241 ut
->uu_sigmask
= ut
->uu_vforkmask
;
242 p2
->p_flag
&= ~P_INVFORK
;
243 p2
->p_vforkact
= (void *)0;
245 thread_set_parent(cur_act
, p2
->p_pid
);
248 retval
[0] = p2
->p_pid
;
249 retval
[1] = 0; /* mark parent */
256 procdup(struct proc
*child
, struct proc
*parent
)
260 kern_return_t result
;
262 if (parent
->task
== kernel_task
)
263 result
= task_create_internal(TASK_NULL
, FALSE
, &task
);
265 result
= task_create_internal(parent
->task
, TRUE
, &task
);
266 if (result
!= KERN_SUCCESS
)
267 printf("fork/procdup: task_create failed. Code: 0x%x\n", result
);
269 /* task->proc = child; */
270 set_bsdtask_info(task
, child
);
271 if (parent
->p_flag
& P_LP64
) {
272 task_set_64bit(task
, TRUE
);
273 child
->p_flag
|= P_LP64
;
275 /* LP64todo - clean up this hacked mapping of commpage */
276 pmap_map_sharedpage(task
, get_map_pmap(get_task_map(task
)));
277 vm_map_commpage64(get_task_map(task
));
280 task_set_64bit(task
, FALSE
);
281 child
->p_flag
&= ~P_LP64
;
283 if (child
->p_nice
!= 0)
284 resetpriority(child
);
286 result
= thread_create(task
, &thread
);
287 if (result
!= KERN_SUCCESS
)
288 printf("fork/procdup: thread_create failed. Code: 0x%x\n", result
);
295 fork1(p1
, flags
, retval
)
300 register struct proc
*p2
;
307 * Although process entries are dynamically created, we still keep
308 * a global limit on the maximum number we will create. Don't allow
309 * a nonprivileged user to use the last process; don't let root
310 * exceed the limit. The variable nprocs is the current number of
311 * processes, maxproc is the limit.
313 uid
= kauth_cred_get()->cr_ruid
;
314 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
321 * Increment the count of procs running with this uid. Don't allow
322 * a nonprivileged user to exceed their current limit.
324 count
= chgproccnt(uid
, 1);
325 if (uid
!= 0 && count
> p1
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
326 (void)chgproccnt(uid
, -1);
330 /* The newly created process comes with signal lock held */
331 newth
= cloneproc(p1
, 1);
333 /* p2 = newth->task->proc; */
334 p2
= (struct proc
*)(get_bsdtask_info(get_threadtask(newth
)));
335 set_security_token(p2
); /* propagate change of PID */
337 AUDIT_ARG(pid
, p2
->p_pid
);
339 thread_set_child(newth
, p2
->p_pid
);
341 microtime(&p2
->p_stats
->p_start
);
342 p2
->p_acflag
= AFORK
;
345 * Preserve synchronization semantics of vfork. If waiting for
346 * child to exec or exit, set P_PPWAIT on child, and sleep on our
347 * proc (in case of exit).
349 if (flags
== DOVFORK
)
350 p2
->p_flag
|= P_PPWAIT
;
351 /* drop the signal lock on the child */
354 (void) thread_resume(newth
);
356 /* drop the extra references we got during the creation */
357 if ((t
= (task_t
)get_threadtask(newth
)) != NULL
) {
360 thread_deallocate(newth
);
362 KNOTE(&p1
->p_klist
, NOTE_FORK
| p2
->p_pid
);
364 while (p2
->p_flag
& P_PPWAIT
)
365 tsleep(p1
, PWAIT
, "ppwait", 0);
367 retval
[0] = p2
->p_pid
;
368 retval
[1] = 0; /* mark parent */
376 * Create a new process from a specified process.
377 * On return newly created child process has signal
378 * lock held to block delivery of signal to it if called with
379 * lock set. fork() code needs to explicity remove this lock
380 * before signals can be delivered
384 register struct proc
*p1
;
387 register struct proc
*p2
;
390 p2
= (struct proc
*)forkproc(p1
,lock
);
393 th
= procdup(p2
, p1
); /* child, parent */
395 LIST_INSERT_AFTER(p1
, p2
, p_pglist
);
397 LIST_INSERT_HEAD(&p1
->p_children
, p2
, p_sibling
);
398 LIST_INIT(&p2
->p_children
);
399 LIST_INSERT_HEAD(&allproc
, p2
, p_list
);
400 LIST_INSERT_HEAD(PIDHASH(p2
->p_pid
), p2
, p_hash
);
401 TAILQ_INIT(&p2
->p_evlist
);
403 * Make child runnable, set start time.
412 register struct proc
*p1
;
415 register struct proc
*p2
, *newproc
;
416 static int nextpid
= 0, pidchecked
= 0;
418 /* Allocate new proc. */
419 MALLOC_ZONE(newproc
, struct proc
*,
420 sizeof *newproc
, M_PROC
, M_WAITOK
);
422 panic("forkproc: M_PROC zone exhausted");
423 MALLOC_ZONE(newproc
->p_stats
, struct pstats
*,
424 sizeof *newproc
->p_stats
, M_SUBPROC
, M_WAITOK
);
425 if (newproc
->p_stats
== NULL
)
426 panic("forkproc: M_SUBPROC zone exhausted (p_stats)");
427 MALLOC_ZONE(newproc
->p_sigacts
, struct sigacts
*,
428 sizeof *newproc
->p_sigacts
, M_SUBPROC
, M_WAITOK
);
429 if (newproc
->p_sigacts
== NULL
)
430 panic("forkproc: M_SUBPROC zone exhausted (p_sigacts)");
433 * Find an unused process ID. We remember a range of unused IDs
434 * ready to use (from nextpid+1 through pidchecked-1).
439 * If the process ID prototype has wrapped around,
440 * restart somewhat above 0, as the low-numbered procs
441 * tend to include daemons that don't exit.
443 if (nextpid
>= PID_MAX
) {
447 if (nextpid
>= pidchecked
) {
450 pidchecked
= PID_MAX
;
452 * Scan the active and zombie procs to check whether this pid
453 * is in use. Remember the lowest pid that's greater
454 * than nextpid, so we can avoid checking for a while.
456 p2
= allproc
.lh_first
;
458 for (; p2
!= 0; p2
= p2
->p_list
.le_next
) {
459 while (p2
->p_pid
== nextpid
||
460 p2
->p_pgrp
->pg_id
== nextpid
||
461 p2
->p_session
->s_sid
== nextpid
) {
463 if (nextpid
>= pidchecked
)
466 if (p2
->p_pid
> nextpid
&& pidchecked
> p2
->p_pid
)
467 pidchecked
= p2
->p_pid
;
468 if (p2
->p_pgrp
&& p2
->p_pgrp
->pg_id
> nextpid
&&
469 pidchecked
> p2
->p_pgrp
->pg_id
)
470 pidchecked
= p2
->p_pgrp
->pg_id
;
471 if (p2
->p_session
->s_sid
> nextpid
&&
472 pidchecked
> p2
->p_session
->s_sid
)
473 pidchecked
= p2
->p_session
->s_sid
;
477 p2
= zombproc
.lh_first
;
485 p2
->p_shutdownstate
= 0;
489 * Make a proc table entry for the new process.
490 * Start by zeroing the section of proc that is zero-initialized,
491 * then copy the section that is copied directly from the parent.
493 bzero(&p2
->p_startzero
,
494 (unsigned) ((caddr_t
)&p2
->p_endzero
- (caddr_t
)&p2
->p_startzero
));
495 bcopy(&p1
->p_startcopy
, &p2
->p_startcopy
,
496 (unsigned) ((caddr_t
)&p2
->p_endcopy
- (caddr_t
)&p2
->p_startcopy
));
497 p2
->vm_shm
= (void *)NULL
; /* Make sure it is zero */
500 * Some flags are inherited from the parent.
501 * Duplicate sub-structures as needed.
502 * Increase reference counts on shared objects.
503 * The p_stats and p_sigacts substructs are set in vm_fork.
505 p2
->p_flag
= (p1
->p_flag
& (P_LP64
| P_CLASSIC
| P_AFFINITY
));
506 if (p1
->p_flag
& P_PROFIL
)
509 * Note that if the current thread has an assumed identity, this
510 * credential will be granted to the new process.
512 p2
->p_ucred
= kauth_cred_get_with_ref();
514 lck_mtx_init(&p2
->p_mlock
, proc_lck_grp
, proc_lck_attr
);
515 lck_mtx_init(&p2
->p_fdmlock
, proc_lck_grp
, proc_lck_attr
);
516 klist_init(&p2
->p_klist
);
518 /* bump references to the text vnode */
519 p2
->p_textvp
= p1
->p_textvp
;
521 vnode_rele(p2
->p_textvp
);
523 /* XXX may fail to copy descriptors to child */
524 p2
->p_fd
= fdcopy(p1
);
527 /* XXX may fail to attach shm to child */
528 (void)shmfork(p1
,p2
);
531 * If p_limit is still copy-on-write, bump refcnt,
532 * otherwise get a copy that won't be modified.
533 * (If PL_SHAREMOD is clear, the structure is shared
536 if (p1
->p_limit
->p_lflags
& PL_SHAREMOD
)
537 p2
->p_limit
= limcopy(p1
->p_limit
);
539 p2
->p_limit
= p1
->p_limit
;
540 p2
->p_limit
->p_refcnt
++;
543 bzero(&p2
->p_stats
->pstat_startzero
,
544 (unsigned) ((caddr_t
)&p2
->p_stats
->pstat_endzero
-
545 (caddr_t
)&p2
->p_stats
->pstat_startzero
));
546 bcopy(&p1
->p_stats
->pstat_startcopy
, &p2
->p_stats
->pstat_startcopy
,
547 ((caddr_t
)&p2
->p_stats
->pstat_endcopy
-
548 (caddr_t
)&p2
->p_stats
->pstat_startcopy
));
550 bzero(&p2
->p_stats
->user_p_prof
, sizeof(struct user_uprof
));
552 if (p1
->p_sigacts
!= NULL
)
553 (void)memcpy(p2
->p_sigacts
,
554 p1
->p_sigacts
, sizeof *p2
->p_sigacts
);
556 (void)memset(p2
->p_sigacts
, 0, sizeof *p2
->p_sigacts
);
558 if (p1
->p_session
->s_ttyvp
!= NULL
&& p1
->p_flag
& P_CONTROLT
)
559 p2
->p_flag
|= P_CONTROLT
;
561 p2
->p_argslen
= p1
->p_argslen
;
562 p2
->p_argc
= p1
->p_argc
;
566 p2
->p_debugger
= 0; /* don't inherit */
567 lockinit(&p2
->signal_lock
, PVM
, "signal", 0, 0);
568 /* block all signals to reach the process */
572 p2
->sigwait_thread
= NULL
;
573 p2
->exit_thread
= NULL
;
574 p2
->user_stack
= p1
->user_stack
;
579 p2
->p_internalref
= 0;
580 TAILQ_INIT(&p2
->p_uthlist
);
581 TAILQ_INIT(&p2
->aio_activeq
);
582 TAILQ_INIT(&p2
->aio_doneq
);
583 p2
->aio_active_count
= 0;
584 p2
->aio_done_count
= 0;
588 * Copy traceflag and tracefile if enabled.
589 * If not inherited, these were zeroed above.
591 if (p1
->p_traceflag
&KTRFAC_INHERIT
) {
592 p2
->p_traceflag
= p1
->p_traceflag
;
593 if ((p2
->p_tracep
= p1
->p_tracep
) != NULL
) {
594 vnode_ref(p2
->p_tracep
);
605 lck_mtx_lock(&p
->p_mlock
);
609 proc_unlock(proc_t p
)
611 lck_mtx_unlock(&p
->p_mlock
);
614 #include <kern/zalloc.h>
616 struct zone
*uthread_zone
;
617 int uthread_zone_inited
= 0;
620 uthread_zone_init(void)
622 if (!uthread_zone_inited
) {
623 uthread_zone
= zinit(sizeof(struct uthread
),
624 THREAD_MAX
* sizeof(struct uthread
),
625 THREAD_CHUNK
* sizeof(struct uthread
),
627 uthread_zone_inited
= 1;
632 uthread_alloc(task_t task
, thread_t thr_act
)
635 struct uthread
*uth
, *uth_parent
;
637 boolean_t funnel_state
;
639 if (!uthread_zone_inited
)
642 ut
= (void *)zalloc(uthread_zone
);
643 bzero(ut
, sizeof(struct uthread
));
645 p
= (struct proc
*) get_bsdtask_info(task
);
646 uth
= (struct uthread
*)ut
;
649 * Thread inherits credential from the creating thread, if both
650 * are in the same task.
652 * If the creating thread has no credential or is from another
653 * task we can leave the new thread credential NULL. If it needs
654 * one later, it will be lazily assigned from the task's process.
656 uth_parent
= (struct uthread
*)get_bsdthread_info(current_thread());
657 if ((task
== current_task()) &&
658 (uth_parent
!= NULL
) &&
659 (uth_parent
->uu_ucred
!= NOCRED
)) {
660 uth
->uu_ucred
= uth_parent
->uu_ucred
;
661 kauth_cred_ref(uth
->uu_ucred
);
662 /* the credential we just inherited is an assumed credential */
663 if (uth_parent
->uu_flag
& UT_SETUID
)
664 uth
->uu_flag
|= UT_SETUID
;
666 uth
->uu_ucred
= NOCRED
;
669 if (task
!= kernel_task
) {
671 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
673 if (uth_parent
->uu_flag
& UT_SAS_OLDMASK
)
674 uth
->uu_sigmask
= uth_parent
->uu_oldmask
;
676 uth
->uu_sigmask
= uth_parent
->uu_sigmask
;
678 uth
->uu_act
= thr_act
;
681 TAILQ_INSERT_TAIL(&p
->p_uthlist
, uth
, uu_list
);
684 (void)thread_funnel_set(kernel_flock
, funnel_state
);
692 uthread_free(task_t task
, void *uthread
, void * bsd_info
)
695 struct uthread
*uth
= (struct uthread
*)uthread
;
696 struct proc
* p
= (struct proc
*)bsd_info
;
697 boolean_t funnel_state
;
700 * Per-thread audit state should never last beyond system
701 * call return. Since we don't audit the thread creation/
702 * removal, the thread state pointer should never be
703 * non-NULL when we get here.
705 assert(uth
->uu_ar
== NULL
);
707 sel
= &uth
->uu_select
;
708 /* cleanup the select bit space */
710 FREE(sel
->ibits
, M_TEMP
);
711 FREE(sel
->obits
, M_TEMP
);
714 if (sel
->allocsize
&& sel
->wqset
){
715 kfree(sel
->wqset
, sel
->allocsize
);
722 if (uth
->uu_ucred
!= NOCRED
)
723 kauth_cred_rele(uth
->uu_ucred
);
725 if ((task
!= kernel_task
) && p
) {
726 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
728 TAILQ_REMOVE(&p
->p_uthlist
, uth
, uu_list
);
730 (void)thread_funnel_set(kernel_flock
, funnel_state
);
732 /* and free the uthread itself */
733 zfree(uthread_zone
, uthread
);