proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / kern / kern_exit.c
CommitLineData
984263bc
MD
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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
39 * $FreeBSD: src/sys/kern/kern_exit.c,v 1.92.2.11 2003/01/13 22:51:16 dillon Exp $
dadab5e9 40 * $DragonFly: src/sys/kern/kern_exit.c,v 1.10 2003/06/25 03:55:57 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_compat.h"
44#include "opt_ktrace.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/sysproto.h>
49#include <sys/kernel.h>
50#include <sys/malloc.h>
51#include <sys/proc.h>
52#include <sys/pioctl.h>
53#include <sys/tty.h>
54#include <sys/wait.h>
55#include <sys/vnode.h>
56#include <sys/resourcevar.h>
57#include <sys/signalvar.h>
58#include <sys/ptrace.h>
59#include <sys/acct.h> /* for acct_process() function prototype */
60#include <sys/filedesc.h>
61#include <sys/shm.h>
62#include <sys/sem.h>
63#include <sys/aio.h>
64#include <sys/jail.h>
65
66#include <vm/vm.h>
67#include <vm/vm_param.h>
68#include <sys/lock.h>
69#include <vm/pmap.h>
70#include <vm/vm_map.h>
71#include <vm/vm_zone.h>
72#include <vm/vm_extern.h>
73#include <sys/user.h>
74
75/* Required to be non-static for SysVR4 emulator */
76MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
77
78static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
79
41c20dac 80static int wait1 __P((struct wait_args *, int));
984263bc
MD
81
82/*
83 * callout list for things to do at exit time
84 */
85struct exitlist {
86 exitlist_fn function;
87 TAILQ_ENTRY(exitlist) next;
88};
89
90TAILQ_HEAD(exit_list_head, exitlist);
91static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
92
93/*
94 * exit --
95 * Death of process.
41c20dac
MD
96 *
97 * SYS_EXIT_ARGS(int rval)
984263bc
MD
98 */
99void
41c20dac 100sys_exit(struct sys_exit_args *uap)
984263bc 101{
41c20dac 102 exit1(W_EXITCODE(uap->rval, 0));
984263bc
MD
103 /* NOTREACHED */
104}
105
106/*
107 * Exit: deallocate address space and other resources, change proc state
108 * to zombie, and unlink proc from allproc and parent's lists. Save exit
109 * status and rusage for wait(). Check for child processes and orphan them.
110 */
111void
41c20dac 112exit1(int rv)
984263bc 113{
41c20dac
MD
114 struct proc *p = curproc;
115 struct proc *q, *nq;
116 struct vmspace *vm;
984263bc
MD
117 struct vnode *vtmp;
118 struct exitlist *ep;
119
120 if (p->p_pid == 1) {
121 printf("init died (signal %d, exit %d)\n",
122 WTERMSIG(rv), WEXITSTATUS(rv));
123 panic("Going nowhere without my init!");
124 }
125
126 aio_proc_rundown(p);
127
128 /* are we a task leader? */
129 if(p == p->p_leader) {
130 struct kill_args killArgs;
131 killArgs.signum = SIGKILL;
132 q = p->p_peers;
133 while(q) {
134 killArgs.pid = q->p_pid;
135 /*
136 * The interface for kill is better
137 * than the internal signal
138 */
41c20dac 139 kill(&killArgs);
984263bc
MD
140 nq = q;
141 q = q->p_peers;
142 }
143 while (p->p_peers)
144 tsleep((caddr_t)p, PWAIT, "exit1", 0);
145 }
146
147#ifdef PGINPROF
148 vmsizmon();
149#endif
150 STOPEVENT(p, S_EXIT, rv);
151 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */
152
153 /*
154 * Check if any loadable modules need anything done at process exit.
155 * e.g. SYSV IPC stuff
156 * XXX what if one of these generates an error?
157 */
158 TAILQ_FOREACH(ep, &exit_list, next)
dadab5e9 159 (*ep->function)(p->p_thread);
984263bc
MD
160
161 if (p->p_flag & P_PROFIL)
162 stopprofclock(p);
163 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
164 M_ZOMBIE, M_WAITOK);
165 /*
166 * If parent is waiting for us to exit or exec,
167 * P_PPWAIT is set; we will wakeup the parent below.
168 */
169 p->p_flag &= ~(P_TRACED | P_PPWAIT);
170 p->p_flag |= P_WEXIT;
171 SIGEMPTYSET(p->p_siglist);
172 if (timevalisset(&p->p_realtimer.it_value))
173 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
174
175 /*
176 * Reset any sigio structures pointing to us as a result of
177 * F_SETOWN with our pid.
178 */
179 funsetownlst(&p->p_sigiolst);
180
181 /*
182 * Close open files and release open-file table.
183 * This may block!
184 */
185 fdfree(p);
186
187 if(p->p_leader->p_peers) {
188 q = p->p_leader;
189 while(q->p_peers != p)
190 q = q->p_peers;
191 q->p_peers = p->p_peers;
192 wakeup((caddr_t)p->p_leader);
193 }
194
195 /*
196 * XXX Shutdown SYSV semaphores
197 */
198 semexit(p);
199
200 /* The next two chunks should probably be moved to vmspace_exit. */
201 vm = p->p_vmspace;
202 /*
203 * Release user portion of address space.
204 * This releases references to vnodes,
205 * which could cause I/O if the file has been unlinked.
206 * Need to do this early enough that we can still sleep.
207 * Can't free the entire vmspace as the kernel stack
208 * may be mapped within that space also.
209 *
210 * Processes sharing the same vmspace may exit in one order, and
211 * get cleaned up by vmspace_exit() in a different order. The
212 * last exiting process to reach this point releases as much of
213 * the environment as it can, and the last process cleaned up
214 * by vmspace_exit() (which decrements exitingcnt) cleans up the
215 * remainder.
216 */
217 ++vm->vm_exitingcnt;
218 if (--vm->vm_refcnt == 0) {
219 if (vm->vm_shm)
220 shmexit(p);
221 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
222 VM_MAXUSER_ADDRESS);
223 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
224 VM_MAXUSER_ADDRESS);
225 }
226
227 if (SESS_LEADER(p)) {
228 register struct session *sp = p->p_session;
229
230 if (sp->s_ttyvp) {
231 /*
232 * Controlling process.
233 * Signal foreground pgrp,
234 * drain controlling terminal
235 * and revoke access to controlling terminal.
236 */
237 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
238 if (sp->s_ttyp->t_pgrp)
239 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
240 (void) ttywait(sp->s_ttyp);
241 /*
242 * The tty could have been revoked
243 * if we blocked.
244 */
245 if (sp->s_ttyvp)
246 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
247 }
248 if (sp->s_ttyvp)
249 vrele(sp->s_ttyvp);
250 sp->s_ttyvp = NULL;
251 /*
252 * s_ttyp is not zero'd; we use this to indicate
253 * that the session once had a controlling terminal.
254 * (for logging and informational purposes)
255 */
256 }
257 sp->s_leader = NULL;
258 }
259 fixjobc(p, p->p_pgrp, 0);
260 (void)acct_process(p);
261#ifdef KTRACE
262 /*
263 * release trace file
264 */
265 p->p_traceflag = 0; /* don't trace the vrele() */
266 if ((vtmp = p->p_tracep) != NULL) {
267 p->p_tracep = NULL;
268 vrele(vtmp);
269 }
270#endif
271 /*
272 * Release reference to text vnode
273 */
274 if ((vtmp = p->p_textvp) != NULL) {
275 p->p_textvp = NULL;
276 vrele(vtmp);
277 }
278
ae8050a4
MD
279 /*
280 * Once we set SZOMB the process can get reaped. To prevent this
281 * from occuring we obtain an exclusive access lock on the underlying
282 * thread which will not be released until the thread has been
283 * completed switched out.
284 */
285 lwkt_exlock(&curthread->td_rwlock, "exit");
286
984263bc
MD
287 /*
288 * Remove proc from allproc queue and pidhash chain.
289 * Place onto zombproc. Unlink from parent's child list.
290 */
291 LIST_REMOVE(p, p_list);
292 LIST_INSERT_HEAD(&zombproc, p, p_list);
293 p->p_stat = SZOMB;
294
295 LIST_REMOVE(p, p_hash);
296
297 q = LIST_FIRST(&p->p_children);
298 if (q) /* only need this if any child is S_ZOMB */
299 wakeup((caddr_t) initproc);
300 for (; q != 0; q = nq) {
301 nq = LIST_NEXT(q, p_sibling);
302 LIST_REMOVE(q, p_sibling);
303 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
304 q->p_pptr = initproc;
305 q->p_sigparent = SIGCHLD;
306 /*
307 * Traced processes are killed
308 * since their existence means someone is screwing up.
309 */
310 if (q->p_flag & P_TRACED) {
311 q->p_flag &= ~P_TRACED;
312 psignal(q, SIGKILL);
313 }
314 }
315
316 /*
317 * Save exit status and final rusage info, adding in child rusage
318 * info and self times.
319 */
320 p->p_xstat = rv;
321 *p->p_ru = p->p_stats->p_ru;
322 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
323 ruadd(p->p_ru, &p->p_stats->p_cru);
324
984263bc
MD
325 /*
326 * notify interested parties of our demise.
327 */
328 KNOTE(&p->p_klist, NOTE_EXIT);
329
330 /*
331 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT
332 * flag set, notify process 1 instead (and hope it will handle
333 * this situation).
334 */
335 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
336 struct proc *pp = p->p_pptr;
337 proc_reparent(p, initproc);
338 /*
339 * If this was the last child of our parent, notify
340 * parent, so in case he was wait(2)ing, he will
341 * continue.
342 */
343 if (LIST_EMPTY(&pp->p_children))
344 wakeup((caddr_t)pp);
345 }
346
347 if (p->p_sigparent && p->p_pptr != initproc) {
348 psignal(p->p_pptr, p->p_sigparent);
349 } else {
350 psignal(p->p_pptr, SIGCHLD);
351 }
352
353 wakeup((caddr_t)p->p_pptr);
354#if defined(tahoe)
355 /* move this to cpu_exit */
b7c628e4 356 p->p_thread->td_pcb->pcb_saveacc.faddr = (float *)NULL;
984263bc
MD
357#endif
358 /*
8ad65e08
MD
359 * cpu_exit is responsible for clearing curproc, since
360 * it is heavily integrated with the thread/switching sequence.
361 *
984263bc
MD
362 * Other substructures are freed from wait().
363 */
984263bc
MD
364 if (--p->p_limit->p_refcnt == 0) {
365 FREE(p->p_limit, M_SUBPROC);
366 p->p_limit = NULL;
367 }
368
369 /*
370 * Finally, call machine-dependent code to release the remaining
371 * resources including address space, the kernel stack and pcb.
372 * The address space is released by "vmspace_free(p->p_vmspace)";
373 * This is machine-dependent, as we may have to change stacks
374 * or ensure that the current one isn't reallocated before we
375 * finish. cpu_exit will end with a call to cpu_switch(), finishing
376 * our execution (pun intended).
377 */
378 cpu_exit(p);
379}
380
381#ifdef COMPAT_43
41c20dac
MD
382/*
383 * owait()
384 *
385 * owait_args(int dummy)
386 */
984263bc 387int
41c20dac 388owait(struct owait_args *uap)
984263bc
MD
389{
390 struct wait_args w;
391
392 w.options = 0;
393 w.rusage = NULL;
394 w.pid = WAIT_ANY;
395 w.status = NULL;
41c20dac 396 return (wait1(&w, 1));
984263bc
MD
397}
398#endif /* COMPAT_43 */
399
400int
41c20dac 401wait4(struct wait_args *uap)
984263bc 402{
41c20dac 403 return (wait1(uap, 0));
984263bc
MD
404}
405
41c20dac
MD
406/*
407 * wait1()
408 *
409 * wait_args(int pid, int *status, int options, struct rusage *rusage)
410 */
984263bc 411static int
41c20dac 412wait1(struct wait_args *uap, int compat)
984263bc 413{
41c20dac
MD
414 struct proc *q = curproc;
415 struct proc *p, *t;
416 int status, nfound, error;
984263bc
MD
417
418 if (uap->pid == 0)
419 uap->pid = -q->p_pgid;
420 if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
421 return (EINVAL);
422loop:
423 nfound = 0;
424 LIST_FOREACH(p, &q->p_children, p_sibling) {
425 if (uap->pid != WAIT_ANY &&
426 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
427 continue;
428
429 /* This special case handles a kthread spawned by linux_clone
430 * (see linux_misc.c). The linux_wait4 and linux_waitpid functions
431 * need to be able to distinguish between waiting on a process and
432 * waiting on a thread. It is a thread if p_sigparent is not SIGCHLD,
433 * and the WLINUXCLONE option signifies we want to wait for threads
434 * and not processes.
435 */
436 if ((p->p_sigparent != SIGCHLD) ^ ((uap->options & WLINUXCLONE) != 0))
437 continue;
438
439 nfound++;
440 if (p->p_stat == SZOMB) {
ae8050a4
MD
441 /*
442 * This is a tad nasty because lwkt_*() functions can
443 * block, causing our information to become out of
444 * date.
445 *
446 * YYY there may be some inefficiency here.
447 */
448 if ((p->p_flag & P_EXITINTERLOCK) == 0) {
449 lwkt_exlock(&p->p_thread->td_rwlock, "reap");
450 p->p_flag |= P_EXITINTERLOCK;
451 lwkt_exunlock(&p->p_thread->td_rwlock);
452 goto loop;
453 }
454 KASSERT(p->p_lock == 0, ("p_lock not 0! %p", p));
455
984263bc
MD
456 /* charge childs scheduling cpu usage to parent */
457 if (curproc->p_pid != 1) {
458 curproc->p_estcpu =
459 ESTCPULIM(curproc->p_estcpu + p->p_estcpu);
460 }
461
462 q->p_retval[0] = p->p_pid;
463#ifdef COMPAT_43
464 if (compat)
465 q->p_retval[1] = p->p_xstat;
466 else
467#endif
468 if (uap->status) {
469 status = p->p_xstat; /* convert to int */
470 if ((error = copyout((caddr_t)&status,
471 (caddr_t)uap->status, sizeof(status))))
472 return (error);
473 }
474 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
475 (caddr_t)uap->rusage, sizeof (struct rusage))))
476 return (error);
477 /*
478 * If we got the child via a ptrace 'attach',
479 * we need to give it back to the old parent.
480 */
481 if (p->p_oppid && (t = pfind(p->p_oppid))) {
482 p->p_oppid = 0;
483 proc_reparent(p, t);
484 psignal(t, SIGCHLD);
485 wakeup((caddr_t)t);
486 return (0);
487 }
488 p->p_xstat = 0;
489 ruadd(&q->p_stats->p_cru, p->p_ru);
490 FREE(p->p_ru, M_ZOMBIE);
491 p->p_ru = NULL;
492
493 /*
494 * Decrement the count of procs running with this uid.
495 */
41c20dac 496 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
984263bc
MD
497
498 /*
499 * Free up credentials.
500 */
41c20dac
MD
501 crfree(p->p_ucred);
502 p->p_ucred = NULL;
984263bc
MD
503
504 /*
505 * Remove unused arguments
506 */
507 if (p->p_args && --p->p_args->ar_ref == 0)
508 FREE(p->p_args, M_PARGS);
509
510 /*
511 * Finally finished with old proc entry.
512 * Unlink it from its process group and free it.
513 */
514 leavepgrp(p);
515 LIST_REMOVE(p, p_list); /* off zombproc */
516 LIST_REMOVE(p, p_sibling);
517
518 if (--p->p_procsig->ps_refcnt == 0) {
519 if (p->p_sigacts != &p->p_addr->u_sigacts)
520 FREE(p->p_sigacts, M_SUBPROC);
521 FREE(p->p_procsig, M_SUBPROC);
522 p->p_procsig = NULL;
523 }
524
984263bc
MD
525 vm_waitproc(p);
526 zfree(proc_zone, p);
527 nprocs--;
528 return (0);
529 }
530 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
531 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
532 p->p_flag |= P_WAITED;
533 q->p_retval[0] = p->p_pid;
534#ifdef COMPAT_43
535 if (compat) {
536 q->p_retval[1] = W_STOPCODE(p->p_xstat);
537 error = 0;
538 } else
539#endif
540 if (uap->status) {
541 status = W_STOPCODE(p->p_xstat);
542 error = copyout((caddr_t)&status,
543 (caddr_t)uap->status, sizeof(status));
544 } else
545 error = 0;
546 return (error);
547 }
548 }
549 if (nfound == 0)
550 return (ECHILD);
551 if (uap->options & WNOHANG) {
552 q->p_retval[0] = 0;
553 return (0);
554 }
555 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
556 return (error);
557 goto loop;
558}
559
560/*
561 * make process 'parent' the new parent of process 'child'.
562 */
563void
564proc_reparent(child, parent)
565 register struct proc *child;
566 register struct proc *parent;
567{
568
569 if (child->p_pptr == parent)
570 return;
571
572 LIST_REMOVE(child, p_sibling);
573 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
574 child->p_pptr = parent;
575}
576
577/*
578 * The next two functions are to handle adding/deleting items on the
579 * exit callout list
580 *
581 * at_exit():
582 * Take the arguments given and put them onto the exit callout list,
583 * However first make sure that it's not already there.
584 * returns 0 on success.
585 */
586
587int
588at_exit(function)
589 exitlist_fn function;
590{
591 struct exitlist *ep;
592
593#ifdef INVARIANTS
594 /* Be noisy if the programmer has lost track of things */
595 if (rm_at_exit(function))
596 printf("WARNING: exit callout entry (%p) already present\n",
597 function);
598#endif
599 ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
600 if (ep == NULL)
601 return (ENOMEM);
602 ep->function = function;
603 TAILQ_INSERT_TAIL(&exit_list, ep, next);
604 return (0);
605}
606
607/*
608 * Scan the exit callout list for the given item and remove it.
609 * Returns the number of items removed (0 or 1)
610 */
611int
612rm_at_exit(function)
613 exitlist_fn function;
614{
615 struct exitlist *ep;
616
617 TAILQ_FOREACH(ep, &exit_list, next) {
618 if (ep->function == function) {
619 TAILQ_REMOVE(&exit_list, ep, next);
620 free(ep, M_ATEXIT);
621 return(1);
622 }
623 }
624 return (0);
625}
626
627void check_sigacts (void)
628{
629 struct proc *p = curproc;
630 struct sigacts *pss;
631 int s;
632
633 if (p->p_procsig->ps_refcnt == 1 &&
634 p->p_sigacts != &p->p_addr->u_sigacts) {
635 pss = p->p_sigacts;
636 s = splhigh();
637 p->p_addr->u_sigacts = *pss;
638 p->p_sigacts = &p->p_addr->u_sigacts;
639 splx(s);
640 FREE(pss, M_SUBPROC);
641 }
642}
643