]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 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_exit.c 8.7 (Berkeley) 2/12/94 | |
67 | */ | |
2d21ac55 A |
68 | /* |
69 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 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 | */ | |
1c79356b A |
74 | |
75 | #include <machine/reg.h> | |
76 | #include <machine/psl.h> | |
77 | ||
78 | #include "compat_43.h" | |
79 | ||
80 | #include <sys/param.h> | |
81 | #include <sys/systm.h> | |
82 | #include <sys/ioctl.h> | |
91447636 | 83 | #include <sys/proc_internal.h> |
2d21ac55 | 84 | #include <sys/proc.h> |
91447636 | 85 | #include <sys/kauth.h> |
1c79356b A |
86 | #include <sys/tty.h> |
87 | #include <sys/time.h> | |
88 | #include <sys/resource.h> | |
89 | #include <sys/kernel.h> | |
1c79356b | 90 | #include <sys/wait.h> |
91447636 A |
91 | #include <sys/file_internal.h> |
92 | #include <sys/vnode_internal.h> | |
1c79356b A |
93 | #include <sys/syslog.h> |
94 | #include <sys/malloc.h> | |
95 | #include <sys/resourcevar.h> | |
96 | #include <sys/ptrace.h> | |
97 | #include <sys/user.h> | |
55e303ae | 98 | #include <sys/aio_kern.h> |
91447636 A |
99 | #include <sys/sysproto.h> |
100 | #include <sys/signalvar.h> | |
101 | #include <sys/filedesc.h> /* fdfree */ | |
2d21ac55 | 102 | #if SYSV_SHM |
91447636 | 103 | #include <sys/shm_internal.h> /* shmexit */ |
2d21ac55 | 104 | #endif |
91447636 | 105 | #include <sys/acct.h> /* acct_process */ |
e5568f75 A |
106 | |
107 | #include <bsm/audit_kernel.h> | |
108 | #include <bsm/audit_kevents.h> | |
1c79356b A |
109 | |
110 | #include <mach/mach_types.h> | |
91447636 A |
111 | |
112 | #include <kern/kern_types.h> | |
113 | #include <kern/kalloc.h> | |
114 | #include <kern/task.h> | |
1c79356b | 115 | #include <kern/thread.h> |
2d21ac55 | 116 | #include <kern/thread_call.h> |
9bccf70c | 117 | #include <kern/sched_prim.h> |
1c79356b | 118 | #include <kern/assert.h> |
2d21ac55 A |
119 | #if CONFIG_DTRACE |
120 | /* Do not include dtrace.h, it redefines kmem_[alloc/free] */ | |
121 | extern void (*dtrace_fasttrap_exit_ptr)(proc_t); | |
122 | extern void (*dtrace_helpers_cleanup)(proc_t); | |
123 | extern void dtrace_lazy_dofs_destroy(proc_t); | |
124 | ||
125 | #include <sys/dtrace_ptss.h> | |
126 | #endif | |
127 | ||
128 | #if CONFIG_MACF | |
129 | #include <security/mac.h> | |
130 | #include <sys/syscall.h> | |
9bccf70c | 131 | #endif |
1c79356b | 132 | |
91447636 A |
133 | #include <mach/mach_types.h> |
134 | #include <mach/task.h> | |
135 | #include <mach/thread_act.h> | |
136 | #include <mach/mach_traps.h> /* init_process */ | |
137 | ||
2d21ac55 A |
138 | #include <sys/sdt.h> |
139 | ||
1c79356b | 140 | extern char init_task_failure_data[]; |
2d21ac55 A |
141 | void proc_prepareexit(proc_t p, int rv); |
142 | void vfork_exit(proc_t p, int rv); | |
143 | void vproc_exit(proc_t p); | |
91447636 | 144 | __private_extern__ void munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p); |
2d21ac55 | 145 | static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int locked, int droplock); |
91447636 A |
146 | |
147 | /* | |
148 | * Things which should have prototypes in headers, but don't | |
149 | */ | |
91447636 | 150 | void *get_bsduthreadarg(thread_t); |
2d21ac55 | 151 | void proc_exit(proc_t p); |
91447636 A |
152 | int wait1continue(int result); |
153 | int waitidcontinue(int result); | |
154 | int *get_bsduthreadrval(thread_t); | |
2d21ac55 A |
155 | kern_return_t sys_perf_notify(thread_t thread, int pid); |
156 | kern_return_t abnormal_exit_notify(mach_exception_data_type_t code, | |
157 | mach_exception_data_type_t subcode); | |
158 | int in_shutdown(void); | |
159 | void workqueue_exit(struct proc *); | |
160 | void delay(int); | |
161 | ||
91447636 A |
162 | /* |
163 | * NOTE: Source and target may *NOT* overlap! | |
164 | * XXX Should share code with bsd/dev/ppc/unix_signal.c | |
165 | */ | |
166 | static void | |
167 | siginfo_64to32(user_siginfo_t *in, siginfo_t *out) | |
168 | { | |
169 | out->si_signo = in->si_signo; | |
170 | out->si_errno = in->si_errno; | |
171 | out->si_code = in->si_code; | |
172 | out->si_pid = in->si_pid; | |
173 | out->si_uid = in->si_uid; | |
174 | out->si_status = in->si_status; | |
175 | out->si_addr = CAST_DOWN(void *,in->si_addr); | |
176 | /* following cast works for sival_int because of padding */ | |
177 | out->si_value.sival_ptr = CAST_DOWN(void *,in->si_value.sival_ptr); | |
178 | out->si_band = in->si_band; /* range reduction */ | |
2d21ac55 | 179 | out->__pad[0] = in->pad[0]; /* mcontext.ss.r1 */ |
91447636 | 180 | } |
1c79356b A |
181 | |
182 | /* | |
183 | * exit -- | |
184 | * Death of process. | |
185 | */ | |
1c79356b | 186 | void |
2d21ac55 | 187 | exit(proc_t p, struct exit_args *uap, int *retval) |
1c79356b | 188 | { |
0b4e3aa0 | 189 | exit1(p, W_EXITCODE(uap->rval, 0), retval); |
1c79356b | 190 | |
9bccf70c | 191 | /* drop funnel before we return */ |
1c79356b A |
192 | thread_exception_return(); |
193 | /* NOTREACHED */ | |
194 | while (TRUE) | |
9bccf70c | 195 | thread_block(THREAD_CONTINUE_NULL); |
1c79356b A |
196 | /* NOTREACHED */ |
197 | } | |
198 | ||
199 | /* | |
200 | * Exit: deallocate address space and other resources, change proc state | |
201 | * to zombie, and unlink proc from allproc and parent's lists. Save exit | |
202 | * status and rusage for wait(). Check for child processes and orphan them. | |
203 | */ | |
0b4e3aa0 | 204 | int |
2d21ac55 | 205 | exit1(proc_t p, int rv, int *retval) |
1c79356b | 206 | { |
91447636 | 207 | thread_t self = current_thread(); |
1c79356b | 208 | struct task *task = p->task; |
1c79356b A |
209 | struct uthread *ut; |
210 | ||
211 | /* | |
212 | * If a thread in this task has already | |
213 | * called exit(), then halt any others | |
214 | * right here. | |
215 | */ | |
0b4e3aa0 | 216 | |
55e303ae | 217 | ut = get_bsdthread_info(self); |
91447636 A |
218 | if (ut->uu_flag & UT_VFORK) { |
219 | vfork_exit(p, rv); | |
2d21ac55 | 220 | vfork_return(p , retval, p->p_pid); |
0b4e3aa0 A |
221 | unix_syscall_return(0); |
222 | /* NOT REACHED */ | |
223 | } | |
2d21ac55 A |
224 | |
225 | /* | |
226 | * The parameter list of audit_syscall_exit() was augmented to | |
227 | * take the Darwin syscall number as the first parameter, | |
228 | * which is currently required by mac_audit_postselect(). | |
229 | */ | |
230 | ||
231 | AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */ | |
232 | ||
233 | DTRACE_PROC1(exit, int, CLD_EXITED); | |
234 | ||
235 | proc_lock(p); | |
1c79356b A |
236 | while (p->exit_thread != self) { |
237 | if (sig_try_locked(p) <= 0) { | |
55e303ae | 238 | if (get_threadtask(self) != task) { |
2d21ac55 | 239 | proc_unlock(p); |
0b4e3aa0 | 240 | return(0); |
1c79356b | 241 | } |
2d21ac55 | 242 | proc_unlock(p); |
55e303ae | 243 | thread_terminate(self); |
1c79356b A |
244 | thread_exception_return(); |
245 | /* NOTREACHED */ | |
246 | } | |
247 | sig_lock_to_exit(p); | |
248 | } | |
4a3eedf9 | 249 | if (p == initproc) { |
2d21ac55 | 250 | proc_unlock(p); |
1c79356b A |
251 | printf("pid 1 exited (signal %d, exit %d)", |
252 | WTERMSIG(rv), WEXITSTATUS(rv)); | |
2d21ac55 A |
253 | panic("%s died\nState at Last Exception:\n\n%s", |
254 | (p->p_comm[0] != '\0' ? | |
255 | p->p_comm : | |
256 | "launchd"), | |
1c79356b A |
257 | init_task_failure_data); |
258 | } | |
259 | ||
2d21ac55 | 260 | p->p_lflag |= P_LEXIT; |
1c79356b A |
261 | p->p_xstat = rv; |
262 | ||
2d21ac55 A |
263 | proc_unlock(p); |
264 | ||
265 | proc_prepareexit(p, rv); | |
266 | ||
1c79356b A |
267 | /* task terminate will call proc_terminate and that cleans it up */ |
268 | task_terminate_internal(task); | |
269 | ||
0b4e3aa0 | 270 | return(0); |
1c79356b A |
271 | } |
272 | ||
273 | void | |
2d21ac55 | 274 | proc_prepareexit(proc_t p, int rv) |
1c79356b | 275 | { |
2d21ac55 | 276 | mach_exception_data_type_t code, subcode; |
1c79356b | 277 | struct uthread *ut; |
91447636 | 278 | thread_t self = current_thread(); |
2d21ac55 | 279 | ut = get_bsdthread_info(self); |
55e303ae | 280 | |
2d21ac55 A |
281 | /* If a core should be generated, notify crash reporter */ |
282 | if (!in_shutdown() && hassigprop(WTERMSIG(rv), SA_CORE)) { | |
283 | /* | |
284 | * Workaround for processes checking up on PT_DENY_ATTACH: | |
285 | * should be backed out post-Leopard (details in 5431025). | |
286 | */ | |
287 | if ((SIGSEGV == WTERMSIG(rv)) && | |
288 | (p->p_pptr->p_lflag & P_LNOATTACH)) { | |
289 | goto skipcheck; | |
290 | } | |
291 | ||
292 | /* | |
293 | * Crash Reporter looks for the signal value, original exception | |
294 | * type, and low 20 bits of the original code in code[0] | |
295 | * (8, 4, and 20 bits respectively). code[1] is unmodified. | |
296 | */ | |
297 | code = ((WTERMSIG(rv) & 0xff) << 24) | | |
298 | ((ut->uu_exception & 0x0f) << 20) | | |
299 | ((int)ut->uu_code & 0xfffff); | |
300 | subcode = ut->uu_subcode; | |
301 | (void) abnormal_exit_notify(code, subcode); | |
302 | } | |
303 | ||
304 | skipcheck: | |
91447636 | 305 | /* Notify the perf server */ |
2d21ac55 | 306 | (void)sys_perf_notify(self, p->p_pid); |
1c79356b | 307 | |
1c79356b A |
308 | /* |
309 | * Remove proc from allproc queue and from pidhash chain. | |
310 | * Need to do this before we do anything that can block. | |
311 | * Not doing causes things like mount() find this on allproc | |
312 | * in partially cleaned state. | |
313 | */ | |
2d21ac55 A |
314 | |
315 | proc_list_lock(); | |
316 | ||
1c79356b | 317 | LIST_REMOVE(p, p_list); |
55e303ae | 318 | LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */ |
2d21ac55 A |
319 | /* will not be visible via proc_find */ |
320 | p->p_listflag |= P_LIST_EXITED; | |
321 | ||
322 | proc_list_unlock(); | |
323 | ||
1c79356b A |
324 | |
325 | #ifdef PGINPROF | |
326 | vmsizmon(); | |
327 | #endif | |
328 | /* | |
329 | * If parent is waiting for us to exit or exec, | |
2d21ac55 | 330 | * P_LPPWAIT is set; we will wakeup the parent below. |
1c79356b | 331 | */ |
2d21ac55 A |
332 | proc_lock(p); |
333 | p->p_lflag &= ~(P_LTRACED | P_LPPWAIT); | |
91447636 | 334 | p->p_sigignore = ~(sigcantmask); |
9bccf70c | 335 | ut->uu_siglist = 0; |
2d21ac55 | 336 | proc_unlock(p); |
1c79356b A |
337 | } |
338 | ||
339 | void | |
2d21ac55 | 340 | proc_exit(proc_t p) |
1c79356b | 341 | { |
2d21ac55 A |
342 | proc_t q; |
343 | proc_t pp; | |
1c79356b | 344 | struct task *task = p->task; |
2d21ac55 A |
345 | boolean_t fstate; |
346 | vnode_t tvp = NULLVP; | |
347 | struct pgrp * pg; | |
348 | struct session *sessp; | |
349 | struct uthread * uth; | |
1c79356b A |
350 | |
351 | /* This can happen if thread_terminate of the single thread | |
352 | * process | |
353 | */ | |
354 | ||
2d21ac55 A |
355 | uth = (struct uthread *)get_bsdthread_info(current_thread()); |
356 | ||
357 | proc_lock(p); | |
358 | if( !(p->p_lflag & P_LEXIT)) { | |
359 | p->p_lflag |= P_LEXIT; | |
360 | proc_unlock(p); | |
361 | proc_prepareexit(p, 0); | |
362 | proc_lock(p); | |
1c79356b A |
363 | } |
364 | ||
91447636 | 365 | p->p_lflag |= P_LPEXIT; |
2d21ac55 A |
366 | proc_unlock(p); |
367 | ||
368 | #if CONFIG_DTRACE | |
369 | /* | |
370 | * Free any outstanding lazy dof entries. It is imperative we | |
371 | * always call dtrace_lazy_dofs_destroy, rather than null check | |
372 | * and call if !NULL. If we NULL test, during lazy dof faulting | |
373 | * we can race with the faulting code and proceed from here to | |
374 | * beyond the helpers cleanup. The lazy dof faulting will then | |
375 | * install new helpers which will never be cleaned up, and leak. | |
376 | */ | |
377 | dtrace_lazy_dofs_destroy(p); | |
378 | ||
379 | /* | |
380 | * Clean up any DTrace helper actions or probes for the process. | |
381 | */ | |
382 | if (p->p_dtrace_helpers != NULL) { | |
383 | (*dtrace_helpers_cleanup)(p); | |
384 | } | |
385 | ||
386 | /* | |
387 | * Clean up any DTrace probes associated with this process. | |
388 | */ | |
389 | /* | |
390 | * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(), | |
391 | * call this after dtrace_helpers_cleanup() | |
392 | */ | |
393 | proc_lock(p); | |
394 | if (p->p_dtrace_probes && dtrace_fasttrap_exit_ptr) { | |
395 | (*dtrace_fasttrap_exit_ptr)(p); | |
396 | } | |
397 | proc_unlock(p); | |
398 | #endif | |
399 | ||
91447636 | 400 | /* XXX Zombie allocation may fail, in which case stats get lost */ |
1c79356b A |
401 | MALLOC_ZONE(p->p_ru, struct rusage *, |
402 | sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK); | |
403 | ||
55e303ae A |
404 | /* |
405 | * need to cancel async IO requests that can be cancelled and wait for those | |
406 | * already active. MAY BLOCK! | |
407 | */ | |
ff6e181a | 408 | |
2d21ac55 A |
409 | proc_refdrain(p); |
410 | ||
411 | workqueue_exit(p); | |
ff6e181a | 412 | |
55e303ae A |
413 | _aio_exit( p ); |
414 | ||
1c79356b A |
415 | /* |
416 | * Close open files and release open-file table. | |
417 | * This may block! | |
418 | */ | |
419 | fdfree(p); | |
420 | ||
593a1d5f A |
421 | if (uth->uu_lowpri_window) { |
422 | /* | |
423 | * task is marked as a low priority I/O type | |
424 | * and the I/O we issued while flushing files on close | |
425 | * collided with normal I/O operations... | |
426 | * no need to throttle this thread since its going away | |
427 | * but we do need to update our bookeeping w/r to throttled threads | |
428 | */ | |
429 | throttle_lowpri_io(FALSE); | |
430 | } | |
431 | ||
2d21ac55 | 432 | #if SYSV_SHM |
1c79356b A |
433 | /* Close ref SYSV Shared memory*/ |
434 | if (p->vm_shm) | |
435 | shmexit(p); | |
2d21ac55 A |
436 | #endif |
437 | #if SYSV_SEM | |
9bccf70c A |
438 | /* Release SYSV semaphores */ |
439 | semexit(p); | |
2d21ac55 | 440 | #endif |
1c79356b | 441 | |
2d21ac55 A |
442 | sessp = proc_session(p); |
443 | if (SESS_LEADER(p, sessp)) { | |
444 | ||
445 | /* Protected by funnel for tty accesses */ | |
446 | fstate = thread_funnel_set(kernel_flock, TRUE); | |
1c79356b | 447 | |
2d21ac55 | 448 | if (sessp->s_ttyvp != NULLVP) { |
fa4905b1 | 449 | struct vnode *ttyvp; |
2d21ac55 | 450 | int ttyvid; |
91447636 | 451 | struct vfs_context context; |
2d21ac55 A |
452 | struct tty * tp; |
453 | ||
fa4905b1 | 454 | |
1c79356b A |
455 | /* |
456 | * Controlling process. | |
457 | * Signal foreground pgrp, | |
458 | * drain controlling terminal | |
459 | * and revoke access to controlling terminal. | |
460 | */ | |
2d21ac55 A |
461 | tp = sessp->s_ttyp; |
462 | ||
463 | if ((tp != TTY_NULL) && (tp->t_session == sessp)) { | |
464 | tty_pgsignal(tp, SIGHUP, 1); | |
465 | (void) ttywait(tp); | |
1c79356b A |
466 | /* |
467 | * The tty could have been revoked | |
468 | * if we blocked. | |
469 | */ | |
2d21ac55 A |
470 | |
471 | session_lock(sessp); | |
472 | ttyvp = sessp->s_ttyvp; | |
473 | ttyvid = sessp->s_ttyvid; | |
474 | sessp->s_ttyvp = NULL; | |
475 | sessp->s_ttyvid = 0; | |
476 | sessp->s_ttyp = NULL; | |
477 | sessp->s_ttypgrpid = NO_PID; | |
478 | session_unlock(sessp); | |
479 | ||
480 | if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) { | |
481 | context.vc_thread = proc_thread(p); /* XXX */ | |
482 | context.vc_ucred = kauth_cred_proc_ref(p); | |
483 | VNOP_REVOKE(ttyvp, REVOKEALL, &context); | |
484 | vnode_put(ttyvp); | |
485 | kauth_cred_unref(&context.vc_ucred); | |
486 | } | |
487 | } else { | |
488 | session_lock(sessp); | |
489 | ttyvp = sessp->s_ttyvp; | |
490 | sessp->s_ttyvp = NULL; | |
491 | sessp->s_ttyvid = 0; | |
492 | sessp->s_ttyp = NULL; | |
493 | sessp->s_ttypgrpid = NO_PID; | |
494 | session_unlock(sessp); | |
1c79356b | 495 | } |
2d21ac55 | 496 | if (ttyvp) |
91447636 | 497 | vnode_rele(ttyvp); |
1c79356b A |
498 | /* |
499 | * s_ttyp is not zero'd; we use this to indicate | |
500 | * that the session once had a controlling terminal. | |
501 | * (for logging and informational purposes) | |
502 | */ | |
503 | } | |
2d21ac55 A |
504 | |
505 | (void) thread_funnel_set(kernel_flock, fstate); | |
506 | session_lock(sessp); | |
507 | sessp->s_leader = NULL; | |
508 | session_unlock(sessp); | |
1c79356b | 509 | } |
2d21ac55 A |
510 | session_rele(sessp); |
511 | ||
512 | pg = proc_pgrp(p); | |
513 | fixjobc(p, pg, 0); | |
514 | pg_rele(pg); | |
1c79356b | 515 | |
1c79356b | 516 | p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
9bccf70c A |
517 | (void)acct_process(p); |
518 | ||
2d21ac55 A |
519 | proc_list_lock(); |
520 | /* wait till parentrefs are dropped and grant no more */ | |
521 | proc_childdrainstart(p); | |
522 | while ((q = p->p_children.lh_first) != NULL) { | |
523 | q->p_listflag |= P_LIST_DEADPARENT; | |
524 | if (q->p_stat == SZOMB) { | |
525 | if (p != q->p_pptr) | |
526 | panic("parent child linkage broken"); | |
527 | /* check for sysctl zomb lookup */ | |
528 | while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { | |
529 | msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); | |
530 | } | |
531 | q->p_listflag |= P_LIST_WAITING; | |
532 | /* | |
533 | * This is a named reference and it is not granted | |
534 | * if the reap is already in progress. So we get | |
535 | * the reference here exclusively and their can be | |
536 | * no waiters. So there is no need for a wakeup | |
537 | * after we are done. AlsO the reap frees the structure | |
538 | * and the proc struct cannot be used for wakeups as well. | |
539 | * It is safe to use q here as this is system reap | |
540 | */ | |
541 | (void)reap_child_locked(p, q, 1, 1, 0); | |
542 | } else { | |
543 | proc_reparentlocked(q, initproc, 0, 1); | |
544 | /* | |
545 | * Traced processes are killed | |
546 | * since their existence means someone is messing up. | |
547 | */ | |
548 | if (q->p_lflag & P_LTRACED) { | |
549 | proc_list_unlock(); | |
550 | proc_lock(q); | |
551 | q->p_lflag &= ~P_LTRACED; | |
552 | if (q->sigwait_thread) { | |
553 | proc_unlock(q); | |
554 | /* | |
555 | * The sigwait_thread could be stopped at a | |
556 | * breakpoint. Wake it up to kill. | |
557 | * Need to do this as it could be a thread which is not | |
558 | * the first thread in the task. So any attempts to kill | |
559 | * the process would result into a deadlock on q->sigwait. | |
560 | */ | |
561 | thread_resume((thread_t)q->sigwait_thread); | |
562 | clear_wait(q->sigwait_thread, THREAD_INTERRUPTED); | |
563 | threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0); | |
564 | } else | |
565 | proc_unlock(q); | |
566 | psignal(q, SIGKILL); | |
567 | proc_list_lock(); | |
1c79356b | 568 | } |
1c79356b A |
569 | } |
570 | } | |
571 | ||
2d21ac55 A |
572 | proc_childdrainend(p); |
573 | proc_list_unlock(); | |
574 | ||
575 | /* | |
576 | * Release reference to text vnode | |
577 | */ | |
578 | tvp = p->p_textvp; | |
579 | p->p_textvp = NULL; | |
580 | if (tvp != NULLVP) { | |
581 | vnode_rele(tvp); | |
582 | } | |
583 | ||
1c79356b A |
584 | /* |
585 | * Save exit status and final rusage info, adding in child rusage | |
91447636 A |
586 | * info and self times. If we were unable to allocate a zombie |
587 | * structure, this information is lost. | |
1c79356b | 588 | */ |
2d21ac55 | 589 | /* No need for locking here as no one than this thread can access this */ |
91447636 A |
590 | if (p->p_ru != NULL) { |
591 | *p->p_ru = p->p_stats->p_ru; | |
1c79356b | 592 | |
91447636 A |
593 | timerclear(&p->p_ru->ru_utime); |
594 | timerclear(&p->p_ru->ru_stime); | |
1c79356b | 595 | |
91447636 | 596 | if (task) { |
2d21ac55 | 597 | task_basic_info_32_data_t tinfo; |
1c79356b | 598 | task_thread_times_info_data_t ttimesinfo; |
2d21ac55 A |
599 | task_events_info_data_t teventsinfo; |
600 | mach_msg_type_number_t task_info_stuff, task_ttimes_stuff; | |
601 | mach_msg_type_number_t task_events_stuff; | |
1c79356b A |
602 | struct timeval ut,st; |
603 | ||
2d21ac55 A |
604 | task_info_stuff = TASK_BASIC_INFO_32_COUNT; |
605 | task_info(task, TASK_BASIC2_INFO_32, | |
91447636 | 606 | (task_info_t)&tinfo, &task_info_stuff); |
1c79356b A |
607 | p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds; |
608 | p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds; | |
609 | p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds; | |
610 | p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds; | |
611 | ||
2d21ac55 A |
612 | p->p_ru->ru_maxrss = tinfo.resident_size; |
613 | ||
1c79356b A |
614 | task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT; |
615 | task_info(task, TASK_THREAD_TIMES_INFO, | |
91447636 | 616 | (task_info_t)&ttimesinfo, &task_ttimes_stuff); |
1c79356b A |
617 | |
618 | ut.tv_sec = ttimesinfo.user_time.seconds; | |
619 | ut.tv_usec = ttimesinfo.user_time.microseconds; | |
620 | st.tv_sec = ttimesinfo.system_time.seconds; | |
621 | st.tv_usec = ttimesinfo.system_time.microseconds; | |
622 | timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime); | |
623 | timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime); | |
2d21ac55 A |
624 | |
625 | task_events_stuff = TASK_EVENTS_INFO_COUNT; | |
626 | task_info(task, TASK_EVENTS_INFO, | |
627 | (task_info_t)&teventsinfo, &task_events_stuff); | |
628 | ||
629 | p->p_ru->ru_minflt = (teventsinfo.faults - | |
630 | teventsinfo.pageins); | |
631 | p->p_ru->ru_majflt = teventsinfo.pageins; | |
632 | p->p_ru->ru_nivcsw = (teventsinfo.csw - | |
633 | p->p_ru->ru_nvcsw); | |
634 | if (p->p_ru->ru_nivcsw < 0) | |
635 | p->p_ru->ru_nivcsw = 0; | |
91447636 | 636 | } |
1c79356b | 637 | |
91447636 A |
638 | ruadd(p->p_ru, &p->p_stats->p_cru); |
639 | } | |
1c79356b A |
640 | |
641 | /* | |
642 | * Free up profiling buffers. | |
643 | */ | |
644 | { | |
645 | struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn; | |
646 | ||
647 | p1 = p0->pr_next; | |
648 | p0->pr_next = NULL; | |
649 | p0->pr_scale = 0; | |
650 | ||
651 | for (; p1 != NULL; p1 = pn) { | |
652 | pn = p1->pr_next; | |
91447636 | 653 | kfree(p1, sizeof *p1); |
1c79356b A |
654 | } |
655 | } | |
656 | ||
2d21ac55 A |
657 | proc_spinlock(p); |
658 | if (thread_call_cancel(p->p_rcall)) | |
659 | p->p_ractive--; | |
660 | ||
661 | while (p->p_ractive > 0) { | |
662 | proc_spinunlock(p); | |
663 | ||
664 | delay(1); | |
665 | ||
666 | proc_spinlock(p); | |
667 | } | |
668 | proc_spinunlock(p); | |
669 | ||
670 | thread_call_free(p->p_rcall); | |
671 | p->p_rcall = NULL; | |
672 | ||
1c79356b A |
673 | /* |
674 | * Other substructures are freed from wait(). | |
675 | */ | |
2d21ac55 | 676 | FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS); |
1c79356b A |
677 | p->p_stats = NULL; |
678 | ||
2d21ac55 | 679 | FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS); |
1c79356b A |
680 | p->p_sigacts = NULL; |
681 | ||
2d21ac55 | 682 | proc_limitdrop(p, 1); |
1c79356b A |
683 | p->p_limit = NULL; |
684 | ||
2d21ac55 | 685 | |
1c79356b A |
686 | /* |
687 | * Finish up by terminating the task | |
688 | * and halt this thread (only if a | |
689 | * member of the task exiting). | |
690 | */ | |
691 | p->task = TASK_NULL; | |
1c79356b A |
692 | set_bsdtask_info(task, NULL); |
693 | ||
2d21ac55 | 694 | proc_knote(p, NOTE_EXIT); |
55e303ae | 695 | |
2d21ac55 A |
696 | /* mark the thread as the one that is doing proc_exit |
697 | * no need to hold proc lock in uthread_free | |
698 | */ | |
699 | uth->uu_flag |= UT_PROCEXIT; | |
1c79356b A |
700 | /* |
701 | * Notify parent that we're gone. | |
702 | */ | |
2d21ac55 A |
703 | pp = proc_parent(p); |
704 | if (pp->p_flag & P_NOCLDWAIT) { | |
9bccf70c | 705 | |
2d21ac55 A |
706 | #if 3839178 |
707 | /* | |
708 | * If the parent is ignoring SIGCHLD, then POSIX requires | |
709 | * us to not add the resource usage to the parent process - | |
710 | * we are only going to hand it off to init to get reaped. | |
711 | * We should contest the standard in this case on the basis | |
712 | * of RLIMIT_CPU. | |
713 | */ | |
714 | #else /* !3839178 */ | |
55e303ae A |
715 | /* |
716 | * Add child resource usage to parent before giving | |
91447636 A |
717 | * zombie to init. If we were unable to allocate a |
718 | * zombie structure, this information is lost. | |
55e303ae | 719 | */ |
2d21ac55 A |
720 | if (p->p_ru != NULL) { |
721 | proc_lock(pp); | |
722 | ruadd(&pp->p_stats->p_cru, p->p_ru); | |
723 | proc_unlock(pp); | |
724 | } | |
725 | #endif /* !3839178 */ | |
55e303ae | 726 | |
2d21ac55 A |
727 | /* kernel can reap this one, no need to move it to launchd */ |
728 | proc_list_lock(); | |
729 | p->p_listflag |= P_LIST_DEADPARENT; | |
730 | proc_list_unlock(); | |
9bccf70c | 731 | } |
2d21ac55 A |
732 | if ((p->p_listflag & P_LIST_DEADPARENT) == 0) { |
733 | if (pp != initproc) { | |
734 | proc_lock(pp); | |
735 | pp->si_pid = p->p_pid; | |
736 | pp->si_status = p->p_xstat; | |
737 | pp->si_code = CLD_EXITED; | |
738 | /* | |
739 | * p_ucred usage is safe as it is an exiting process | |
740 | * and reference is dropped in reap | |
741 | */ | |
742 | pp->si_uid = p->p_ucred->cr_ruid; | |
743 | proc_unlock(pp); | |
744 | } | |
745 | /* mark as a zombie */ | |
746 | /* No need to take proc lock as all refs are drained and | |
747 | * no one except parent (reaping ) can look at this. | |
748 | * The write is to an int and is coherent. Also parent is | |
749 | * keyed off of list lock for reaping | |
750 | */ | |
751 | p->p_stat = SZOMB; | |
752 | /* | |
753 | * The current process can be reaped so, no one | |
754 | * can depend on this | |
755 | */ | |
1c79356b | 756 | |
2d21ac55 A |
757 | psignal(pp, SIGCHLD); |
758 | ||
759 | /* and now wakeup the parent */ | |
760 | proc_list_lock(); | |
761 | wakeup((caddr_t)pp); | |
762 | proc_list_unlock(); | |
763 | } else { | |
764 | /* should be fine as parent proc would be initproc */ | |
765 | /* mark as a zombie */ | |
766 | /* No need to take proc lock as all refs are drained and | |
767 | * no one except parent (reaping ) can look at this. | |
768 | * The write is to an int and is coherent. Also parent is | |
769 | * keyed off of list lock for reaping | |
770 | */ | |
771 | proc_list_lock(); | |
772 | p->p_stat = SZOMB; | |
773 | /* check for sysctl zomb lookup */ | |
774 | while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { | |
775 | msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); | |
776 | } | |
777 | /* safe to use p as this is a system reap */ | |
778 | p->p_listflag |= P_LIST_WAITING; | |
779 | /* | |
780 | * This is a named reference and it is not granted | |
781 | * if the reap is already in progress. So we get | |
782 | * the reference here exclusively and their can be | |
783 | * no waiters. So there is no need for a wakeup | |
784 | * after we are done. AlsO the reap frees the structure | |
785 | * and the proc struct cannot be used for wakeups as well. | |
786 | * It is safe to use p here as this is system reap | |
787 | */ | |
788 | (void)reap_child_locked(pp, p, 1, 1, 1); | |
789 | /* list lock dropped by reap_child_locked */ | |
790 | } | |
593a1d5f A |
791 | if (uth->uu_lowpri_window) { |
792 | /* | |
793 | * task is marked as a low priority I/O type and we've | |
794 | * somehow picked up another throttle during exit processing... | |
795 | * no need to throttle this thread since its going away | |
796 | * but we do need to update our bookeeping w/r to throttled threads | |
797 | */ | |
798 | throttle_lowpri_io(FALSE); | |
799 | } | |
91447636 | 800 | |
2d21ac55 | 801 | proc_rele(pp); |
1c79356b | 802 | |
1c79356b A |
803 | } |
804 | ||
805 | ||
91447636 | 806 | /* |
2d21ac55 | 807 | * reap_child_locked |
91447636 A |
808 | * |
809 | * Description: Given a process from which all status information needed | |
810 | * has already been extracted, if the process is a ptrace | |
811 | * attach process, detach it and give it back to its real | |
812 | * parent, else recover all resources remaining associated | |
813 | * with it. | |
814 | * | |
2d21ac55 A |
815 | * Parameters: proc_t parent Parent of process being reaped |
816 | * proc_t child Process to reap | |
91447636 A |
817 | * |
818 | * Returns: 0 Process was not reaped because it | |
819 | * came from an attach | |
820 | * 1 Process was reaped | |
821 | */ | |
822 | static int | |
2d21ac55 | 823 | reap_child_locked(proc_t parent, proc_t child, int deadparent, int locked, int droplock) |
1c79356b | 824 | { |
2d21ac55 | 825 | proc_t trace_parent; /* Traced parent process, if tracing */ |
1c79356b | 826 | |
91447636 A |
827 | /* |
828 | * If we got the child via a ptrace 'attach', | |
829 | * we need to give it back to the old parent. | |
830 | */ | |
2d21ac55 A |
831 | if (locked == 1) |
832 | proc_list_unlock(); | |
833 | if (child->p_oppid && (trace_parent = proc_find(child->p_oppid))) { | |
834 | proc_lock(child); | |
91447636 | 835 | child->p_oppid = 0; |
2d21ac55 | 836 | proc_unlock(child); |
91447636 | 837 | if (trace_parent != initproc) { |
2d21ac55 A |
838 | /* |
839 | * proc internal fileds and p_ucred usage safe | |
840 | * here as child is dead and is not reaped or | |
841 | * reparented yet | |
842 | */ | |
843 | proc_lock(trace_parent); | |
91447636 A |
844 | trace_parent->si_pid = child->p_pid; |
845 | trace_parent->si_status = child->p_xstat; | |
846 | trace_parent->si_code = CLD_CONTINUED; | |
847 | trace_parent->si_uid = child->p_ucred->cr_ruid; | |
2d21ac55 | 848 | proc_unlock(trace_parent); |
91447636 | 849 | } |
2d21ac55 | 850 | proc_reparentlocked(child, trace_parent, 1, 0); |
91447636 | 851 | psignal(trace_parent, SIGCHLD); |
2d21ac55 | 852 | proc_list_lock(); |
91447636 | 853 | wakeup((caddr_t)trace_parent); |
2d21ac55 A |
854 | child->p_listflag &= ~P_LIST_WAITING; |
855 | wakeup(&child->p_stat); | |
856 | proc_list_unlock(); | |
857 | proc_rele(trace_parent); | |
858 | if ((locked == 1) && (droplock == 0)) | |
859 | proc_list_lock(); | |
91447636 A |
860 | return (0); |
861 | } | |
2d21ac55 A |
862 | |
863 | proc_knote(child, NOTE_REAP); | |
864 | ||
91447636 A |
865 | child->p_xstat = 0; |
866 | if (child->p_ru) { | |
2d21ac55 A |
867 | proc_lock(parent); |
868 | #if 3839178 | |
869 | /* | |
870 | * If the parent is ignoring SIGCHLD, then POSIX requires | |
871 | * us to not add the resource usage to the parent process - | |
872 | * we are only going to hand it off to init to get reaped. | |
873 | * We should contest the standard in this case on the basis | |
874 | * of RLIMIT_CPU. | |
875 | */ | |
876 | if (!(parent->p_flag & P_NOCLDWAIT)) | |
877 | #endif /* 3839178 */ | |
878 | ruadd(&parent->p_stats->p_cru, child->p_ru); | |
879 | proc_unlock(parent); | |
91447636 A |
880 | FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE); |
881 | child->p_ru = NULL; | |
882 | } else { | |
883 | printf("Warning : lost p_ru for %s\n", child->p_comm); | |
884 | } | |
1c79356b | 885 | |
91447636 A |
886 | /* |
887 | * Decrement the count of procs running with this uid. | |
2d21ac55 A |
888 | * p_ucred usage is safe here as it is an exited process. |
889 | * and refernce is dropped after these calls down below | |
890 | * (locking protection is provided by list lock held in chgproccnt) | |
91447636 A |
891 | */ |
892 | (void)chgproccnt(child->p_ucred->cr_ruid, -1); | |
1c79356b | 893 | |
2d21ac55 A |
894 | #if CONFIG_LCTX |
895 | ALLLCTX_LOCK; | |
896 | leavelctx(child); | |
897 | ALLLCTX_UNLOCK; | |
898 | #endif | |
899 | ||
91447636 A |
900 | /* |
901 | * Free up credentials. | |
902 | */ | |
0c530ab8 A |
903 | if (IS_VALID_CRED(child->p_ucred)) { |
904 | kauth_cred_unref(&child->p_ucred); | |
905 | } | |
1c79356b | 906 | |
2d21ac55 A |
907 | /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */ |
908 | ||
91447636 A |
909 | /* |
910 | * Finally finished with old proc entry. | |
911 | * Unlink it from its process group and free it. | |
912 | */ | |
913 | leavepgrp(child); | |
2d21ac55 A |
914 | |
915 | proc_list_lock(); | |
91447636 | 916 | LIST_REMOVE(child, p_list); /* off zombproc */ |
2d21ac55 | 917 | parent->p_childrencnt--; |
91447636 | 918 | LIST_REMOVE(child, p_sibling); |
2d21ac55 A |
919 | /* If there are no more children wakeup parent */ |
920 | if ((deadparent != 0) && (LIST_EMPTY(&parent->p_children))) | |
921 | wakeup((caddr_t)parent); /* with list lock held */ | |
922 | child->p_listflag &= ~P_LIST_WAITING; | |
b36670ce | 923 | wakeup(&child->p_stat); |
1c79356b | 924 | |
2d21ac55 A |
925 | /* Take it out of process hash */ |
926 | LIST_REMOVE(child, p_hash); | |
927 | child->p_listflag &= ~P_LIST_INHASH; | |
928 | proc_checkdeadrefs(child); | |
929 | nprocs--; | |
930 | ||
931 | proc_list_unlock(); | |
932 | ||
91447636 A |
933 | lck_mtx_destroy(&child->p_mlock, proc_lck_grp); |
934 | lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp); | |
2d21ac55 A |
935 | #if CONFIG_DTRACE |
936 | lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp); | |
937 | #endif | |
938 | lck_spin_destroy(&child->p_slock, proc_lck_grp); | |
939 | workqueue_destroy_lock(child); | |
940 | ||
91447636 | 941 | FREE_ZONE(child, sizeof *child, M_PROC); |
2d21ac55 A |
942 | if ((locked == 1) && (droplock == 0)) |
943 | proc_list_lock(); | |
944 | ||
91447636 | 945 | return (1); |
1c79356b A |
946 | } |
947 | ||
1c79356b A |
948 | |
949 | int | |
91447636 | 950 | wait1continue(int result) |
1c79356b | 951 | { |
7b1edb79 | 952 | void *vt; |
91447636 | 953 | thread_t thread; |
7b1edb79 | 954 | int *retval; |
2d21ac55 | 955 | proc_t p; |
1c79356b | 956 | |
7b1edb79 A |
957 | if (result) |
958 | return(result); | |
1c79356b | 959 | |
7b1edb79 | 960 | p = current_proc(); |
91447636 A |
961 | thread = current_thread(); |
962 | vt = get_bsduthreadarg(thread); | |
963 | retval = get_bsduthreadrval(thread); | |
2d21ac55 | 964 | return(wait4(p, (struct wait4_args *)vt, retval)); |
1c79356b A |
965 | } |
966 | ||
967 | int | |
2d21ac55 | 968 | wait4(proc_t q, struct wait4_args *uap, register_t *retval) |
1c79356b | 969 | { |
2d21ac55 A |
970 | __pthread_testcancel(1); |
971 | return(wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval)); | |
972 | } | |
973 | ||
974 | int | |
975 | wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, register_t *retval) | |
976 | { | |
977 | int nfound; | |
978 | proc_t p; | |
1c79356b A |
979 | int status, error; |
980 | ||
1c79356b | 981 | if (uap->pid == 0) |
2d21ac55 | 982 | uap->pid = -q->p_pgrpid; |
1c79356b A |
983 | |
984 | loop: | |
2d21ac55 A |
985 | proc_list_lock(); |
986 | loop1: | |
1c79356b A |
987 | nfound = 0; |
988 | for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { | |
989 | if (uap->pid != WAIT_ANY && | |
990 | p->p_pid != uap->pid && | |
2d21ac55 | 991 | p->p_pgrpid != -(uap->pid)) |
1c79356b | 992 | continue; |
2d21ac55 | 993 | |
1c79356b | 994 | nfound++; |
91447636 A |
995 | |
996 | /* XXX This is racy because we don't get the lock!!!! */ | |
997 | ||
2d21ac55 A |
998 | if (p->p_listflag & P_LIST_WAITING) { |
999 | (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); | |
1000 | goto loop1; | |
7b1edb79 | 1001 | } |
2d21ac55 A |
1002 | p->p_listflag |= P_LIST_WAITING; /* only allow single thread to wait() */ |
1003 | ||
7b1edb79 | 1004 | |
1c79356b | 1005 | if (p->p_stat == SZOMB) { |
2d21ac55 A |
1006 | proc_list_unlock(); |
1007 | #if CONFIG_MACF | |
1008 | if ((error = mac_proc_check_wait(q, p)) != 0) | |
1009 | goto out; | |
1010 | #endif | |
1c79356b | 1011 | retval[0] = p->p_pid; |
1c79356b | 1012 | if (uap->status) { |
2d21ac55 A |
1013 | /* Legacy apps expect only 8 bits of status */ |
1014 | status = 0xffff & p->p_xstat; /* convert to int */ | |
91447636 A |
1015 | error = copyout((caddr_t)&status, |
1016 | uap->status, | |
1017 | sizeof(status)); | |
2d21ac55 A |
1018 | if (error) |
1019 | goto out; | |
1c79356b | 1020 | } |
91447636 A |
1021 | if (uap->rusage) { |
1022 | if (p->p_ru == NULL) { | |
1023 | error = ENOMEM; | |
1024 | } else { | |
1025 | if (IS_64BIT_PROCESS(q)) { | |
1026 | struct user_rusage my_rusage; | |
1027 | munge_rusage(p->p_ru, &my_rusage); | |
1028 | error = copyout((caddr_t)&my_rusage, | |
1029 | uap->rusage, | |
1030 | sizeof (my_rusage)); | |
1031 | } | |
1032 | else { | |
1033 | error = copyout((caddr_t)p->p_ru, | |
1034 | uap->rusage, | |
1035 | sizeof (struct rusage)); | |
1036 | } | |
9bccf70c | 1037 | } |
91447636 | 1038 | /* information unavailable? */ |
2d21ac55 A |
1039 | if (error) |
1040 | goto out; | |
1c79356b A |
1041 | } |
1042 | ||
91447636 | 1043 | /* Clean up */ |
2d21ac55 A |
1044 | if (!reap_child_locked(q, p, 0, 0, 0)) { |
1045 | proc_list_lock(); | |
1046 | p->p_listflag &= ~P_LIST_WAITING; | |
b36670ce | 1047 | wakeup(&p->p_stat); |
2d21ac55 | 1048 | proc_list_unlock(); |
b36670ce | 1049 | } |
91447636 | 1050 | |
1c79356b A |
1051 | return (0); |
1052 | } | |
2d21ac55 A |
1053 | if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 && |
1054 | (p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) { | |
1055 | proc_list_unlock(); | |
1056 | #if CONFIG_MACF | |
1057 | if ((error = mac_proc_check_wait(q, p)) != 0) | |
1058 | goto out; | |
1059 | #endif | |
1060 | proc_lock(p); | |
1061 | p->p_lflag |= P_LWAITED; | |
1062 | proc_unlock(p); | |
1c79356b | 1063 | retval[0] = p->p_pid; |
1c79356b A |
1064 | if (uap->status) { |
1065 | status = W_STOPCODE(p->p_xstat); | |
1066 | error = copyout((caddr_t)&status, | |
91447636 | 1067 | uap->status, |
1c79356b A |
1068 | sizeof(status)); |
1069 | } else | |
1070 | error = 0; | |
2d21ac55 A |
1071 | goto out; |
1072 | } | |
1073 | /* | |
1074 | * If we are waiting for continued processses, and this | |
1075 | * process was continued | |
1076 | */ | |
1077 | if ((uap->options & WCONTINUED) && | |
1078 | (p->p_flag & P_CONTINUED)) { | |
1079 | proc_list_unlock(); | |
1080 | #if CONFIG_MACF | |
1081 | if ((error = mac_proc_check_wait(q, p)) != 0) | |
1082 | goto out; | |
1083 | #endif | |
1084 | ||
1085 | /* Prevent other process for waiting for this event */ | |
1086 | OSBitAndAtomic(~((uint32_t)P_CONTINUED), (UInt32 *)&p->p_flag); | |
1087 | retval[0] = p->p_pid; | |
1088 | if (uap->status) { | |
1089 | status = W_STOPCODE(SIGCONT); | |
1090 | error = copyout((caddr_t)&status, | |
1091 | uap->status, | |
1092 | sizeof(status)); | |
1093 | } else | |
1094 | error = 0; | |
1095 | goto out; | |
1c79356b | 1096 | } |
2d21ac55 | 1097 | p->p_listflag &= ~P_LIST_WAITING; |
7b1edb79 | 1098 | wakeup(&p->p_stat); |
1c79356b | 1099 | } |
2d21ac55 A |
1100 | /* list lock is held when we get here any which way */ |
1101 | if (nfound == 0) { | |
1102 | proc_list_unlock(); | |
1c79356b | 1103 | return (ECHILD); |
2d21ac55 | 1104 | } |
7b1edb79 | 1105 | |
1c79356b A |
1106 | if (uap->options & WNOHANG) { |
1107 | retval[0] = 0; | |
2d21ac55 | 1108 | proc_list_unlock(); |
1c79356b A |
1109 | return (0); |
1110 | } | |
1111 | ||
2d21ac55 | 1112 | if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue))) |
91447636 A |
1113 | return (error); |
1114 | ||
1115 | goto loop; | |
2d21ac55 A |
1116 | out: |
1117 | proc_list_lock(); | |
1118 | p->p_listflag &= ~P_LIST_WAITING; | |
1119 | wakeup(&p->p_stat); | |
1120 | proc_list_unlock(); | |
1121 | return (error); | |
91447636 A |
1122 | } |
1123 | ||
1124 | ||
1125 | int | |
1126 | waitidcontinue(int result) | |
1127 | { | |
1128 | void *vt; | |
1129 | thread_t thread; | |
1130 | int *retval; | |
91447636 A |
1131 | |
1132 | if (result) | |
1133 | return(result); | |
1134 | ||
91447636 A |
1135 | thread = current_thread(); |
1136 | vt = get_bsduthreadarg(thread); | |
1137 | retval = get_bsduthreadrval(thread); | |
2d21ac55 | 1138 | return(waitid(current_proc(), (struct waitid_args *)vt, retval)); |
91447636 A |
1139 | } |
1140 | ||
1141 | /* | |
1142 | * Description: Suspend the calling thread until one child of the process | |
1143 | * containing the calling thread changes state. | |
1144 | * | |
1145 | * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL | |
1146 | * uap->id pid_t or gid_t or ignored | |
1147 | * uap->infop Address of signinfo_t struct in | |
1148 | * user space into which to return status | |
1149 | * uap->options flag values | |
1150 | * | |
1151 | * Returns: 0 Success | |
1152 | * !0 Error returning status to user space | |
1153 | */ | |
1154 | int | |
2d21ac55 A |
1155 | waitid(proc_t q, struct waitid_args *uap, register_t *retval) |
1156 | { | |
1157 | __pthread_testcancel(1); | |
1158 | return(waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval)); | |
1159 | } | |
1160 | ||
1161 | int | |
1162 | waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap, __unused register_t *retval) | |
91447636 A |
1163 | { |
1164 | user_siginfo_t collect64; /* siginfo data to return to caller */ | |
1165 | ||
2d21ac55 A |
1166 | int nfound; |
1167 | proc_t p; | |
91447636 A |
1168 | int error; |
1169 | ||
2d21ac55 A |
1170 | /* |
1171 | * Forced validation of options for T.waitpid 21; should be a TSD! | |
1172 | * This will pass the test, but note that we have more bits than the | |
1173 | * standard specifies that we will allow in, in this case. The test | |
1174 | * passes because they light all the bits, not just the ones we allow, | |
1175 | * and so the following check returns EINVAL like the test wants. | |
1176 | */ | |
1177 | if (((uap->options & (WNOHANG|WNOWAIT|WCONTINUED|WUNTRACED|WSTOPPED|WEXITED)) != uap->options) || | |
1178 | (uap->options == 0)) | |
1179 | return (EINVAL); /* bits set that aren't recognized */ | |
1180 | ||
1181 | /* | |
1182 | * Overly critical options checking, per POSIX | |
1183 | */ | |
1184 | switch(uap->idtype) { | |
1185 | case P_PID: /* child with process ID equal to... */ | |
1186 | case P_PGID: /* child with process group ID equal to... */ | |
1187 | if (((int)uap->id) < 0) | |
1188 | return (EINVAL); | |
1189 | break; | |
1190 | case P_ALL: /* any child */ | |
1191 | break; | |
1192 | } | |
1193 | ||
91447636 | 1194 | loop: |
2d21ac55 A |
1195 | proc_list_lock(); |
1196 | loop1: | |
91447636 A |
1197 | nfound = 0; |
1198 | for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { | |
1199 | switch(uap->idtype) { | |
1200 | case P_PID: /* child with process ID equal to... */ | |
1201 | if (p->p_pid != (pid_t)uap->id) | |
1202 | continue; | |
1203 | break; | |
1204 | case P_PGID: /* child with process group ID equal to... */ | |
2d21ac55 | 1205 | if (p->p_pgrpid != (pid_t)uap->id) |
91447636 A |
1206 | continue; |
1207 | break; | |
1208 | case P_ALL: /* any child */ | |
1209 | break; | |
1210 | } | |
1211 | ||
1212 | /* XXX This is racy because we don't get the lock!!!! */ | |
1213 | ||
1214 | /* | |
1215 | * Wait collision; go to sleep and restart; used to maintain | |
1216 | * the single return for waited process guarantee. | |
1217 | */ | |
2d21ac55 A |
1218 | if (p->p_listflag & P_LIST_WAITING) { |
1219 | (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitidcoll", 0); | |
1220 | goto loop1; | |
91447636 | 1221 | } |
2d21ac55 | 1222 | p->p_listflag |= P_LIST_WAITING; /* mark busy */ |
91447636 A |
1223 | |
1224 | nfound++; | |
1225 | ||
1226 | /* | |
1227 | * Types of processes we are interested in | |
1228 | * | |
1229 | * XXX Don't know what to do for WCONTINUED?!? | |
1230 | */ | |
1231 | switch(p->p_stat) { | |
1232 | case SZOMB: /* Exited */ | |
1233 | if (!(uap->options & WEXITED)) | |
1234 | break; | |
1235 | ||
2d21ac55 A |
1236 | /* drop the lock and the thread is going to return */ |
1237 | proc_list_unlock(); | |
1238 | ||
91447636 | 1239 | /* Collect "siginfo" information for caller */ |
2d21ac55 | 1240 | collect64.si_signo = SIGCHLD; |
91447636 A |
1241 | collect64.si_code = 0; |
1242 | collect64.si_errno = 0; | |
1243 | collect64.si_pid = 0; | |
1244 | collect64.si_uid = 0; | |
1245 | collect64.si_addr = 0; | |
2d21ac55 | 1246 | collect64.si_status = WEXITSTATUS(p->p_xstat); |
91447636 A |
1247 | collect64.si_band = 0; |
1248 | ||
1249 | if (IS_64BIT_PROCESS(p)) { | |
1250 | error = copyout((caddr_t)&collect64, | |
1251 | uap->infop, | |
1252 | sizeof(collect64)); | |
1253 | } else { | |
1254 | siginfo_t collect; | |
1255 | siginfo_64to32(&collect64,&collect); | |
1256 | error = copyout((caddr_t)&collect, | |
1257 | uap->infop, | |
1258 | sizeof(collect)); | |
1259 | } | |
1260 | /* information unavailable? */ | |
2d21ac55 A |
1261 | if (error) |
1262 | goto out; | |
91447636 A |
1263 | |
1264 | /* Prevent other process for waiting for this event? */ | |
1265 | if (!(uap->options & WNOWAIT)) { | |
1266 | /* Clean up */ | |
2d21ac55 A |
1267 | if (!reap_child_locked(q, p, 0, 0, 0)) { |
1268 | proc_list_lock(); | |
1269 | p->p_listflag &= ~P_LIST_WAITING; | |
b36670ce | 1270 | wakeup(&p->p_stat); |
2d21ac55 | 1271 | proc_list_unlock(); |
b36670ce | 1272 | } |
2d21ac55 A |
1273 | } else { |
1274 | proc_list_lock(); | |
1275 | p->p_listflag &= ~P_LIST_WAITING; | |
1276 | proc_list_unlock(); | |
91447636 A |
1277 | } |
1278 | ||
1279 | return (0); | |
1280 | ||
1281 | case SSTOP: /* Stopped */ | |
1282 | /* | |
1283 | * If we are not interested in stopped processes, then | |
1284 | * ignore this one. | |
1285 | */ | |
1286 | if (!(uap->options & WSTOPPED)) | |
1287 | break; | |
1288 | ||
1289 | /* | |
1290 | * If someone has already waited it, we lost a race | |
1291 | * to be the one to return status. | |
1292 | */ | |
2d21ac55 | 1293 | if ((p->p_lflag & P_LWAITED) != 0) |
91447636 A |
1294 | break; |
1295 | ||
2d21ac55 A |
1296 | /* drop the lock and the thread is going to return */ |
1297 | proc_list_unlock(); | |
91447636 A |
1298 | |
1299 | /* Collect "siginfo" information for caller */ | |
2d21ac55 | 1300 | collect64.si_signo = SIGCHLD; |
91447636 A |
1301 | collect64.si_code = 0; |
1302 | collect64.si_errno = 0; | |
1303 | collect64.si_pid = 0; | |
1304 | collect64.si_uid = 0; | |
1305 | collect64.si_addr = 0; | |
2d21ac55 | 1306 | proc_lock(p); |
91447636 | 1307 | collect64.si_status = p->p_xstat; |
2d21ac55 | 1308 | proc_unlock(p); |
91447636 A |
1309 | collect64.si_band = 0; |
1310 | ||
1311 | if (IS_64BIT_PROCESS(p)) { | |
1312 | error = copyout((caddr_t)&collect64, | |
1313 | uap->infop, | |
1314 | sizeof(collect64)); | |
1315 | } else { | |
1316 | siginfo_t collect; | |
1317 | siginfo_64to32(&collect64,&collect); | |
1318 | error = copyout((caddr_t)&collect, | |
1319 | uap->infop, | |
1320 | sizeof(collect)); | |
1321 | } | |
1322 | /* information unavailable? */ | |
2d21ac55 A |
1323 | if (error) |
1324 | goto out; | |
91447636 A |
1325 | |
1326 | /* Prevent other process for waiting for this event? */ | |
1327 | if (!(uap->options & WNOWAIT)) { | |
2d21ac55 A |
1328 | proc_lock(p); |
1329 | p->p_lflag |= P_LWAITED; | |
1330 | proc_unlock(p); | |
91447636 A |
1331 | } |
1332 | ||
2d21ac55 A |
1333 | error = 0; |
1334 | goto out; | |
91447636 A |
1335 | |
1336 | default: /* All others */ | |
1337 | /* ...meaning Continued */ | |
1338 | if (!(uap->options & WCONTINUED)) | |
1339 | break; | |
1340 | ||
1341 | /* | |
1342 | * If the flag isn't set, then this process has not | |
1343 | * been stopped and continued, or the status has | |
1344 | * already been reaped by another caller of waitid(). | |
1345 | */ | |
1346 | if ((p->p_flag & P_CONTINUED) == 0) | |
1347 | break; | |
1348 | ||
2d21ac55 A |
1349 | /* drop the lock and the thread is going to return */ |
1350 | proc_list_unlock(); | |
1351 | ||
91447636 | 1352 | /* Collect "siginfo" information for caller */ |
2d21ac55 A |
1353 | proc_lock(p); |
1354 | collect64.si_signo = SIGCHLD; | |
1355 | collect64.si_code = CLD_CONTINUED; | |
91447636 | 1356 | collect64.si_errno = 0; |
2d21ac55 | 1357 | collect64.si_pid = p->p_contproc; |
91447636 A |
1358 | collect64.si_uid = 0; |
1359 | collect64.si_addr = 0; | |
1360 | collect64.si_status = p->p_xstat; | |
1361 | collect64.si_band = 0; | |
2d21ac55 | 1362 | proc_unlock(p); |
91447636 A |
1363 | |
1364 | if (IS_64BIT_PROCESS(p)) { | |
1365 | error = copyout((caddr_t)&collect64, | |
1366 | uap->infop, | |
1367 | sizeof(collect64)); | |
1368 | } else { | |
1369 | siginfo_t collect; | |
1370 | siginfo_64to32(&collect64,&collect); | |
1371 | error = copyout((caddr_t)&collect, | |
1372 | uap->infop, | |
1373 | sizeof(collect)); | |
1374 | } | |
1375 | /* information unavailable? */ | |
2d21ac55 A |
1376 | if (error) |
1377 | goto out; | |
91447636 A |
1378 | |
1379 | /* Prevent other process for waiting for this event? */ | |
1380 | if (!(uap->options & WNOWAIT)) { | |
2d21ac55 | 1381 | OSBitAndAtomic(~((uint32_t)P_CONTINUED), (UInt32 *)&p->p_flag); |
91447636 A |
1382 | } |
1383 | ||
2d21ac55 A |
1384 | error = 0; |
1385 | goto out; | |
91447636 | 1386 | } |
2d21ac55 | 1387 | /* LIST LOCK IS HELD HERE */ |
91447636 | 1388 | /* Not a process we are interested in; go on to next child */ |
2d21ac55 A |
1389 | |
1390 | p->p_listflag &= ~P_LIST_WAITING; | |
91447636 A |
1391 | wakeup(&p->p_stat); |
1392 | } | |
1393 | ||
2d21ac55 | 1394 | /* list lock is always held */ |
91447636 | 1395 | /* No child processes that could possibly satisfy the request? */ |
2d21ac55 A |
1396 | if (nfound == 0) { |
1397 | proc_list_unlock(); | |
91447636 | 1398 | return (ECHILD); |
2d21ac55 | 1399 | } |
91447636 A |
1400 | |
1401 | if (uap->options & WNOHANG) { | |
2d21ac55 | 1402 | proc_list_unlock(); |
91447636 A |
1403 | return (0); |
1404 | } | |
1405 | ||
2d21ac55 | 1406 | if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue))) |
1c79356b | 1407 | return (error); |
7b1edb79 | 1408 | |
1c79356b | 1409 | goto loop; |
2d21ac55 A |
1410 | out: |
1411 | proc_list_lock(); | |
1412 | p->p_listflag &= ~P_LIST_WAITING; | |
1413 | wakeup(&p->p_stat); | |
1414 | proc_list_unlock(); | |
1415 | return (error); | |
1c79356b A |
1416 | } |
1417 | ||
1418 | /* | |
1419 | * make process 'parent' the new parent of process 'child'. | |
1420 | */ | |
1421 | void | |
2d21ac55 | 1422 | proc_reparentlocked(proc_t child, proc_t parent, int cansignal, int locked) |
1c79356b | 1423 | { |
2d21ac55 | 1424 | proc_t oldparent = PROC_NULL; |
1c79356b A |
1425 | |
1426 | if (child->p_pptr == parent) | |
1427 | return; | |
1428 | ||
2d21ac55 A |
1429 | if (locked == 0) |
1430 | proc_list_lock(); | |
1431 | ||
1432 | oldparent = child->p_pptr; | |
1433 | #if __PROC_INTERNAL_DEBUG | |
1434 | if (oldparent == PROC_NULL) | |
1435 | panic("proc_reparent: process %x does not have a parent\n", (unsigned int)child); | |
1436 | #endif | |
1437 | ||
1c79356b | 1438 | LIST_REMOVE(child, p_sibling); |
2d21ac55 A |
1439 | #if __PROC_INTERNAL_DEBUG |
1440 | if (oldparent->p_childrencnt == 0) | |
1441 | panic("process children count already 0\n"); | |
1442 | #endif | |
1443 | oldparent->p_childrencnt--; | |
1444 | #if __PROC_INTERNAL_DEBUG1 | |
1445 | if (oldparent->p_childrencnt < 0) | |
1446 | panic("process children count -ve\n"); | |
1447 | #endif | |
1c79356b | 1448 | LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); |
2d21ac55 | 1449 | parent->p_childrencnt++; |
1c79356b | 1450 | child->p_pptr = parent; |
2d21ac55 A |
1451 | child->p_ppid = parent->p_pid; |
1452 | ||
1453 | proc_list_unlock(); | |
91447636 | 1454 | |
2d21ac55 | 1455 | if ((cansignal != 0) && (initproc == parent) && (child->p_stat == SZOMB)) |
91447636 | 1456 | psignal(initproc, SIGCHLD); |
2d21ac55 A |
1457 | if (locked == 1) |
1458 | proc_list_lock(); | |
1c79356b A |
1459 | } |
1460 | ||
1c79356b A |
1461 | /* |
1462 | * Make the current process an "init" process, meaning | |
1463 | * that it doesn't have a parent, and that it won't be | |
1464 | * gunned down by kill(-1, 0). | |
1465 | */ | |
7b1edb79 | 1466 | kern_return_t |
91447636 | 1467 | init_process(__unused struct init_process_args *args) |
1c79356b | 1468 | { |
2d21ac55 | 1469 | proc_t p = current_proc(); |
1c79356b | 1470 | |
e5568f75 | 1471 | AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS); |
91447636 | 1472 | if (suser(kauth_cred_get(), &p->p_acflag)) { |
e5568f75 | 1473 | AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS); |
1c79356b | 1474 | return(KERN_NO_ACCESS); |
e5568f75 | 1475 | } |
1c79356b | 1476 | |
2d21ac55 | 1477 | if (p->p_pid != 1 && p->p_pgrpid != p->p_pid) |
1c79356b | 1478 | enterpgrp(p, p->p_pid, 0); |
2d21ac55 | 1479 | OSBitOrAtomic(P_SYSTEM, (UInt32 *)&p->p_flag); |
1c79356b A |
1480 | |
1481 | /* | |
1482 | * Take us out of the sibling chain, and | |
1483 | * out of our parent's child chain. | |
1484 | */ | |
2d21ac55 | 1485 | proc_list_lock(); |
1c79356b A |
1486 | LIST_REMOVE(p, p_sibling); |
1487 | p->p_sibling.le_prev = NULL; | |
1488 | p->p_sibling.le_next = NULL; | |
1489 | p->p_pptr = kernproc; | |
2d21ac55 A |
1490 | p->p_ppid = 0; |
1491 | proc_list_unlock(); | |
1492 | ||
1c79356b | 1493 | |
e5568f75 | 1494 | AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS); |
1c79356b A |
1495 | return(KERN_SUCCESS); |
1496 | } | |
1497 | ||
7b1edb79 | 1498 | |
0b4e3aa0 A |
1499 | /* |
1500 | * Exit: deallocate address space and other resources, change proc state | |
1501 | * to zombie, and unlink proc from allproc and parent's lists. Save exit | |
1502 | * status and rusage for wait(). Check for child processes and orphan them. | |
1503 | */ | |
1504 | ||
91447636 | 1505 | void |
2d21ac55 A |
1506 | vfork_exit(proc_t p, int rv) |
1507 | { | |
1508 | vfork_exit_internal(p, rv, 0); | |
1509 | } | |
1510 | ||
1511 | void | |
1512 | vfork_exit_internal(proc_t p, int rv, int forceexit) | |
0b4e3aa0 | 1513 | { |
91447636 A |
1514 | thread_t self = current_thread(); |
1515 | #ifdef FIXME | |
0b4e3aa0 | 1516 | struct task *task = p->task; |
91447636 | 1517 | #endif |
0b4e3aa0 A |
1518 | struct uthread *ut; |
1519 | ||
91447636 A |
1520 | /* |
1521 | * If a thread in this task has already | |
1522 | * called exit(), then halt any others | |
1523 | * right here. | |
1524 | */ | |
1525 | ||
1526 | ut = get_bsdthread_info(self); | |
91447636 | 1527 | |
55e303ae | 1528 | |
2d21ac55 A |
1529 | proc_lock(p); |
1530 | if ((p->p_lflag & P_LPEXIT) == P_LPEXIT) { | |
1531 | /* | |
1532 | * This happens when a parent exits/killed and vfork is in progress | |
1533 | * other threads. But shutdown code for ex has already called exit1() | |
1534 | */ | |
1535 | proc_unlock(p); | |
1536 | return; | |
1537 | } | |
1538 | p->p_lflag |= (P_LEXIT | P_LPEXIT); | |
1539 | proc_unlock(p); | |
1540 | ||
1541 | if (forceexit == 0) { | |
1542 | /* | |
1543 | * parent of a vfork child has already called exit() and the | |
1544 | * thread that has vfork in proress terminates. So there is no | |
1545 | * separate address space here and it has already been marked for | |
1546 | * termination. This was never covered before and could cause problems | |
1547 | * if we block here for outside code. | |
1548 | */ | |
1549 | /* Notify the perf server */ | |
1550 | (void)sys_perf_notify(self, p->p_pid); | |
1551 | } | |
55e303ae | 1552 | |
0b4e3aa0 A |
1553 | /* |
1554 | * Remove proc from allproc queue and from pidhash chain. | |
1555 | * Need to do this before we do anything that can block. | |
1556 | * Not doing causes things like mount() find this on allproc | |
1557 | * in partially cleaned state. | |
1558 | */ | |
2d21ac55 A |
1559 | |
1560 | proc_list_lock(); | |
1561 | ||
0b4e3aa0 | 1562 | LIST_REMOVE(p, p_list); |
55e303ae | 1563 | LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */ |
2d21ac55 A |
1564 | /* will not be visible via proc_find */ |
1565 | p->p_listflag |= P_LIST_EXITED; | |
0b4e3aa0 | 1566 | |
2d21ac55 | 1567 | proc_list_unlock(); |
0b4e3aa0 | 1568 | |
2d21ac55 | 1569 | proc_lock(p); |
0b4e3aa0 | 1570 | p->p_xstat = rv; |
2d21ac55 A |
1571 | p->p_lflag &= ~(P_LTRACED | P_LPPWAIT); |
1572 | p->p_sigignore = ~0; | |
1573 | proc_unlock(p); | |
1574 | ||
1575 | proc_spinlock(p); | |
1576 | if (thread_call_cancel(p->p_rcall)) | |
1577 | p->p_ractive--; | |
1578 | ||
1579 | while (p->p_ractive > 0) { | |
1580 | proc_spinunlock(p); | |
1581 | ||
1582 | delay(1); | |
1583 | ||
1584 | proc_spinlock(p); | |
1585 | } | |
1586 | proc_spinunlock(p); | |
1587 | ||
1588 | thread_call_free(p->p_rcall); | |
1589 | p->p_rcall = NULL; | |
1590 | ||
1591 | ut->uu_siglist = 0; | |
0b4e3aa0 | 1592 | |
55e303ae | 1593 | vproc_exit(p); |
0b4e3aa0 A |
1594 | } |
1595 | ||
0b4e3aa0 | 1596 | void |
2d21ac55 | 1597 | vproc_exit(proc_t p) |
0b4e3aa0 | 1598 | { |
2d21ac55 A |
1599 | proc_t q; |
1600 | proc_t pp; | |
1601 | ||
1602 | vnode_t tvp; | |
91447636 | 1603 | #ifdef FIXME |
0b4e3aa0 | 1604 | struct task *task = p->task; |
91447636 | 1605 | #endif |
2d21ac55 A |
1606 | struct pgrp * pg; |
1607 | struct session *sessp; | |
1608 | boolean_t fstate; | |
0b4e3aa0 | 1609 | |
91447636 | 1610 | /* XXX Zombie allocation may fail, in which case stats get lost */ |
0b4e3aa0 A |
1611 | MALLOC_ZONE(p->p_ru, struct rusage *, |
1612 | sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK); | |
1613 | ||
2d21ac55 A |
1614 | |
1615 | proc_refdrain(p); | |
1616 | ||
0b4e3aa0 A |
1617 | /* |
1618 | * Close open files and release open-file table. | |
1619 | * This may block! | |
1620 | */ | |
1621 | fdfree(p); | |
1622 | ||
2d21ac55 A |
1623 | sessp = proc_session(p); |
1624 | if (SESS_LEADER(p, sessp)) { | |
1625 | ||
1626 | /* Protected by funnel for tty accesses */ | |
1627 | fstate = thread_funnel_set(kernel_flock, TRUE); | |
0b4e3aa0 | 1628 | |
2d21ac55 | 1629 | if (sessp->s_ttyvp != NULLVP) { |
fa4905b1 | 1630 | struct vnode *ttyvp; |
2d21ac55 | 1631 | int ttyvid; |
91447636 | 1632 | struct vfs_context context; |
2d21ac55 | 1633 | struct tty * tp; |
fa4905b1 | 1634 | |
0b4e3aa0 A |
1635 | /* |
1636 | * Controlling process. | |
1637 | * Signal foreground pgrp, | |
1638 | * drain controlling terminal | |
1639 | * and revoke access to controlling terminal. | |
1640 | */ | |
2d21ac55 A |
1641 | tp = sessp->s_ttyp; |
1642 | ||
1643 | if ((tp != TTY_NULL) && (tp->t_session == sessp)) { | |
1644 | tty_pgsignal(tp, SIGHUP, 1); | |
1645 | (void) ttywait(tp); | |
0b4e3aa0 A |
1646 | /* |
1647 | * The tty could have been revoked | |
1648 | * if we blocked. | |
1649 | */ | |
2d21ac55 A |
1650 | |
1651 | session_lock(sessp); | |
1652 | ttyvp = sessp->s_ttyvp; | |
1653 | ttyvid = sessp->s_ttyvid; | |
1654 | sessp->s_ttyvp = NULL; | |
1655 | sessp->s_ttyvid = 0; | |
1656 | sessp->s_ttyp = NULL; | |
1657 | sessp->s_ttypgrpid = NO_PID; | |
1658 | session_unlock(sessp); | |
1659 | ||
1660 | if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) { | |
1661 | context.vc_thread = proc_thread(p); /* XXX */ | |
1662 | context.vc_ucred = kauth_cred_proc_ref(p); | |
1663 | VNOP_REVOKE(ttyvp, REVOKEALL, &context); | |
1664 | vnode_put(ttyvp); | |
1665 | kauth_cred_unref(&context.vc_ucred); | |
1666 | } | |
1667 | } else { | |
1668 | session_lock(sessp); | |
1669 | ttyvp = sessp->s_ttyvp; | |
1670 | sessp->s_ttyvp = NULL; | |
1671 | sessp->s_ttyvid = 0; | |
1672 | sessp->s_ttyp = NULL; | |
1673 | sessp->s_ttypgrpid = NO_PID; | |
1674 | session_unlock(sessp); | |
0b4e3aa0 | 1675 | } |
2d21ac55 | 1676 | if (ttyvp) |
91447636 | 1677 | vnode_rele(ttyvp); |
0b4e3aa0 A |
1678 | /* |
1679 | * s_ttyp is not zero'd; we use this to indicate | |
1680 | * that the session once had a controlling terminal. | |
1681 | * (for logging and informational purposes) | |
1682 | */ | |
1683 | } | |
2d21ac55 A |
1684 | (void) thread_funnel_set(kernel_flock, fstate); |
1685 | ||
1686 | session_lock(sessp); | |
1687 | sessp->s_leader = NULL; | |
1688 | session_unlock(sessp); | |
0b4e3aa0 | 1689 | } |
2d21ac55 | 1690 | session_rele(sessp); |
0b4e3aa0 | 1691 | |
2d21ac55 A |
1692 | pg = proc_pgrp(p); |
1693 | fixjobc(p, pg, 0); | |
1694 | pg_rele(pg); | |
9bccf70c | 1695 | |
2d21ac55 | 1696 | p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
0b4e3aa0 | 1697 | |
2d21ac55 A |
1698 | proc_list_lock(); |
1699 | proc_childdrainstart(p); | |
1700 | while ((q = p->p_children.lh_first) != NULL) { | |
1701 | q->p_listflag |= P_LIST_DEADPARENT; | |
1702 | if (q->p_stat == SZOMB) { | |
1703 | if (p != q->p_pptr) | |
1704 | panic("parent child linkage broken"); | |
1705 | /* check for lookups by zomb sysctl */ | |
1706 | while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { | |
1707 | msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); | |
1708 | } | |
1709 | q->p_listflag |= P_LIST_WAITING; | |
1710 | /* | |
1711 | * This is a named reference and it is not granted | |
1712 | * if the reap is already in progress. So we get | |
1713 | * the reference here exclusively and their can be | |
1714 | * no waiters. So there is no need for a wakeup | |
1715 | * after we are done. AlsO the reap frees the structure | |
1716 | * and the proc struct cannot be used for wakeups as well. | |
1717 | * It is safe to use q here as this is system reap | |
1718 | */ | |
1719 | (void)reap_child_locked(p, q, 1, 1, 0); | |
1720 | } else { | |
1721 | proc_reparentlocked(q, initproc, 0, 1); | |
1722 | /* | |
1723 | * Traced processes are killed | |
1724 | * since their existence means someone is messing up. | |
1725 | */ | |
1726 | if (q->p_lflag & P_LTRACED) { | |
1727 | proc_list_unlock(); | |
1728 | proc_lock(q); | |
1729 | q->p_lflag &= ~P_LTRACED; | |
1730 | if (q->sigwait_thread) { | |
1731 | proc_unlock(q); | |
1732 | /* | |
1733 | * The sigwait_thread could be stopped at a | |
1734 | * breakpoint. Wake it up to kill. | |
1735 | * Need to do this as it could be a thread which is not | |
1736 | * the first thread in the task. So any attempts to kill | |
1737 | * the process would result into a deadlock on q->sigwait. | |
1738 | */ | |
1739 | thread_resume((thread_t)q->sigwait_thread); | |
1740 | clear_wait(q->sigwait_thread, THREAD_INTERRUPTED); | |
1741 | threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0); | |
1742 | } else | |
1743 | proc_unlock(q); | |
1744 | ||
1745 | psignal(q, SIGKILL); | |
1746 | proc_list_lock(); | |
0b4e3aa0 | 1747 | } |
0b4e3aa0 A |
1748 | } |
1749 | } | |
1750 | ||
2d21ac55 A |
1751 | proc_childdrainend(p); |
1752 | proc_list_unlock(); | |
1753 | ||
1754 | /* | |
1755 | * Release reference to text vnode | |
1756 | */ | |
1757 | tvp = p->p_textvp; | |
1758 | p->p_textvp = NULL; | |
1759 | if (tvp != NULLVP) { | |
1760 | vnode_rele(tvp); | |
1761 | } | |
1762 | ||
0b4e3aa0 A |
1763 | /* |
1764 | * Save exit status and final rusage info, adding in child rusage | |
91447636 A |
1765 | * info and self times. If we were unable to allocate a zombie |
1766 | * structure, this information is lost. | |
0b4e3aa0 | 1767 | */ |
2d21ac55 | 1768 | /* No need for locking here as no one than this thread can access this */ |
91447636 A |
1769 | if (p->p_ru != NULL) { |
1770 | *p->p_ru = p->p_stats->p_ru; | |
1771 | timerclear(&p->p_ru->ru_utime); | |
1772 | timerclear(&p->p_ru->ru_stime); | |
0b4e3aa0 A |
1773 | |
1774 | #ifdef FIXME | |
91447636 | 1775 | if (task) { |
0b4e3aa0 A |
1776 | task_basic_info_data_t tinfo; |
1777 | task_thread_times_info_data_t ttimesinfo; | |
1778 | int task_info_stuff, task_ttimes_stuff; | |
1779 | struct timeval ut,st; | |
1780 | ||
1781 | task_info_stuff = TASK_BASIC_INFO_COUNT; | |
1782 | task_info(task, TASK_BASIC_INFO, | |
1783 | &tinfo, &task_info_stuff); | |
1784 | p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds; | |
1785 | p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds; | |
1786 | p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds; | |
1787 | p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds; | |
1788 | ||
1789 | task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT; | |
1790 | task_info(task, TASK_THREAD_TIMES_INFO, | |
1791 | &ttimesinfo, &task_ttimes_stuff); | |
1792 | ||
1793 | ut.tv_sec = ttimesinfo.user_time.seconds; | |
1794 | ut.tv_usec = ttimesinfo.user_time.microseconds; | |
1795 | st.tv_sec = ttimesinfo.system_time.seconds; | |
1796 | st.tv_usec = ttimesinfo.system_time.microseconds; | |
1797 | timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime); | |
91447636 A |
1798 | timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime); |
1799 | } | |
0b4e3aa0 A |
1800 | #endif /* FIXME */ |
1801 | ||
91447636 A |
1802 | ruadd(p->p_ru, &p->p_stats->p_cru); |
1803 | } | |
0b4e3aa0 A |
1804 | |
1805 | /* | |
1806 | * Free up profiling buffers. | |
1807 | */ | |
1808 | { | |
1809 | struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn; | |
1810 | ||
1811 | p1 = p0->pr_next; | |
1812 | p0->pr_next = NULL; | |
1813 | p0->pr_scale = 0; | |
1814 | ||
1815 | for (; p1 != NULL; p1 = pn) { | |
1816 | pn = p1->pr_next; | |
91447636 | 1817 | kfree(p1, sizeof *p1); |
0b4e3aa0 A |
1818 | } |
1819 | } | |
1820 | ||
1821 | /* | |
1822 | * Other substructures are freed from wait(). | |
1823 | */ | |
2d21ac55 | 1824 | FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS); |
0b4e3aa0 A |
1825 | p->p_stats = NULL; |
1826 | ||
2d21ac55 | 1827 | FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS); |
0b4e3aa0 A |
1828 | p->p_sigacts = NULL; |
1829 | ||
2d21ac55 | 1830 | proc_limitdrop(p, 1); |
0b4e3aa0 A |
1831 | p->p_limit = NULL; |
1832 | ||
1833 | /* | |
1834 | * Finish up by terminating the task | |
1835 | * and halt this thread (only if a | |
1836 | * member of the task exiting). | |
1837 | */ | |
1838 | p->task = TASK_NULL; | |
1839 | ||
1840 | /* | |
1841 | * Notify parent that we're gone. | |
1842 | */ | |
2d21ac55 A |
1843 | pp = proc_parent(p); |
1844 | if ((p->p_listflag & P_LIST_DEADPARENT) == 0) { | |
1845 | if (pp != initproc) { | |
1846 | proc_lock(pp); | |
1847 | pp->si_pid = p->p_pid; | |
1848 | pp->si_status = p->p_xstat; | |
1849 | pp->si_code = CLD_EXITED; | |
1850 | /* | |
1851 | * p_ucred usage is safe as it is an exiting process | |
1852 | * and reference is dropped in reap | |
1853 | */ | |
1854 | pp->si_uid = p->p_ucred->cr_ruid; | |
1855 | proc_unlock(pp); | |
1856 | } | |
1857 | /* mark as a zombie */ | |
1858 | /* mark as a zombie */ | |
1859 | /* No need to take proc lock as all refs are drained and | |
1860 | * no one except parent (reaping ) can look at this. | |
1861 | * The write is to an int and is coherent. Also parent is | |
1862 | * keyed off of list lock for reaping | |
1863 | */ | |
1864 | p->p_stat = SZOMB; | |
0b4e3aa0 | 1865 | |
2d21ac55 | 1866 | psignal(pp, SIGCHLD); |
91447636 | 1867 | |
2d21ac55 A |
1868 | /* and now wakeup the parent */ |
1869 | proc_list_lock(); | |
1870 | wakeup((caddr_t)pp); | |
1871 | proc_list_unlock(); | |
1872 | } else { | |
1873 | proc_list_lock(); | |
1874 | p->p_stat = SZOMB; | |
1875 | /* check for lookups by zomb sysctl */ | |
1876 | while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) { | |
1877 | msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0); | |
1878 | } | |
1879 | p->p_listflag |= P_LIST_WAITING; | |
1880 | /* | |
1881 | * This is a named reference and it is not granted | |
1882 | * if the reap is already in progress. So we get | |
1883 | * the reference here exclusively and their can be | |
1884 | * no waiters. So there is no need for a wakeup | |
1885 | * after we are done. AlsO the reap frees the structure | |
1886 | * and the proc struct cannot be used for wakeups as well. | |
1887 | * It is safe to use p here as this is system reap | |
1888 | */ | |
1889 | (void)reap_child_locked(pp, p, 0, 1, 1); | |
1890 | /* list lock dropped by reap_child_locked */ | |
1891 | } | |
1892 | proc_rele(pp); | |
0b4e3aa0 | 1893 | } |
91447636 A |
1894 | |
1895 | ||
1896 | /* | |
1897 | * munge_rusage | |
1898 | * LP64 support - long is 64 bits if we are dealing with a 64 bit user | |
1899 | * process. We munge the kernel (32 bit) version of rusage into the | |
1900 | * 64 bit version. | |
1901 | */ | |
1902 | __private_extern__ void | |
1903 | munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p) | |
1904 | { | |
1905 | /* timeval changes size, so utime and stime need special handling */ | |
1906 | a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec; | |
1907 | a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec; | |
1908 | a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec; | |
1909 | a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec; | |
1910 | /* | |
1911 | * everything else can be a direct assign, since there is no loss | |
1912 | * of precision implied boing 32->64. | |
1913 | */ | |
1914 | a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss; | |
1915 | a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss; | |
1916 | a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss; | |
1917 | a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss; | |
1918 | a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt; | |
1919 | a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt; | |
1920 | a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap; | |
1921 | a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock; | |
1922 | a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock; | |
1923 | a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd; | |
1924 | a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv; | |
1925 | a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals; | |
1926 | a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw; | |
1927 | a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw; | |
1928 | } |