]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
3 | * | |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb A |
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 | |
14 | * agreement. | |
15 | * | |
16 | * Please obtain a copy of the License at | |
17 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
18 | * file. | |
19 | * | |
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. | |
27 | * | |
28 | * @APPLE_LICENSE_OSREFERENCE_HEADER_END@ | |
1c79356b A |
29 | */ |
30 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
31 | /* | |
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. | |
39 | * | |
40 | * Redistribution and use in source and binary forms, with or without | |
41 | * modification, are permitted provided that the following conditions | |
42 | * are met: | |
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. | |
55 | * | |
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 | |
66 | * SUCH DAMAGE. | |
67 | * | |
68 | * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95 | |
69 | */ | |
70 | ||
55e303ae | 71 | #include <kern/assert.h> |
1c79356b A |
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> | |
91447636 A |
77 | #include <sys/proc_internal.h> |
78 | #include <sys/kauth.h> | |
1c79356b A |
79 | #include <sys/user.h> |
80 | #include <sys/resourcevar.h> | |
91447636 A |
81 | #include <sys/vnode_internal.h> |
82 | #include <sys/file_internal.h> | |
1c79356b | 83 | #include <sys/acct.h> |
9bccf70c | 84 | #if KTRACE |
1c79356b | 85 | #include <sys/ktrace.h> |
9bccf70c | 86 | #endif |
1c79356b | 87 | |
91447636 A |
88 | #include <bsm/audit_kernel.h> |
89 | ||
1c79356b | 90 | #include <mach/mach_types.h> |
91447636 A |
91 | #include <kern/kern_types.h> |
92 | #include <kern/kalloc.h> | |
1c79356b | 93 | #include <kern/mach_param.h> |
91447636 A |
94 | #include <kern/task.h> |
95 | #include <kern/zalloc.h> | |
1c79356b A |
96 | |
97 | #include <machine/spl.h> | |
98 | ||
91447636 A |
99 | #include <vm/vm_protos.h> // for vm_map_commpage64 |
100 | ||
101 | thread_t cloneproc(struct proc *, int); | |
0b4e3aa0 | 102 | struct proc * forkproc(struct proc *, int); |
91447636 | 103 | thread_t procdup(struct proc *child, struct proc *parent); |
1c79356b A |
104 | |
105 | #define DOFORK 0x1 /* fork() system call */ | |
106 | #define DOVFORK 0x2 /* vfork() system call */ | |
107 | static int fork1(struct proc *, long, register_t *); | |
108 | ||
109 | /* | |
110 | * fork system call. | |
111 | */ | |
112 | int | |
91447636 | 113 | fork(struct proc *p, __unused void *uap, register_t *retval) |
1c79356b A |
114 | { |
115 | return (fork1(p, (long)DOFORK, retval)); | |
116 | } | |
117 | ||
118 | /* | |
119 | * vfork system call | |
120 | */ | |
121 | int | |
91447636 | 122 | vfork(struct proc *p, void *uap, register_t *retval) |
1c79356b | 123 | { |
0b4e3aa0 A |
124 | register struct proc * newproc; |
125 | register uid_t uid; | |
91447636 | 126 | thread_t cur_act = (thread_t)current_thread(); |
0b4e3aa0 A |
127 | int count; |
128 | task_t t; | |
129 | uthread_t ut; | |
91447636 | 130 | |
0b4e3aa0 A |
131 | /* |
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. | |
137 | */ | |
91447636 | 138 | uid = kauth_cred_get()->cr_ruid; |
0b4e3aa0 A |
139 | if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { |
140 | tablefull("proc"); | |
141 | retval[1] = 0; | |
142 | return (EAGAIN); | |
143 | } | |
144 | ||
145 | /* | |
146 | * Increment the count of procs running with this uid. Don't allow | |
147 | * a nonprivileged user to exceed their current limit. | |
148 | */ | |
149 | count = chgproccnt(uid, 1); | |
150 | if (uid != 0 && count > p->p_rlimit[RLIMIT_NPROC].rlim_cur) { | |
151 | (void)chgproccnt(uid, -1); | |
152 | return (EAGAIN); | |
153 | } | |
154 | ||
155 | ut = (struct uthread *)get_bsdthread_info(cur_act); | |
91447636 | 156 | if (ut->uu_flag & UT_VFORK) { |
0b4e3aa0 | 157 | printf("vfork called recursively by %s\n", p->p_comm); |
55e303ae | 158 | (void)chgproccnt(uid, -1); |
0b4e3aa0 A |
159 | return (EINVAL); |
160 | } | |
161 | p->p_flag |= P_VFORK; | |
162 | p->p_vforkcnt++; | |
163 | ||
164 | /* The newly created process comes with signal lock held */ | |
165 | newproc = (struct proc *)forkproc(p,1); | |
166 | ||
e5568f75 A |
167 | AUDIT_ARG(pid, newproc->p_pid); |
168 | ||
0b4e3aa0 A |
169 | LIST_INSERT_AFTER(p, newproc, p_pglist); |
170 | newproc->p_pptr = p; | |
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; | |
180 | ||
91447636 | 181 | ut->uu_flag |= UT_VFORK; |
0b4e3aa0 A |
182 | ut->uu_proc = newproc; |
183 | ut->uu_userstate = (void *)act_thread_csave(); | |
9bccf70c | 184 | ut->uu_vforkmask = ut->uu_sigmask; |
0b4e3aa0 | 185 | |
91447636 A |
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; | |
190 | } | |
191 | ||
0b4e3aa0 A |
192 | thread_set_child(cur_act, newproc->p_pid); |
193 | ||
91447636 | 194 | microtime(&newproc->p_stats->p_start); |
0b4e3aa0 A |
195 | newproc->p_acflag = AFORK; |
196 | ||
197 | /* | |
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). | |
201 | */ | |
202 | newproc->p_flag |= P_PPWAIT; | |
203 | ||
204 | /* drop the signal lock on the child */ | |
205 | signal_unlock(newproc); | |
206 | ||
207 | retval[0] = newproc->p_pid; | |
208 | retval[1] = 1; /* mark child */ | |
209 | ||
210 | return (0); | |
1c79356b A |
211 | } |
212 | ||
0b4e3aa0 A |
213 | /* |
214 | * Return to parent vfork ehread() | |
215 | */ | |
216 | void | |
91447636 A |
217 | vfork_return(__unused thread_t th_act, struct proc *p, struct proc *p2, |
218 | register_t *retval) | |
0b4e3aa0 | 219 | { |
91447636 | 220 | thread_t cur_act = (thread_t)current_thread(); |
0b4e3aa0 A |
221 | uthread_t ut; |
222 | ||
91447636 | 223 | ut = (struct uthread *)get_bsdthread_info(cur_act); |
0b4e3aa0 A |
224 | |
225 | act_thread_catt(ut->uu_userstate); | |
226 | ||
227 | /* Make sure only one at this time */ | |
91447636 A |
228 | p->p_vforkcnt--; |
229 | if (p->p_vforkcnt <0) | |
230 | panic("vfork cnt is -ve"); | |
231 | if (p->p_vforkcnt <=0) | |
232 | p->p_flag &= ~P_VFORK; | |
0b4e3aa0 | 233 | ut->uu_userstate = 0; |
91447636 A |
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; | |
239 | } | |
0b4e3aa0 | 240 | ut->uu_proc = 0; |
9bccf70c | 241 | ut->uu_sigmask = ut->uu_vforkmask; |
0b4e3aa0 A |
242 | p2->p_flag &= ~P_INVFORK; |
243 | p2->p_vforkact = (void *)0; | |
244 | ||
91447636 | 245 | thread_set_parent(cur_act, p2->p_pid); |
0b4e3aa0 A |
246 | |
247 | if (retval) { | |
248 | retval[0] = p2->p_pid; | |
249 | retval[1] = 0; /* mark parent */ | |
250 | } | |
251 | ||
252 | return; | |
253 | } | |
254 | ||
91447636 A |
255 | thread_t |
256 | procdup(struct proc *child, struct proc *parent) | |
0b4e3aa0 | 257 | { |
91447636 | 258 | thread_t thread; |
0b4e3aa0 A |
259 | task_t task; |
260 | kern_return_t result; | |
0b4e3aa0 A |
261 | |
262 | if (parent->task == kernel_task) | |
5d5c5d0d | 263 | result = task_create_internal(TASK_NULL, FALSE, FALSE, &task); |
0b4e3aa0 | 264 | else |
5d5c5d0d | 265 | result = task_create_internal(parent->task, TRUE, (parent->p_flag & P_LP64), &task); |
0b4e3aa0 A |
266 | if (result != KERN_SUCCESS) |
267 | printf("fork/procdup: task_create failed. Code: 0x%x\n", result); | |
268 | child->task = task; | |
269 | /* task->proc = child; */ | |
270 | set_bsdtask_info(task, child); | |
91447636 A |
271 | if (parent->p_flag & P_LP64) { |
272 | task_set_64bit(task, TRUE); | |
5d5c5d0d | 273 | vm_map_set_64bit(get_task_map(task)); |
91447636 | 274 | child->p_flag |= P_LP64; |
91447636 A |
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)); | |
91447636 A |
278 | } else { |
279 | task_set_64bit(task, FALSE); | |
5d5c5d0d | 280 | vm_map_set_32bit(get_task_map(task)); |
91447636 | 281 | child->p_flag &= ~P_LP64; |
5d5c5d0d A |
282 | #ifdef __i386__ |
283 | /* | |
284 | * On Intel, the comm page doesn't get mapped automatically | |
285 | * because it goes beyond the end of the VM map in the current | |
286 | * 3GB/1GB address space model. | |
287 | * XXX This explicit mapping will probably become unnecessary | |
288 | * when we switch to the new 4GB/4GB address space model. | |
289 | */ | |
290 | vm_map_commpage32(get_task_map(task)); | |
291 | #endif /* __i386__ */ | |
91447636 | 292 | } |
0b4e3aa0 A |
293 | if (child->p_nice != 0) |
294 | resetpriority(child); | |
55e303ae | 295 | |
0b4e3aa0 A |
296 | result = thread_create(task, &thread); |
297 | if (result != KERN_SUCCESS) | |
298 | printf("fork/procdup: thread_create failed. Code: 0x%x\n", result); | |
299 | ||
300 | return(thread); | |
301 | } | |
302 | ||
303 | ||
1c79356b A |
304 | static int |
305 | fork1(p1, flags, retval) | |
306 | struct proc *p1; | |
307 | long flags; | |
308 | register_t *retval; | |
309 | { | |
310 | register struct proc *p2; | |
311 | register uid_t uid; | |
91447636 A |
312 | thread_t newth; |
313 | int count; | |
314 | task_t t; | |
1c79356b A |
315 | |
316 | /* | |
317 | * Although process entries are dynamically created, we still keep | |
318 | * a global limit on the maximum number we will create. Don't allow | |
319 | * a nonprivileged user to use the last process; don't let root | |
320 | * exceed the limit. The variable nprocs is the current number of | |
321 | * processes, maxproc is the limit. | |
322 | */ | |
91447636 | 323 | uid = kauth_cred_get()->cr_ruid; |
1c79356b A |
324 | if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { |
325 | tablefull("proc"); | |
326 | retval[1] = 0; | |
327 | return (EAGAIN); | |
328 | } | |
329 | ||
330 | /* | |
331 | * Increment the count of procs running with this uid. Don't allow | |
332 | * a nonprivileged user to exceed their current limit. | |
333 | */ | |
334 | count = chgproccnt(uid, 1); | |
335 | if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) { | |
336 | (void)chgproccnt(uid, -1); | |
337 | return (EAGAIN); | |
338 | } | |
339 | ||
340 | /* The newly created process comes with signal lock held */ | |
341 | newth = cloneproc(p1, 1); | |
9bccf70c | 342 | thread_dup(newth); |
1c79356b A |
343 | /* p2 = newth->task->proc; */ |
344 | p2 = (struct proc *)(get_bsdtask_info(get_threadtask(newth))); | |
a3d08fcd | 345 | set_security_token(p2); /* propagate change of PID */ |
1c79356b | 346 | |
e5568f75 A |
347 | AUDIT_ARG(pid, p2->p_pid); |
348 | ||
1c79356b A |
349 | thread_set_child(newth, p2->p_pid); |
350 | ||
91447636 | 351 | microtime(&p2->p_stats->p_start); |
1c79356b A |
352 | p2->p_acflag = AFORK; |
353 | ||
354 | /* | |
355 | * Preserve synchronization semantics of vfork. If waiting for | |
356 | * child to exec or exit, set P_PPWAIT on child, and sleep on our | |
357 | * proc (in case of exit). | |
358 | */ | |
359 | if (flags == DOVFORK) | |
360 | p2->p_flag |= P_PPWAIT; | |
361 | /* drop the signal lock on the child */ | |
362 | signal_unlock(p2); | |
363 | ||
364 | (void) thread_resume(newth); | |
365 | ||
366 | /* drop the extra references we got during the creation */ | |
91447636 | 367 | if ((t = (task_t)get_threadtask(newth)) != NULL) { |
1c79356b A |
368 | task_deallocate(t); |
369 | } | |
91447636 | 370 | thread_deallocate(newth); |
1c79356b | 371 | |
55e303ae A |
372 | KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); |
373 | ||
1c79356b A |
374 | while (p2->p_flag & P_PPWAIT) |
375 | tsleep(p1, PWAIT, "ppwait", 0); | |
376 | ||
377 | retval[0] = p2->p_pid; | |
378 | retval[1] = 0; /* mark parent */ | |
379 | ||
380 | return (0); | |
381 | } | |
382 | ||
383 | /* | |
384 | * cloneproc() | |
385 | * | |
386 | * Create a new process from a specified process. | |
387 | * On return newly created child process has signal | |
388 | * lock held to block delivery of signal to it if called with | |
389 | * lock set. fork() code needs to explicity remove this lock | |
390 | * before signals can be delivered | |
391 | */ | |
91447636 | 392 | thread_t |
1c79356b A |
393 | cloneproc(p1, lock) |
394 | register struct proc *p1; | |
395 | register int lock; | |
0b4e3aa0 A |
396 | { |
397 | register struct proc *p2; | |
91447636 | 398 | thread_t th; |
0b4e3aa0 A |
399 | |
400 | p2 = (struct proc *)forkproc(p1,lock); | |
9bccf70c A |
401 | |
402 | ||
0b4e3aa0 A |
403 | th = procdup(p2, p1); /* child, parent */ |
404 | ||
405 | LIST_INSERT_AFTER(p1, p2, p_pglist); | |
406 | p2->p_pptr = p1; | |
407 | LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling); | |
408 | LIST_INIT(&p2->p_children); | |
409 | LIST_INSERT_HEAD(&allproc, p2, p_list); | |
410 | LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); | |
411 | TAILQ_INIT(&p2->p_evlist); | |
412 | /* | |
413 | * Make child runnable, set start time. | |
414 | */ | |
415 | p2->p_stat = SRUN; | |
416 | ||
417 | return(th); | |
418 | } | |
419 | ||
420 | struct proc * | |
421 | forkproc(p1, lock) | |
422 | register struct proc *p1; | |
423 | register int lock; | |
1c79356b A |
424 | { |
425 | register struct proc *p2, *newproc; | |
426 | static int nextpid = 0, pidchecked = 0; | |
1c79356b A |
427 | |
428 | /* Allocate new proc. */ | |
429 | MALLOC_ZONE(newproc, struct proc *, | |
430 | sizeof *newproc, M_PROC, M_WAITOK); | |
91447636 A |
431 | if (newproc == NULL) |
432 | panic("forkproc: M_PROC zone exhausted"); | |
1c79356b A |
433 | MALLOC_ZONE(newproc->p_stats, struct pstats *, |
434 | sizeof *newproc->p_stats, M_SUBPROC, M_WAITOK); | |
91447636 A |
435 | if (newproc->p_stats == NULL) |
436 | panic("forkproc: M_SUBPROC zone exhausted (p_stats)"); | |
1c79356b A |
437 | MALLOC_ZONE(newproc->p_sigacts, struct sigacts *, |
438 | sizeof *newproc->p_sigacts, M_SUBPROC, M_WAITOK); | |
91447636 A |
439 | if (newproc->p_sigacts == NULL) |
440 | panic("forkproc: M_SUBPROC zone exhausted (p_sigacts)"); | |
1c79356b A |
441 | |
442 | /* | |
443 | * Find an unused process ID. We remember a range of unused IDs | |
444 | * ready to use (from nextpid+1 through pidchecked-1). | |
445 | */ | |
446 | nextpid++; | |
447 | retry: | |
448 | /* | |
449 | * If the process ID prototype has wrapped around, | |
450 | * restart somewhat above 0, as the low-numbered procs | |
451 | * tend to include daemons that don't exit. | |
452 | */ | |
453 | if (nextpid >= PID_MAX) { | |
454 | nextpid = 100; | |
455 | pidchecked = 0; | |
456 | } | |
457 | if (nextpid >= pidchecked) { | |
458 | int doingzomb = 0; | |
459 | ||
460 | pidchecked = PID_MAX; | |
461 | /* | |
462 | * Scan the active and zombie procs to check whether this pid | |
463 | * is in use. Remember the lowest pid that's greater | |
464 | * than nextpid, so we can avoid checking for a while. | |
465 | */ | |
466 | p2 = allproc.lh_first; | |
467 | again: | |
468 | for (; p2 != 0; p2 = p2->p_list.le_next) { | |
469 | while (p2->p_pid == nextpid || | |
9bccf70c A |
470 | p2->p_pgrp->pg_id == nextpid || |
471 | p2->p_session->s_sid == nextpid) { | |
1c79356b A |
472 | nextpid++; |
473 | if (nextpid >= pidchecked) | |
474 | goto retry; | |
475 | } | |
476 | if (p2->p_pid > nextpid && pidchecked > p2->p_pid) | |
477 | pidchecked = p2->p_pid; | |
478 | if (p2->p_pgrp && p2->p_pgrp->pg_id > nextpid && | |
479 | pidchecked > p2->p_pgrp->pg_id) | |
480 | pidchecked = p2->p_pgrp->pg_id; | |
9bccf70c A |
481 | if (p2->p_session->s_sid > nextpid && |
482 | pidchecked > p2->p_session->s_sid) | |
483 | pidchecked = p2->p_session->s_sid; | |
1c79356b A |
484 | } |
485 | if (!doingzomb) { | |
486 | doingzomb = 1; | |
487 | p2 = zombproc.lh_first; | |
488 | goto again; | |
489 | } | |
490 | } | |
491 | ||
492 | nprocs++; | |
493 | p2 = newproc; | |
494 | p2->p_stat = SIDL; | |
91447636 | 495 | p2->p_shutdownstate = 0; |
1c79356b A |
496 | p2->p_pid = nextpid; |
497 | ||
498 | /* | |
499 | * Make a proc table entry for the new process. | |
500 | * Start by zeroing the section of proc that is zero-initialized, | |
501 | * then copy the section that is copied directly from the parent. | |
502 | */ | |
503 | bzero(&p2->p_startzero, | |
504 | (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); | |
505 | bcopy(&p1->p_startcopy, &p2->p_startcopy, | |
506 | (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); | |
507 | p2->vm_shm = (void *)NULL; /* Make sure it is zero */ | |
508 | ||
55e303ae | 509 | /* |
91447636 | 510 | * Some flags are inherited from the parent. |
1c79356b A |
511 | * Duplicate sub-structures as needed. |
512 | * Increase reference counts on shared objects. | |
513 | * The p_stats and p_sigacts substructs are set in vm_fork. | |
514 | */ | |
5d5c5d0d | 515 | p2->p_flag = (p1->p_flag & (P_LP64 | P_TRANSLATED | P_AFFINITY)); |
1c79356b A |
516 | if (p1->p_flag & P_PROFIL) |
517 | startprofclock(p2); | |
91447636 A |
518 | /* |
519 | * Note that if the current thread has an assumed identity, this | |
520 | * credential will be granted to the new process. | |
521 | */ | |
522 | p2->p_ucred = kauth_cred_get_with_ref(); | |
523 | ||
524 | lck_mtx_init(&p2->p_mlock, proc_lck_grp, proc_lck_attr); | |
525 | lck_mtx_init(&p2->p_fdmlock, proc_lck_grp, proc_lck_attr); | |
55e303ae | 526 | klist_init(&p2->p_klist); |
1c79356b | 527 | |
9bccf70c | 528 | /* bump references to the text vnode */ |
1c79356b | 529 | p2->p_textvp = p1->p_textvp; |
91447636 A |
530 | if (p2->p_textvp) { |
531 | vnode_rele(p2->p_textvp); | |
532 | } | |
533 | /* XXX may fail to copy descriptors to child */ | |
1c79356b | 534 | p2->p_fd = fdcopy(p1); |
91447636 | 535 | |
1c79356b | 536 | if (p1->vm_shm) { |
91447636 A |
537 | /* XXX may fail to attach shm to child */ |
538 | (void)shmfork(p1,p2); | |
1c79356b A |
539 | } |
540 | /* | |
541 | * If p_limit is still copy-on-write, bump refcnt, | |
542 | * otherwise get a copy that won't be modified. | |
543 | * (If PL_SHAREMOD is clear, the structure is shared | |
544 | * copy-on-write.) | |
545 | */ | |
546 | if (p1->p_limit->p_lflags & PL_SHAREMOD) | |
547 | p2->p_limit = limcopy(p1->p_limit); | |
548 | else { | |
549 | p2->p_limit = p1->p_limit; | |
550 | p2->p_limit->p_refcnt++; | |
551 | } | |
552 | ||
553 | bzero(&p2->p_stats->pstat_startzero, | |
554 | (unsigned) ((caddr_t)&p2->p_stats->pstat_endzero - | |
555 | (caddr_t)&p2->p_stats->pstat_startzero)); | |
556 | bcopy(&p1->p_stats->pstat_startcopy, &p2->p_stats->pstat_startcopy, | |
557 | ((caddr_t)&p2->p_stats->pstat_endcopy - | |
558 | (caddr_t)&p2->p_stats->pstat_startcopy)); | |
559 | ||
91447636 A |
560 | bzero(&p2->p_stats->user_p_prof, sizeof(struct user_uprof)); |
561 | ||
1c79356b A |
562 | if (p1->p_sigacts != NULL) |
563 | (void)memcpy(p2->p_sigacts, | |
564 | p1->p_sigacts, sizeof *p2->p_sigacts); | |
565 | else | |
566 | (void)memset(p2->p_sigacts, 0, sizeof *p2->p_sigacts); | |
567 | ||
568 | if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) | |
569 | p2->p_flag |= P_CONTROLT; | |
570 | ||
55e303ae A |
571 | p2->p_argslen = p1->p_argslen; |
572 | p2->p_argc = p1->p_argc; | |
1c79356b A |
573 | p2->p_xstat = 0; |
574 | p2->p_ru = NULL; | |
575 | ||
576 | p2->p_debugger = 0; /* don't inherit */ | |
577 | lockinit(&p2->signal_lock, PVM, "signal", 0, 0); | |
578 | /* block all signals to reach the process */ | |
579 | if (lock) | |
580 | signal_lock(p2); | |
581 | p2->sigwait = FALSE; | |
582 | p2->sigwait_thread = NULL; | |
583 | p2->exit_thread = NULL; | |
584 | p2->user_stack = p1->user_stack; | |
0b4e3aa0 A |
585 | p2->p_vforkcnt = 0; |
586 | p2->p_vforkact = 0; | |
91447636 | 587 | p2->p_lflag = 0; |
b36670ce | 588 | p2->p_ladvflag = 0; |
ff6e181a | 589 | p2->p_internalref = 0; |
9bccf70c | 590 | TAILQ_INIT(&p2->p_uthlist); |
55e303ae A |
591 | TAILQ_INIT(&p2->aio_activeq); |
592 | TAILQ_INIT(&p2->aio_doneq); | |
593 | p2->aio_active_count = 0; | |
594 | p2->aio_done_count = 0; | |
1c79356b A |
595 | |
596 | #if KTRACE | |
597 | /* | |
598 | * Copy traceflag and tracefile if enabled. | |
599 | * If not inherited, these were zeroed above. | |
600 | */ | |
601 | if (p1->p_traceflag&KTRFAC_INHERIT) { | |
602 | p2->p_traceflag = p1->p_traceflag; | |
e5568f75 | 603 | if ((p2->p_tracep = p1->p_tracep) != NULL) { |
91447636 | 604 | vnode_ref(p2->p_tracep); |
e5568f75 | 605 | } |
1c79356b A |
606 | } |
607 | #endif | |
0b4e3aa0 | 608 | return(p2); |
1c79356b | 609 | |
1c79356b A |
610 | } |
611 | ||
91447636 A |
612 | void |
613 | proc_lock(proc_t p) | |
614 | { | |
615 | lck_mtx_lock(&p->p_mlock); | |
616 | } | |
617 | ||
618 | void | |
619 | proc_unlock(proc_t p) | |
620 | { | |
621 | lck_mtx_unlock(&p->p_mlock); | |
622 | } | |
623 | ||
1c79356b A |
624 | #include <kern/zalloc.h> |
625 | ||
626 | struct zone *uthread_zone; | |
627 | int uthread_zone_inited = 0; | |
628 | ||
629 | void | |
91447636 | 630 | uthread_zone_init(void) |
1c79356b A |
631 | { |
632 | if (!uthread_zone_inited) { | |
633 | uthread_zone = zinit(sizeof(struct uthread), | |
91447636 A |
634 | THREAD_MAX * sizeof(struct uthread), |
635 | THREAD_CHUNK * sizeof(struct uthread), | |
636 | "uthreads"); | |
1c79356b A |
637 | uthread_zone_inited = 1; |
638 | } | |
639 | } | |
640 | ||
641 | void * | |
91447636 | 642 | uthread_alloc(task_t task, thread_t thr_act ) |
1c79356b | 643 | { |
9bccf70c A |
644 | struct proc *p; |
645 | struct uthread *uth, *uth_parent; | |
1c79356b | 646 | void *ut; |
9bccf70c | 647 | boolean_t funnel_state; |
1c79356b A |
648 | |
649 | if (!uthread_zone_inited) | |
650 | uthread_zone_init(); | |
651 | ||
652 | ut = (void *)zalloc(uthread_zone); | |
653 | bzero(ut, sizeof(struct uthread)); | |
9bccf70c | 654 | |
91447636 A |
655 | p = (struct proc *) get_bsdtask_info(task); |
656 | uth = (struct uthread *)ut; | |
9bccf70c | 657 | |
91447636 A |
658 | /* |
659 | * Thread inherits credential from the creating thread, if both | |
660 | * are in the same task. | |
661 | * | |
662 | * If the creating thread has no credential or is from another | |
663 | * task we can leave the new thread credential NULL. If it needs | |
664 | * one later, it will be lazily assigned from the task's process. | |
665 | */ | |
666 | uth_parent = (struct uthread *)get_bsdthread_info(current_thread()); | |
667 | if ((task == current_task()) && | |
668 | (uth_parent != NULL) && | |
669 | (uth_parent->uu_ucred != NOCRED)) { | |
670 | uth->uu_ucred = uth_parent->uu_ucred; | |
671 | kauth_cred_ref(uth->uu_ucred); | |
672 | /* the credential we just inherited is an assumed credential */ | |
673 | if (uth_parent->uu_flag & UT_SETUID) | |
674 | uth->uu_flag |= UT_SETUID; | |
675 | } else { | |
676 | uth->uu_ucred = NOCRED; | |
677 | } | |
678 | ||
679 | if (task != kernel_task) { | |
680 | ||
9bccf70c | 681 | funnel_state = thread_funnel_set(kernel_flock, TRUE); |
9bccf70c | 682 | if (uth_parent) { |
91447636 | 683 | if (uth_parent->uu_flag & UT_SAS_OLDMASK) |
9bccf70c A |
684 | uth->uu_sigmask = uth_parent->uu_oldmask; |
685 | else | |
686 | uth->uu_sigmask = uth_parent->uu_sigmask; | |
687 | } | |
688 | uth->uu_act = thr_act; | |
689 | //signal_lock(p); | |
91447636 | 690 | if (p) { |
9bccf70c | 691 | TAILQ_INSERT_TAIL(&p->p_uthlist, uth, uu_list); |
91447636 | 692 | } |
9bccf70c A |
693 | //signal_unlock(p); |
694 | (void)thread_funnel_set(kernel_flock, funnel_state); | |
695 | } | |
696 | ||
1c79356b A |
697 | return (ut); |
698 | } | |
699 | ||
0b4e3aa0 | 700 | |
1c79356b | 701 | void |
91447636 | 702 | uthread_free(task_t task, void *uthread, void * bsd_info) |
1c79356b A |
703 | { |
704 | struct _select *sel; | |
705 | struct uthread *uth = (struct uthread *)uthread; | |
9bccf70c | 706 | struct proc * p = (struct proc *)bsd_info; |
9bccf70c | 707 | boolean_t funnel_state; |
55e303ae A |
708 | |
709 | /* | |
710 | * Per-thread audit state should never last beyond system | |
711 | * call return. Since we don't audit the thread creation/ | |
712 | * removal, the thread state pointer should never be | |
713 | * non-NULL when we get here. | |
714 | */ | |
715 | assert(uth->uu_ar == NULL); | |
1c79356b | 716 | |
91447636 | 717 | sel = &uth->uu_select; |
1c79356b A |
718 | /* cleanup the select bit space */ |
719 | if (sel->nbytes) { | |
720 | FREE(sel->ibits, M_TEMP); | |
721 | FREE(sel->obits, M_TEMP); | |
722 | } | |
723 | ||
91447636 A |
724 | if (sel->allocsize && sel->wqset){ |
725 | kfree(sel->wqset, sel->allocsize); | |
726 | sel->count = 0; | |
0b4e3aa0 | 727 | sel->allocsize = 0; |
91447636 | 728 | sel->wqset = 0; |
0b4e3aa0 A |
729 | sel->wql = 0; |
730 | } | |
731 | ||
91447636 A |
732 | if (uth->uu_ucred != NOCRED) |
733 | kauth_cred_rele(uth->uu_ucred); | |
e5568f75 | 734 | |
91447636 | 735 | if ((task != kernel_task) && p) { |
9bccf70c | 736 | funnel_state = thread_funnel_set(kernel_flock, TRUE); |
91447636 A |
737 | //signal_lock(p); |
738 | TAILQ_REMOVE(&p->p_uthlist, uth, uu_list); | |
739 | //signal_unlock(p); | |
9bccf70c A |
740 | (void)thread_funnel_set(kernel_flock, funnel_state); |
741 | } | |
1c79356b | 742 | /* and free the uthread itself */ |
91447636 | 743 | zfree(uthread_zone, uthread); |
1c79356b | 744 | } |