]>
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 * 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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
64 #include <kern/assert.h>
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/filedesc.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/proc_internal.h>
71 #include <sys/kauth.h>
73 #include <sys/resourcevar.h>
74 #include <sys/vnode_internal.h>
75 #include <sys/file_internal.h>
78 #include <sys/ktrace.h>
81 #include <bsm/audit_kernel.h>
83 #include <mach/mach_types.h>
84 #include <kern/kern_types.h>
85 #include <kern/kalloc.h>
86 #include <kern/mach_param.h>
87 #include <kern/task.h>
88 #include <kern/zalloc.h>
90 #include <machine/spl.h>
92 #include <vm/vm_protos.h> // for vm_map_commpage64
94 thread_t
cloneproc(struct proc
*, int);
95 struct proc
* forkproc(struct proc
*, int);
96 thread_t
procdup(struct proc
*child
, struct proc
*parent
);
98 #define DOFORK 0x1 /* fork() system call */
99 #define DOVFORK 0x2 /* vfork() system call */
100 static int fork1(struct proc
*, long, register_t
*);
106 fork(struct proc
*p
, __unused
void *uap
, register_t
*retval
)
108 return (fork1(p
, (long)DOFORK
, retval
));
115 vfork(struct proc
*p
, void *uap
, register_t
*retval
)
117 register struct proc
* newproc
;
119 thread_t cur_act
= (thread_t
)current_thread();
125 * Although process entries are dynamically created, we still keep
126 * a global limit on the maximum number we will create. Don't allow
127 * a nonprivileged user to use the last process; don't let root
128 * exceed the limit. The variable nprocs is the current number of
129 * processes, maxproc is the limit.
131 uid
= kauth_cred_get()->cr_ruid
;
132 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
139 * Increment the count of procs running with this uid. Don't allow
140 * a nonprivileged user to exceed their current limit.
142 count
= chgproccnt(uid
, 1);
143 if (uid
!= 0 && count
> p
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
144 (void)chgproccnt(uid
, -1);
148 ut
= (struct uthread
*)get_bsdthread_info(cur_act
);
149 if (ut
->uu_flag
& UT_VFORK
) {
150 printf("vfork called recursively by %s\n", p
->p_comm
);
151 (void)chgproccnt(uid
, -1);
154 p
->p_flag
|= P_VFORK
;
157 /* The newly created process comes with signal lock held */
158 newproc
= (struct proc
*)forkproc(p
,1);
160 AUDIT_ARG(pid
, newproc
->p_pid
);
162 LIST_INSERT_AFTER(p
, newproc
, p_pglist
);
164 newproc
->task
= p
->task
;
165 LIST_INSERT_HEAD(&p
->p_children
, newproc
, p_sibling
);
166 LIST_INIT(&newproc
->p_children
);
167 LIST_INSERT_HEAD(&allproc
, newproc
, p_list
);
168 LIST_INSERT_HEAD(PIDHASH(newproc
->p_pid
), newproc
, p_hash
);
169 TAILQ_INIT(& newproc
->p_evlist
);
170 newproc
->p_stat
= SRUN
;
171 newproc
->p_flag
|= P_INVFORK
;
172 newproc
->p_vforkact
= cur_act
;
174 ut
->uu_flag
|= UT_VFORK
;
175 ut
->uu_proc
= newproc
;
176 ut
->uu_userstate
= (void *)act_thread_csave();
177 ut
->uu_vforkmask
= ut
->uu_sigmask
;
179 /* temporarily drop thread-set-id state */
180 if (ut
->uu_flag
& UT_SETUID
) {
181 ut
->uu_flag
|= UT_WASSETUID
;
182 ut
->uu_flag
&= ~UT_SETUID
;
185 thread_set_child(cur_act
, newproc
->p_pid
);
187 microtime(&newproc
->p_stats
->p_start
);
188 newproc
->p_acflag
= AFORK
;
191 * Preserve synchronization semantics of vfork. If waiting for
192 * child to exec or exit, set P_PPWAIT on child, and sleep on our
193 * proc (in case of exit).
195 newproc
->p_flag
|= P_PPWAIT
;
197 /* drop the signal lock on the child */
198 signal_unlock(newproc
);
200 retval
[0] = newproc
->p_pid
;
201 retval
[1] = 1; /* mark child */
207 * Return to parent vfork ehread()
210 vfork_return(__unused thread_t th_act
, struct proc
*p
, struct proc
*p2
,
213 thread_t cur_act
= (thread_t
)current_thread();
216 ut
= (struct uthread
*)get_bsdthread_info(cur_act
);
218 act_thread_catt(ut
->uu_userstate
);
220 /* Make sure only one at this time */
222 if (p
->p_vforkcnt
<0)
223 panic("vfork cnt is -ve");
224 if (p
->p_vforkcnt
<=0)
225 p
->p_flag
&= ~P_VFORK
;
226 ut
->uu_userstate
= 0;
227 ut
->uu_flag
&= ~UT_VFORK
;
228 /* restore thread-set-id state */
229 if (ut
->uu_flag
& UT_WASSETUID
) {
230 ut
->uu_flag
|= UT_SETUID
;
231 ut
->uu_flag
&= UT_WASSETUID
;
234 ut
->uu_sigmask
= ut
->uu_vforkmask
;
235 p2
->p_flag
&= ~P_INVFORK
;
236 p2
->p_vforkact
= (void *)0;
238 thread_set_parent(cur_act
, p2
->p_pid
);
241 retval
[0] = p2
->p_pid
;
242 retval
[1] = 0; /* mark parent */
249 procdup(struct proc
*child
, struct proc
*parent
)
253 kern_return_t result
;
255 if (parent
->task
== kernel_task
)
256 result
= task_create_internal(TASK_NULL
, FALSE
, &task
);
258 result
= task_create_internal(parent
->task
, TRUE
, &task
);
259 if (result
!= KERN_SUCCESS
)
260 printf("fork/procdup: task_create failed. Code: 0x%x\n", result
);
262 /* task->proc = child; */
263 set_bsdtask_info(task
, child
);
264 if (parent
->p_flag
& P_LP64
) {
265 task_set_64bit(task
, TRUE
);
266 child
->p_flag
|= P_LP64
;
268 /* LP64todo - clean up this hacked mapping of commpage */
269 pmap_map_sharedpage(task
, get_map_pmap(get_task_map(task
)));
270 vm_map_commpage64(get_task_map(task
));
273 task_set_64bit(task
, FALSE
);
274 child
->p_flag
&= ~P_LP64
;
276 if (child
->p_nice
!= 0)
277 resetpriority(child
);
279 result
= thread_create(task
, &thread
);
280 if (result
!= KERN_SUCCESS
)
281 printf("fork/procdup: thread_create failed. Code: 0x%x\n", result
);
288 fork1(p1
, flags
, retval
)
293 register struct proc
*p2
;
300 * Although process entries are dynamically created, we still keep
301 * a global limit on the maximum number we will create. Don't allow
302 * a nonprivileged user to use the last process; don't let root
303 * exceed the limit. The variable nprocs is the current number of
304 * processes, maxproc is the limit.
306 uid
= kauth_cred_get()->cr_ruid
;
307 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
314 * Increment the count of procs running with this uid. Don't allow
315 * a nonprivileged user to exceed their current limit.
317 count
= chgproccnt(uid
, 1);
318 if (uid
!= 0 && count
> p1
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
319 (void)chgproccnt(uid
, -1);
323 /* The newly created process comes with signal lock held */
324 newth
= cloneproc(p1
, 1);
326 /* p2 = newth->task->proc; */
327 p2
= (struct proc
*)(get_bsdtask_info(get_threadtask(newth
)));
328 set_security_token(p2
); /* propagate change of PID */
330 AUDIT_ARG(pid
, p2
->p_pid
);
332 thread_set_child(newth
, p2
->p_pid
);
334 microtime(&p2
->p_stats
->p_start
);
335 p2
->p_acflag
= AFORK
;
338 * Preserve synchronization semantics of vfork. If waiting for
339 * child to exec or exit, set P_PPWAIT on child, and sleep on our
340 * proc (in case of exit).
342 if (flags
== DOVFORK
)
343 p2
->p_flag
|= P_PPWAIT
;
344 /* drop the signal lock on the child */
347 (void) thread_resume(newth
);
349 /* drop the extra references we got during the creation */
350 if ((t
= (task_t
)get_threadtask(newth
)) != NULL
) {
353 thread_deallocate(newth
);
355 KNOTE(&p1
->p_klist
, NOTE_FORK
| p2
->p_pid
);
357 while (p2
->p_flag
& P_PPWAIT
)
358 tsleep(p1
, PWAIT
, "ppwait", 0);
360 retval
[0] = p2
->p_pid
;
361 retval
[1] = 0; /* mark parent */
369 * Create a new process from a specified process.
370 * On return newly created child process has signal
371 * lock held to block delivery of signal to it if called with
372 * lock set. fork() code needs to explicity remove this lock
373 * before signals can be delivered
377 register struct proc
*p1
;
380 register struct proc
*p2
;
383 p2
= (struct proc
*)forkproc(p1
,lock
);
386 th
= procdup(p2
, p1
); /* child, parent */
388 LIST_INSERT_AFTER(p1
, p2
, p_pglist
);
390 LIST_INSERT_HEAD(&p1
->p_children
, p2
, p_sibling
);
391 LIST_INIT(&p2
->p_children
);
392 LIST_INSERT_HEAD(&allproc
, p2
, p_list
);
393 LIST_INSERT_HEAD(PIDHASH(p2
->p_pid
), p2
, p_hash
);
394 TAILQ_INIT(&p2
->p_evlist
);
396 * Make child runnable, set start time.
405 register struct proc
*p1
;
408 register struct proc
*p2
, *newproc
;
409 static int nextpid
= 0, pidchecked
= 0;
411 /* Allocate new proc. */
412 MALLOC_ZONE(newproc
, struct proc
*,
413 sizeof *newproc
, M_PROC
, M_WAITOK
);
415 panic("forkproc: M_PROC zone exhausted");
416 MALLOC_ZONE(newproc
->p_stats
, struct pstats
*,
417 sizeof *newproc
->p_stats
, M_SUBPROC
, M_WAITOK
);
418 if (newproc
->p_stats
== NULL
)
419 panic("forkproc: M_SUBPROC zone exhausted (p_stats)");
420 MALLOC_ZONE(newproc
->p_sigacts
, struct sigacts
*,
421 sizeof *newproc
->p_sigacts
, M_SUBPROC
, M_WAITOK
);
422 if (newproc
->p_sigacts
== NULL
)
423 panic("forkproc: M_SUBPROC zone exhausted (p_sigacts)");
426 * Find an unused process ID. We remember a range of unused IDs
427 * ready to use (from nextpid+1 through pidchecked-1).
432 * If the process ID prototype has wrapped around,
433 * restart somewhat above 0, as the low-numbered procs
434 * tend to include daemons that don't exit.
436 if (nextpid
>= PID_MAX
) {
440 if (nextpid
>= pidchecked
) {
443 pidchecked
= PID_MAX
;
445 * Scan the active and zombie procs to check whether this pid
446 * is in use. Remember the lowest pid that's greater
447 * than nextpid, so we can avoid checking for a while.
449 p2
= allproc
.lh_first
;
451 for (; p2
!= 0; p2
= p2
->p_list
.le_next
) {
452 while (p2
->p_pid
== nextpid
||
453 p2
->p_pgrp
->pg_id
== nextpid
||
454 p2
->p_session
->s_sid
== nextpid
) {
456 if (nextpid
>= pidchecked
)
459 if (p2
->p_pid
> nextpid
&& pidchecked
> p2
->p_pid
)
460 pidchecked
= p2
->p_pid
;
461 if (p2
->p_pgrp
&& p2
->p_pgrp
->pg_id
> nextpid
&&
462 pidchecked
> p2
->p_pgrp
->pg_id
)
463 pidchecked
= p2
->p_pgrp
->pg_id
;
464 if (p2
->p_session
->s_sid
> nextpid
&&
465 pidchecked
> p2
->p_session
->s_sid
)
466 pidchecked
= p2
->p_session
->s_sid
;
470 p2
= zombproc
.lh_first
;
478 p2
->p_shutdownstate
= 0;
482 * Make a proc table entry for the new process.
483 * Start by zeroing the section of proc that is zero-initialized,
484 * then copy the section that is copied directly from the parent.
486 bzero(&p2
->p_startzero
,
487 (unsigned) ((caddr_t
)&p2
->p_endzero
- (caddr_t
)&p2
->p_startzero
));
488 bcopy(&p1
->p_startcopy
, &p2
->p_startcopy
,
489 (unsigned) ((caddr_t
)&p2
->p_endcopy
- (caddr_t
)&p2
->p_startcopy
));
490 p2
->vm_shm
= (void *)NULL
; /* Make sure it is zero */
493 * Some flags are inherited from the parent.
494 * Duplicate sub-structures as needed.
495 * Increase reference counts on shared objects.
496 * The p_stats and p_sigacts substructs are set in vm_fork.
498 p2
->p_flag
= (p1
->p_flag
& (P_LP64
| P_CLASSIC
| P_AFFINITY
));
499 if (p1
->p_flag
& P_PROFIL
)
502 * Note that if the current thread has an assumed identity, this
503 * credential will be granted to the new process.
505 p2
->p_ucred
= kauth_cred_get_with_ref();
507 lck_mtx_init(&p2
->p_mlock
, proc_lck_grp
, proc_lck_attr
);
508 lck_mtx_init(&p2
->p_fdmlock
, proc_lck_grp
, proc_lck_attr
);
509 klist_init(&p2
->p_klist
);
511 /* bump references to the text vnode */
512 p2
->p_textvp
= p1
->p_textvp
;
514 vnode_rele(p2
->p_textvp
);
516 /* XXX may fail to copy descriptors to child */
517 p2
->p_fd
= fdcopy(p1
);
520 /* XXX may fail to attach shm to child */
521 (void)shmfork(p1
,p2
);
524 * If p_limit is still copy-on-write, bump refcnt,
525 * otherwise get a copy that won't be modified.
526 * (If PL_SHAREMOD is clear, the structure is shared
529 if (p1
->p_limit
->p_lflags
& PL_SHAREMOD
)
530 p2
->p_limit
= limcopy(p1
->p_limit
);
532 p2
->p_limit
= p1
->p_limit
;
533 p2
->p_limit
->p_refcnt
++;
536 bzero(&p2
->p_stats
->pstat_startzero
,
537 (unsigned) ((caddr_t
)&p2
->p_stats
->pstat_endzero
-
538 (caddr_t
)&p2
->p_stats
->pstat_startzero
));
539 bcopy(&p1
->p_stats
->pstat_startcopy
, &p2
->p_stats
->pstat_startcopy
,
540 ((caddr_t
)&p2
->p_stats
->pstat_endcopy
-
541 (caddr_t
)&p2
->p_stats
->pstat_startcopy
));
543 bzero(&p2
->p_stats
->user_p_prof
, sizeof(struct user_uprof
));
545 if (p1
->p_sigacts
!= NULL
)
546 (void)memcpy(p2
->p_sigacts
,
547 p1
->p_sigacts
, sizeof *p2
->p_sigacts
);
549 (void)memset(p2
->p_sigacts
, 0, sizeof *p2
->p_sigacts
);
551 if (p1
->p_session
->s_ttyvp
!= NULL
&& p1
->p_flag
& P_CONTROLT
)
552 p2
->p_flag
|= P_CONTROLT
;
554 p2
->p_argslen
= p1
->p_argslen
;
555 p2
->p_argc
= p1
->p_argc
;
559 p2
->p_debugger
= 0; /* don't inherit */
560 lockinit(&p2
->signal_lock
, PVM
, "signal", 0, 0);
561 /* block all signals to reach the process */
565 p2
->sigwait_thread
= NULL
;
566 p2
->exit_thread
= NULL
;
567 p2
->user_stack
= p1
->user_stack
;
572 p2
->p_internalref
= 0;
573 TAILQ_INIT(&p2
->p_uthlist
);
574 TAILQ_INIT(&p2
->aio_activeq
);
575 TAILQ_INIT(&p2
->aio_doneq
);
576 p2
->aio_active_count
= 0;
577 p2
->aio_done_count
= 0;
581 * Copy traceflag and tracefile if enabled.
582 * If not inherited, these were zeroed above.
584 if (p1
->p_traceflag
&KTRFAC_INHERIT
) {
585 p2
->p_traceflag
= p1
->p_traceflag
;
586 if ((p2
->p_tracep
= p1
->p_tracep
) != NULL
) {
587 vnode_ref(p2
->p_tracep
);
598 lck_mtx_lock(&p
->p_mlock
);
602 proc_unlock(proc_t p
)
604 lck_mtx_unlock(&p
->p_mlock
);
607 #include <kern/zalloc.h>
609 struct zone
*uthread_zone
;
610 int uthread_zone_inited
= 0;
613 uthread_zone_init(void)
615 if (!uthread_zone_inited
) {
616 uthread_zone
= zinit(sizeof(struct uthread
),
617 THREAD_MAX
* sizeof(struct uthread
),
618 THREAD_CHUNK
* sizeof(struct uthread
),
620 uthread_zone_inited
= 1;
625 uthread_alloc(task_t task
, thread_t thr_act
)
628 struct uthread
*uth
, *uth_parent
;
630 boolean_t funnel_state
;
632 if (!uthread_zone_inited
)
635 ut
= (void *)zalloc(uthread_zone
);
636 bzero(ut
, sizeof(struct uthread
));
638 p
= (struct proc
*) get_bsdtask_info(task
);
639 uth
= (struct uthread
*)ut
;
642 * Thread inherits credential from the creating thread, if both
643 * are in the same task.
645 * If the creating thread has no credential or is from another
646 * task we can leave the new thread credential NULL. If it needs
647 * one later, it will be lazily assigned from the task's process.
649 uth_parent
= (struct uthread
*)get_bsdthread_info(current_thread());
650 if ((task
== current_task()) &&
651 (uth_parent
!= NULL
) &&
652 (uth_parent
->uu_ucred
!= NOCRED
)) {
653 uth
->uu_ucred
= uth_parent
->uu_ucred
;
654 kauth_cred_ref(uth
->uu_ucred
);
655 /* the credential we just inherited is an assumed credential */
656 if (uth_parent
->uu_flag
& UT_SETUID
)
657 uth
->uu_flag
|= UT_SETUID
;
659 uth
->uu_ucred
= NOCRED
;
662 if (task
!= kernel_task
) {
664 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
666 if (uth_parent
->uu_flag
& UT_SAS_OLDMASK
)
667 uth
->uu_sigmask
= uth_parent
->uu_oldmask
;
669 uth
->uu_sigmask
= uth_parent
->uu_sigmask
;
671 uth
->uu_act
= thr_act
;
674 TAILQ_INSERT_TAIL(&p
->p_uthlist
, uth
, uu_list
);
677 (void)thread_funnel_set(kernel_flock
, funnel_state
);
685 uthread_free(task_t task
, void *uthread
, void * bsd_info
)
688 struct uthread
*uth
= (struct uthread
*)uthread
;
689 struct proc
* p
= (struct proc
*)bsd_info
;
690 boolean_t funnel_state
;
693 * Per-thread audit state should never last beyond system
694 * call return. Since we don't audit the thread creation/
695 * removal, the thread state pointer should never be
696 * non-NULL when we get here.
698 assert(uth
->uu_ar
== NULL
);
700 sel
= &uth
->uu_select
;
701 /* cleanup the select bit space */
703 FREE(sel
->ibits
, M_TEMP
);
704 FREE(sel
->obits
, M_TEMP
);
707 if (sel
->allocsize
&& sel
->wqset
){
708 kfree(sel
->wqset
, sel
->allocsize
);
715 if (uth
->uu_ucred
!= NOCRED
)
716 kauth_cred_rele(uth
->uu_ucred
);
718 if ((task
!= kernel_task
) && p
) {
719 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
721 TAILQ_REMOVE(&p
->p_uthlist
, uth
, uu_list
);
723 (void)thread_funnel_set(kernel_flock
, funnel_state
);
725 /* and free the uthread itself */
726 zfree(uthread_zone
, uthread
);