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