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