kern_exit.c (6520495abc109aa5af083b7cde70e4cf9c338de0) | kern_exit.c (b4490c6e937e404832d1169ca7dbac3b2e712355) |
---|---|
1/*- 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 161 unchanged lines hidden (view full) --- 170 171/* 172 * exit -- death of process. 173 */ 174void 175sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 176{ 177 | 1/*- 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 161 unchanged lines hidden (view full) --- 170 171/* 172 * exit -- death of process. 173 */ 174void 175sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 176{ 177 |
178 exit1(td, W_EXITCODE(uap->rval, 0)); | 178 exit1(td, uap->rval, 0); |
179 /* NOTREACHED */ 180} 181 182/* 183 * Exit: deallocate address space and other resources, change proc state to 184 * zombie, and unlink proc from allproc and parent's lists. Save exit status 185 * and rusage for wait(). Check for child processes and orphan them. 186 */ 187void | 179 /* NOTREACHED */ 180} 181 182/* 183 * Exit: deallocate address space and other resources, change proc state to 184 * zombie, and unlink proc from allproc and parent's lists. Save exit status 185 * and rusage for wait(). Check for child processes and orphan them. 186 */ 187void |
188exit1(struct thread *td, int rv) | 188exit1(struct thread *td, int rval, int signo) |
189{ 190 struct proc *p, *nq, *q, *t; 191 struct thread *tdt; 192 struct vnode *ttyvp = NULL; 193 194 mtx_assert(&Giant, MA_NOTOWNED); | 189{ 190 struct proc *p, *nq, *q, *t; 191 struct thread *tdt; 192 struct vnode *ttyvp = NULL; 193 194 mtx_assert(&Giant, MA_NOTOWNED); |
195 KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo)); |
|
195 196 p = td->td_proc; 197 /* 198 * XXX in case we're rebooting we just let init die in order to 199 * work around an unsolved stack overflow seen very late during 200 * shutdown on sparc64 when the gmirror worker process exists. 201 */ 202 if (p == initproc && rebooting == 0) { | 196 197 p = td->td_proc; 198 /* 199 * XXX in case we're rebooting we just let init die in order to 200 * work around an unsolved stack overflow seen very late during 201 * shutdown on sparc64 when the gmirror worker process exists. 202 */ 203 if (p == initproc && rebooting == 0) { |
203 printf("init died (signal %d, exit %d)\n", 204 WTERMSIG(rv), WEXITSTATUS(rv)); | 204 printf("init died (signal %d, exit %d)\n", signo, rval); |
205 panic("Going nowhere without my init!"); 206 } 207 208 /* 209 * Deref SU mp, since the thread does not return to userspace. 210 */ 211 if (softdep_ast_cleanup != NULL) 212 softdep_ast_cleanup(); --- 39 unchanged lines hidden (view full) --- 252 * might appear while process lock was dropped in 253 * thread_single(). 254 */ 255 thread_suspend_check(0); 256 } 257 KASSERT(p->p_numthreads == 1, 258 ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 259 racct_sub(p, RACCT_NTHR, 1); | 205 panic("Going nowhere without my init!"); 206 } 207 208 /* 209 * Deref SU mp, since the thread does not return to userspace. 210 */ 211 if (softdep_ast_cleanup != NULL) 212 softdep_ast_cleanup(); --- 39 unchanged lines hidden (view full) --- 252 * might appear while process lock was dropped in 253 * thread_single(). 254 */ 255 thread_suspend_check(0); 256 } 257 KASSERT(p->p_numthreads == 1, 258 ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 259 racct_sub(p, RACCT_NTHR, 1); |
260 261 /* Let event handler change exit status */ 262 p->p_xexit = rval; 263 p->p_xsig = signo; 264 |
|
260 /* 261 * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 262 * on our vmspace, so we should block below until they have 263 * released their reference to us. Note that if they have 264 * requested S_EXIT stops we will block here until they ack 265 * via PIOCCONT. 266 */ | 265 /* 266 * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 267 * on our vmspace, so we should block below until they have 268 * released their reference to us. Note that if they have 269 * requested S_EXIT stops we will block here until they ack 270 * via PIOCCONT. 271 */ |
267 _STOPEVENT(p, S_EXIT, rv); | 272 _STOPEVENT(p, S_EXIT, 0); |
268 269 /* 270 * Ignore any pending request to stop due to a stop signal. 271 * Once P_WEXIT is set, future requests will be ignored as 272 * well. 273 */ 274 p->p_flag &= ~P_STOPPED_SIG; 275 KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); --- 8 unchanged lines hidden (view full) --- 284 285 /* 286 * Wait for any processes that have a hold on our vmspace to 287 * release their reference. 288 */ 289 while (p->p_lock > 0) 290 msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 291 | 273 274 /* 275 * Ignore any pending request to stop due to a stop signal. 276 * Once P_WEXIT is set, future requests will be ignored as 277 * well. 278 */ 279 p->p_flag &= ~P_STOPPED_SIG; 280 KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); --- 8 unchanged lines hidden (view full) --- 289 290 /* 291 * Wait for any processes that have a hold on our vmspace to 292 * release their reference. 293 */ 294 while (p->p_lock > 0) 295 msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 296 |
292 p->p_xstat = rv; /* Let event handler change exit status */ | |
293 PROC_UNLOCK(p); 294 /* Drain the limit callout while we don't have the proc locked */ 295 callout_drain(&p->p_limco); 296 297#ifdef AUDIT 298 /* 299 * The Sun BSM exit token contains two components: an exit status as 300 * passed to exit(), and a return value to indicate what sort of exit 301 * it was. The exit status is WEXITSTATUS(rv), but it's not clear 302 * what the return value is. 303 */ | 297 PROC_UNLOCK(p); 298 /* Drain the limit callout while we don't have the proc locked */ 299 callout_drain(&p->p_limco); 300 301#ifdef AUDIT 302 /* 303 * The Sun BSM exit token contains two components: an exit status as 304 * passed to exit(), and a return value to indicate what sort of exit 305 * it was. The exit status is WEXITSTATUS(rv), but it's not clear 306 * what the return value is. 307 */ |
304 AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0); | 308 AUDIT_ARG_EXIT(rval, 0); |
305 AUDIT_SYSCALL_EXIT(0, td); 306#endif 307 308 /* Are we a task leader with peers? */ 309 if (p->p_peers != NULL && p == p->p_leader) { 310 mtx_lock(&ppeers_lock); 311 q = p->p_peers; 312 while (q != NULL) { --- 4 unchanged lines hidden (view full) --- 317 } 318 while (p->p_peers != NULL) 319 msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 320 mtx_unlock(&ppeers_lock); 321 } 322 323 /* 324 * Check if any loadable modules need anything done at process exit. | 309 AUDIT_SYSCALL_EXIT(0, td); 310#endif 311 312 /* Are we a task leader with peers? */ 313 if (p->p_peers != NULL && p == p->p_leader) { 314 mtx_lock(&ppeers_lock); 315 q = p->p_peers; 316 while (q != NULL) { --- 4 unchanged lines hidden (view full) --- 321 } 322 while (p->p_peers != NULL) 323 msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 324 mtx_unlock(&ppeers_lock); 325 } 326 327 /* 328 * Check if any loadable modules need anything done at process exit. |
325 * E.g. SYSV IPC stuff | 329 * E.g. SYSV IPC stuff. 330 * Event handler could change exit status. |
326 * XXX what if one of these generates an error? 327 */ 328 EVENTHANDLER_INVOKE(process_exit, p); 329 330 /* 331 * If parent is waiting for us to exit or exec, 332 * P_PPWAIT is set; we will wakeup the parent below. 333 */ 334 PROC_LOCK(p); | 331 * XXX what if one of these generates an error? 332 */ 333 EVENTHANDLER_INVOKE(process_exit, p); 334 335 /* 336 * If parent is waiting for us to exit or exec, 337 * P_PPWAIT is set; we will wakeup the parent below. 338 */ 339 PROC_LOCK(p); |
335 rv = p->p_xstat; /* Event handler could change exit status */ | |
336 stopprofclock(p); 337 p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 338 339 /* 340 * Stop the real interval timer. If the handler is currently 341 * executing, prevent it from rearming itself and let it finish. 342 */ 343 if (timevalisset(&p->p_realtimer.it_value) && --- 212 unchanged lines hidden (view full) --- 556 557 /* 558 * Notify interested parties of our demise. 559 */ 560 KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 561 562#ifdef KDTRACE_HOOKS 563 int reason = CLD_EXITED; | 340 stopprofclock(p); 341 p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 342 343 /* 344 * Stop the real interval timer. If the handler is currently 345 * executing, prevent it from rearming itself and let it finish. 346 */ 347 if (timevalisset(&p->p_realtimer.it_value) && --- 212 unchanged lines hidden (view full) --- 560 561 /* 562 * Notify interested parties of our demise. 563 */ 564 KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 565 566#ifdef KDTRACE_HOOKS 567 int reason = CLD_EXITED; |
564 if (WCOREDUMP(rv)) | 568 if (WCOREDUMP(signo)) |
565 reason = CLD_DUMPED; | 569 reason = CLD_DUMPED; |
566 else if (WIFSIGNALED(rv)) | 570 else if (WIFSIGNALED(signo)) |
567 reason = CLD_KILLED; 568 SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 569#endif 570 571 /* 572 * Just delete all entries in the p_klist. At this point we won't 573 * report any more events, and there are nasty race conditions that 574 * can beat us if we don't. --- 162 unchanged lines hidden (view full) --- 737 if (sig == SIGKILL) { 738 sbuf_trim(sb); 739 sbuf_printf(sb, " (Reason text inaccessible)"); 740 } 741 sbuf_cat(sb, "\n"); 742 sbuf_finish(sb); 743 log(LOG_INFO, "%s", sbuf_data(sb)); 744 sbuf_delete(sb); | 571 reason = CLD_KILLED; 572 SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 573#endif 574 575 /* 576 * Just delete all entries in the p_klist. At this point we won't 577 * report any more events, and there are nasty race conditions that 578 * can beat us if we don't. --- 162 unchanged lines hidden (view full) --- 741 if (sig == SIGKILL) { 742 sbuf_trim(sb); 743 sbuf_printf(sb, " (Reason text inaccessible)"); 744 } 745 sbuf_cat(sb, "\n"); 746 sbuf_finish(sb); 747 log(LOG_INFO, "%s", sbuf_data(sb)); 748 sbuf_delete(sb); |
745 exit1(td, W_EXITCODE(0, sig)); | 749 exit1(td, 0, sig); |
746 return (0); 747} 748 749 750#ifdef COMPAT_43 751/* 752 * The dirty work is handled by kern_wait(). 753 */ --- 82 unchanged lines hidden (view full) --- 836 PROC_LOCK_ASSERT(p, MA_OWNED); 837 PROC_SLOCK_ASSERT(p, MA_OWNED); 838 KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 839 840 q = td->td_proc; 841 842 PROC_SUNLOCK(p); 843 if (status) | 750 return (0); 751} 752 753 754#ifdef COMPAT_43 755/* 756 * The dirty work is handled by kern_wait(). 757 */ --- 82 unchanged lines hidden (view full) --- 840 PROC_LOCK_ASSERT(p, MA_OWNED); 841 PROC_SLOCK_ASSERT(p, MA_OWNED); 842 KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 843 844 q = td->td_proc; 845 846 PROC_SUNLOCK(p); 847 if (status) |
844 *status = p->p_xstat; /* convert to int */ | 848 *status = KW_EXITCODE(p->p_xexit, p->p_xsig); |
845 if (options & WNOWAIT) { 846 /* 847 * Only poll, returning the status. Caller does not wish to 848 * release the proc struct just yet. 849 */ 850 PROC_UNLOCK(p); 851 sx_xunlock(&proctree_lock); 852 return; --- 47 unchanged lines hidden (view full) --- 900 sx_xunlock(&proctree_lock); 901 902 /* 903 * Removal from allproc list and process group list paired with 904 * PROC_LOCK which was executed during that time should guarantee 905 * nothing can reach this process anymore. As such further locking 906 * is unnecessary. 907 */ | 849 if (options & WNOWAIT) { 850 /* 851 * Only poll, returning the status. Caller does not wish to 852 * release the proc struct just yet. 853 */ 854 PROC_UNLOCK(p); 855 sx_xunlock(&proctree_lock); 856 return; --- 47 unchanged lines hidden (view full) --- 904 sx_xunlock(&proctree_lock); 905 906 /* 907 * Removal from allproc list and process group list paired with 908 * PROC_LOCK which was executed during that time should guarantee 909 * nothing can reach this process anymore. As such further locking 910 * is unnecessary. 911 */ |
908 p->p_xstat = 0; /* XXX: why? */ | 912 p->p_xexit = p->p_xsig = 0; /* XXX: why? */ |
909 910 PROC_LOCK(q); 911 ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 912 PROC_UNLOCK(q); 913 914 /* 915 * Decrement the count of procs running with this uid. 916 */ --- 142 unchanged lines hidden (view full) --- 1059 * notification. 1060 */ 1061 siginfo->si_signo = SIGCHLD; 1062 1063 /* 1064 * This is still a rough estimate. We will fix the 1065 * cases TRAPPED, STOPPED, and CONTINUED later. 1066 */ | 913 914 PROC_LOCK(q); 915 ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 916 PROC_UNLOCK(q); 917 918 /* 919 * Decrement the count of procs running with this uid. 920 */ --- 142 unchanged lines hidden (view full) --- 1063 * notification. 1064 */ 1065 siginfo->si_signo = SIGCHLD; 1066 1067 /* 1068 * This is still a rough estimate. We will fix the 1069 * cases TRAPPED, STOPPED, and CONTINUED later. 1070 */ |
1067 if (WCOREDUMP(p->p_xstat)) { | 1071 if (WCOREDUMP(p->p_xsig)) { |
1068 siginfo->si_code = CLD_DUMPED; | 1072 siginfo->si_code = CLD_DUMPED; |
1069 siginfo->si_status = WTERMSIG(p->p_xstat); 1070 } else if (WIFSIGNALED(p->p_xstat)) { | 1073 siginfo->si_status = WTERMSIG(p->p_xsig); 1074 } else if (WIFSIGNALED(p->p_xsig)) { |
1071 siginfo->si_code = CLD_KILLED; | 1075 siginfo->si_code = CLD_KILLED; |
1072 siginfo->si_status = WTERMSIG(p->p_xstat); | 1076 siginfo->si_status = WTERMSIG(p->p_xsig); |
1073 } else { 1074 siginfo->si_code = CLD_EXITED; | 1077 } else { 1078 siginfo->si_code = CLD_EXITED; |
1075 siginfo->si_status = WEXITSTATUS(p->p_xstat); | 1079 siginfo->si_status = p->p_xexit; |
1076 } 1077 1078 siginfo->si_pid = p->p_pid; 1079 siginfo->si_uid = p->p_ucred->cr_uid; 1080 1081 /* 1082 * The si_addr field would be useful additional 1083 * detail, but apparently the PC value may be lost --- 134 unchanged lines hidden (view full) --- 1218 (p->p_suspcount == p->p_numthreads) && 1219 ((p->p_flag & P_WAITED) == 0)) { 1220 PROC_SUNLOCK(p); 1221 if ((options & WNOWAIT) == 0) 1222 p->p_flag |= P_WAITED; 1223 sx_xunlock(&proctree_lock); 1224 1225 if (status != NULL) | 1080 } 1081 1082 siginfo->si_pid = p->p_pid; 1083 siginfo->si_uid = p->p_ucred->cr_uid; 1084 1085 /* 1086 * The si_addr field would be useful additional 1087 * detail, but apparently the PC value may be lost --- 134 unchanged lines hidden (view full) --- 1222 (p->p_suspcount == p->p_numthreads) && 1223 ((p->p_flag & P_WAITED) == 0)) { 1224 PROC_SUNLOCK(p); 1225 if ((options & WNOWAIT) == 0) 1226 p->p_flag |= P_WAITED; 1227 sx_xunlock(&proctree_lock); 1228 1229 if (status != NULL) |
1226 *status = W_STOPCODE(p->p_xstat); | 1230 *status = W_STOPCODE(p->p_xsig); |
1227 if (siginfo != NULL) { | 1231 if (siginfo != NULL) { |
1228 siginfo->si_status = p->p_xstat; | 1232 siginfo->si_status = p->p_xsig; |
1229 siginfo->si_code = CLD_TRAPPED; 1230 } 1231 if ((options & WNOWAIT) == 0) { 1232 PROC_LOCK(q); 1233 sigqueue_take(p->p_ksi); 1234 PROC_UNLOCK(q); 1235 } 1236 1237 CTR4(KTR_PTRACE, 1238 "wait: returning trapped pid %d status %#x (xstat %d) xthread %d", | 1233 siginfo->si_code = CLD_TRAPPED; 1234 } 1235 if ((options & WNOWAIT) == 0) { 1236 PROC_LOCK(q); 1237 sigqueue_take(p->p_ksi); 1238 PROC_UNLOCK(q); 1239 } 1240 1241 CTR4(KTR_PTRACE, 1242 "wait: returning trapped pid %d status %#x (xstat %d) xthread %d", |
1239 p->p_pid, W_STOPCODE(p->p_xstat), p->p_xstat, | 1243 p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig, |
1240 p->p_xthread != NULL ? p->p_xthread->td_tid : -1); 1241 PROC_UNLOCK(p); 1242 td->td_retval[0] = pid; 1243 return (0); 1244 } 1245 if ((options & WUNTRACED) != 0 && 1246 (p->p_flag & P_STOPPED_SIG) != 0 && 1247 (p->p_suspcount == p->p_numthreads) && 1248 ((p->p_flag & P_WAITED) == 0)) { 1249 PROC_SUNLOCK(p); 1250 if ((options & WNOWAIT) == 0) 1251 p->p_flag |= P_WAITED; 1252 sx_xunlock(&proctree_lock); 1253 1254 if (status != NULL) | 1244 p->p_xthread != NULL ? p->p_xthread->td_tid : -1); 1245 PROC_UNLOCK(p); 1246 td->td_retval[0] = pid; 1247 return (0); 1248 } 1249 if ((options & WUNTRACED) != 0 && 1250 (p->p_flag & P_STOPPED_SIG) != 0 && 1251 (p->p_suspcount == p->p_numthreads) && 1252 ((p->p_flag & P_WAITED) == 0)) { 1253 PROC_SUNLOCK(p); 1254 if ((options & WNOWAIT) == 0) 1255 p->p_flag |= P_WAITED; 1256 sx_xunlock(&proctree_lock); 1257 1258 if (status != NULL) |
1255 *status = W_STOPCODE(p->p_xstat); | 1259 *status = W_STOPCODE(p->p_xsig); |
1256 if (siginfo != NULL) { | 1260 if (siginfo != NULL) { |
1257 siginfo->si_status = p->p_xstat; | 1261 siginfo->si_status = p->p_xsig; |
1258 siginfo->si_code = CLD_STOPPED; 1259 } 1260 if ((options & WNOWAIT) == 0) { 1261 PROC_LOCK(q); 1262 sigqueue_take(p->p_ksi); 1263 PROC_UNLOCK(q); 1264 } 1265 --- 108 unchanged lines hidden --- | 1262 siginfo->si_code = CLD_STOPPED; 1263 } 1264 if ((options & WNOWAIT) == 0) { 1265 PROC_LOCK(q); 1266 sigqueue_take(p->p_ksi); 1267 PROC_UNLOCK(q); 1268 } 1269 --- 108 unchanged lines hidden --- |