]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
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 | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
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 | ||
69 | #include <kern/assert.h> | |
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> | |
75 | #include <sys/proc_internal.h> | |
76 | #include <sys/kauth.h> | |
77 | #include <sys/user.h> | |
78 | #include <sys/resourcevar.h> | |
79 | #include <sys/vnode_internal.h> | |
80 | #include <sys/file_internal.h> | |
81 | #include <sys/acct.h> | |
82 | #if KTRACE | |
83 | #include <sys/ktrace.h> | |
84 | #endif | |
85 | ||
86 | #include <bsm/audit_kernel.h> | |
87 | ||
88 | #include <mach/mach_types.h> | |
89 | #include <kern/kern_types.h> | |
90 | #include <kern/kalloc.h> | |
91 | #include <kern/mach_param.h> | |
92 | #include <kern/task.h> | |
93 | #include <kern/zalloc.h> | |
94 | ||
95 | #include <machine/spl.h> | |
96 | ||
97 | #include <vm/vm_protos.h> // for vm_map_commpage64 | |
98 | ||
99 | thread_t cloneproc(struct proc *, int); | |
100 | struct proc * forkproc(struct proc *, int); | |
101 | thread_t procdup(struct proc *child, struct proc *parent); | |
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 | |
111 | fork(struct proc *p, __unused void *uap, register_t *retval) | |
112 | { | |
113 | return (fork1(p, (long)DOFORK, retval)); | |
114 | } | |
115 | ||
116 | /* | |
117 | * vfork system call | |
118 | */ | |
119 | int | |
120 | vfork(struct proc *p, void *uap, register_t *retval) | |
121 | { | |
122 | register struct proc * newproc; | |
123 | register uid_t uid; | |
124 | thread_t cur_act = (thread_t)current_thread(); | |
125 | int count; | |
126 | task_t t; | |
127 | uthread_t ut; | |
128 | ||
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 | */ | |
136 | uid = kauth_cred_get()->cr_ruid; | |
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); | |
154 | if (ut->uu_flag & UT_VFORK) { | |
155 | printf("vfork called recursively by %s\n", p->p_comm); | |
156 | (void)chgproccnt(uid, -1); | |
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 | ||
165 | AUDIT_ARG(pid, newproc->p_pid); | |
166 | ||
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 | ||
179 | ut->uu_flag |= UT_VFORK; | |
180 | ut->uu_proc = newproc; | |
181 | ut->uu_userstate = (void *)act_thread_csave(); | |
182 | ut->uu_vforkmask = ut->uu_sigmask; | |
183 | ||
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 | ||
190 | thread_set_child(cur_act, newproc->p_pid); | |
191 | ||
192 | microtime(&newproc->p_stats->p_start); | |
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); | |
209 | } | |
210 | ||
211 | /* | |
212 | * Return to parent vfork ehread() | |
213 | */ | |
214 | void | |
215 | vfork_return(__unused thread_t th_act, struct proc *p, struct proc *p2, | |
216 | register_t *retval) | |
217 | { | |
218 | thread_t cur_act = (thread_t)current_thread(); | |
219 | uthread_t ut; | |
220 | ||
221 | ut = (struct uthread *)get_bsdthread_info(cur_act); | |
222 | ||
223 | act_thread_catt(ut->uu_userstate); | |
224 | ||
225 | /* Make sure only one at this time */ | |
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; | |
231 | ut->uu_userstate = 0; | |
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 | } | |
238 | ut->uu_proc = 0; | |
239 | ut->uu_sigmask = ut->uu_vforkmask; | |
240 | p2->p_flag &= ~P_INVFORK; | |
241 | p2->p_vforkact = (void *)0; | |
242 | ||
243 | thread_set_parent(cur_act, p2->p_pid); | |
244 | ||
245 | if (retval) { | |
246 | retval[0] = p2->p_pid; | |
247 | retval[1] = 0; /* mark parent */ | |
248 | } | |
249 | ||
250 | return; | |
251 | } | |
252 | ||
253 | thread_t | |
254 | procdup(struct proc *child, struct proc *parent) | |
255 | { | |
256 | thread_t thread; | |
257 | task_t task; | |
258 | kern_return_t result; | |
259 | ||
260 | if (parent->task == kernel_task) | |
261 | result = task_create_internal(TASK_NULL, FALSE, FALSE, &task); | |
262 | else | |
263 | result = task_create_internal(parent->task, TRUE, (parent->p_flag & P_LP64), &task); | |
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); | |
269 | if (parent->p_flag & P_LP64) { | |
270 | task_set_64bit(task, TRUE); | |
271 | vm_map_set_64bit(get_task_map(task)); | |
272 | child->p_flag |= P_LP64; | |
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)); | |
276 | } else { | |
277 | task_set_64bit(task, FALSE); | |
278 | vm_map_set_32bit(get_task_map(task)); | |
279 | child->p_flag &= ~P_LP64; | |
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__ */ | |
290 | } | |
291 | if (child->p_nice != 0) | |
292 | resetpriority(child); | |
293 | ||
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 | ||
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; | |
310 | thread_t newth; | |
311 | int count; | |
312 | task_t t; | |
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 | */ | |
321 | uid = kauth_cred_get()->cr_ruid; | |
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); | |
340 | thread_dup(newth); | |
341 | /* p2 = newth->task->proc; */ | |
342 | p2 = (struct proc *)(get_bsdtask_info(get_threadtask(newth))); | |
343 | set_security_token(p2); /* propagate change of PID */ | |
344 | ||
345 | AUDIT_ARG(pid, p2->p_pid); | |
346 | ||
347 | thread_set_child(newth, p2->p_pid); | |
348 | ||
349 | microtime(&p2->p_stats->p_start); | |
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 */ | |
365 | if ((t = (task_t)get_threadtask(newth)) != NULL) { | |
366 | task_deallocate(t); | |
367 | } | |
368 | thread_deallocate(newth); | |
369 | ||
370 | KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); | |
371 | ||
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 | */ | |
390 | thread_t | |
391 | cloneproc(p1, lock) | |
392 | register struct proc *p1; | |
393 | register int lock; | |
394 | { | |
395 | register struct proc *p2; | |
396 | thread_t th; | |
397 | ||
398 | p2 = (struct proc *)forkproc(p1,lock); | |
399 | ||
400 | ||
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; | |
422 | { | |
423 | register struct proc *p2, *newproc; | |
424 | static int nextpid = 0, pidchecked = 0; | |
425 | ||
426 | /* Allocate new proc. */ | |
427 | MALLOC_ZONE(newproc, struct proc *, | |
428 | sizeof *newproc, M_PROC, M_WAITOK); | |
429 | if (newproc == NULL) | |
430 | panic("forkproc: M_PROC zone exhausted"); | |
431 | MALLOC_ZONE(newproc->p_stats, struct pstats *, | |
432 | sizeof *newproc->p_stats, M_SUBPROC, M_WAITOK); | |
433 | if (newproc->p_stats == NULL) | |
434 | panic("forkproc: M_SUBPROC zone exhausted (p_stats)"); | |
435 | MALLOC_ZONE(newproc->p_sigacts, struct sigacts *, | |
436 | sizeof *newproc->p_sigacts, M_SUBPROC, M_WAITOK); | |
437 | if (newproc->p_sigacts == NULL) | |
438 | panic("forkproc: M_SUBPROC zone exhausted (p_sigacts)"); | |
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 || | |
468 | p2->p_pgrp->pg_id == nextpid || | |
469 | p2->p_session->s_sid == nextpid) { | |
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; | |
479 | if (p2->p_session->s_sid > nextpid && | |
480 | pidchecked > p2->p_session->s_sid) | |
481 | pidchecked = p2->p_session->s_sid; | |
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; | |
493 | p2->p_shutdownstate = 0; | |
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 | ||
507 | /* | |
508 | * Some flags are inherited from the parent. | |
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 | */ | |
513 | p2->p_flag = (p1->p_flag & (P_LP64 | P_TRANSLATED | P_AFFINITY)); | |
514 | if (p1->p_flag & P_PROFIL) | |
515 | startprofclock(p2); | |
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); | |
524 | klist_init(&p2->p_klist); | |
525 | ||
526 | /* bump references to the text vnode */ | |
527 | p2->p_textvp = p1->p_textvp; | |
528 | if (p2->p_textvp) { | |
529 | vnode_rele(p2->p_textvp); | |
530 | } | |
531 | /* XXX may fail to copy descriptors to child */ | |
532 | p2->p_fd = fdcopy(p1); | |
533 | ||
534 | if (p1->vm_shm) { | |
535 | /* XXX may fail to attach shm to child */ | |
536 | (void)shmfork(p1,p2); | |
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 | ||
558 | bzero(&p2->p_stats->user_p_prof, sizeof(struct user_uprof)); | |
559 | ||
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 | ||
569 | p2->p_argslen = p1->p_argslen; | |
570 | p2->p_argc = p1->p_argc; | |
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; | |
583 | p2->p_vforkcnt = 0; | |
584 | p2->p_vforkact = 0; | |
585 | p2->p_lflag = 0; | |
586 | p2->p_ladvflag = 0; | |
587 | p2->p_internalref = 0; | |
588 | TAILQ_INIT(&p2->p_uthlist); | |
589 | TAILQ_INIT(&p2->aio_activeq); | |
590 | TAILQ_INIT(&p2->aio_doneq); | |
591 | p2->aio_active_count = 0; | |
592 | p2->aio_done_count = 0; | |
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; | |
601 | if ((p2->p_tracep = p1->p_tracep) != NULL) { | |
602 | vnode_ref(p2->p_tracep); | |
603 | } | |
604 | } | |
605 | #endif | |
606 | return(p2); | |
607 | ||
608 | } | |
609 | ||
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 | ||
622 | #include <kern/zalloc.h> | |
623 | ||
624 | struct zone *uthread_zone; | |
625 | int uthread_zone_inited = 0; | |
626 | ||
627 | void | |
628 | uthread_zone_init(void) | |
629 | { | |
630 | if (!uthread_zone_inited) { | |
631 | uthread_zone = zinit(sizeof(struct uthread), | |
632 | THREAD_MAX * sizeof(struct uthread), | |
633 | THREAD_CHUNK * sizeof(struct uthread), | |
634 | "uthreads"); | |
635 | uthread_zone_inited = 1; | |
636 | } | |
637 | } | |
638 | ||
639 | void * | |
640 | uthread_alloc(task_t task, thread_t thr_act ) | |
641 | { | |
642 | struct proc *p; | |
643 | struct uthread *uth, *uth_parent; | |
644 | void *ut; | |
645 | boolean_t funnel_state; | |
646 | ||
647 | if (!uthread_zone_inited) | |
648 | uthread_zone_init(); | |
649 | ||
650 | ut = (void *)zalloc(uthread_zone); | |
651 | bzero(ut, sizeof(struct uthread)); | |
652 | ||
653 | p = (struct proc *) get_bsdtask_info(task); | |
654 | uth = (struct uthread *)ut; | |
655 | ||
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) && | |
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); | |
674 | uth->uu_ucred = uth_parent->uu_ucred; | |
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 | ||
684 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
685 | if (uth_parent) { | |
686 | if (uth_parent->uu_flag & UT_SAS_OLDMASK) | |
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); | |
693 | if (p) { | |
694 | TAILQ_INSERT_TAIL(&p->p_uthlist, uth, uu_list); | |
695 | } | |
696 | //signal_unlock(p); | |
697 | (void)thread_funnel_set(kernel_flock, funnel_state); | |
698 | } | |
699 | ||
700 | return (ut); | |
701 | } | |
702 | ||
703 | ||
704 | void | |
705 | uthread_free(task_t task, void *uthread, void * bsd_info) | |
706 | { | |
707 | struct _select *sel; | |
708 | struct uthread *uth = (struct uthread *)uthread; | |
709 | struct proc * p = (struct proc *)bsd_info; | |
710 | boolean_t funnel_state; | |
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); | |
719 | ||
720 | sel = &uth->uu_select; | |
721 | /* cleanup the select bit space */ | |
722 | if (sel->nbytes) { | |
723 | FREE(sel->ibits, M_TEMP); | |
724 | FREE(sel->obits, M_TEMP); | |
725 | } | |
726 | ||
727 | if (sel->allocsize && sel->wqset){ | |
728 | kfree(sel->wqset, sel->allocsize); | |
729 | sel->count = 0; | |
730 | sel->allocsize = 0; | |
731 | sel->wqset = 0; | |
732 | sel->wql = 0; | |
733 | } | |
734 | ||
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 | } | |
740 | ||
741 | if ((task != kernel_task) && p) { | |
742 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
743 | //signal_lock(p); | |
744 | TAILQ_REMOVE(&p->p_uthlist, uth, uu_list); | |
745 | //signal_unlock(p); | |
746 | (void)thread_funnel_set(kernel_flock, funnel_state); | |
747 | } | |
748 | /* and free the uthread itself */ | |
749 | zfree(uthread_zone, uthread); | |
750 | } |