]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
3e170ce0 | 2 | * Copyright (c) 2000-2007, 2015 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 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 | */ | |
2d21ac55 A |
68 | /* |
69 | * NOTICE: This file was modified by McAfee Research in 2004 to introduce | |
70 | * support for mandatory and extensible security protections. This notice | |
71 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
72 | * Version 2.0. | |
73 | */ | |
74 | /* | |
75 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
76 | * support for mandatory and extensible security protections. This notice | |
77 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
78 | * Version 2.0. | |
79 | */ | |
1c79356b | 80 | |
55e303ae | 81 | #include <kern/assert.h> |
1c79356b A |
82 | #include <sys/param.h> |
83 | #include <sys/systm.h> | |
84 | #include <sys/filedesc.h> | |
85 | #include <sys/kernel.h> | |
86 | #include <sys/malloc.h> | |
91447636 A |
87 | #include <sys/proc_internal.h> |
88 | #include <sys/kauth.h> | |
1c79356b A |
89 | #include <sys/user.h> |
90 | #include <sys/resourcevar.h> | |
91447636 A |
91 | #include <sys/vnode_internal.h> |
92 | #include <sys/file_internal.h> | |
1c79356b | 93 | #include <sys/acct.h> |
2d21ac55 A |
94 | #include <sys/codesign.h> |
95 | #include <sys/sysproto.h> | |
490019cf A |
96 | #if CONFIG_PERSONAS |
97 | #include <sys/persona.h> | |
98 | #endif | |
2d21ac55 A |
99 | #if CONFIG_DTRACE |
100 | /* Do not include dtrace.h, it redefines kmem_[alloc/free] */ | |
101 | extern void dtrace_fasttrap_fork(proc_t, proc_t); | |
102 | extern void (*dtrace_helpers_fork)(proc_t, proc_t); | |
fe8ab488 | 103 | extern void (*dtrace_proc_waitfor_exec_ptr)(proc_t); |
2d21ac55 A |
104 | extern void dtrace_lazy_dofs_duplicate(proc_t, proc_t); |
105 | ||
fe8ab488 A |
106 | /* |
107 | * Since dtrace_proc_waitfor_exec_ptr can be added/removed in dtrace_subr.c, | |
108 | * we will store its value before actually calling it. | |
109 | */ | |
110 | static void (*dtrace_proc_waitfor_hook)(proc_t) = NULL; | |
111 | ||
2d21ac55 | 112 | #include <sys/dtrace_ptss.h> |
9bccf70c | 113 | #endif |
1c79356b | 114 | |
b0d623f7 | 115 | #include <security/audit/audit.h> |
91447636 | 116 | |
1c79356b | 117 | #include <mach/mach_types.h> |
fe8ab488 | 118 | #include <kern/coalition.h> |
91447636 A |
119 | #include <kern/kern_types.h> |
120 | #include <kern/kalloc.h> | |
1c79356b | 121 | #include <kern/mach_param.h> |
91447636 | 122 | #include <kern/task.h> |
4b17d6b6 | 123 | #include <kern/thread.h> |
2d21ac55 | 124 | #include <kern/thread_call.h> |
91447636 | 125 | #include <kern/zalloc.h> |
1c79356b A |
126 | |
127 | #include <machine/spl.h> | |
128 | ||
2d21ac55 A |
129 | #if CONFIG_MACF |
130 | #include <security/mac.h> | |
131 | #include <security/mac_mach_internal.h> | |
132 | #endif | |
133 | ||
134 | #include <vm/vm_map.h> | |
135 | #include <vm/vm_protos.h> | |
136 | #include <vm/vm_shared_region.h> | |
137 | ||
138 | #include <sys/shm_internal.h> /* for shmfork() */ | |
139 | #include <mach/task.h> /* for thread_create() */ | |
140 | #include <mach/thread_act.h> /* for thread_resume() */ | |
91447636 | 141 | |
2d21ac55 A |
142 | #include <sys/sdt.h> |
143 | ||
316670eb A |
144 | #if CONFIG_MEMORYSTATUS |
145 | #include <sys/kern_memorystatus.h> | |
146 | #endif | |
147 | ||
2d21ac55 A |
148 | /* XXX routines which should have Mach prototypes, but don't */ |
149 | void thread_set_parent(thread_t parent, int pid); | |
150 | extern void act_thread_catt(void *ctx); | |
151 | void thread_set_child(thread_t child, int pid); | |
152 | void *act_thread_csave(void); | |
153 | ||
154 | ||
3e170ce0 | 155 | thread_t cloneproc(task_t, coalition_t *, proc_t, int, int); |
b0d623f7 A |
156 | proc_t forkproc(proc_t); |
157 | void forkproc_free(proc_t); | |
3e170ce0 | 158 | thread_t fork_create_child(task_t parent_task, coalition_t *parent_coalitions, proc_t child, int inherit_memory, int is64bit); |
b0d623f7 A |
159 | void proc_vfork_begin(proc_t parent_proc); |
160 | void proc_vfork_end(proc_t parent_proc); | |
1c79356b A |
161 | |
162 | #define DOFORK 0x1 /* fork() system call */ | |
163 | #define DOVFORK 0x2 /* vfork() system call */ | |
1c79356b | 164 | |
b0d623f7 A |
165 | /* |
166 | * proc_vfork_begin | |
167 | * | |
168 | * Description: start a vfork on a process | |
169 | * | |
170 | * Parameters: parent_proc process (re)entering vfork state | |
171 | * | |
172 | * Returns: (void) | |
173 | * | |
174 | * Notes: Although this function increments a count, a count in | |
175 | * excess of 1 is not currently supported. According to the | |
176 | * POSIX standard, calling anything other than execve() or | |
316670eb A |
177 | * _exit() following a vfork(), including calling vfork() |
178 | * itself again, will result in undefined behaviour | |
b0d623f7 A |
179 | */ |
180 | void | |
181 | proc_vfork_begin(proc_t parent_proc) | |
182 | { | |
183 | proc_lock(parent_proc); | |
184 | parent_proc->p_lflag |= P_LVFORK; | |
185 | parent_proc->p_vforkcnt++; | |
186 | proc_unlock(parent_proc); | |
187 | } | |
188 | ||
189 | /* | |
190 | * proc_vfork_end | |
191 | * | |
192 | * Description: stop a vfork on a process | |
193 | * | |
194 | * Parameters: parent_proc process leaving vfork state | |
195 | * | |
196 | * Returns: (void) | |
197 | * | |
316670eb | 198 | * Notes: Decrements the count; currently, reentrancy of vfork() |
b0d623f7 A |
199 | * is unsupported on the current process |
200 | */ | |
201 | void | |
202 | proc_vfork_end(proc_t parent_proc) | |
203 | { | |
204 | proc_lock(parent_proc); | |
205 | parent_proc->p_vforkcnt--; | |
206 | if (parent_proc->p_vforkcnt < 0) | |
207 | panic("vfork cnt is -ve"); | |
b0d623f7 A |
208 | if (parent_proc->p_vforkcnt == 0) |
209 | parent_proc->p_lflag &= ~P_LVFORK; | |
210 | proc_unlock(parent_proc); | |
211 | } | |
212 | ||
1c79356b A |
213 | |
214 | /* | |
2d21ac55 A |
215 | * vfork |
216 | * | |
217 | * Description: vfork system call | |
218 | * | |
219 | * Parameters: void [no arguments] | |
220 | * | |
221 | * Retval: 0 (to child process) | |
222 | * !0 pid of child (to parent process) | |
223 | * -1 error (see "Returns:") | |
224 | * | |
225 | * Returns: EAGAIN Administrative limit reached | |
b0d623f7 | 226 | * EINVAL vfork() called during vfork() |
2d21ac55 A |
227 | * ENOMEM Failed to allocate new process |
228 | * | |
229 | * Note: After a successful call to this function, the parent process | |
230 | * has its task, thread, and uthread lent to the child process, | |
231 | * and control is returned to the caller; if this function is | |
232 | * invoked as a system call, the return is to user space, and | |
233 | * is effectively running on the child process. | |
234 | * | |
235 | * Subsequent calls that operate on process state are permitted, | |
236 | * though discouraged, and will operate on the child process; any | |
237 | * operations on the task, thread, or uthread will result in | |
238 | * changes in the parent state, and, if inheritable, the child | |
239 | * state, when a task, thread, and uthread are realized for the | |
240 | * child process at execve() time, will also be effected. Given | |
241 | * this, it's recemmended that people use the posix_spawn() call | |
242 | * instead. | |
b0d623f7 A |
243 | * |
244 | * BLOCK DIAGRAM OF VFORK | |
245 | * | |
246 | * Before: | |
247 | * | |
248 | * ,----------------. ,-------------. | |
249 | * | | task | | | |
250 | * | parent_thread | ------> | parent_task | | |
251 | * | | <.list. | | | |
252 | * `----------------' `-------------' | |
253 | * uthread | ^ bsd_info | ^ | |
254 | * v | vc_thread v | task | |
255 | * ,----------------. ,-------------. | |
256 | * | | | | | |
257 | * | parent_uthread | <.list. | parent_proc | <-- current_proc() | |
258 | * | | | | | |
259 | * `----------------' `-------------' | |
260 | * uu_proc | | |
261 | * v | |
262 | * NULL | |
263 | * | |
264 | * After: | |
265 | * | |
266 | * ,----------------. ,-------------. | |
267 | * | | task | | | |
268 | * ,----> | parent_thread | ------> | parent_task | | |
269 | * | | | <.list. | | | |
270 | * | `----------------' `-------------' | |
271 | * | uthread | ^ bsd_info | ^ | |
272 | * | v | vc_thread v | task | |
273 | * | ,----------------. ,-------------. | |
274 | * | | | | | | |
275 | * | | parent_uthread | <.list. | parent_proc | | |
276 | * | | | | | | |
277 | * | `----------------' `-------------' | |
278 | * | uu_proc | . list | |
279 | * | v v | |
280 | * | ,----------------. | |
281 | * `----- | | | |
282 | * p_vforkact | child_proc | <-- current_proc() | |
283 | * | | | |
284 | * `----------------' | |
285 | */ | |
286 | int | |
287 | vfork(proc_t parent_proc, __unused struct vfork_args *uap, int32_t *retval) | |
288 | { | |
289 | thread_t child_thread; | |
290 | int err; | |
291 | ||
3e170ce0 | 292 | if ((err = fork1(parent_proc, &child_thread, PROC_CREATE_VFORK, NULL)) != 0) { |
b0d623f7 A |
293 | retval[1] = 0; |
294 | } else { | |
39236c6e A |
295 | uthread_t ut = get_bsdthread_info(current_thread()); |
296 | proc_t child_proc = ut->uu_proc; | |
b0d623f7 A |
297 | |
298 | retval[0] = child_proc->p_pid; | |
299 | retval[1] = 1; /* flag child return for user space */ | |
300 | ||
301 | /* | |
302 | * Drop the signal lock on the child which was taken on our | |
303 | * behalf by forkproc()/cloneproc() to prevent signals being | |
304 | * received by the child in a partially constructed state. | |
305 | */ | |
306 | proc_signalend(child_proc, 0); | |
307 | proc_transend(child_proc, 0); | |
308 | ||
b0d623f7 A |
309 | proc_knote(parent_proc, NOTE_FORK | child_proc->p_pid); |
310 | DTRACE_PROC1(create, proc_t, child_proc); | |
39236c6e | 311 | ut->uu_flag &= ~UT_VFORKING; |
b0d623f7 A |
312 | } |
313 | ||
39236c6e | 314 | return (err); |
b0d623f7 A |
315 | } |
316 | ||
317 | ||
318 | /* | |
319 | * fork1 | |
320 | * | |
321 | * Description: common code used by all new process creation other than the | |
322 | * bootstrap of the initial process on the system | |
323 | * | |
324 | * Parameters: parent_proc parent process of the process being | |
325 | * child_threadp pointer to location to receive the | |
326 | * Mach thread_t of the child process | |
327 | * breated | |
328 | * kind kind of creation being requested | |
3e170ce0 A |
329 | * coalitions if spawn, the set of coalitions the |
330 | * child process should join, or NULL to | |
fe8ab488 A |
331 | * inherit the parent's. On non-spawns, |
332 | * this param is ignored and the child | |
3e170ce0 A |
333 | * always inherits the parent's |
334 | * coalitions. | |
b0d623f7 A |
335 | * |
336 | * Notes: Permissable values for 'kind': | |
337 | * | |
338 | * PROC_CREATE_FORK Create a complete process which will | |
339 | * return actively running in both the | |
340 | * parent and the child; the child copies | |
341 | * the parent address space. | |
342 | * PROC_CREATE_SPAWN Create a complete process which will | |
343 | * return actively running in the parent | |
344 | * only after returning actively running | |
345 | * in the child; the child address space | |
346 | * is newly created by an image activator, | |
347 | * after which the child is run. | |
348 | * PROC_CREATE_VFORK Creates a partial process which will | |
349 | * borrow the parent task, thread, and | |
350 | * uthread to return running in the child; | |
351 | * the child address space and other parts | |
352 | * are lazily created at execve() time, or | |
353 | * the child is terminated, and the parent | |
354 | * does not actively run until that | |
355 | * happens. | |
356 | * | |
357 | * At first it may seem strange that we return the child thread | |
358 | * address rather than process structure, since the process is | |
359 | * the only part guaranteed to be "new"; however, since we do | |
360 | * not actualy adjust other references between Mach and BSD (see | |
361 | * the block diagram above the implementation of vfork()), this | |
362 | * is the only method which guarantees us the ability to get | |
363 | * back to the other information. | |
1c79356b A |
364 | */ |
365 | int | |
3e170ce0 | 366 | fork1(proc_t parent_proc, thread_t *child_threadp, int kind, coalition_t *coalitions) |
1c79356b | 367 | { |
b0d623f7 A |
368 | thread_t parent_thread = (thread_t)current_thread(); |
369 | uthread_t parent_uthread = (uthread_t)get_bsdthread_info(parent_thread); | |
370 | proc_t child_proc = NULL; /* set in switch, but compiler... */ | |
371 | thread_t child_thread = NULL; | |
2d21ac55 | 372 | uid_t uid; |
0b4e3aa0 | 373 | int count; |
b0d623f7 A |
374 | int err = 0; |
375 | int spawn = 0; | |
91447636 | 376 | |
0b4e3aa0 A |
377 | /* |
378 | * Although process entries are dynamically created, we still keep | |
379 | * a global limit on the maximum number we will create. Don't allow | |
380 | * a nonprivileged user to use the last process; don't let root | |
381 | * exceed the limit. The variable nprocs is the current number of | |
382 | * processes, maxproc is the limit. | |
383 | */ | |
6d2010ae | 384 | uid = kauth_getruid(); |
2d21ac55 | 385 | proc_list_lock(); |
0b4e3aa0 | 386 | if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { |
2d21ac55 | 387 | proc_list_unlock(); |
0b4e3aa0 | 388 | tablefull("proc"); |
0b4e3aa0 A |
389 | return (EAGAIN); |
390 | } | |
2d21ac55 | 391 | proc_list_unlock(); |
0b4e3aa0 A |
392 | |
393 | /* | |
394 | * Increment the count of procs running with this uid. Don't allow | |
2d21ac55 A |
395 | * a nonprivileged user to exceed their current limit, which is |
396 | * always less than what an rlim_t can hold. | |
397 | * (locking protection is provided by list lock held in chgproccnt) | |
0b4e3aa0 A |
398 | */ |
399 | count = chgproccnt(uid, 1); | |
2d21ac55 | 400 | if (uid != 0 && |
b0d623f7 A |
401 | (rlim_t)count > parent_proc->p_rlimit[RLIMIT_NPROC].rlim_cur) { |
402 | err = EAGAIN; | |
403 | goto bad; | |
0b4e3aa0 | 404 | } |
2d21ac55 A |
405 | |
406 | #if CONFIG_MACF | |
407 | /* | |
408 | * Determine if MAC policies applied to the process will allow | |
b0d623f7 | 409 | * it to fork. This is an advisory-only check. |
2d21ac55 | 410 | */ |
b0d623f7 | 411 | err = mac_proc_check_fork(parent_proc); |
2d21ac55 | 412 | if (err != 0) { |
b0d623f7 | 413 | goto bad; |
2d21ac55 A |
414 | } |
415 | #endif | |
416 | ||
b0d623f7 A |
417 | switch(kind) { |
418 | case PROC_CREATE_VFORK: | |
419 | /* | |
420 | * Prevent a vfork while we are in vfork(); we should | |
421 | * also likely preventing a fork here as well, and this | |
422 | * check should then be outside the switch statement, | |
423 | * since the proc struct contents will copy from the | |
424 | * child and the tash/thread/uthread from the parent in | |
425 | * that case. We do not support vfork() in vfork() | |
426 | * because we don't have to; the same non-requirement | |
427 | * is true of both fork() and posix_spawn() and any | |
428 | * call other than execve() amd _exit(), but we've | |
429 | * been historically lenient, so we continue to be so | |
430 | * (for now). | |
431 | * | |
432 | * <rdar://6640521> Probably a source of random panics | |
433 | */ | |
434 | if (parent_uthread->uu_flag & UT_VFORK) { | |
435 | printf("fork1 called within vfork by %s\n", parent_proc->p_comm); | |
436 | err = EINVAL; | |
437 | goto bad; | |
438 | } | |
0b4e3aa0 | 439 | |
2d21ac55 | 440 | /* |
b0d623f7 A |
441 | * Flag us in progress; if we chose to support vfork() in |
442 | * vfork(), we would chain our parent at this point (in | |
443 | * effect, a stack push). We don't, since we actually want | |
444 | * to disallow everything not specified in the standard | |
2d21ac55 | 445 | */ |
b0d623f7 A |
446 | proc_vfork_begin(parent_proc); |
447 | ||
448 | /* The newly created process comes with signal lock held */ | |
449 | if ((child_proc = forkproc(parent_proc)) == NULL) { | |
450 | /* Failed to allocate new process */ | |
451 | proc_vfork_end(parent_proc); | |
452 | err = ENOMEM; | |
453 | goto bad; | |
454 | } | |
2d21ac55 | 455 | |
b0d623f7 | 456 | // XXX BEGIN: wants to move to be common code (and safe) |
2d21ac55 | 457 | #if CONFIG_MACF |
b0d623f7 A |
458 | /* |
459 | * allow policies to associate the credential/label that | |
460 | * we referenced from the parent ... with the child | |
461 | * JMM - this really isn't safe, as we can drop that | |
462 | * association without informing the policy in other | |
463 | * situations (keep long enough to get policies changed) | |
464 | */ | |
465 | mac_cred_label_associate_fork(child_proc->p_ucred, child_proc); | |
2d21ac55 A |
466 | #endif |
467 | ||
b0d623f7 A |
468 | /* |
469 | * Propogate change of PID - may get new cred if auditing. | |
470 | * | |
471 | * NOTE: This has no effect in the vfork case, since | |
472 | * child_proc->task != current_task(), but we duplicate it | |
473 | * because this is probably, ultimately, wrong, since we | |
474 | * will be running in the "child" which is the parent task | |
475 | * with the wrong token until we get to the execve() or | |
476 | * _exit() call; a lot of "undefined" can happen before | |
477 | * that. | |
478 | * | |
479 | * <rdar://6640530> disallow everything but exeve()/_exit()? | |
480 | */ | |
481 | set_security_token(child_proc); | |
2d21ac55 | 482 | |
b0d623f7 | 483 | AUDIT_ARG(pid, child_proc->p_pid); |
2d21ac55 | 484 | |
b0d623f7 | 485 | // XXX END: wants to move to be common code (and safe) |
2d21ac55 | 486 | |
b0d623f7 A |
487 | /* |
488 | * BORROW PARENT TASK, THREAD, UTHREAD FOR CHILD | |
489 | * | |
490 | * Note: this is where we would "push" state instead of setting | |
491 | * it for nested vfork() support (see proc_vfork_end() for | |
492 | * description if issues here). | |
493 | */ | |
494 | child_proc->task = parent_proc->task; | |
0b4e3aa0 | 495 | |
b0d623f7 A |
496 | child_proc->p_lflag |= P_LINVFORK; |
497 | child_proc->p_vforkact = parent_thread; | |
498 | child_proc->p_stat = SRUN; | |
0b4e3aa0 | 499 | |
39236c6e A |
500 | /* |
501 | * Until UT_VFORKING is cleared at the end of the vfork | |
502 | * syscall, the process identity of this thread is slightly | |
503 | * murky. | |
504 | * | |
505 | * As long as UT_VFORK and it's associated field (uu_proc) | |
506 | * is set, current_proc() will always return the child process. | |
507 | * | |
508 | * However dtrace_proc_selfpid() returns the parent pid to | |
509 | * ensure that e.g. the proc:::create probe actions accrue | |
510 | * to the parent. (Otherwise the child magically seems to | |
511 | * have created itself!) | |
512 | */ | |
513 | parent_uthread->uu_flag |= UT_VFORK | UT_VFORKING; | |
b0d623f7 A |
514 | parent_uthread->uu_proc = child_proc; |
515 | parent_uthread->uu_userstate = (void *)act_thread_csave(); | |
516 | parent_uthread->uu_vforkmask = parent_uthread->uu_sigmask; | |
0b4e3aa0 | 517 | |
b0d623f7 A |
518 | /* temporarily drop thread-set-id state */ |
519 | if (parent_uthread->uu_flag & UT_SETUID) { | |
520 | parent_uthread->uu_flag |= UT_WASSETUID; | |
521 | parent_uthread->uu_flag &= ~UT_SETUID; | |
522 | } | |
0b4e3aa0 | 523 | |
b0d623f7 A |
524 | /* blow thread state information */ |
525 | /* XXX is this actually necessary, given syscall return? */ | |
526 | thread_set_child(parent_thread, child_proc->p_pid); | |
527 | ||
528 | child_proc->p_acflag = AFORK; /* forked but not exec'ed */ | |
529 | ||
530 | /* | |
531 | * Preserve synchronization semantics of vfork. If | |
532 | * waiting for child to exec or exit, set P_PPWAIT | |
533 | * on child, and sleep on our proc (in case of exit). | |
534 | */ | |
535 | child_proc->p_lflag |= P_LPPWAIT; | |
536 | pinsertchild(parent_proc, child_proc); /* set visible */ | |
537 | ||
538 | break; | |
539 | ||
540 | case PROC_CREATE_SPAWN: | |
541 | /* | |
542 | * A spawned process differs from a forked process in that | |
543 | * the spawned process does not carry around the parents | |
544 | * baggage with regard to address space copying, dtrace, | |
545 | * and so on. | |
546 | */ | |
547 | spawn = 1; | |
548 | ||
549 | /* FALLSTHROUGH */ | |
550 | ||
551 | case PROC_CREATE_FORK: | |
552 | /* | |
553 | * When we clone the parent process, we are going to inherit | |
554 | * its task attributes and memory, since when we fork, we | |
555 | * will, in effect, create a duplicate of it, with only minor | |
556 | * differences. Contrarily, spawned processes do not inherit. | |
557 | */ | |
fe8ab488 | 558 | if ((child_thread = cloneproc(parent_proc->task, |
3e170ce0 | 559 | spawn ? coalitions : NULL, |
fe8ab488 A |
560 | parent_proc, |
561 | spawn ? FALSE : TRUE, | |
562 | FALSE)) == NULL) { | |
b0d623f7 A |
563 | /* Failed to create thread */ |
564 | err = EAGAIN; | |
565 | goto bad; | |
566 | } | |
567 | ||
568 | /* copy current thread state into the child thread (only for fork) */ | |
569 | if (!spawn) { | |
570 | thread_dup(child_thread); | |
571 | } | |
572 | ||
573 | /* child_proc = child_thread->task->proc; */ | |
574 | child_proc = (proc_t)(get_bsdtask_info(get_threadtask(child_thread))); | |
0b4e3aa0 | 575 | |
b0d623f7 A |
576 | // XXX BEGIN: wants to move to be common code (and safe) |
577 | #if CONFIG_MACF | |
578 | /* | |
579 | * allow policies to associate the credential/label that | |
580 | * we referenced from the parent ... with the child | |
581 | * JMM - this really isn't safe, as we can drop that | |
582 | * association without informing the policy in other | |
583 | * situations (keep long enough to get policies changed) | |
584 | */ | |
585 | mac_cred_label_associate_fork(child_proc->p_ucred, child_proc); | |
586 | #endif | |
587 | ||
588 | /* | |
589 | * Propogate change of PID - may get new cred if auditing. | |
590 | * | |
591 | * NOTE: This has no effect in the vfork case, since | |
592 | * child_proc->task != current_task(), but we duplicate it | |
593 | * because this is probably, ultimately, wrong, since we | |
594 | * will be running in the "child" which is the parent task | |
595 | * with the wrong token until we get to the execve() or | |
596 | * _exit() call; a lot of "undefined" can happen before | |
597 | * that. | |
598 | * | |
599 | * <rdar://6640530> disallow everything but exeve()/_exit()? | |
600 | */ | |
601 | set_security_token(child_proc); | |
0b4e3aa0 | 602 | |
b0d623f7 | 603 | AUDIT_ARG(pid, child_proc->p_pid); |
2d21ac55 | 604 | |
b0d623f7 A |
605 | // XXX END: wants to move to be common code (and safe) |
606 | ||
607 | /* | |
608 | * Blow thread state information; this is what gives the child | |
609 | * process its "return" value from a fork() call. | |
610 | * | |
611 | * Note: this should probably move to fork() proper, since it | |
612 | * is not relevent to spawn, and the value won't matter | |
613 | * until we resume the child there. If you are in here | |
614 | * refactoring code, consider doing this at the same time. | |
615 | */ | |
616 | thread_set_child(child_thread, child_proc->p_pid); | |
617 | ||
618 | child_proc->p_acflag = AFORK; /* forked but not exec'ed */ | |
619 | ||
620 | // <rdar://6598155> dtrace code cleanup needed | |
621 | #if CONFIG_DTRACE | |
622 | /* | |
623 | * This code applies to new processes who are copying the task | |
624 | * and thread state and address spaces of their parent process. | |
625 | */ | |
626 | if (!spawn) { | |
627 | // <rdar://6598155> call dtrace specific function here instead of all this... | |
628 | /* | |
629 | * APPLE NOTE: Solaris does a sprlock() and drops the | |
630 | * proc_lock here. We're cheating a bit and only taking | |
631 | * the p_dtrace_sprlock lock. A full sprlock would | |
632 | * task_suspend the parent. | |
633 | */ | |
634 | lck_mtx_lock(&parent_proc->p_dtrace_sprlock); | |
635 | ||
636 | /* | |
637 | * Remove all DTrace tracepoints from the child process. We | |
638 | * need to do this _before_ duplicating USDT providers since | |
639 | * any associated probes may be immediately enabled. | |
640 | */ | |
641 | if (parent_proc->p_dtrace_count > 0) { | |
642 | dtrace_fasttrap_fork(parent_proc, child_proc); | |
643 | } | |
644 | ||
645 | lck_mtx_unlock(&parent_proc->p_dtrace_sprlock); | |
646 | ||
647 | /* | |
648 | * Duplicate any lazy dof(s). This must be done while NOT | |
649 | * holding the parent sprlock! Lock ordering is | |
650 | * dtrace_dof_mode_lock, then sprlock. It is imperative we | |
651 | * always call dtrace_lazy_dofs_duplicate, rather than null | |
652 | * check and call if !NULL. If we NULL test, during lazy dof | |
653 | * faulting we can race with the faulting code and proceed | |
654 | * from here to beyond the helpers copy. The lazy dof | |
655 | * faulting will then fail to copy the helpers to the child | |
656 | * process. | |
657 | */ | |
658 | dtrace_lazy_dofs_duplicate(parent_proc, child_proc); | |
659 | ||
660 | /* | |
661 | * Duplicate any helper actions and providers. The SFORKING | |
662 | * we set above informs the code to enable USDT probes that | |
663 | * sprlock() may fail because the child is being forked. | |
664 | */ | |
665 | /* | |
666 | * APPLE NOTE: As best I can tell, Apple's sprlock() equivalent | |
667 | * never fails to find the child. We do not set SFORKING. | |
668 | */ | |
669 | if (parent_proc->p_dtrace_helpers != NULL && dtrace_helpers_fork) { | |
670 | (*dtrace_helpers_fork)(parent_proc, child_proc); | |
671 | } | |
672 | ||
673 | } | |
674 | #endif /* CONFIG_DTRACE */ | |
490019cf A |
675 | if (!spawn) { |
676 | /* | |
677 | * Of note, we need to initialize the bank context behind | |
678 | * the protection of the proc_trans lock to prevent a race with exit. | |
679 | */ | |
680 | task_bank_init(get_threadtask(child_thread)); | |
681 | } | |
b0d623f7 A |
682 | |
683 | break; | |
684 | ||
685 | default: | |
686 | panic("fork1 called with unknown kind %d", kind); | |
687 | break; | |
688 | } | |
689 | ||
690 | ||
691 | /* return the thread pointer to the caller */ | |
692 | *child_threadp = child_thread; | |
693 | ||
694 | bad: | |
695 | /* | |
696 | * In the error case, we return a 0 value for the returned pid (but | |
697 | * it is ignored in the trampoline due to the error return); this | |
698 | * is probably not necessary. | |
699 | */ | |
700 | if (err) { | |
701 | (void)chgproccnt(uid, -1); | |
702 | } | |
0b4e3aa0 | 703 | |
b0d623f7 | 704 | return (err); |
1c79356b A |
705 | } |
706 | ||
b0d623f7 | 707 | |
0b4e3aa0 | 708 | /* |
2d21ac55 A |
709 | * vfork_return |
710 | * | |
711 | * Description: "Return" to parent vfork thread() following execve/_exit; | |
712 | * this is done by reassociating the parent process structure | |
713 | * with the task, thread, and uthread. | |
714 | * | |
316670eb A |
715 | * Refer to the ASCII art above vfork() to figure out the |
716 | * state we're undoing. | |
717 | * | |
b0d623f7 | 718 | * Parameters: child_proc Child process |
2d21ac55 A |
719 | * retval System call return value array |
720 | * rval Return value to present to parent | |
721 | * | |
722 | * Returns: void | |
723 | * | |
316670eb A |
724 | * Notes: The caller resumes or exits the parent, as appropriate, after |
725 | * calling this function. | |
0b4e3aa0 A |
726 | */ |
727 | void | |
b0d623f7 | 728 | vfork_return(proc_t child_proc, int32_t *retval, int rval) |
0b4e3aa0 | 729 | { |
316670eb A |
730 | task_t parent_task = get_threadtask(child_proc->p_vforkact); |
731 | proc_t parent_proc = get_bsdtask_info(parent_task); | |
732 | thread_t th = current_thread(); | |
733 | uthread_t uth = get_bsdthread_info(th); | |
0b4e3aa0 | 734 | |
316670eb | 735 | act_thread_catt(uth->uu_userstate); |
0b4e3aa0 | 736 | |
316670eb | 737 | /* clear vfork state in parent proc structure */ |
b0d623f7 A |
738 | proc_vfork_end(parent_proc); |
739 | ||
740 | /* REPATRIATE PARENT TASK, THREAD, UTHREAD */ | |
316670eb A |
741 | uth->uu_userstate = 0; |
742 | uth->uu_flag &= ~UT_VFORK; | |
91447636 | 743 | /* restore thread-set-id state */ |
316670eb A |
744 | if (uth->uu_flag & UT_WASSETUID) { |
745 | uth->uu_flag |= UT_SETUID; | |
746 | uth->uu_flag &= UT_WASSETUID; | |
91447636 | 747 | } |
316670eb A |
748 | uth->uu_proc = 0; |
749 | uth->uu_sigmask = uth->uu_vforkmask; | |
750 | ||
751 | proc_lock(child_proc); | |
752 | child_proc->p_lflag &= ~P_LINVFORK; | |
753 | child_proc->p_vforkact = 0; | |
754 | proc_unlock(child_proc); | |
0b4e3aa0 | 755 | |
316670eb | 756 | thread_set_parent(th, rval); |
0b4e3aa0 A |
757 | |
758 | if (retval) { | |
2d21ac55 | 759 | retval[0] = rval; |
0b4e3aa0 A |
760 | retval[1] = 0; /* mark parent */ |
761 | } | |
0b4e3aa0 A |
762 | } |
763 | ||
2d21ac55 A |
764 | |
765 | /* | |
766 | * fork_create_child | |
767 | * | |
768 | * Description: Common operations associated with the creation of a child | |
769 | * process | |
770 | * | |
771 | * Parameters: parent_task parent task | |
3e170ce0 | 772 | * parent_coalitions parent's set of coalitions |
b0d623f7 | 773 | * child_proc child process |
2d21ac55 A |
774 | * inherit_memory TRUE, if the parents address space is |
775 | * to be inherited by the child | |
776 | * is64bit TRUE, if the child being created will | |
777 | * be associated with a 64 bit process | |
778 | * rather than a 32 bit process | |
779 | * | |
780 | * Note: This code is called in the fork() case, from the execve() call | |
781 | * graph, if implementing an execve() following a vfork(), from | |
782 | * the posix_spawn() call graph (which implicitly includes a | |
783 | * vfork() equivalent call, and in the system bootstrap case. | |
784 | * | |
785 | * It creates a new task and thread (and as a side effect of the | |
3e170ce0 | 786 | * thread creation, a uthread) in the parent coalition set, which is |
fe8ab488 A |
787 | * then associated with the process 'child'. If the parent |
788 | * process address space is to be inherited, then a flag | |
789 | * indicates that the newly created task should inherit this from | |
790 | * the child task. | |
2d21ac55 A |
791 | * |
792 | * As a special concession to bootstrapping the initial process | |
793 | * in the system, it's possible for 'parent_task' to be TASK_NULL; | |
794 | * in this case, 'inherit_memory' MUST be FALSE. | |
795 | */ | |
91447636 | 796 | thread_t |
3e170ce0 | 797 | fork_create_child(task_t parent_task, coalition_t *parent_coalitions, proc_t child_proc, int inherit_memory, int is64bit) |
0b4e3aa0 | 798 | { |
2d21ac55 A |
799 | thread_t child_thread = NULL; |
800 | task_t child_task; | |
801 | kern_return_t result; | |
802 | ||
803 | /* Create a new task for the child process */ | |
804 | result = task_create_internal(parent_task, | |
3e170ce0 | 805 | parent_coalitions, |
2d21ac55 A |
806 | inherit_memory, |
807 | is64bit, | |
808 | &child_task); | |
809 | if (result != KERN_SUCCESS) { | |
39236c6e A |
810 | printf("%s: task_create_internal failed. Code: %d\n", |
811 | __func__, result); | |
2d21ac55 A |
812 | goto bad; |
813 | } | |
0b4e3aa0 | 814 | |
b0d623f7 A |
815 | /* Set the child process task to the new task */ |
816 | child_proc->task = child_task; | |
2d21ac55 | 817 | |
b0d623f7 A |
818 | /* Set child task process to child proc */ |
819 | set_bsdtask_info(child_task, child_proc); | |
2d21ac55 A |
820 | |
821 | /* Propagate CPU limit timer from parent */ | |
b0d623f7 | 822 | if (timerisset(&child_proc->p_rlim_cpu)) |
2d21ac55 A |
823 | task_vtimer_set(child_task, TASK_VTIMER_RLIM); |
824 | ||
825 | /* Set/clear 64 bit vm_map flag */ | |
826 | if (is64bit) | |
827 | vm_map_set_64bit(get_task_map(child_task)); | |
0b4e3aa0 | 828 | else |
2d21ac55 A |
829 | vm_map_set_32bit(get_task_map(child_task)); |
830 | ||
b0d623f7 A |
831 | /* |
832 | * Set child process BSD visible scheduler priority if nice value | |
833 | * inherited from parent | |
834 | */ | |
835 | if (child_proc->p_nice != 0) | |
836 | resetpriority(child_proc); | |
0b4e3aa0 | 837 | |
2d21ac55 | 838 | /* Create a new thread for the child process */ |
3e170ce0 | 839 | result = thread_create_with_continuation(child_task, &child_thread, (thread_continue_t)proc_wait_to_return); |
2d21ac55 | 840 | if (result != KERN_SUCCESS) { |
39236c6e A |
841 | printf("%s: thread_create failed. Code: %d\n", |
842 | __func__, result); | |
2d21ac55 A |
843 | task_deallocate(child_task); |
844 | child_task = NULL; | |
845 | } | |
4b17d6b6 A |
846 | |
847 | /* | |
39236c6e A |
848 | * Tag thread as being the first thread in its task. |
849 | */ | |
4b17d6b6 A |
850 | thread_set_tag(child_thread, THREAD_TAG_MAINTHREAD); |
851 | ||
2d21ac55 A |
852 | bad: |
853 | thread_yield_internal(1); | |
854 | ||
855 | return(child_thread); | |
0b4e3aa0 A |
856 | } |
857 | ||
858 | ||
2d21ac55 A |
859 | /* |
860 | * fork | |
861 | * | |
862 | * Description: fork system call. | |
863 | * | |
864 | * Parameters: parent Parent process to fork | |
865 | * uap (void) [unused] | |
866 | * retval Return value | |
867 | * | |
868 | * Returns: 0 Success | |
869 | * EAGAIN Resource unavailable, try again | |
b0d623f7 A |
870 | * |
871 | * Notes: Attempts to create a new child process which inherits state | |
872 | * from the parent process. If successful, the call returns | |
873 | * having created an initially suspended child process with an | |
874 | * extra Mach task and thread reference, for which the thread | |
875 | * is initially suspended. Until we resume the child process, | |
876 | * it is not yet running. | |
877 | * | |
878 | * The return information to the child is contained in the | |
879 | * thread state structure of the new child, and does not | |
880 | * become visible to the child through a normal return process, | |
881 | * since it never made the call into the kernel itself in the | |
882 | * first place. | |
883 | * | |
884 | * After resuming the thread, this function returns directly to | |
885 | * the parent process which invoked the fork() system call. | |
886 | * | |
887 | * Important: The child thread_resume occurs before the parent returns; | |
888 | * depending on scheduling latency, this means that it is not | |
889 | * deterministic as to whether the parent or child is scheduled | |
890 | * to run first. It is entirely possible that the child could | |
891 | * run to completion prior to the parent running. | |
2d21ac55 A |
892 | */ |
893 | int | |
b0d623f7 | 894 | fork(proc_t parent_proc, __unused struct fork_args *uap, int32_t *retval) |
2d21ac55 | 895 | { |
b0d623f7 | 896 | thread_t child_thread; |
2d21ac55 | 897 | int err; |
1c79356b | 898 | |
b0d623f7 | 899 | retval[1] = 0; /* flag parent return for user space */ |
1c79356b | 900 | |
3e170ce0 | 901 | if ((err = fork1(parent_proc, &child_thread, PROC_CREATE_FORK, NULL)) == 0) { |
b0d623f7 A |
902 | task_t child_task; |
903 | proc_t child_proc; | |
2d21ac55 | 904 | |
b0d623f7 A |
905 | /* Return to the parent */ |
906 | child_proc = (proc_t)get_bsdthreadtask_info(child_thread); | |
907 | retval[0] = child_proc->p_pid; | |
2d21ac55 | 908 | |
b0d623f7 A |
909 | /* |
910 | * Drop the signal lock on the child which was taken on our | |
911 | * behalf by forkproc()/cloneproc() to prevent signals being | |
912 | * received by the child in a partially constructed state. | |
913 | */ | |
914 | proc_signalend(child_proc, 0); | |
915 | proc_transend(child_proc, 0); | |
2d21ac55 | 916 | |
b0d623f7 A |
917 | /* flag the fork has occurred */ |
918 | proc_knote(parent_proc, NOTE_FORK | child_proc->p_pid); | |
919 | DTRACE_PROC1(create, proc_t, child_proc); | |
2d21ac55 | 920 | |
fe8ab488 A |
921 | #if CONFIG_DTRACE |
922 | if ((dtrace_proc_waitfor_hook = dtrace_proc_waitfor_exec_ptr) != NULL) | |
923 | (*dtrace_proc_waitfor_hook)(child_proc); | |
924 | #endif | |
925 | ||
b0d623f7 | 926 | /* "Return" to the child */ |
3e170ce0 | 927 | proc_clear_return_wait(child_proc, child_thread); |
2d21ac55 | 928 | |
b0d623f7 A |
929 | /* drop the extra references we got during the creation */ |
930 | if ((child_task = (task_t)get_threadtask(child_thread)) != NULL) { | |
931 | task_deallocate(child_task); | |
932 | } | |
933 | thread_deallocate(child_thread); | |
2d21ac55 A |
934 | } |
935 | ||
b0d623f7 | 936 | return(err); |
1c79356b A |
937 | } |
938 | ||
b0d623f7 | 939 | |
1c79356b | 940 | /* |
2d21ac55 A |
941 | * cloneproc |
942 | * | |
943 | * Description: Create a new process from a specified process. | |
944 | * | |
b0d623f7 A |
945 | * Parameters: parent_task The parent task to be cloned, or |
946 | * TASK_NULL is task characteristics | |
947 | * are not to be inherited | |
948 | * be cloned, or TASK_NULL if the new | |
949 | * task is not to inherit the VM | |
950 | * characteristics of the parent | |
951 | * parent_proc The parent process to be cloned | |
952 | * inherit_memory True if the child is to inherit | |
953 | * memory from the parent; if this is | |
954 | * non-NULL, then the parent_task must | |
955 | * also be non-NULL | |
39236c6e A |
956 | * memstat_internal Whether to track the process in the |
957 | * jetsam priority list (if configured) | |
1c79356b | 958 | * |
2d21ac55 A |
959 | * Returns: !NULL pointer to new child thread |
960 | * NULL Failure (unspecified) | |
961 | * | |
962 | * Note: On return newly created child process has signal lock held | |
963 | * to block delivery of signal to it if called with lock set. | |
964 | * fork() code needs to explicity remove this lock before | |
965 | * signals can be delivered | |
966 | * | |
967 | * In the case of bootstrap, this function can be called from | |
968 | * bsd_utaskbootstrap() in order to bootstrap the first process; | |
969 | * the net effect is to provide a uthread structure for the | |
b0d623f7 A |
970 | * kernel process associated with the kernel task. |
971 | * | |
972 | * XXX: Tristating using the value parent_task as the major key | |
973 | * and inherit_memory as the minor key is something we should | |
974 | * refactor later; we owe the current semantics, ultimately, | |
975 | * to the semantics of task_create_internal. For now, we will | |
976 | * live with this being somewhat awkward. | |
1c79356b | 977 | */ |
91447636 | 978 | thread_t |
3e170ce0 | 979 | cloneproc(task_t parent_task, coalition_t *parent_coalitions, proc_t parent_proc, int inherit_memory, int memstat_internal) |
0b4e3aa0 | 980 | { |
39236c6e A |
981 | #if !CONFIG_MEMORYSTATUS |
982 | #pragma unused(memstat_internal) | |
983 | #endif | |
b0d623f7 A |
984 | task_t child_task; |
985 | proc_t child_proc; | |
986 | thread_t child_thread = NULL; | |
0b4e3aa0 | 987 | |
b0d623f7 | 988 | if ((child_proc = forkproc(parent_proc)) == NULL) { |
2d21ac55 A |
989 | /* Failed to allocate new process */ |
990 | goto bad; | |
991 | } | |
9bccf70c | 992 | |
3e170ce0 | 993 | child_thread = fork_create_child(parent_task, parent_coalitions, child_proc, inherit_memory, (parent_task == TASK_NULL) ? FALSE : (parent_proc->p_flag & P_LP64)); |
b0d623f7 A |
994 | |
995 | if (child_thread == NULL) { | |
2d21ac55 A |
996 | /* |
997 | * Failed to create thread; now we must deconstruct the new | |
998 | * process previously obtained from forkproc(). | |
999 | */ | |
b0d623f7 | 1000 | forkproc_free(child_proc); |
2d21ac55 A |
1001 | goto bad; |
1002 | } | |
9bccf70c | 1003 | |
b0d623f7 A |
1004 | child_task = get_threadtask(child_thread); |
1005 | if (parent_proc->p_flag & P_LP64) { | |
1006 | task_set_64bit(child_task, TRUE); | |
1007 | OSBitOrAtomic(P_LP64, (UInt32 *)&child_proc->p_flag); | |
b0d623f7 A |
1008 | } else { |
1009 | task_set_64bit(child_task, FALSE); | |
1010 | OSBitAndAtomic(~((uint32_t)P_LP64), (UInt32 *)&child_proc->p_flag); | |
1011 | } | |
1012 | ||
39236c6e A |
1013 | #if CONFIG_MEMORYSTATUS |
1014 | if (memstat_internal) { | |
1015 | proc_list_lock(); | |
1016 | child_proc->p_memstat_state |= P_MEMSTAT_INTERNAL; | |
1017 | proc_list_unlock(); | |
1018 | } | |
1019 | #endif | |
1020 | ||
2d21ac55 | 1021 | /* make child visible */ |
b0d623f7 | 1022 | pinsertchild(parent_proc, child_proc); |
0b4e3aa0 | 1023 | |
0b4e3aa0 A |
1024 | /* |
1025 | * Make child runnable, set start time. | |
1026 | */ | |
b0d623f7 | 1027 | child_proc->p_stat = SRUN; |
2d21ac55 | 1028 | bad: |
b0d623f7 | 1029 | return(child_thread); |
0b4e3aa0 A |
1030 | } |
1031 | ||
b0d623f7 | 1032 | |
2d21ac55 A |
1033 | /* |
1034 | * Destroy a process structure that resulted from a call to forkproc(), but | |
1035 | * which must be returned to the system because of a subsequent failure | |
1036 | * preventing it from becoming active. | |
1037 | * | |
1038 | * Parameters: p The incomplete process from forkproc() | |
2d21ac55 A |
1039 | * |
1040 | * Returns: (void) | |
1041 | * | |
1042 | * Note: This function should only be used in an error handler following | |
b0d623f7 | 1043 | * a call to forkproc(). |
2d21ac55 A |
1044 | * |
1045 | * Operations occur in reverse order of those in forkproc(). | |
1046 | */ | |
1047 | void | |
b0d623f7 | 1048 | forkproc_free(proc_t p) |
1c79356b | 1049 | { |
ecc0ceb4 A |
1050 | #if CONFIG_PERSONAS |
1051 | persona_proc_drop(p); | |
1052 | #endif /* CONFIG_PERSONAS */ | |
1053 | ||
1054 | #if PSYNCH | |
1055 | pth_proc_hashdelete(p); | |
1056 | #endif /* PSYNCH */ | |
1057 | ||
1058 | workqueue_destroy_lock(p); | |
2d21ac55 | 1059 | |
b0d623f7 A |
1060 | /* We held signal and a transition locks; drop them */ |
1061 | proc_signalend(p, 0); | |
1062 | proc_transend(p, 0); | |
1c79356b A |
1063 | |
1064 | /* | |
2d21ac55 A |
1065 | * If we have our own copy of the resource limits structure, we |
1066 | * need to free it. If it's a shared copy, we need to drop our | |
1067 | * reference on it. | |
1c79356b | 1068 | */ |
2d21ac55 A |
1069 | proc_limitdrop(p, 0); |
1070 | p->p_limit = NULL; | |
1071 | ||
1072 | #if SYSV_SHM | |
1073 | /* Need to drop references to the shared memory segment(s), if any */ | |
1074 | if (p->vm_shm) { | |
1075 | /* | |
1076 | * Use shmexec(): we have no address space, so no mappings | |
1077 | * | |
1078 | * XXX Yes, the routine is badly named. | |
1079 | */ | |
1080 | shmexec(p); | |
1081 | } | |
1082 | #endif | |
1083 | ||
1084 | /* Need to undo the effects of the fdcopy(), if any */ | |
1085 | fdfree(p); | |
1086 | ||
1087 | /* | |
1088 | * Drop the reference on a text vnode pointer, if any | |
1089 | * XXX This code is broken in forkproc(); see <rdar://4256419>; | |
1090 | * XXX if anyone ever uses this field, we will be extremely unhappy. | |
1091 | */ | |
1092 | if (p->p_textvp) { | |
1093 | vnode_rele(p->p_textvp); | |
1094 | p->p_textvp = NULL; | |
1095 | } | |
1096 | ||
1097 | /* Stop the profiling clock */ | |
1098 | stopprofclock(p); | |
1099 | ||
6d2010ae A |
1100 | /* Update the audit session proc count */ |
1101 | AUDIT_SESSION_PROCEXIT(p); | |
1102 | ||
ecc0ceb4 A |
1103 | #if CONFIG_FINE_LOCK_GROUPS |
1104 | lck_mtx_destroy(&p->p_mlock, proc_mlock_grp); | |
1105 | lck_mtx_destroy(&p->p_fdmlock, proc_fdmlock_grp); | |
1106 | lck_mtx_destroy(&p->p_ucred_mlock, proc_ucred_mlock_grp); | |
1107 | #if CONFIG_DTRACE | |
1108 | lck_mtx_destroy(&p->p_dtrace_sprlock, proc_lck_grp); | |
1109 | #endif | |
1110 | lck_spin_destroy(&p->p_slock, proc_slock_grp); | |
1111 | #else /* CONFIG_FINE_LOCK_GROUPS */ | |
1112 | lck_mtx_destroy(&p->p_mlock, proc_lck_grp); | |
1113 | lck_mtx_destroy(&p->p_fdmlock, proc_lck_grp); | |
1114 | lck_mtx_destroy(&p->p_ucred_mlock, proc_lck_grp); | |
1115 | #if CONFIG_DTRACE | |
1116 | lck_mtx_destroy(&p->p_dtrace_sprlock, proc_lck_grp); | |
1117 | #endif | |
1118 | lck_spin_destroy(&p->p_slock, proc_lck_grp); | |
1119 | #endif /* CONFIG_FINE_LOCK_GROUPS */ | |
1120 | ||
2d21ac55 A |
1121 | /* Release the credential reference */ |
1122 | kauth_cred_unref(&p->p_ucred); | |
1123 | ||
1124 | proc_list_lock(); | |
1125 | /* Decrement the count of processes in the system */ | |
1126 | nprocs--; | |
ecc0ceb4 A |
1127 | |
1128 | /* Take it out of process hash */ | |
1129 | LIST_REMOVE(p, p_hash); | |
1130 | ||
2d21ac55 A |
1131 | proc_list_unlock(); |
1132 | ||
1133 | thread_call_free(p->p_rcall); | |
1134 | ||
1135 | /* Free allocated memory */ | |
1136 | FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS); | |
1137 | FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS); | |
1138 | proc_checkdeadrefs(p); | |
1139 | FREE_ZONE(p, sizeof *p, M_PROC); | |
1140 | } | |
1141 | ||
1142 | ||
1143 | /* | |
1144 | * forkproc | |
1145 | * | |
1146 | * Description: Create a new process structure, given a parent process | |
1147 | * structure. | |
1148 | * | |
b0d623f7 | 1149 | * Parameters: parent_proc The parent process |
2d21ac55 A |
1150 | * |
1151 | * Returns: !NULL The new process structure | |
1152 | * NULL Error (insufficient free memory) | |
1153 | * | |
1154 | * Note: When successful, the newly created process structure is | |
1155 | * partially initialized; if a caller needs to deconstruct the | |
1156 | * returned structure, they must call forkproc_free() to do so. | |
1157 | */ | |
1158 | proc_t | |
b0d623f7 | 1159 | forkproc(proc_t parent_proc) |
2d21ac55 | 1160 | { |
b0d623f7 | 1161 | proc_t child_proc; /* Our new process */ |
593a1d5f | 1162 | static int nextpid = 0, pidwrap = 0, nextpidversion = 0; |
6d2010ae | 1163 | static uint64_t nextuniqueid = 0; |
2d21ac55 A |
1164 | int error = 0; |
1165 | struct session *sessp; | |
b0d623f7 | 1166 | uthread_t parent_uthread = (uthread_t)get_bsdthread_info(current_thread()); |
2d21ac55 | 1167 | |
b0d623f7 A |
1168 | MALLOC_ZONE(child_proc, proc_t , sizeof *child_proc, M_PROC, M_WAITOK); |
1169 | if (child_proc == NULL) { | |
2d21ac55 A |
1170 | printf("forkproc: M_PROC zone exhausted\n"); |
1171 | goto bad; | |
1172 | } | |
1173 | /* zero it out as we need to insert in hash */ | |
b0d623f7 | 1174 | bzero(child_proc, sizeof *child_proc); |
2d21ac55 | 1175 | |
b0d623f7 A |
1176 | MALLOC_ZONE(child_proc->p_stats, struct pstats *, |
1177 | sizeof *child_proc->p_stats, M_PSTATS, M_WAITOK); | |
1178 | if (child_proc->p_stats == NULL) { | |
2d21ac55 | 1179 | printf("forkproc: M_SUBPROC zone exhausted (p_stats)\n"); |
b0d623f7 A |
1180 | FREE_ZONE(child_proc, sizeof *child_proc, M_PROC); |
1181 | child_proc = NULL; | |
2d21ac55 A |
1182 | goto bad; |
1183 | } | |
b0d623f7 A |
1184 | MALLOC_ZONE(child_proc->p_sigacts, struct sigacts *, |
1185 | sizeof *child_proc->p_sigacts, M_SIGACTS, M_WAITOK); | |
1186 | if (child_proc->p_sigacts == NULL) { | |
2d21ac55 | 1187 | printf("forkproc: M_SUBPROC zone exhausted (p_sigacts)\n"); |
b0d623f7 A |
1188 | FREE_ZONE(child_proc->p_stats, sizeof *child_proc->p_stats, M_PSTATS); |
1189 | FREE_ZONE(child_proc, sizeof *child_proc, M_PROC); | |
1190 | child_proc = NULL; | |
2d21ac55 A |
1191 | goto bad; |
1192 | } | |
b0d623f7 A |
1193 | |
1194 | /* allocate a callout for use by interval timers */ | |
1195 | child_proc->p_rcall = thread_call_allocate((thread_call_func_t)realitexpire, child_proc); | |
1196 | if (child_proc->p_rcall == NULL) { | |
1197 | FREE_ZONE(child_proc->p_sigacts, sizeof *child_proc->p_sigacts, M_SIGACTS); | |
1198 | FREE_ZONE(child_proc->p_stats, sizeof *child_proc->p_stats, M_PSTATS); | |
1199 | FREE_ZONE(child_proc, sizeof *child_proc, M_PROC); | |
1200 | child_proc = NULL; | |
2d21ac55 A |
1201 | goto bad; |
1202 | } | |
1203 | ||
1204 | ||
1205 | /* | |
1206 | * Find an unused PID. | |
1207 | */ | |
1208 | ||
1209 | proc_list_lock(); | |
1210 | ||
1c79356b A |
1211 | nextpid++; |
1212 | retry: | |
1213 | /* | |
1214 | * If the process ID prototype has wrapped around, | |
1215 | * restart somewhat above 0, as the low-numbered procs | |
1216 | * tend to include daemons that don't exit. | |
1217 | */ | |
1218 | if (nextpid >= PID_MAX) { | |
1219 | nextpid = 100; | |
2d21ac55 | 1220 | pidwrap = 1; |
1c79356b | 1221 | } |
2d21ac55 | 1222 | if (pidwrap != 0) { |
1c79356b | 1223 | |
2d21ac55 A |
1224 | /* if the pid stays in hash both for zombie and runniing state */ |
1225 | if (pfind_locked(nextpid) != PROC_NULL) { | |
1226 | nextpid++; | |
1227 | goto retry; | |
1c79356b | 1228 | } |
1c79356b | 1229 | |
2d21ac55 A |
1230 | if (pgfind_internal(nextpid) != PGRP_NULL) { |
1231 | nextpid++; | |
1232 | goto retry; | |
1233 | } | |
1234 | if (session_find_internal(nextpid) != SESSION_NULL) { | |
1235 | nextpid++; | |
1236 | goto retry; | |
1237 | } | |
1238 | } | |
1c79356b | 1239 | nprocs++; |
b0d623f7 | 1240 | child_proc->p_pid = nextpid; |
3e170ce0 | 1241 | child_proc->p_responsible_pid = nextpid; /* initially responsible for self */ |
b0d623f7 | 1242 | child_proc->p_idversion = nextpidversion++; |
6d2010ae A |
1243 | /* kernel process is handcrafted and not from fork, so start from 1 */ |
1244 | child_proc->p_uniqueid = ++nextuniqueid; | |
2d21ac55 | 1245 | #if 1 |
b0d623f7 A |
1246 | if (child_proc->p_pid != 0) { |
1247 | if (pfind_locked(child_proc->p_pid) != PROC_NULL) | |
2d21ac55 A |
1248 | panic("proc in the list already\n"); |
1249 | } | |
1250 | #endif | |
1251 | /* Insert in the hash */ | |
b0d623f7 A |
1252 | child_proc->p_listflag |= (P_LIST_INHASH | P_LIST_INCREATE); |
1253 | LIST_INSERT_HEAD(PIDHASH(child_proc->p_pid), child_proc, p_hash); | |
2d21ac55 A |
1254 | proc_list_unlock(); |
1255 | ||
1256 | ||
1257 | /* | |
1258 | * We've identified the PID we are going to use; initialize the new | |
1259 | * process structure. | |
1260 | */ | |
b0d623f7 A |
1261 | child_proc->p_stat = SIDL; |
1262 | child_proc->p_pgrpid = PGRPID_DEAD; | |
1c79356b A |
1263 | |
1264 | /* | |
b0d623f7 A |
1265 | * The zero'ing of the proc was at the allocation time due to need |
1266 | * for insertion to hash. Copy the section that is to be copied | |
1267 | * directly from the parent. | |
1c79356b | 1268 | */ |
b0d623f7 A |
1269 | bcopy(&parent_proc->p_startcopy, &child_proc->p_startcopy, |
1270 | (unsigned) ((caddr_t)&child_proc->p_endcopy - (caddr_t)&child_proc->p_startcopy)); | |
1c79356b | 1271 | |
55e303ae | 1272 | /* |
91447636 | 1273 | * Some flags are inherited from the parent. |
1c79356b A |
1274 | * Duplicate sub-structures as needed. |
1275 | * Increase reference counts on shared objects. | |
1276 | * The p_stats and p_sigacts substructs are set in vm_fork. | |
1277 | */ | |
3e170ce0 | 1278 | child_proc->p_flag = (parent_proc->p_flag & (P_LP64 | P_DISABLE_ASLR | P_DELAYIDLESLEEP | P_SUGID)); |
b0d623f7 A |
1279 | if (parent_proc->p_flag & P_PROFIL) |
1280 | startprofclock(child_proc); | |
316670eb | 1281 | |
39236c6e | 1282 | child_proc->p_vfs_iopolicy = (parent_proc->p_vfs_iopolicy & (P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY)); |
316670eb | 1283 | |
91447636 A |
1284 | /* |
1285 | * Note that if the current thread has an assumed identity, this | |
1286 | * credential will be granted to the new process. | |
1287 | */ | |
b0d623f7 | 1288 | child_proc->p_ucred = kauth_cred_get_with_ref(); |
6d2010ae A |
1289 | /* update cred on proc */ |
1290 | PROC_UPDATE_CREDS_ONPROC(child_proc); | |
1291 | /* update audit session proc count */ | |
1292 | AUDIT_SESSION_PROCNEW(child_proc); | |
91447636 | 1293 | |
6d2010ae | 1294 | #if CONFIG_FINE_LOCK_GROUPS |
b0d623f7 A |
1295 | lck_mtx_init(&child_proc->p_mlock, proc_mlock_grp, proc_lck_attr); |
1296 | lck_mtx_init(&child_proc->p_fdmlock, proc_fdmlock_grp, proc_lck_attr); | |
4bd07ac2 | 1297 | lck_mtx_init(&child_proc->p_ucred_mlock, proc_ucred_mlock_grp, proc_lck_attr); |
b0d623f7 A |
1298 | #if CONFIG_DTRACE |
1299 | lck_mtx_init(&child_proc->p_dtrace_sprlock, proc_lck_grp, proc_lck_attr); | |
1300 | #endif | |
1301 | lck_spin_init(&child_proc->p_slock, proc_slock_grp, proc_lck_attr); | |
6d2010ae A |
1302 | #else /* !CONFIG_FINE_LOCK_GROUPS */ |
1303 | lck_mtx_init(&child_proc->p_mlock, proc_lck_grp, proc_lck_attr); | |
1304 | lck_mtx_init(&child_proc->p_fdmlock, proc_lck_grp, proc_lck_attr); | |
4bd07ac2 | 1305 | lck_mtx_init(&child_proc->p_ucred_mlock, proc_lck_grp, proc_lck_attr); |
6d2010ae A |
1306 | #if CONFIG_DTRACE |
1307 | lck_mtx_init(&child_proc->p_dtrace_sprlock, proc_lck_grp, proc_lck_attr); | |
1308 | #endif | |
1309 | lck_spin_init(&child_proc->p_slock, proc_lck_grp, proc_lck_attr); | |
1310 | #endif /* !CONFIG_FINE_LOCK_GROUPS */ | |
b0d623f7 | 1311 | klist_init(&child_proc->p_klist); |
2d21ac55 | 1312 | |
b0d623f7 | 1313 | if (child_proc->p_textvp != NULLVP) { |
2d21ac55 A |
1314 | /* bump references to the text vnode */ |
1315 | /* Need to hold iocount across the ref call */ | |
b0d623f7 A |
1316 | if (vnode_getwithref(child_proc->p_textvp) == 0) { |
1317 | error = vnode_ref(child_proc->p_textvp); | |
1318 | vnode_put(child_proc->p_textvp); | |
2d21ac55 | 1319 | if (error != 0) |
b0d623f7 | 1320 | child_proc->p_textvp = NULLVP; |
2d21ac55 | 1321 | } |
91447636 | 1322 | } |
2d21ac55 | 1323 | |
b0d623f7 A |
1324 | /* |
1325 | * Copy the parents per process open file table to the child; if | |
1326 | * there is a per-thread current working directory, set the childs | |
1327 | * per-process current working directory to that instead of the | |
1328 | * parents. | |
1329 | * | |
1330 | * XXX may fail to copy descriptors to child | |
1331 | */ | |
1332 | child_proc->p_fd = fdcopy(parent_proc, parent_uthread->uu_cdir); | |
91447636 | 1333 | |
2d21ac55 | 1334 | #if SYSV_SHM |
b0d623f7 | 1335 | if (parent_proc->vm_shm) { |
91447636 | 1336 | /* XXX may fail to attach shm to child */ |
b0d623f7 | 1337 | (void)shmfork(parent_proc, child_proc); |
1c79356b | 1338 | } |
2d21ac55 | 1339 | #endif |
1c79356b | 1340 | /* |
2d21ac55 | 1341 | * inherit the limit structure to child |
1c79356b | 1342 | */ |
b0d623f7 | 1343 | proc_limitfork(parent_proc, child_proc); |
2d21ac55 | 1344 | |
b0d623f7 A |
1345 | if (child_proc->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { |
1346 | uint64_t rlim_cur = child_proc->p_limit->pl_rlimit[RLIMIT_CPU].rlim_cur; | |
1347 | child_proc->p_rlim_cpu.tv_sec = (rlim_cur > __INT_MAX__) ? __INT_MAX__ : rlim_cur; | |
1c79356b A |
1348 | } |
1349 | ||
b0d623f7 A |
1350 | /* Intialize new process stats, including start time */ |
1351 | /* <rdar://6640543> non-zeroed portion contains garbage AFAICT */ | |
39236c6e A |
1352 | bzero(child_proc->p_stats, sizeof(*child_proc->p_stats)); |
1353 | microtime_with_abstime(&child_proc->p_start, &child_proc->p_stats->ps_start); | |
b0d623f7 A |
1354 | |
1355 | if (parent_proc->p_sigacts != NULL) | |
1356 | (void)memcpy(child_proc->p_sigacts, | |
1357 | parent_proc->p_sigacts, sizeof *child_proc->p_sigacts); | |
1c79356b | 1358 | else |
b0d623f7 | 1359 | (void)memset(child_proc->p_sigacts, 0, sizeof *child_proc->p_sigacts); |
1c79356b | 1360 | |
b0d623f7 A |
1361 | sessp = proc_session(parent_proc); |
1362 | if (sessp->s_ttyvp != NULL && parent_proc->p_flag & P_CONTROLT) | |
1363 | OSBitOrAtomic(P_CONTROLT, &child_proc->p_flag); | |
2d21ac55 | 1364 | session_rele(sessp); |
1c79356b | 1365 | |
b0d623f7 A |
1366 | /* |
1367 | * block all signals to reach the process. | |
1368 | * no transition race should be occuring with the child yet, | |
1369 | * but indicate that the process is in (the creation) transition. | |
1370 | */ | |
1371 | proc_signalstart(child_proc, 0); | |
fe8ab488 | 1372 | proc_transstart(child_proc, 0, 0); |
3e170ce0 | 1373 | proc_set_return_wait(child_proc); |
fe8ab488 A |
1374 | |
1375 | child_proc->p_pcaction = 0; | |
b0d623f7 | 1376 | |
b0d623f7 A |
1377 | TAILQ_INIT(&child_proc->p_uthlist); |
1378 | TAILQ_INIT(&child_proc->p_aio_activeq); | |
1379 | TAILQ_INIT(&child_proc->p_aio_doneq); | |
2d21ac55 | 1380 | |
2d21ac55 | 1381 | /* Inherit the parent flags for code sign */ |
c331a0be | 1382 | child_proc->p_csflags = (parent_proc->p_csflags & ~CS_KILLED); |
b0d623f7 A |
1383 | |
1384 | /* | |
1385 | * All processes have work queue locks; cleaned up by | |
1386 | * reap_child_locked() | |
1387 | */ | |
1388 | workqueue_init_lock(child_proc); | |
1389 | ||
1390 | /* | |
1391 | * Copy work queue information | |
1392 | * | |
1393 | * Note: This should probably only happen in the case where we are | |
1394 | * creating a child that is a copy of the parent; since this | |
1395 | * routine is called in the non-duplication case of vfork() | |
1396 | * or posix_spawn(), then this information should likely not | |
1397 | * be duplicated. | |
1398 | * | |
1399 | * <rdar://6640553> Work queue pointers that no longer point to code | |
1400 | */ | |
1401 | child_proc->p_wqthread = parent_proc->p_wqthread; | |
1402 | child_proc->p_threadstart = parent_proc->p_threadstart; | |
1403 | child_proc->p_pthsize = parent_proc->p_pthsize; | |
1404 | child_proc->p_targconc = parent_proc->p_targconc; | |
1405 | if ((parent_proc->p_lflag & P_LREGISTER) != 0) { | |
1406 | child_proc->p_lflag |= P_LREGISTER; | |
1407 | } | |
3e170ce0 | 1408 | child_proc->p_wqkqueue = NULL; |
b0d623f7 | 1409 | child_proc->p_dispatchqueue_offset = parent_proc->p_dispatchqueue_offset; |
39236c6e | 1410 | child_proc->p_dispatchqueue_serialno_offset = parent_proc->p_dispatchqueue_serialno_offset; |
b0d623f7 A |
1411 | #if PSYNCH |
1412 | pth_proc_hashinit(child_proc); | |
1413 | #endif /* PSYNCH */ | |
2d21ac55 | 1414 | |
490019cf A |
1415 | #if CONFIG_PERSONAS |
1416 | child_proc->p_persona = NULL; | |
1417 | error = persona_proc_inherit(child_proc, parent_proc); | |
1418 | if (error != 0) { | |
1419 | printf("forkproc: persona_proc_inherit failed (persona %d being destroyed?)\n", persona_get_uid(parent_proc->p_persona)); | |
1420 | forkproc_free(child_proc); | |
1421 | child_proc = NULL; | |
1422 | goto bad; | |
1423 | } | |
1424 | #endif | |
1425 | ||
39236c6e A |
1426 | #if CONFIG_MEMORYSTATUS |
1427 | /* Memorystatus + jetsam init */ | |
1428 | child_proc->p_memstat_state = 0; | |
1429 | child_proc->p_memstat_effectivepriority = JETSAM_PRIORITY_DEFAULT; | |
1430 | child_proc->p_memstat_requestedpriority = JETSAM_PRIORITY_DEFAULT; | |
1431 | child_proc->p_memstat_userdata = 0; | |
1432 | #if CONFIG_FREEZE | |
1433 | child_proc->p_memstat_suspendedfootprint = 0; | |
1434 | #endif | |
1435 | child_proc->p_memstat_dirty = 0; | |
1436 | child_proc->p_memstat_idledeadline = 0; | |
1437 | #endif /* CONFIG_MEMORYSTATUS */ | |
316670eb | 1438 | |
2d21ac55 | 1439 | bad: |
b0d623f7 | 1440 | return(child_proc); |
1c79356b A |
1441 | } |
1442 | ||
91447636 A |
1443 | void |
1444 | proc_lock(proc_t p) | |
1445 | { | |
4bd07ac2 | 1446 | lck_mtx_assert(proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED); |
91447636 A |
1447 | lck_mtx_lock(&p->p_mlock); |
1448 | } | |
1449 | ||
1450 | void | |
1451 | proc_unlock(proc_t p) | |
1452 | { | |
1453 | lck_mtx_unlock(&p->p_mlock); | |
1454 | } | |
1455 | ||
2d21ac55 A |
1456 | void |
1457 | proc_spinlock(proc_t p) | |
1458 | { | |
1459 | lck_spin_lock(&p->p_slock); | |
1460 | } | |
1461 | ||
1462 | void | |
1463 | proc_spinunlock(proc_t p) | |
1464 | { | |
1465 | lck_spin_unlock(&p->p_slock); | |
1466 | } | |
1467 | ||
1468 | void | |
1469 | proc_list_lock(void) | |
1470 | { | |
1471 | lck_mtx_lock(proc_list_mlock); | |
1472 | } | |
1473 | ||
1474 | void | |
1475 | proc_list_unlock(void) | |
1476 | { | |
1477 | lck_mtx_unlock(proc_list_mlock); | |
1478 | } | |
1479 | ||
4bd07ac2 A |
1480 | void |
1481 | proc_ucred_lock(proc_t p) | |
1482 | { | |
1483 | lck_mtx_lock(&p->p_ucred_mlock); | |
1484 | } | |
1485 | ||
1486 | void | |
1487 | proc_ucred_unlock(proc_t p) | |
1488 | { | |
1489 | lck_mtx_unlock(&p->p_ucred_mlock); | |
1490 | } | |
1491 | ||
1c79356b A |
1492 | #include <kern/zalloc.h> |
1493 | ||
1494 | struct zone *uthread_zone; | |
2d21ac55 | 1495 | static int uthread_zone_inited = 0; |
1c79356b | 1496 | |
2d21ac55 | 1497 | static void |
91447636 | 1498 | uthread_zone_init(void) |
1c79356b A |
1499 | { |
1500 | if (!uthread_zone_inited) { | |
1501 | uthread_zone = zinit(sizeof(struct uthread), | |
b0d623f7 | 1502 | thread_max * sizeof(struct uthread), |
91447636 A |
1503 | THREAD_CHUNK * sizeof(struct uthread), |
1504 | "uthreads"); | |
1c79356b A |
1505 | uthread_zone_inited = 1; |
1506 | } | |
1507 | } | |
1508 | ||
1509 | void * | |
b0d623f7 | 1510 | uthread_alloc(task_t task, thread_t thread, int noinherit) |
1c79356b | 1511 | { |
2d21ac55 A |
1512 | proc_t p; |
1513 | uthread_t uth; | |
1514 | uthread_t uth_parent; | |
1c79356b A |
1515 | void *ut; |
1516 | ||
1517 | if (!uthread_zone_inited) | |
1518 | uthread_zone_init(); | |
1519 | ||
1520 | ut = (void *)zalloc(uthread_zone); | |
1521 | bzero(ut, sizeof(struct uthread)); | |
9bccf70c | 1522 | |
2d21ac55 A |
1523 | p = (proc_t) get_bsdtask_info(task); |
1524 | uth = (uthread_t)ut; | |
316670eb | 1525 | uth->uu_thread = thread; |
9bccf70c | 1526 | |
91447636 A |
1527 | /* |
1528 | * Thread inherits credential from the creating thread, if both | |
1529 | * are in the same task. | |
1530 | * | |
1531 | * If the creating thread has no credential or is from another | |
1532 | * task we can leave the new thread credential NULL. If it needs | |
1533 | * one later, it will be lazily assigned from the task's process. | |
1534 | */ | |
2d21ac55 | 1535 | uth_parent = (uthread_t)get_bsdthread_info(current_thread()); |
b0d623f7 | 1536 | if ((noinherit == 0) && task == current_task() && |
2d21ac55 A |
1537 | uth_parent != NULL && |
1538 | IS_VALID_CRED(uth_parent->uu_ucred)) { | |
0c530ab8 A |
1539 | /* |
1540 | * XXX The new thread is, in theory, being created in context | |
1541 | * XXX of parent thread, so a direct reference to the parent | |
1542 | * XXX is OK. | |
1543 | */ | |
1544 | kauth_cred_ref(uth_parent->uu_ucred); | |
91447636 | 1545 | uth->uu_ucred = uth_parent->uu_ucred; |
91447636 A |
1546 | /* the credential we just inherited is an assumed credential */ |
1547 | if (uth_parent->uu_flag & UT_SETUID) | |
1548 | uth->uu_flag |= UT_SETUID; | |
1549 | } else { | |
b0d623f7 A |
1550 | /* sometimes workqueue threads are created out task context */ |
1551 | if ((task != kernel_task) && (p != PROC_NULL)) | |
1552 | uth->uu_ucred = kauth_cred_proc_ref(p); | |
1553 | else | |
1554 | uth->uu_ucred = NOCRED; | |
91447636 | 1555 | } |
2d21ac55 | 1556 | |
91447636 | 1557 | |
2d21ac55 | 1558 | if ((task != kernel_task) && p) { |
91447636 | 1559 | |
2d21ac55 | 1560 | proc_lock(p); |
b0d623f7 A |
1561 | if (noinherit != 0) { |
1562 | /* workq threads will not inherit masks */ | |
1563 | uth->uu_sigmask = ~workq_threadmask; | |
1564 | } else if (uth_parent) { | |
91447636 | 1565 | if (uth_parent->uu_flag & UT_SAS_OLDMASK) |
9bccf70c A |
1566 | uth->uu_sigmask = uth_parent->uu_oldmask; |
1567 | else | |
1568 | uth->uu_sigmask = uth_parent->uu_sigmask; | |
1569 | } | |
2d21ac55 A |
1570 | uth->uu_context.vc_thread = thread; |
1571 | TAILQ_INSERT_TAIL(&p->p_uthlist, uth, uu_list); | |
1572 | proc_unlock(p); | |
1573 | ||
1574 | #if CONFIG_DTRACE | |
1575 | if (p->p_dtrace_ptss_pages != NULL) { | |
1576 | uth->t_dtrace_scratch = dtrace_ptss_claim_entry(p); | |
91447636 | 1577 | } |
2d21ac55 | 1578 | #endif |
9bccf70c A |
1579 | } |
1580 | ||
1c79356b A |
1581 | return (ut); |
1582 | } | |
1583 | ||
3e170ce0 A |
1584 | /* |
1585 | * This routine frees the thread name field of the uthread_t structure. Split out of | |
1586 | * uthread_cleanup() so it can be called separately on the threads of a corpse after | |
1587 | * the corpse notification has been sent, and the handler has had a chance to extract | |
1588 | * the thread names. | |
1589 | */ | |
1590 | void | |
1591 | uthread_cleanup_name(void *uthread) | |
1592 | { | |
1593 | uthread_t uth = (uthread_t)uthread; | |
1594 | ||
1595 | /* | |
1596 | * <rdar://17834538> | |
1597 | * Set pth_name to NULL before calling free(). | |
1598 | * Previously there was a race condition in the | |
1599 | * case this code was executing during a stackshot | |
1600 | * where the stackshot could try and copy pth_name | |
1601 | * after it had been freed and before if was marked | |
1602 | * as null. | |
1603 | */ | |
1604 | if (uth->pth_name != NULL) { | |
1605 | void *pth_name = uth->pth_name; | |
1606 | uth->pth_name = NULL; | |
1607 | kfree(pth_name, MAXTHREADNAMESIZE); | |
1608 | } | |
1609 | return; | |
1610 | } | |
0b4e3aa0 | 1611 | |
2d21ac55 A |
1612 | /* |
1613 | * This routine frees all the BSD context in uthread except the credential. | |
1614 | * It does not free the uthread structure as well | |
1615 | */ | |
1c79356b | 1616 | void |
3e170ce0 | 1617 | uthread_cleanup(task_t task, void *uthread, void * bsd_info, boolean_t is_corpse) |
1c79356b A |
1618 | { |
1619 | struct _select *sel; | |
2d21ac55 A |
1620 | uthread_t uth = (uthread_t)uthread; |
1621 | proc_t p = (proc_t)bsd_info; | |
593a1d5f | 1622 | |
4bd07ac2 A |
1623 | #if PROC_REF_DEBUG |
1624 | if (__improbable(uthread_get_proc_refcount(uthread) != 0)) { | |
1625 | panic("uthread_cleanup called for uthread %p with uu_proc_refcount != 0", uthread); | |
1626 | } | |
1627 | #endif | |
1628 | ||
b0d623f7 | 1629 | if (uth->uu_lowpri_window || uth->uu_throttle_info) { |
3e170ce0 | 1630 | /* |
593a1d5f A |
1631 | * task is marked as a low priority I/O type |
1632 | * and we've somehow managed to not dismiss the throttle | |
1633 | * through the normal exit paths back to user space... | |
1634 | * no need to throttle this thread since its going away | |
1635 | * but we do need to update our bookeeping w/r to throttled threads | |
b0d623f7 A |
1636 | * |
1637 | * Calling this routine will clean up any throttle info reference | |
1638 | * still inuse by the thread. | |
593a1d5f | 1639 | */ |
39236c6e | 1640 | throttle_lowpri_io(0); |
593a1d5f | 1641 | } |
55e303ae A |
1642 | /* |
1643 | * Per-thread audit state should never last beyond system | |
1644 | * call return. Since we don't audit the thread creation/ | |
1645 | * removal, the thread state pointer should never be | |
1646 | * non-NULL when we get here. | |
1647 | */ | |
1648 | assert(uth->uu_ar == NULL); | |
1c79356b | 1649 | |
91447636 | 1650 | sel = &uth->uu_select; |
1c79356b A |
1651 | /* cleanup the select bit space */ |
1652 | if (sel->nbytes) { | |
1653 | FREE(sel->ibits, M_TEMP); | |
1654 | FREE(sel->obits, M_TEMP); | |
2d21ac55 A |
1655 | sel->nbytes = 0; |
1656 | } | |
1657 | ||
1658 | if (uth->uu_cdir) { | |
1659 | vnode_rele(uth->uu_cdir); | |
1660 | uth->uu_cdir = NULLVP; | |
1c79356b A |
1661 | } |
1662 | ||
3e170ce0 A |
1663 | if (uth->uu_wqset) { |
1664 | if (waitq_set_is_valid(uth->uu_wqset)) | |
1665 | waitq_set_deinit(uth->uu_wqset); | |
1666 | FREE(uth->uu_wqset, M_SELECT); | |
1667 | uth->uu_wqset = NULL; | |
1668 | uth->uu_wqstate_sz = 0; | |
0b4e3aa0 | 1669 | } |
3e170ce0 A |
1670 | |
1671 | /* | |
1672 | * defer the removal of the thread name on process corpses until the corpse has | |
1673 | * been autopsied. | |
fe8ab488 | 1674 | */ |
3e170ce0 A |
1675 | if (!is_corpse) { |
1676 | uthread_cleanup_name(uth); | |
b0d623f7 | 1677 | } |
fe8ab488 | 1678 | |
2d21ac55 A |
1679 | if ((task != kernel_task) && p) { |
1680 | ||
1681 | if (((uth->uu_flag & UT_VFORK) == UT_VFORK) && (uth->uu_proc != PROC_NULL)) { | |
1682 | vfork_exit_internal(uth->uu_proc, 0, 1); | |
1683 | } | |
b0d623f7 A |
1684 | /* |
1685 | * Remove the thread from the process list and | |
1686 | * transfer [appropriate] pending signals to the process. | |
1687 | */ | |
2d21ac55 A |
1688 | if (get_bsdtask_info(task) == p) { |
1689 | proc_lock(p); | |
1690 | TAILQ_REMOVE(&p->p_uthlist, uth, uu_list); | |
b0d623f7 | 1691 | p->p_siglist |= (uth->uu_siglist & execmask & (~p->p_sigignore | sigcantmask)); |
2d21ac55 A |
1692 | proc_unlock(p); |
1693 | } | |
1694 | #if CONFIG_DTRACE | |
b0d623f7 A |
1695 | struct dtrace_ptss_page_entry *tmpptr = uth->t_dtrace_scratch; |
1696 | uth->t_dtrace_scratch = NULL; | |
1697 | if (tmpptr != NULL) { | |
1698 | dtrace_ptss_release_entry(p, tmpptr); | |
2d21ac55 A |
1699 | } |
1700 | #endif | |
1701 | } | |
1702 | } | |
1703 | ||
1704 | /* This routine releases the credential stored in uthread */ | |
1705 | void | |
1706 | uthread_cred_free(void *uthread) | |
1707 | { | |
1708 | uthread_t uth = (uthread_t)uthread; | |
1709 | ||
1710 | /* and free the uthread itself */ | |
0c530ab8 A |
1711 | if (IS_VALID_CRED(uth->uu_ucred)) { |
1712 | kauth_cred_t oldcred = uth->uu_ucred; | |
1713 | uth->uu_ucred = NOCRED; | |
1714 | kauth_cred_unref(&oldcred); | |
1715 | } | |
2d21ac55 | 1716 | } |
e5568f75 | 1717 | |
2d21ac55 A |
1718 | /* This routine frees the uthread structure held in thread structure */ |
1719 | void | |
1720 | uthread_zone_free(void *uthread) | |
1721 | { | |
22ba694c A |
1722 | uthread_t uth = (uthread_t)uthread; |
1723 | ||
1724 | if (uth->t_tombstone) { | |
1725 | kfree(uth->t_tombstone, sizeof(struct doc_tombstone)); | |
1726 | uth->t_tombstone = NULL; | |
1727 | } | |
1728 | ||
1c79356b | 1729 | /* and free the uthread itself */ |
91447636 | 1730 | zfree(uthread_zone, uthread); |
1c79356b | 1731 | } |