]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_fork.c
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/filedesc.h>
69 #include <sys/kernel.h>
70 #include <sys/malloc.h>
73 #include <sys/resourcevar.h>
74 #include <sys/vnode.h>
78 #include <sys/ktrace.h>
81 #include <mach/mach_types.h>
82 #include <kern/mach_param.h>
84 #include <machine/spl.h>
86 thread_act_t
cloneproc(struct proc
*, int);
87 struct proc
* forkproc(struct proc
*, int);
88 thread_act_t
procdup();
90 #define DOFORK 0x1 /* fork() system call */
91 #define DOVFORK 0x2 /* vfork() system call */
92 static int fork1(struct proc
*, long, register_t
*);
103 return (fork1(p
, (long)DOFORK
, retval
));
110 vfork(p
, uap
, retval
)
115 register struct proc
* newproc
;
117 thread_act_t cur_act
= (thread_act_t
)current_act();
123 * Although process entries are dynamically created, we still keep
124 * a global limit on the maximum number we will create. Don't allow
125 * a nonprivileged user to use the last process; don't let root
126 * exceed the limit. The variable nprocs is the current number of
127 * processes, maxproc is the limit.
129 uid
= p
->p_cred
->p_ruid
;
130 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
137 * Increment the count of procs running with this uid. Don't allow
138 * a nonprivileged user to exceed their current limit.
140 count
= chgproccnt(uid
, 1);
141 if (uid
!= 0 && count
> p
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
142 (void)chgproccnt(uid
, -1);
146 ut
= (struct uthread
*)get_bsdthread_info(cur_act
);
147 if (ut
->uu_flag
& P_VFORK
) {
148 printf("vfork called recursively by %s\n", p
->p_comm
);
151 p
->p_flag
|= P_VFORK
;
154 /* The newly created process comes with signal lock held */
155 newproc
= (struct proc
*)forkproc(p
,1);
157 LIST_INSERT_AFTER(p
, newproc
, p_pglist
);
159 newproc
->task
= p
->task
;
160 LIST_INSERT_HEAD(&p
->p_children
, newproc
, p_sibling
);
161 LIST_INIT(&newproc
->p_children
);
162 LIST_INSERT_HEAD(&allproc
, newproc
, p_list
);
163 LIST_INSERT_HEAD(PIDHASH(newproc
->p_pid
), newproc
, p_hash
);
164 TAILQ_INIT(& newproc
->p_evlist
);
165 newproc
->p_stat
= SRUN
;
166 newproc
->p_flag
|= P_INVFORK
;
167 newproc
->p_vforkact
= cur_act
;
169 ut
->uu_flag
|= P_VFORK
;
170 ut
->uu_proc
= newproc
;
171 ut
->uu_userstate
= (void *)act_thread_csave();
172 ut
->uu_vforkmask
= ut
->uu_sigmask
;
174 thread_set_child(cur_act
, newproc
->p_pid
);
176 newproc
->p_stats
->p_start
= time
;
177 newproc
->p_acflag
= AFORK
;
180 * Preserve synchronization semantics of vfork. If waiting for
181 * child to exec or exit, set P_PPWAIT on child, and sleep on our
182 * proc (in case of exit).
184 newproc
->p_flag
|= P_PPWAIT
;
186 /* drop the signal lock on the child */
187 signal_unlock(newproc
);
189 retval
[0] = newproc
->p_pid
;
190 retval
[1] = 1; /* mark child */
196 * Return to parent vfork ehread()
199 vfork_return(th_act
, p
, p2
, retval
)
207 thread_t newth
, self
= current_thread();
208 thread_act_t cur_act
= (thread_act_t
)current_act();
213 ut
= (struct uthread
*)get_bsdthread_info(cur_act
);
215 act_thread_catt(ut
->uu_userstate
);
217 /* Make sure only one at this time */
219 if (p
->p_vforkcnt
<0)
220 panic("vfork cnt is -ve");
221 if (p
->p_vforkcnt
<=0)
222 p
->p_flag
&= ~P_VFORK
;
223 ut
->uu_userstate
= 0;
224 ut
->uu_flag
&= ~P_VFORK
;
226 ut
->uu_sigmask
= ut
->uu_vforkmask
;
227 p2
->p_flag
&= ~P_INVFORK
;
228 p2
->p_vforkact
= (void *)0;
230 thread_set_parent(cur_act
, p2
->p_pid
);
233 retval
[0] = p2
->p_pid
;
234 retval
[1] = 0; /* mark parent */
247 kern_return_t result
;
249 extern task_t kernel_task
;
251 if (parent
->task
== kernel_task
)
252 result
= task_create_local(TASK_NULL
, FALSE
, FALSE
, &task
);
254 result
= task_create_local(parent
->task
, TRUE
, FALSE
, &task
);
255 if (result
!= KERN_SUCCESS
)
256 printf("fork/procdup: task_create failed. Code: 0x%x\n", result
);
258 /* task->proc = child; */
259 set_bsdtask_info(task
, child
);
260 if (child
->p_nice
!= 0)
261 resetpriority(child
);
263 result
= thread_create(task
, &thread
);
264 if (result
!= KERN_SUCCESS
)
265 printf("fork/procdup: thread_create failed. Code: 0x%x\n", result
);
272 fork1(p1
, flags
, retval
)
277 register struct proc
*p2
;
284 * Although process entries are dynamically created, we still keep
285 * a global limit on the maximum number we will create. Don't allow
286 * a nonprivileged user to use the last process; don't let root
287 * exceed the limit. The variable nprocs is the current number of
288 * processes, maxproc is the limit.
290 uid
= p1
->p_cred
->p_ruid
;
291 if ((nprocs
>= maxproc
- 1 && uid
!= 0) || nprocs
>= maxproc
) {
298 * Increment the count of procs running with this uid. Don't allow
299 * a nonprivileged user to exceed their current limit.
301 count
= chgproccnt(uid
, 1);
302 if (uid
!= 0 && count
> p1
->p_rlimit
[RLIMIT_NPROC
].rlim_cur
) {
303 (void)chgproccnt(uid
, -1);
307 /* The newly created process comes with signal lock held */
308 newth
= cloneproc(p1
, 1);
310 /* p2 = newth->task->proc; */
311 p2
= (struct proc
*)(get_bsdtask_info(get_threadtask(newth
)));
313 thread_set_child(newth
, p2
->p_pid
);
316 p2
->p_stats
->p_start
= time
;
318 p2
->p_acflag
= AFORK
;
321 * Preserve synchronization semantics of vfork. If waiting for
322 * child to exec or exit, set P_PPWAIT on child, and sleep on our
323 * proc (in case of exit).
325 if (flags
== DOVFORK
)
326 p2
->p_flag
|= P_PPWAIT
;
327 /* drop the signal lock on the child */
330 (void) thread_resume(newth
);
332 /* drop the extra references we got during the creation */
333 if (t
= (task_t
)get_threadtask(newth
)) {
336 act_deallocate(newth
);
338 while (p2
->p_flag
& P_PPWAIT
)
339 tsleep(p1
, PWAIT
, "ppwait", 0);
341 retval
[0] = p2
->p_pid
;
342 retval
[1] = 0; /* mark parent */
350 * Create a new process from a specified process.
351 * On return newly created child process has signal
352 * lock held to block delivery of signal to it if called with
353 * lock set. fork() code needs to explicity remove this lock
354 * before signals can be delivered
358 register struct proc
*p1
;
361 register struct proc
*p2
;
364 p2
= (struct proc
*)forkproc(p1
,lock
);
367 th
= procdup(p2
, p1
); /* child, parent */
369 LIST_INSERT_AFTER(p1
, p2
, p_pglist
);
371 LIST_INSERT_HEAD(&p1
->p_children
, p2
, p_sibling
);
372 LIST_INIT(&p2
->p_children
);
373 LIST_INSERT_HEAD(&allproc
, p2
, p_list
);
374 LIST_INSERT_HEAD(PIDHASH(p2
->p_pid
), p2
, p_hash
);
375 TAILQ_INIT(&p2
->p_evlist
);
377 * Make child runnable, set start time.
386 register struct proc
*p1
;
389 register struct proc
*p2
, *newproc
;
390 static int nextpid
= 0, pidchecked
= 0;
393 /* Allocate new proc. */
394 MALLOC_ZONE(newproc
, struct proc
*,
395 sizeof *newproc
, M_PROC
, M_WAITOK
);
396 MALLOC_ZONE(newproc
->p_cred
, struct pcred
*,
397 sizeof *newproc
->p_cred
, M_SUBPROC
, M_WAITOK
);
398 MALLOC_ZONE(newproc
->p_stats
, struct pstats
*,
399 sizeof *newproc
->p_stats
, M_SUBPROC
, M_WAITOK
);
400 MALLOC_ZONE(newproc
->p_sigacts
, struct sigacts
*,
401 sizeof *newproc
->p_sigacts
, M_SUBPROC
, M_WAITOK
);
404 * Find an unused process ID. We remember a range of unused IDs
405 * ready to use (from nextpid+1 through pidchecked-1).
410 * If the process ID prototype has wrapped around,
411 * restart somewhat above 0, as the low-numbered procs
412 * tend to include daemons that don't exit.
414 if (nextpid
>= PID_MAX
) {
418 if (nextpid
>= pidchecked
) {
421 pidchecked
= PID_MAX
;
423 * Scan the active and zombie procs to check whether this pid
424 * is in use. Remember the lowest pid that's greater
425 * than nextpid, so we can avoid checking for a while.
427 p2
= allproc
.lh_first
;
429 for (; p2
!= 0; p2
= p2
->p_list
.le_next
) {
430 while (p2
->p_pid
== nextpid
||
431 p2
->p_pgrp
->pg_id
== nextpid
||
432 p2
->p_session
->s_sid
== nextpid
) {
434 if (nextpid
>= pidchecked
)
437 if (p2
->p_pid
> nextpid
&& pidchecked
> p2
->p_pid
)
438 pidchecked
= p2
->p_pid
;
439 if (p2
->p_pgrp
&& p2
->p_pgrp
->pg_id
> nextpid
&&
440 pidchecked
> p2
->p_pgrp
->pg_id
)
441 pidchecked
= p2
->p_pgrp
->pg_id
;
442 if (p2
->p_session
->s_sid
> nextpid
&&
443 pidchecked
> p2
->p_session
->s_sid
)
444 pidchecked
= p2
->p_session
->s_sid
;
448 p2
= zombproc
.lh_first
;
459 * Make a proc table entry for the new process.
460 * Start by zeroing the section of proc that is zero-initialized,
461 * then copy the section that is copied directly from the parent.
463 bzero(&p2
->p_startzero
,
464 (unsigned) ((caddr_t
)&p2
->p_endzero
- (caddr_t
)&p2
->p_startzero
));
465 bcopy(&p1
->p_startcopy
, &p2
->p_startcopy
,
466 (unsigned) ((caddr_t
)&p2
->p_endcopy
- (caddr_t
)&p2
->p_startcopy
));
467 p2
->vm_shm
= (void *)NULL
; /* Make sure it is zero */
470 * Duplicate sub-structures as needed.
471 * Increase reference counts on shared objects.
472 * The p_stats and p_sigacts substructs are set in vm_fork.
474 p2
->p_flag
= P_INMEM
;
475 if (p1
->p_flag
& P_PROFIL
)
477 bcopy(p1
->p_cred
, p2
->p_cred
, sizeof(*p2
->p_cred
));
478 p2
->p_cred
->p_refcnt
= 1;
480 lockinit(&p2
->p_cred
->pc_lock
, PLOCK
, "proc cred", 0, 0);
482 /* bump references to the text vnode */
483 p2
->p_textvp
= p1
->p_textvp
;
487 p2
->p_fd
= fdcopy(p1
);
492 * If p_limit is still copy-on-write, bump refcnt,
493 * otherwise get a copy that won't be modified.
494 * (If PL_SHAREMOD is clear, the structure is shared
497 if (p1
->p_limit
->p_lflags
& PL_SHAREMOD
)
498 p2
->p_limit
= limcopy(p1
->p_limit
);
500 p2
->p_limit
= p1
->p_limit
;
501 p2
->p_limit
->p_refcnt
++;
504 bzero(&p2
->p_stats
->pstat_startzero
,
505 (unsigned) ((caddr_t
)&p2
->p_stats
->pstat_endzero
-
506 (caddr_t
)&p2
->p_stats
->pstat_startzero
));
507 bcopy(&p1
->p_stats
->pstat_startcopy
, &p2
->p_stats
->pstat_startcopy
,
508 ((caddr_t
)&p2
->p_stats
->pstat_endcopy
-
509 (caddr_t
)&p2
->p_stats
->pstat_startcopy
));
511 if (p1
->p_sigacts
!= NULL
)
512 (void)memcpy(p2
->p_sigacts
,
513 p1
->p_sigacts
, sizeof *p2
->p_sigacts
);
515 (void)memset(p2
->p_sigacts
, 0, sizeof *p2
->p_sigacts
);
517 if (p1
->p_session
->s_ttyvp
!= NULL
&& p1
->p_flag
& P_CONTROLT
)
518 p2
->p_flag
|= P_CONTROLT
;
523 p2
->p_debugger
= 0; /* don't inherit */
524 lockinit(&p2
->signal_lock
, PVM
, "signal", 0, 0);
525 /* block all signals to reach the process */
529 p2
->sigwait_thread
= NULL
;
530 p2
->exit_thread
= NULL
;
531 p2
->user_stack
= p1
->user_stack
;
532 p2
->p_xxxsigpending
= 0;
535 TAILQ_INIT(&p2
->p_uthlist
);
539 * Copy traceflag and tracefile if enabled.
540 * If not inherited, these were zeroed above.
542 if (p1
->p_traceflag
&KTRFAC_INHERIT
) {
543 p2
->p_traceflag
= p1
->p_traceflag
;
544 if ((p2
->p_tracep
= p1
->p_tracep
) != NULL
)
552 #include <kern/zalloc.h>
554 struct zone
*uthread_zone
;
555 int uthread_zone_inited
= 0;
560 if (!uthread_zone_inited
) {
561 uthread_zone
= zinit(sizeof(struct uthread
),
562 THREAD_MAX
* sizeof(struct uthread
),
563 THREAD_CHUNK
* sizeof(struct uthread
),
565 uthread_zone_inited
= 1;
570 uthread_alloc(task_t task
, thread_act_t thr_act
)
573 struct uthread
*uth
, *uth_parent
;
575 extern task_t kernel_task
;
576 boolean_t funnel_state
;
578 if (!uthread_zone_inited
)
581 ut
= (void *)zalloc(uthread_zone
);
582 bzero(ut
, sizeof(struct uthread
));
584 if (task
!= kernel_task
) {
585 uth
= (struct uthread
*)ut
;
586 p
= get_bsdtask_info(task
);
588 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
589 uth_parent
= (struct uthread
*)get_bsdthread_info(current_act());
591 if (uth_parent
->uu_flag
& USAS_OLDMASK
)
592 uth
->uu_sigmask
= uth_parent
->uu_oldmask
;
594 uth
->uu_sigmask
= uth_parent
->uu_sigmask
;
596 uth
->uu_act
= thr_act
;
599 TAILQ_INSERT_TAIL(&p
->p_uthlist
, uth
, uu_list
);
601 (void)thread_funnel_set(kernel_flock
, funnel_state
);
609 uthread_free(task_t task
, void *uthread
, void * bsd_info
)
612 struct uthread
*uth
= (struct uthread
*)uthread
;
613 struct proc
* p
= (struct proc
*)bsd_info
;
614 extern task_t kernel_task
;
616 boolean_t funnel_state
;
618 sel
= &uth
->uu_state
.ss_select
;
619 /* cleanup the select bit space */
621 FREE(sel
->ibits
, M_TEMP
);
622 FREE(sel
->obits
, M_TEMP
);
625 if (sel
->allocsize
&& uth
->uu_wqsub
){
626 kfree(uth
->uu_wqsub
, sel
->allocsize
);
627 sel
->count
= sel
->nfcount
= 0;
633 if ((task
!= kernel_task
) && p
) {
634 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
636 TAILQ_REMOVE(&p
->p_uthlist
, uth
, uu_list
);
638 (void)thread_funnel_set(kernel_flock
, funnel_state
);
640 /* and free the uthread itself */
641 zfree(uthread_zone
, (vm_offset_t
)uthread
);