| 1 | /* |
| 2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_LICENSE_HEADER_START@ |
| 5 | * |
| 6 | * The contents of this file constitute Original Code as defined in and |
| 7 | * are subject to the Apple Public Source License Version 1.1 (the |
| 8 | * "License"). You may not use this file except in compliance with the |
| 9 | * License. Please obtain a copy of the License at |
| 10 | * http://www.apple.com/publicsource and read it before using this file. |
| 11 | * |
| 12 | * This Original Code and all software distributed under the License are |
| 13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
| 17 | * License for the specific language governing rights and limitations |
| 18 | * under the License. |
| 19 | * |
| 20 | * @APPLE_LICENSE_HEADER_END@ |
| 21 | */ |
| 22 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ |
| 23 | /* |
| 24 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 |
| 25 | * The Regents of the University of California. All rights reserved. |
| 26 | * (c) UNIX System Laboratories, Inc. |
| 27 | * All or some portions of this file are derived from material licensed |
| 28 | * to the University of California by American Telephone and Telegraph |
| 29 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
| 30 | * the permission of UNIX System Laboratories, Inc. |
| 31 | * |
| 32 | * Redistribution and use in source and binary forms, with or without |
| 33 | * modification, are permitted provided that the following conditions |
| 34 | * are met: |
| 35 | * 1. Redistributions of source code must retain the above copyright |
| 36 | * notice, this list of conditions and the following disclaimer. |
| 37 | * 2. Redistributions in binary form must reproduce the above copyright |
| 38 | * notice, this list of conditions and the following disclaimer in the |
| 39 | * documentation and/or other materials provided with the distribution. |
| 40 | * 3. All advertising materials mentioning features or use of this software |
| 41 | * must display the following acknowledgement: |
| 42 | * This product includes software developed by the University of |
| 43 | * California, Berkeley and its contributors. |
| 44 | * 4. Neither the name of the University nor the names of its contributors |
| 45 | * may be used to endorse or promote products derived from this software |
| 46 | * without specific prior written permission. |
| 47 | * |
| 48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 58 | * SUCH DAMAGE. |
| 59 | * |
| 60 | * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 |
| 61 | */ |
| 62 | |
| 63 | #include <machine/reg.h> |
| 64 | #include <machine/psl.h> |
| 65 | |
| 66 | #include "compat_43.h" |
| 67 | |
| 68 | #include <sys/param.h> |
| 69 | #include <sys/systm.h> |
| 70 | #include <sys/ioctl.h> |
| 71 | #include <sys/proc.h> |
| 72 | #include <sys/tty.h> |
| 73 | #include <sys/time.h> |
| 74 | #include <sys/resource.h> |
| 75 | #include <sys/kernel.h> |
| 76 | #include <sys/buf.h> |
| 77 | #include <sys/wait.h> |
| 78 | #include <sys/file.h> |
| 79 | #include <sys/vnode.h> |
| 80 | #include <sys/syslog.h> |
| 81 | #include <sys/malloc.h> |
| 82 | #include <sys/resourcevar.h> |
| 83 | #include <sys/ptrace.h> |
| 84 | #include <sys/user.h> |
| 85 | |
| 86 | #include <mach/mach_types.h> |
| 87 | #include <kern/thread.h> |
| 88 | #include <kern/thread_act.h> |
| 89 | #include <kern/assert.h> |
| 90 | |
| 91 | extern char init_task_failure_data[]; |
| 92 | int exit1 __P((struct proc *, int, int *)); |
| 93 | |
| 94 | /* |
| 95 | * exit -- |
| 96 | * Death of process. |
| 97 | */ |
| 98 | struct exit_args { |
| 99 | int rval; |
| 100 | }; |
| 101 | void |
| 102 | exit(p, uap, retval) |
| 103 | struct proc *p; |
| 104 | struct exit_args *uap; |
| 105 | int *retval; |
| 106 | { |
| 107 | exit1(p, W_EXITCODE(uap->rval, 0), retval); |
| 108 | |
| 109 | /* drop funnel befewo we return */ |
| 110 | thread_funnel_set(kernel_flock, FALSE); |
| 111 | thread_exception_return(); |
| 112 | /* NOTREACHED */ |
| 113 | while (TRUE) |
| 114 | thread_block(0); |
| 115 | /* NOTREACHED */ |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Exit: deallocate address space and other resources, change proc state |
| 120 | * to zombie, and unlink proc from allproc and parent's lists. Save exit |
| 121 | * status and rusage for wait(). Check for child processes and orphan them. |
| 122 | */ |
| 123 | int |
| 124 | exit1(p, rv, retval) |
| 125 | register struct proc *p; |
| 126 | int rv; |
| 127 | int * retval; |
| 128 | { |
| 129 | register struct proc *q, *nq; |
| 130 | thread_t self = current_thread(); |
| 131 | thread_act_t th_act_self = current_act(); |
| 132 | struct task *task = p->task; |
| 133 | register int i,s; |
| 134 | struct uthread *ut; |
| 135 | |
| 136 | /* |
| 137 | * If a thread in this task has already |
| 138 | * called exit(), then halt any others |
| 139 | * right here. |
| 140 | */ |
| 141 | |
| 142 | ut = get_bsdthread_info(th_act_self); |
| 143 | if (ut->uu_flag & P_VFORK) { |
| 144 | vfork_exit(p, rv); |
| 145 | vfork_return(th_act_self, p->p_pptr, p , retval); |
| 146 | unix_syscall_return(0); |
| 147 | /* NOT REACHED */ |
| 148 | } |
| 149 | signal_lock(p); |
| 150 | while (p->exit_thread != self) { |
| 151 | if (sig_try_locked(p) <= 0) { |
| 152 | if (get_threadtask(th_act_self) != task) { |
| 153 | signal_unlock(p); |
| 154 | return(0); |
| 155 | } |
| 156 | signal_unlock(p); |
| 157 | thread_terminate(th_act_self); |
| 158 | thread_funnel_set(kernel_flock, FALSE); |
| 159 | thread_exception_return(); |
| 160 | /* NOTREACHED */ |
| 161 | } |
| 162 | sig_lock_to_exit(p); |
| 163 | } |
| 164 | signal_unlock(p); |
| 165 | if (p->p_pid == 1) { |
| 166 | printf("pid 1 exited (signal %d, exit %d)", |
| 167 | WTERMSIG(rv), WEXITSTATUS(rv)); |
| 168 | panic("init died\nState at Last Exception:\n\n%s", |
| 169 | init_task_failure_data); |
| 170 | } |
| 171 | |
| 172 | s = splsched(); |
| 173 | p->p_flag |= P_WEXIT; |
| 174 | splx(s); |
| 175 | proc_prepareexit(p); |
| 176 | p->p_xstat = rv; |
| 177 | |
| 178 | /* task terminate will call proc_terminate and that cleans it up */ |
| 179 | task_terminate_internal(task); |
| 180 | |
| 181 | /* |
| 182 | * we come back and returns to AST which |
| 183 | * should cleanup the rest |
| 184 | */ |
| 185 | #if 0 |
| 186 | if (task == current_task()) { |
| 187 | thread_exception_return(); |
| 188 | /*NOTREACHED*/ |
| 189 | } |
| 190 | |
| 191 | while (task == current_task()) { |
| 192 | thread_terminate_self(); |
| 193 | /*NOTREACHED*/ |
| 194 | } |
| 195 | #endif |
| 196 | return(0); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | proc_prepareexit(struct proc *p) |
| 201 | { |
| 202 | int s; |
| 203 | struct uthread *ut; |
| 204 | thread_t self = current_thread(); |
| 205 | thread_act_t th_act_self = current_act(); |
| 206 | |
| 207 | /* |
| 208 | * Remove proc from allproc queue and from pidhash chain. |
| 209 | * Need to do this before we do anything that can block. |
| 210 | * Not doing causes things like mount() find this on allproc |
| 211 | * in partially cleaned state. |
| 212 | */ |
| 213 | LIST_REMOVE(p, p_list); |
| 214 | LIST_REMOVE(p, p_hash); |
| 215 | |
| 216 | #ifdef PGINPROF |
| 217 | vmsizmon(); |
| 218 | #endif |
| 219 | /* |
| 220 | * If parent is waiting for us to exit or exec, |
| 221 | * P_PPWAIT is set; we will wakeup the parent below. |
| 222 | */ |
| 223 | p->p_flag &= ~(P_TRACED | P_PPWAIT); |
| 224 | p->p_sigignore = ~0; |
| 225 | p->p_siglist = 0; |
| 226 | ut = get_bsdthread_info(th_act_self); |
| 227 | ut->uu_sig = 0; |
| 228 | untimeout(realitexpire, (caddr_t)p); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | proc_exit(struct proc *p) |
| 233 | { |
| 234 | register struct proc *q, *nq; |
| 235 | thread_t self = current_thread(); |
| 236 | thread_act_t th_act_self = current_act(); |
| 237 | struct task *task = p->task; |
| 238 | register int i,s; |
| 239 | boolean_t funnel_state; |
| 240 | |
| 241 | /* This can happen if thread_terminate of the single thread |
| 242 | * process |
| 243 | */ |
| 244 | |
| 245 | funnel_state = thread_funnel_set(kernel_flock, TRUE); |
| 246 | if( !(p->p_flag & P_WEXIT)) { |
| 247 | s = splsched(); |
| 248 | p->p_flag |= P_WEXIT; |
| 249 | splx(s); |
| 250 | proc_prepareexit(p); |
| 251 | } |
| 252 | |
| 253 | MALLOC_ZONE(p->p_ru, struct rusage *, |
| 254 | sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK); |
| 255 | |
| 256 | /* |
| 257 | * Close open files and release open-file table. |
| 258 | * This may block! |
| 259 | */ |
| 260 | fdfree(p); |
| 261 | |
| 262 | /* Close ref SYSV Shared memory*/ |
| 263 | if (p->vm_shm) |
| 264 | shmexit(p); |
| 265 | |
| 266 | if (SESS_LEADER(p)) { |
| 267 | register struct session *sp = p->p_session; |
| 268 | |
| 269 | if (sp->s_ttyvp) { |
| 270 | struct vnode *ttyvp; |
| 271 | |
| 272 | /* |
| 273 | * Controlling process. |
| 274 | * Signal foreground pgrp, |
| 275 | * drain controlling terminal |
| 276 | * and revoke access to controlling terminal. |
| 277 | */ |
| 278 | if (sp->s_ttyp->t_session == sp) { |
| 279 | if (sp->s_ttyp->t_pgrp) |
| 280 | pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); |
| 281 | (void) ttywait(sp->s_ttyp); |
| 282 | /* |
| 283 | * The tty could have been revoked |
| 284 | * if we blocked. |
| 285 | */ |
| 286 | if (sp->s_ttyvp) |
| 287 | VOP_REVOKE(sp->s_ttyvp, REVOKEALL); |
| 288 | } |
| 289 | ttyvp = sp->s_ttyvp; |
| 290 | sp->s_ttyvp = NULL; |
| 291 | if (ttyvp) |
| 292 | vrele(ttyvp); |
| 293 | /* |
| 294 | * s_ttyp is not zero'd; we use this to indicate |
| 295 | * that the session once had a controlling terminal. |
| 296 | * (for logging and informational purposes) |
| 297 | */ |
| 298 | } |
| 299 | sp->s_leader = NULL; |
| 300 | } |
| 301 | |
| 302 | fixjobc(p, p->p_pgrp, 0); |
| 303 | p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
| 304 | #if KTRACE |
| 305 | /* |
| 306 | * release trace file |
| 307 | */ |
| 308 | p->p_traceflag = 0; /* don't trace the vrele() */ |
| 309 | if (p->p_tracep) { |
| 310 | struct vnode *tvp = p->p_tracep; |
| 311 | p->p_tracep = NULL; |
| 312 | vrele(tvp); |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | |
| 317 | q = p->p_children.lh_first; |
| 318 | if (q) /* only need this if any child is S_ZOMB */ |
| 319 | wakeup((caddr_t) initproc); |
| 320 | for (; q != 0; q = nq) { |
| 321 | nq = q->p_sibling.le_next; |
| 322 | proc_reparent(q, initproc); |
| 323 | /* |
| 324 | * Traced processes are killed |
| 325 | * since their existence means someone is messing up. |
| 326 | */ |
| 327 | if (q->p_flag & P_TRACED) { |
| 328 | q->p_flag &= ~P_TRACED; |
| 329 | if (q->sigwait_thread) { |
| 330 | thread_t sig_shuttle = getshuttle_thread(q->sigwait_thread); |
| 331 | /* |
| 332 | * The sigwait_thread could be stopped at a |
| 333 | * breakpoint. Wake it up to kill. |
| 334 | * Need to do this as it could be a thread which is not |
| 335 | * the first thread in the task. So any attempts to kill |
| 336 | * the process would result into a deadlock on q->sigwait. |
| 337 | */ |
| 338 | thread_resume((struct thread *)q->sigwait_thread); |
| 339 | clear_wait(sig_shuttle, THREAD_INTERRUPTED); |
| 340 | threadsignal(q->sigwait_thread, SIGKILL, 0); |
| 341 | } |
| 342 | psignal(q, SIGKILL); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Save exit status and final rusage info, adding in child rusage |
| 348 | * info and self times. |
| 349 | */ |
| 350 | *p->p_ru = p->p_stats->p_ru; |
| 351 | |
| 352 | timerclear(&p->p_ru->ru_utime); |
| 353 | timerclear(&p->p_ru->ru_stime); |
| 354 | |
| 355 | if (task) { |
| 356 | task_basic_info_data_t tinfo; |
| 357 | task_thread_times_info_data_t ttimesinfo; |
| 358 | int task_info_stuff, task_ttimes_stuff; |
| 359 | struct timeval ut,st; |
| 360 | |
| 361 | task_info_stuff = TASK_BASIC_INFO_COUNT; |
| 362 | task_info(task, TASK_BASIC_INFO, |
| 363 | &tinfo, &task_info_stuff); |
| 364 | p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds; |
| 365 | p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds; |
| 366 | p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds; |
| 367 | p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds; |
| 368 | |
| 369 | task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT; |
| 370 | task_info(task, TASK_THREAD_TIMES_INFO, |
| 371 | &ttimesinfo, &task_ttimes_stuff); |
| 372 | |
| 373 | ut.tv_sec = ttimesinfo.user_time.seconds; |
| 374 | ut.tv_usec = ttimesinfo.user_time.microseconds; |
| 375 | st.tv_sec = ttimesinfo.system_time.seconds; |
| 376 | st.tv_usec = ttimesinfo.system_time.microseconds; |
| 377 | timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime); |
| 378 | timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime); |
| 379 | } |
| 380 | |
| 381 | ruadd(p->p_ru, &p->p_stats->p_cru); |
| 382 | |
| 383 | /* |
| 384 | * Free up profiling buffers. |
| 385 | */ |
| 386 | { |
| 387 | struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn; |
| 388 | |
| 389 | p1 = p0->pr_next; |
| 390 | p0->pr_next = NULL; |
| 391 | p0->pr_scale = 0; |
| 392 | |
| 393 | for (; p1 != NULL; p1 = pn) { |
| 394 | pn = p1->pr_next; |
| 395 | kfree((vm_offset_t)p1, sizeof *p1); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Other substructures are freed from wait(). |
| 401 | */ |
| 402 | FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC); |
| 403 | p->p_stats = NULL; |
| 404 | |
| 405 | FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC); |
| 406 | p->p_sigacts = NULL; |
| 407 | |
| 408 | if (--p->p_limit->p_refcnt == 0) |
| 409 | FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC); |
| 410 | p->p_limit = NULL; |
| 411 | |
| 412 | /* |
| 413 | * Finish up by terminating the task |
| 414 | * and halt this thread (only if a |
| 415 | * member of the task exiting). |
| 416 | */ |
| 417 | p->task = TASK_NULL; |
| 418 | //task->proc = NULL; |
| 419 | set_bsdtask_info(task, NULL); |
| 420 | |
| 421 | /* |
| 422 | * Notify parent that we're gone. |
| 423 | */ |
| 424 | psignal(p->p_pptr, SIGCHLD); |
| 425 | |
| 426 | /* Place onto zombproc. */ |
| 427 | LIST_INSERT_HEAD(&zombproc, p, p_list); |
| 428 | p->p_stat = SZOMB; |
| 429 | |
| 430 | /* and now wakeup the parent */ |
| 431 | wakeup((caddr_t)p->p_pptr); |
| 432 | |
| 433 | (void) thread_funnel_set(kernel_flock, funnel_state); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | struct wait4_args { |
| 438 | int pid; |
| 439 | int *status; |
| 440 | int options; |
| 441 | struct rusage *rusage; |
| 442 | }; |
| 443 | |
| 444 | #if COMPAT_43 |
| 445 | int |
| 446 | owait(p, uap, retval) |
| 447 | struct proc *p; |
| 448 | void *uap; |
| 449 | int *retval; |
| 450 | { |
| 451 | struct wait4_args *a; |
| 452 | |
| 453 | a = (struct wait4_args *)get_bsduthreadarg(current_act()); |
| 454 | |
| 455 | a->options = 0; |
| 456 | a->rusage = NULL; |
| 457 | a->pid = WAIT_ANY; |
| 458 | a->status = NULL; |
| 459 | return (wait1(p, a, retval, 1)); |
| 460 | } |
| 461 | |
| 462 | int |
| 463 | wait4(p, uap, retval) |
| 464 | struct proc *p; |
| 465 | struct wait4_args *uap; |
| 466 | int *retval; |
| 467 | { |
| 468 | return (wait1(p, uap, retval, 0)); |
| 469 | } |
| 470 | |
| 471 | struct owait3_args { |
| 472 | int *status; |
| 473 | int options; |
| 474 | struct rusage *rusage; |
| 475 | }; |
| 476 | |
| 477 | int |
| 478 | owait3(p, uap, retval) |
| 479 | struct proc *p; |
| 480 | struct owait3_args *uap; |
| 481 | int *retval; |
| 482 | { |
| 483 | struct wait4_args *a; |
| 484 | |
| 485 | a = (struct wait4_args *)get_bsduthreadarg(current_act); |
| 486 | |
| 487 | a->rusage = uap->rusage; |
| 488 | a->options = uap->options; |
| 489 | a->status = uap->status; |
| 490 | a->pid = WAIT_ANY; |
| 491 | |
| 492 | return (wait1(p, a, retval, 1)); |
| 493 | } |
| 494 | |
| 495 | #else |
| 496 | #define wait1 wait4 |
| 497 | #endif |
| 498 | |
| 499 | int |
| 500 | wait1continue(result) |
| 501 | { |
| 502 | void *vt; |
| 503 | thread_act_t thread; |
| 504 | int *retval; |
| 505 | struct proc *p; |
| 506 | |
| 507 | if (result) |
| 508 | return(result); |
| 509 | |
| 510 | p = current_proc(); |
| 511 | thread = current_act(); |
| 512 | vt = get_bsduthreadarg(thread); |
| 513 | retval = get_bsduthreadrval(thread); |
| 514 | wait1((struct proc *)p, (struct wait4_args *)vt, retval, 0); |
| 515 | } |
| 516 | |
| 517 | int |
| 518 | wait1(q, uap, retval, compat) |
| 519 | register struct proc *q; |
| 520 | register struct wait4_args *uap; |
| 521 | register_t *retval; |
| 522 | #if COMPAT_43 |
| 523 | int compat; |
| 524 | #endif |
| 525 | { |
| 526 | register int nfound; |
| 527 | register struct proc *p, *t; |
| 528 | int status, error; |
| 529 | struct vnode *tvp; |
| 530 | |
| 531 | retry: |
| 532 | if (uap->pid == 0) |
| 533 | uap->pid = -q->p_pgid; |
| 534 | |
| 535 | loop: |
| 536 | nfound = 0; |
| 537 | for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { |
| 538 | if (uap->pid != WAIT_ANY && |
| 539 | p->p_pid != uap->pid && |
| 540 | p->p_pgid != -(uap->pid)) |
| 541 | continue; |
| 542 | nfound++; |
| 543 | if (p->p_flag & P_WAITING) { |
| 544 | (void)tsleep(&p->p_stat, PWAIT, "waitcoll", 0); |
| 545 | goto loop; |
| 546 | } |
| 547 | p->p_flag |= P_WAITING; /* only allow single thread to wait() */ |
| 548 | |
| 549 | if (p->p_stat == SZOMB) { |
| 550 | retval[0] = p->p_pid; |
| 551 | #if COMPAT_43 |
| 552 | if (compat) |
| 553 | retval[1] = p->p_xstat; |
| 554 | else |
| 555 | #endif |
| 556 | if (uap->status) { |
| 557 | status = p->p_xstat; /* convert to int */ |
| 558 | if (error = copyout((caddr_t)&status, |
| 559 | (caddr_t)uap->status, |
| 560 | sizeof(status))) { |
| 561 | p->p_flag &= ~P_WAITING; |
| 562 | wakeup(&p->p_stat); |
| 563 | return (error); |
| 564 | } |
| 565 | } |
| 566 | if (uap->rusage && |
| 567 | (error = copyout((caddr_t)p->p_ru, |
| 568 | (caddr_t)uap->rusage, |
| 569 | sizeof (struct rusage)))) { |
| 570 | p->p_flag &= ~P_WAITING; |
| 571 | wakeup(&p->p_stat); |
| 572 | return (error); |
| 573 | } |
| 574 | /* |
| 575 | * If we got the child via a ptrace 'attach', |
| 576 | * we need to give it back to the old parent. |
| 577 | */ |
| 578 | if (p->p_oppid && (t = pfind(p->p_oppid))) { |
| 579 | p->p_oppid = 0; |
| 580 | proc_reparent(p, t); |
| 581 | psignal(t, SIGCHLD); |
| 582 | wakeup((caddr_t)t); |
| 583 | p->p_flag &= ~P_WAITING; |
| 584 | wakeup(&p->p_stat); |
| 585 | return (0); |
| 586 | } |
| 587 | p->p_xstat = 0; |
| 588 | if (p->p_ru) { |
| 589 | ruadd(&q->p_stats->p_cru, p->p_ru); |
| 590 | FREE_ZONE(p->p_ru, sizeof *p->p_ru, M_ZOMBIE); |
| 591 | p->p_ru = NULL; |
| 592 | } else { |
| 593 | printf("Warning : lost p_ru for %s\n", p->p_comm); |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Decrement the count of procs running with this uid. |
| 598 | */ |
| 599 | (void)chgproccnt(p->p_cred->p_ruid, -1); |
| 600 | |
| 601 | /* |
| 602 | * Free up credentials. |
| 603 | */ |
| 604 | if (--p->p_cred->p_refcnt == 0) { |
| 605 | struct ucred *ucr = p->p_ucred; |
| 606 | struct pcred *pcr; |
| 607 | |
| 608 | if (ucr != NOCRED) { |
| 609 | p->p_ucred = NOCRED; |
| 610 | crfree(ucr); |
| 611 | } |
| 612 | pcr = p->p_cred; |
| 613 | p->p_cred = NULL; |
| 614 | FREE_ZONE(pcr, sizeof *pcr, M_SUBPROC); |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Release reference to text vnode |
| 619 | */ |
| 620 | tvp = p->p_textvp; |
| 621 | p->p_textvp = NULL; |
| 622 | if (tvp) |
| 623 | vrele(tvp); |
| 624 | |
| 625 | /* |
| 626 | * Finally finished with old proc entry. |
| 627 | * Unlink it from its process group and free it. |
| 628 | */ |
| 629 | leavepgrp(p); |
| 630 | LIST_REMOVE(p, p_list); /* off zombproc */ |
| 631 | LIST_REMOVE(p, p_sibling); |
| 632 | p->p_flag &= ~P_WAITING; |
| 633 | FREE_ZONE(p, sizeof *p, M_PROC); |
| 634 | nprocs--; |
| 635 | wakeup(&p->p_stat); |
| 636 | return (0); |
| 637 | } |
| 638 | if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && |
| 639 | (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { |
| 640 | p->p_flag |= P_WAITED; |
| 641 | retval[0] = p->p_pid; |
| 642 | #if COMPAT_43 |
| 643 | if (compat) { |
| 644 | retval[1] = W_STOPCODE(p->p_xstat); |
| 645 | error = 0; |
| 646 | } else |
| 647 | #endif |
| 648 | if (uap->status) { |
| 649 | status = W_STOPCODE(p->p_xstat); |
| 650 | error = copyout((caddr_t)&status, |
| 651 | (caddr_t)uap->status, |
| 652 | sizeof(status)); |
| 653 | } else |
| 654 | error = 0; |
| 655 | p->p_flag &= ~P_WAITING; |
| 656 | wakeup(&p->p_stat); |
| 657 | return (error); |
| 658 | } |
| 659 | p->p_flag &= ~P_WAITING; |
| 660 | wakeup(&p->p_stat); |
| 661 | } |
| 662 | if (nfound == 0) |
| 663 | return (ECHILD); |
| 664 | |
| 665 | if (uap->options & WNOHANG) { |
| 666 | retval[0] = 0; |
| 667 | return (0); |
| 668 | } |
| 669 | |
| 670 | if (error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue)) |
| 671 | return (error); |
| 672 | |
| 673 | goto loop; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * make process 'parent' the new parent of process 'child'. |
| 678 | */ |
| 679 | void |
| 680 | proc_reparent(child, parent) |
| 681 | register struct proc *child; |
| 682 | register struct proc *parent; |
| 683 | { |
| 684 | |
| 685 | if (child->p_pptr == parent) |
| 686 | return; |
| 687 | |
| 688 | LIST_REMOVE(child, p_sibling); |
| 689 | LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); |
| 690 | child->p_pptr = parent; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Make the current process an "init" process, meaning |
| 695 | * that it doesn't have a parent, and that it won't be |
| 696 | * gunned down by kill(-1, 0). |
| 697 | */ |
| 698 | kern_return_t |
| 699 | init_process(void) |
| 700 | { |
| 701 | register struct proc *p = current_proc(); |
| 702 | |
| 703 | if (suser(p->p_ucred, &p->p_acflag)) |
| 704 | return(KERN_NO_ACCESS); |
| 705 | |
| 706 | if (p->p_pid != 1 && p->p_pgid != p->p_pid) |
| 707 | enterpgrp(p, p->p_pid, 0); |
| 708 | p->p_flag |= P_SYSTEM; |
| 709 | |
| 710 | /* |
| 711 | * Take us out of the sibling chain, and |
| 712 | * out of our parent's child chain. |
| 713 | */ |
| 714 | LIST_REMOVE(p, p_sibling); |
| 715 | p->p_sibling.le_prev = NULL; |
| 716 | p->p_sibling.le_next = NULL; |
| 717 | p->p_pptr = kernproc; |
| 718 | |
| 719 | return(KERN_SUCCESS); |
| 720 | } |
| 721 | |
| 722 | void |
| 723 | process_terminate_self(void) |
| 724 | { |
| 725 | struct proc *p = current_proc(); |
| 726 | |
| 727 | if (p != NULL) { |
| 728 | exit1(p, W_EXITCODE(0, SIGKILL), (int *)NULL); |
| 729 | /*NOTREACHED*/ |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Exit: deallocate address space and other resources, change proc state |
| 735 | * to zombie, and unlink proc from allproc and parent's lists. Save exit |
| 736 | * status and rusage for wait(). Check for child processes and orphan them. |
| 737 | */ |
| 738 | |
| 739 | void |
| 740 | vfork_exit(p, rv) |
| 741 | register struct proc *p; |
| 742 | int rv; |
| 743 | { |
| 744 | register struct proc *q, *nq; |
| 745 | thread_t self = current_thread(); |
| 746 | thread_act_t th_act_self = current_act(); |
| 747 | struct task *task = p->task; |
| 748 | register int i,s; |
| 749 | struct uthread *ut; |
| 750 | |
| 751 | /* |
| 752 | * If a thread in this task has already |
| 753 | * called exit(), then halt any others |
| 754 | * right here. |
| 755 | */ |
| 756 | |
| 757 | ut = get_bsdthread_info(th_act_self); |
| 758 | #ifdef FIXME |
| 759 | signal_lock(p); |
| 760 | while (p->exit_thread != self) { |
| 761 | if (sig_try_locked(p) <= 0) { |
| 762 | if (get_threadtask(th_act_self) != task) { |
| 763 | signal_unlock(p); |
| 764 | return; |
| 765 | } |
| 766 | signal_unlock(p); |
| 767 | thread_terminate(th_act_self); |
| 768 | thread_funnel_set(kernel_flock, FALSE); |
| 769 | thread_exception_return(); |
| 770 | /* NOTREACHED */ |
| 771 | } |
| 772 | sig_lock_to_exit(p); |
| 773 | } |
| 774 | signal_unlock(p); |
| 775 | if (p->p_pid == 1) { |
| 776 | printf("pid 1 exited (signal %d, exit %d)", |
| 777 | WTERMSIG(rv), WEXITSTATUS(rv)); |
| 778 | panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data); |
| 779 | } |
| 780 | #endif /* FIXME */ |
| 781 | |
| 782 | s = splsched(); |
| 783 | p->p_flag |= P_WEXIT; |
| 784 | splx(s); |
| 785 | /* |
| 786 | * Remove proc from allproc queue and from pidhash chain. |
| 787 | * Need to do this before we do anything that can block. |
| 788 | * Not doing causes things like mount() find this on allproc |
| 789 | * in partially cleaned state. |
| 790 | */ |
| 791 | LIST_REMOVE(p, p_list); |
| 792 | LIST_REMOVE(p, p_hash); |
| 793 | /* |
| 794 | * If parent is waiting for us to exit or exec, |
| 795 | * P_PPWAIT is set; we will wakeup the parent below. |
| 796 | */ |
| 797 | p->p_flag &= ~(P_TRACED | P_PPWAIT); |
| 798 | p->p_sigignore = ~0; |
| 799 | p->p_siglist = 0; |
| 800 | |
| 801 | ut->uu_sig = 0; |
| 802 | untimeout(realitexpire, (caddr_t)p); |
| 803 | |
| 804 | p->p_xstat = rv; |
| 805 | |
| 806 | vproc_exit(p); |
| 807 | } |
| 808 | |
| 809 | void |
| 810 | vproc_exit(struct proc *p) |
| 811 | { |
| 812 | register struct proc *q, *nq; |
| 813 | thread_t self = current_thread(); |
| 814 | thread_act_t th_act_self = current_act(); |
| 815 | struct task *task = p->task; |
| 816 | register int i,s; |
| 817 | boolean_t funnel_state; |
| 818 | |
| 819 | MALLOC_ZONE(p->p_ru, struct rusage *, |
| 820 | sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK); |
| 821 | |
| 822 | /* |
| 823 | * Close open files and release open-file table. |
| 824 | * This may block! |
| 825 | */ |
| 826 | fdfree(p); |
| 827 | |
| 828 | /* Close ref SYSV Shared memory*/ |
| 829 | if (p->vm_shm) |
| 830 | shmexit(p); |
| 831 | |
| 832 | if (SESS_LEADER(p)) { |
| 833 | register struct session *sp = p->p_session; |
| 834 | |
| 835 | if (sp->s_ttyvp) { |
| 836 | struct vnode *ttyvp; |
| 837 | |
| 838 | /* |
| 839 | * Controlling process. |
| 840 | * Signal foreground pgrp, |
| 841 | * drain controlling terminal |
| 842 | * and revoke access to controlling terminal. |
| 843 | */ |
| 844 | if (sp->s_ttyp->t_session == sp) { |
| 845 | if (sp->s_ttyp->t_pgrp) |
| 846 | pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); |
| 847 | (void) ttywait(sp->s_ttyp); |
| 848 | /* |
| 849 | * The tty could have been revoked |
| 850 | * if we blocked. |
| 851 | */ |
| 852 | if (sp->s_ttyvp) |
| 853 | VOP_REVOKE(sp->s_ttyvp, REVOKEALL); |
| 854 | } |
| 855 | ttyvp = sp->s_ttyvp; |
| 856 | sp->s_ttyvp = NULL; |
| 857 | if (ttyvp) |
| 858 | vrele(ttyvp); |
| 859 | /* |
| 860 | * s_ttyp is not zero'd; we use this to indicate |
| 861 | * that the session once had a controlling terminal. |
| 862 | * (for logging and informational purposes) |
| 863 | */ |
| 864 | } |
| 865 | sp->s_leader = NULL; |
| 866 | } |
| 867 | |
| 868 | fixjobc(p, p->p_pgrp, 0); |
| 869 | p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; |
| 870 | #if KTRACE |
| 871 | /* |
| 872 | * release trace file |
| 873 | */ |
| 874 | p->p_traceflag = 0; /* don't trace the vrele() */ |
| 875 | if (p->p_tracep) { |
| 876 | struct vnode *tvp = p->p_tracep; |
| 877 | p->p_tracep = NULL; |
| 878 | vrele(tvp); |
| 879 | } |
| 880 | #endif |
| 881 | |
| 882 | q = p->p_children.lh_first; |
| 883 | if (q) /* only need this if any child is S_ZOMB */ |
| 884 | wakeup((caddr_t) initproc); |
| 885 | for (; q != 0; q = nq) { |
| 886 | nq = q->p_sibling.le_next; |
| 887 | proc_reparent(q, initproc); |
| 888 | /* |
| 889 | * Traced processes are killed |
| 890 | * since their existence means someone is messing up. |
| 891 | */ |
| 892 | if (q->p_flag & P_TRACED) { |
| 893 | q->p_flag &= ~P_TRACED; |
| 894 | if (q->sigwait_thread) { |
| 895 | thread_t sig_shuttle = getshuttle_thread(q->sigwait_thread); |
| 896 | /* |
| 897 | * The sigwait_thread could be stopped at a |
| 898 | * breakpoint. Wake it up to kill. |
| 899 | * Need to do this as it could be a thread which is not |
| 900 | * the first thread in the task. So any attempts to kill |
| 901 | * the process would result into a deadlock on q->sigwait. |
| 902 | */ |
| 903 | thread_resume((struct thread *)q->sigwait_thread); |
| 904 | clear_wait(sig_shuttle, THREAD_INTERRUPTED); |
| 905 | threadsignal(q->sigwait_thread, SIGKILL, 0); |
| 906 | } |
| 907 | psignal(q, SIGKILL); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Save exit status and final rusage info, adding in child rusage |
| 913 | * info and self times. |
| 914 | */ |
| 915 | *p->p_ru = p->p_stats->p_ru; |
| 916 | |
| 917 | timerclear(&p->p_ru->ru_utime); |
| 918 | timerclear(&p->p_ru->ru_stime); |
| 919 | |
| 920 | #ifdef FIXME |
| 921 | if (task) { |
| 922 | task_basic_info_data_t tinfo; |
| 923 | task_thread_times_info_data_t ttimesinfo; |
| 924 | int task_info_stuff, task_ttimes_stuff; |
| 925 | struct timeval ut,st; |
| 926 | |
| 927 | task_info_stuff = TASK_BASIC_INFO_COUNT; |
| 928 | task_info(task, TASK_BASIC_INFO, |
| 929 | &tinfo, &task_info_stuff); |
| 930 | p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds; |
| 931 | p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds; |
| 932 | p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds; |
| 933 | p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds; |
| 934 | |
| 935 | task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT; |
| 936 | task_info(task, TASK_THREAD_TIMES_INFO, |
| 937 | &ttimesinfo, &task_ttimes_stuff); |
| 938 | |
| 939 | ut.tv_sec = ttimesinfo.user_time.seconds; |
| 940 | ut.tv_usec = ttimesinfo.user_time.microseconds; |
| 941 | st.tv_sec = ttimesinfo.system_time.seconds; |
| 942 | st.tv_usec = ttimesinfo.system_time.microseconds; |
| 943 | timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime); |
| 944 | timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime); |
| 945 | } |
| 946 | #endif /* FIXME */ |
| 947 | |
| 948 | ruadd(p->p_ru, &p->p_stats->p_cru); |
| 949 | |
| 950 | /* |
| 951 | * Free up profiling buffers. |
| 952 | */ |
| 953 | { |
| 954 | struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn; |
| 955 | |
| 956 | p1 = p0->pr_next; |
| 957 | p0->pr_next = NULL; |
| 958 | p0->pr_scale = 0; |
| 959 | |
| 960 | for (; p1 != NULL; p1 = pn) { |
| 961 | pn = p1->pr_next; |
| 962 | kfree((vm_offset_t)p1, sizeof *p1); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Other substructures are freed from wait(). |
| 968 | */ |
| 969 | FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC); |
| 970 | p->p_stats = NULL; |
| 971 | |
| 972 | FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC); |
| 973 | p->p_sigacts = NULL; |
| 974 | |
| 975 | if (--p->p_limit->p_refcnt == 0) |
| 976 | FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC); |
| 977 | p->p_limit = NULL; |
| 978 | |
| 979 | /* |
| 980 | * Finish up by terminating the task |
| 981 | * and halt this thread (only if a |
| 982 | * member of the task exiting). |
| 983 | */ |
| 984 | p->task = TASK_NULL; |
| 985 | |
| 986 | /* |
| 987 | * Notify parent that we're gone. |
| 988 | */ |
| 989 | psignal(p->p_pptr, SIGCHLD); |
| 990 | |
| 991 | /* Place onto zombproc. */ |
| 992 | LIST_INSERT_HEAD(&zombproc, p, p_list); |
| 993 | p->p_stat = SZOMB; |
| 994 | |
| 995 | /* and now wakeup the parent */ |
| 996 | wakeup((caddr_t)p->p_pptr); |
| 997 | } |