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