This source file includes following definitions.
- sys_exit
- sys_threxit
- exit1
- exit2
- reaper
- sys_wait4
- proc_reparent
- proc_zap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/ioctl.h>
43 #include <sys/proc.h>
44 #include <sys/tty.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/kernel.h>
48 #include <sys/buf.h>
49 #include <sys/wait.h>
50 #include <sys/file.h>
51 #include <sys/vnode.h>
52 #include <sys/syslog.h>
53 #include <sys/malloc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/ptrace.h>
56 #include <sys/acct.h>
57 #include <sys/filedesc.h>
58 #include <sys/signalvar.h>
59 #include <sys/sched.h>
60 #include <sys/ktrace.h>
61 #include <sys/pool.h>
62 #include <sys/mutex.h>
63 #ifdef SYSVSHM
64 #include <sys/shm.h>
65 #endif
66 #ifdef SYSVSEM
67 #include <sys/sem.h>
68 #endif
69
70 #include "systrace.h"
71 #include <dev/systrace.h>
72
73 #include <sys/mount.h>
74 #include <sys/syscallargs.h>
75
76 #include <machine/cpu.h>
77
78 #include <uvm/uvm_extern.h>
79
80
81
82
83
84 int
85 sys_exit(struct proc *p, void *v, register_t *retval)
86 {
87 struct sys_exit_args
88
89 *uap = v;
90
91 exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL);
92
93 return (0);
94 }
95
96 #ifdef RTHREADS
97 int
98 sys_threxit(struct proc *p, void *v, register_t *retval)
99 {
100 struct sys_threxit_args *uap = v;
101
102 exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_THREAD);
103
104 return (0);
105 }
106 #endif
107
108
109
110
111
112
113 void
114 exit1(struct proc *p, int rv, int flags)
115 {
116 struct proc *q, *nq;
117
118 if (p->p_pid == 1)
119 panic("init died (signal %d, exit %d)",
120 WTERMSIG(rv), WEXITSTATUS(rv));
121
122
123 TAILQ_REMOVE(&p->p_p->ps_threads, p, p_thr_link);
124 #ifdef RTHREADS
125 if (TAILQ_EMPTY(&p->p_p->ps_threads))
126 wakeup(&p->p_p->ps_threads);
127
128
129
130
131
132 if (flags == EXIT_NORMAL && p->p_p->ps_mainproc != p &&
133 (p->p_p->ps_mainproc->p_flag & P_WEXIT) == 0) {
134
135
136
137
138 atomic_setbits_int(&p->p_p->ps_mainproc->p_flag, P_IGNEXITRV);
139 p->p_p->ps_mainproc->p_xstat = rv;
140 psignal(p->p_p->ps_mainproc, SIGKILL);
141 tsleep(p->p_p, PUSER, "thrdying", 0);
142 } else if (p == p->p_p->ps_mainproc) {
143 atomic_setbits_int(&p->p_flag, P_WEXIT);
144 if (flags == EXIT_NORMAL) {
145 q = TAILQ_FIRST(&p->p_p->ps_threads);
146 for (; q != NULL; q = nq) {
147 nq = TAILQ_NEXT(q, p_thr_link);
148 atomic_setbits_int(&q->p_flag, P_IGNEXITRV);
149 q->p_xstat = rv;
150 psignal(q, SIGKILL);
151 }
152 }
153 wakeup(p->p_p);
154 while (!TAILQ_EMPTY(&p->p_p->ps_threads))
155 tsleep(&p->p_p->ps_threads, PUSER, "thrdeath", 0);
156 }
157 #endif
158
159 if (p->p_flag & P_PROFIL)
160 stopprofclock(p);
161 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
162
163
164
165
166 atomic_setbits_int(&p->p_flag, P_WEXIT);
167 atomic_clearbits_int(&p->p_flag, P_TRACED);
168 if (p->p_flag & P_PPWAIT) {
169 atomic_clearbits_int(&p->p_flag, P_PPWAIT);
170 wakeup(p->p_pptr);
171 }
172 p->p_sigignore = ~0;
173 p->p_siglist = 0;
174 timeout_del(&p->p_realit_to);
175 timeout_del(&p->p_stats->p_virt_to);
176 timeout_del(&p->p_stats->p_prof_to);
177
178
179
180
181
182 fdfree(p);
183
184 #ifdef SYSVSEM
185 semexit(p);
186 #endif
187 if (SESS_LEADER(p)) {
188 struct session *sp = p->p_session;
189
190 if (sp->s_ttyvp) {
191
192
193
194
195
196
197 if (sp->s_ttyp->t_session == sp) {
198 if (sp->s_ttyp->t_pgrp)
199 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
200 (void) ttywait(sp->s_ttyp);
201
202
203
204
205 if (sp->s_ttyvp)
206 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
207 }
208 if (sp->s_ttyvp)
209 vrele(sp->s_ttyvp);
210 sp->s_ttyvp = NULL;
211
212
213
214
215
216 }
217 sp->s_leader = NULL;
218 }
219 fixjobc(p, p->p_pgrp, 0);
220 #ifdef ACCOUNTING
221 (void)acct_process(p);
222 #endif
223 #ifdef KTRACE
224
225
226
227 p->p_traceflag = 0;
228 if (p->p_tracep)
229 ktrsettracevnode(p, NULL);
230 #endif
231 #if NSYSTRACE > 0
232 if (ISSET(p->p_flag, P_SYSTRACE))
233 systrace_exit(p);
234 #endif
235
236
237
238 p->p_stat = SDEAD;
239
240
241
242
243
244
245
246
247 LIST_REMOVE(p, p_hash);
248 LIST_REMOVE(p, p_list);
249 LIST_INSERT_HEAD(&zombproc, p, p_list);
250
251
252
253
254 q = LIST_FIRST(&p->p_children);
255 if (q)
256 wakeup(initproc);
257 for (; q != 0; q = nq) {
258 nq = LIST_NEXT(q, p_sibling);
259 proc_reparent(q, initproc);
260
261
262
263
264 if (q->p_flag & P_TRACED) {
265 atomic_clearbits_int(&q->p_flag, P_TRACED);
266 psignal(q, SIGKILL);
267 }
268 }
269
270
271
272
273
274
275 if (!(p->p_flag & P_IGNEXITRV))
276 p->p_xstat = rv;
277 *p->p_ru = p->p_stats->p_ru;
278 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
279 ruadd(p->p_ru, &p->p_stats->p_cru);
280
281
282
283
284 p->p_pctcpu = 0;
285
286
287
288
289 KNOTE(&p->p_klist, NOTE_EXIT);
290
291
292
293
294
295
296 if ((p->p_flag & P_NOZOMBIE) || (p->p_pptr->p_flag & P_NOCLDWAIT)) {
297 struct proc *pp = p->p_pptr;
298 proc_reparent(p, initproc);
299
300
301
302
303
304 if (LIST_EMPTY(&pp->p_children))
305 wakeup(pp);
306 }
307
308 if (p->p_exitsig != 0)
309 psignal(p->p_pptr, P_EXITSIG(p));
310 wakeup(p->p_pptr);
311
312
313
314
315 sigactsfree(p);
316
317
318
319
320
321
322
323
324
325
326
327 curproc = NULL;
328
329
330
331
332 if (p->p_emul->e_proc_exit)
333 (*p->p_emul->e_proc_exit)(p);
334
335
336 KERNEL_PROC_UNLOCK(p);
337
338
339
340
341
342
343
344
345
346
347
348 uvmexp.swtch++;
349 cpu_exit(p);
350 }
351
352
353
354
355
356
357
358
359 struct mutex deadproc_mutex = MUTEX_INITIALIZER(IPL_NONE);
360 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
361
362
363
364
365
366
367
368
369
370
371
372
373 void
374 exit2(struct proc *p)
375 {
376 int s;
377
378 mtx_enter(&deadproc_mutex);
379 LIST_INSERT_HEAD(&deadproc, p, p_hash);
380 mtx_leave(&deadproc_mutex);
381
382 wakeup(&deadproc);
383
384 SCHED_LOCK(s);
385 }
386
387
388
389
390
391
392 void
393 reaper(void)
394 {
395 struct proc *p;
396
397 KERNEL_PROC_UNLOCK(curproc);
398
399 for (;;) {
400 mtx_enter(&deadproc_mutex);
401 p = LIST_FIRST(&deadproc);
402 if (p == NULL) {
403
404 mtx_leave(&deadproc_mutex);
405 (void) tsleep(&deadproc, PVM, "reaper", 0);
406 continue;
407 }
408
409
410 LIST_REMOVE(p, p_hash);
411 mtx_leave(&deadproc_mutex);
412 KERNEL_PROC_LOCK(curproc);
413
414
415
416
417
418
419
420 cpu_wait(p);
421
422
423
424
425
426
427 uvm_exit(p);
428
429
430 if ((p->p_flag & P_NOZOMBIE) == 0) {
431 p->p_stat = SZOMB;
432
433
434 psignal(p->p_pptr, SIGCHLD);
435 wakeup(p->p_pptr);
436 } else {
437
438 proc_zap(p);
439 }
440
441 KERNEL_PROC_UNLOCK(curproc);
442 }
443 }
444
445 pid_t
446 sys_wait4(struct proc *q, void *v, register_t *retval)
447 {
448 struct sys_wait4_args
449
450
451
452
453 *uap = v;
454 int nfound;
455 struct proc *p, *t;
456 int status, error;
457
458 if (SCARG(uap, pid) == 0)
459 SCARG(uap, pid) = -q->p_pgid;
460 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG|WCONTINUED))
461 return (EINVAL);
462
463 loop:
464 nfound = 0;
465 LIST_FOREACH(p, &q->p_children, p_sibling) {
466 if ((p->p_flag & P_NOZOMBIE) ||
467 (SCARG(uap, pid) != WAIT_ANY &&
468 p->p_pid != SCARG(uap, pid) &&
469 p->p_pgid != -SCARG(uap, pid)))
470 continue;
471
472
473
474
475
476
477 if ((SCARG(uap, options) & WALTSIG) ?
478 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD))
479 continue;
480
481 nfound++;
482 if (p->p_stat == SZOMB) {
483 retval[0] = p->p_pid;
484
485 if (SCARG(uap, status)) {
486 status = p->p_xstat;
487 error = copyout(&status,
488 SCARG(uap, status), sizeof(status));
489 if (error)
490 return (error);
491 }
492 if (SCARG(uap, rusage) &&
493 (error = copyout(p->p_ru,
494 SCARG(uap, rusage), sizeof(struct rusage))))
495 return (error);
496
497
498
499
500
501 if (p->p_oppid && (t = pfind(p->p_oppid))) {
502 p->p_oppid = 0;
503 proc_reparent(p, t);
504 if (p->p_exitsig != 0)
505 psignal(t, P_EXITSIG(p));
506 wakeup(t);
507 return (0);
508 }
509
510 scheduler_wait_hook(q, p);
511 p->p_xstat = 0;
512 ruadd(&q->p_stats->p_cru, p->p_ru);
513
514 proc_zap(p);
515
516 return (0);
517 }
518 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
519 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
520 atomic_setbits_int(&p->p_flag, P_WAITED);
521 retval[0] = p->p_pid;
522
523 if (SCARG(uap, status)) {
524 status = W_STOPCODE(p->p_xstat);
525 error = copyout(&status, SCARG(uap, status),
526 sizeof(status));
527 } else
528 error = 0;
529 return (error);
530 }
531 if ((SCARG(uap, options) & WCONTINUED) && (p->p_flag & P_CONTINUED)) {
532 atomic_clearbits_int(&p->p_flag, P_CONTINUED);
533 retval[0] = p->p_pid;
534
535 if (SCARG(uap, status)) {
536 status = _WCONTINUED;
537 error = copyout(&status, SCARG(uap, status),
538 sizeof(status));
539 } else
540 error = 0;
541 return (error);
542 }
543 }
544 if (nfound == 0)
545 return (ECHILD);
546 if (SCARG(uap, options) & WNOHANG) {
547 retval[0] = 0;
548 return (0);
549 }
550 if ((error = tsleep(q, PWAIT | PCATCH, "wait", 0)) != 0)
551 return (error);
552 goto loop;
553 }
554
555
556
557
558 void
559 proc_reparent(struct proc *child, struct proc *parent)
560 {
561
562 if (child->p_pptr == parent)
563 return;
564
565 if (parent == initproc)
566 child->p_exitsig = SIGCHLD;
567
568 LIST_REMOVE(child, p_sibling);
569 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
570 child->p_pptr = parent;
571 }
572
573 void
574 proc_zap(struct proc *p)
575 {
576 pool_put(&rusage_pool, p->p_ru);
577 if (p->p_ptstat)
578 free(p->p_ptstat, M_SUBPROC);
579
580
581
582
583
584 leavepgrp(p);
585 LIST_REMOVE(p, p_list);
586 LIST_REMOVE(p, p_sibling);
587
588
589
590
591 (void)chgproccnt(p->p_cred->p_ruid, -1);
592
593
594
595
596 if (p->p_textvp)
597 vrele(p->p_textvp);
598
599
600
601
602
603 #if 0
604 TAILQ_REMOVE(&p->p_p->ps_threads, p, p_thr_link);
605 #endif
606 if (TAILQ_EMPTY(&p->p_p->ps_threads)) {
607 limfree(p->p_p->ps_limit);
608 if (--p->p_p->ps_cred->p_refcnt == 0) {
609 crfree(p->p_p->ps_cred->pc_ucred);
610 pool_put(&pcred_pool, p->p_p->ps_cred);
611 }
612 pool_put(&process_pool, p->p_p);
613 }
614
615 pool_put(&proc_pool, p);
616 nprocs--;
617 }
618