- pre2
[davej-history.git] / kernel / exit.c
blob255446884efaad0954c41f9aa1c0fc503f70c997
1 /*
2 * linux/kernel/exit.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/malloc.h>
9 #include <linux/interrupt.h>
10 #include <linux/smp_lock.h>
11 #include <linux/module.h>
12 #ifdef CONFIG_BSD_PROCESS_ACCT
13 #include <linux/acct.h>
14 #endif
16 #include <asm/uaccess.h>
17 #include <asm/pgtable.h>
18 #include <asm/mmu_context.h>
20 extern void sem_exit (void);
21 extern struct task_struct *child_reaper;
23 int getrusage(struct task_struct *, int, struct rusage *);
25 static void release(struct task_struct * p)
27 if (p != current) {
28 #ifdef CONFIG_SMP
30 * Wait to make sure the process isn't on the
31 * runqueue (active on some other CPU still)
33 for (;;) {
34 spin_lock_irq(&runqueue_lock);
35 if (!p->has_cpu)
36 break;
37 spin_unlock_irq(&runqueue_lock);
38 do {
39 barrier();
40 } while (p->has_cpu);
42 spin_unlock_irq(&runqueue_lock);
43 #endif
44 atomic_dec(&p->user->processes);
45 free_uid(p->user);
46 unhash_process(p);
48 release_thread(p);
49 current->cmin_flt += p->min_flt + p->cmin_flt;
50 current->cmaj_flt += p->maj_flt + p->cmaj_flt;
51 current->cnswap += p->nswap + p->cnswap;
53 * Potentially available timeslices are retrieved
54 * here - this way the parent does not get penalized
55 * for creating too many processes.
57 * (this cannot be used to artificially 'generate'
58 * timeslices, because any timeslice recovered here
59 * was given away by the parent in the first place.)
61 current->counter += p->counter;
62 if (current->counter >= MAX_COUNTER)
63 current->counter = MAX_COUNTER;
64 free_task_struct(p);
65 } else {
66 printk("task releasing itself\n");
71 * This checks not only the pgrp, but falls back on the pid if no
72 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
73 * without this...
75 int session_of_pgrp(int pgrp)
77 struct task_struct *p;
78 int fallback;
80 fallback = -1;
81 read_lock(&tasklist_lock);
82 for_each_task(p) {
83 if (p->session <= 0)
84 continue;
85 if (p->pgrp == pgrp) {
86 fallback = p->session;
87 break;
89 if (p->pid == pgrp)
90 fallback = p->session;
92 read_unlock(&tasklist_lock);
93 return fallback;
97 * Determine if a process group is "orphaned", according to the POSIX
98 * definition in 2.2.2.52. Orphaned process groups are not to be affected
99 * by terminal-generated stop signals. Newly orphaned process groups are
100 * to receive a SIGHUP and a SIGCONT.
102 * "I ask you, have you ever known what it is to be an orphan?"
104 static int will_become_orphaned_pgrp(int pgrp, struct task_struct * ignored_task)
106 struct task_struct *p;
108 read_lock(&tasklist_lock);
109 for_each_task(p) {
110 if ((p == ignored_task) || (p->pgrp != pgrp) ||
111 (p->state == TASK_ZOMBIE) ||
112 (p->p_pptr->pid == 1))
113 continue;
114 if ((p->p_pptr->pgrp != pgrp) &&
115 (p->p_pptr->session == p->session)) {
116 read_unlock(&tasklist_lock);
117 return 0;
120 read_unlock(&tasklist_lock);
121 return 1; /* (sighing) "Often!" */
124 int is_orphaned_pgrp(int pgrp)
126 return will_become_orphaned_pgrp(pgrp, 0);
129 static inline int has_stopped_jobs(int pgrp)
131 int retval = 0;
132 struct task_struct * p;
134 read_lock(&tasklist_lock);
135 for_each_task(p) {
136 if (p->pgrp != pgrp)
137 continue;
138 if (p->state != TASK_STOPPED)
139 continue;
140 retval = 1;
141 break;
143 read_unlock(&tasklist_lock);
144 return retval;
148 * When we die, we re-parent all our children.
149 * Try to give them to another thread in our process
150 * group, and if no such member exists, give it to
151 * the global child reaper process (ie "init")
153 static inline void forget_original_parent(struct task_struct * father)
155 struct task_struct * p, *reaper;
157 read_lock(&tasklist_lock);
159 /* Next in our thread group */
160 reaper = next_thread(father);
161 if (reaper == father)
162 reaper = child_reaper;
164 for_each_task(p) {
165 if (p->p_opptr == father) {
166 /* We dont want people slaying init */
167 p->exit_signal = SIGCHLD;
168 p->self_exec_id++;
169 p->p_opptr = reaper;
170 if (p->pdeath_signal) send_sig(p->pdeath_signal, p, 0);
173 read_unlock(&tasklist_lock);
176 static inline void close_files(struct files_struct * files)
178 int i, j;
180 j = 0;
181 for (;;) {
182 unsigned long set;
183 i = j * __NFDBITS;
184 if (i >= files->max_fdset || i >= files->max_fds)
185 break;
186 set = files->open_fds->fds_bits[j++];
187 while (set) {
188 if (set & 1) {
189 struct file * file = xchg(&files->fd[i], NULL);
190 if (file)
191 filp_close(file, files);
193 i++;
194 set >>= 1;
199 void put_files_struct(struct files_struct *files)
201 if (atomic_dec_and_test(&files->count)) {
202 close_files(files);
204 * Free the fd and fdset arrays if we expanded them.
206 if (files->fd != &files->fd_array[0])
207 free_fd_array(files->fd, files->max_fds);
208 if (files->max_fdset > __FD_SETSIZE) {
209 free_fdset(files->open_fds, files->max_fdset);
210 free_fdset(files->close_on_exec, files->max_fdset);
212 kmem_cache_free(files_cachep, files);
216 static inline void __exit_files(struct task_struct *tsk)
218 struct files_struct * files = tsk->files;
220 if (files) {
221 task_lock(tsk);
222 tsk->files = NULL;
223 task_unlock(tsk);
224 put_files_struct(files);
228 void exit_files(struct task_struct *tsk)
230 __exit_files(tsk);
232 static inline void __put_fs_struct(struct fs_struct *fs)
234 /* No need to hold fs->lock if we are killing it */
235 if (atomic_dec_and_test(&fs->count)) {
236 dput(fs->root);
237 mntput(fs->rootmnt);
238 dput(fs->pwd);
239 mntput(fs->pwdmnt);
240 if (fs->altroot) {
241 dput(fs->altroot);
242 mntput(fs->altrootmnt);
244 kmem_cache_free(fs_cachep, fs);
248 void put_fs_struct(struct fs_struct *fs)
250 __put_fs_struct(fs);
253 static inline void __exit_fs(struct task_struct *tsk)
255 struct fs_struct * fs = tsk->fs;
257 if (fs) {
258 task_lock(tsk);
259 tsk->fs = NULL;
260 task_unlock(tsk);
261 __put_fs_struct(fs);
265 void exit_fs(struct task_struct *tsk)
267 __exit_fs(tsk);
271 * We can use these to temporarily drop into
272 * "lazy TLB" mode and back.
274 struct mm_struct * start_lazy_tlb(void)
276 struct mm_struct *mm = current->mm;
277 current->mm = NULL;
278 /* active_mm is still 'mm' */
279 atomic_inc(&mm->mm_count);
280 enter_lazy_tlb(mm, current, smp_processor_id());
281 return mm;
284 void end_lazy_tlb(struct mm_struct *mm)
286 struct mm_struct *active_mm = current->active_mm;
288 current->mm = mm;
289 if (mm != active_mm) {
290 current->active_mm = mm;
291 activate_mm(active_mm, mm);
293 mmdrop(active_mm);
297 * Turn us into a lazy TLB process if we
298 * aren't already..
300 static inline void __exit_mm(struct task_struct * tsk)
302 struct mm_struct * mm = tsk->mm;
304 if (mm) {
305 atomic_inc(&mm->mm_count);
306 mm_release();
307 if (mm != tsk->active_mm) BUG();
308 /* more a memory barrier than a real lock */
309 task_lock(tsk);
310 tsk->mm = NULL;
311 task_unlock(tsk);
312 enter_lazy_tlb(mm, current, smp_processor_id());
313 mmput(mm);
317 void exit_mm(struct task_struct *tsk)
319 __exit_mm(tsk);
323 * Send signals to all our closest relatives so that they know
324 * to properly mourn us..
326 static void exit_notify(void)
328 struct task_struct * p, *t;
330 forget_original_parent(current);
332 * Check to see if any process groups have become orphaned
333 * as a result of our exiting, and if they have any stopped
334 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
336 * Case i: Our father is in a different pgrp than we are
337 * and we were the only connection outside, so our pgrp
338 * is about to become orphaned.
341 t = current->p_pptr;
343 if ((t->pgrp != current->pgrp) &&
344 (t->session == current->session) &&
345 will_become_orphaned_pgrp(current->pgrp, current) &&
346 has_stopped_jobs(current->pgrp)) {
347 kill_pg(current->pgrp,SIGHUP,1);
348 kill_pg(current->pgrp,SIGCONT,1);
351 /* Let father know we died
353 * Thread signals are configurable, but you aren't going to use
354 * that to send signals to arbitary processes.
355 * That stops right now.
357 * If the parent exec id doesn't match the exec id we saved
358 * when we started then we know the parent has changed security
359 * domain.
361 * If our self_exec id doesn't match our parent_exec_id then
362 * we have changed execution domain as these two values started
363 * the same after a fork.
367 if(current->exit_signal != SIGCHLD &&
368 ( current->parent_exec_id != t->self_exec_id ||
369 current->self_exec_id != current->parent_exec_id)
370 && !capable(CAP_KILL))
371 current->exit_signal = SIGCHLD;
375 * This loop does two things:
377 * A. Make init inherit all the child processes
378 * B. Check to see if any process groups have become orphaned
379 * as a result of our exiting, and if they have any stopped
380 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
383 write_lock_irq(&tasklist_lock);
384 do_notify_parent(current, current->exit_signal);
385 while (current->p_cptr != NULL) {
386 p = current->p_cptr;
387 current->p_cptr = p->p_osptr;
388 p->p_ysptr = NULL;
389 p->ptrace = 0;
391 p->p_pptr = p->p_opptr;
392 p->p_osptr = p->p_pptr->p_cptr;
393 if (p->p_osptr)
394 p->p_osptr->p_ysptr = p;
395 p->p_pptr->p_cptr = p;
396 if (p->state == TASK_ZOMBIE)
397 do_notify_parent(p, p->exit_signal);
399 * process group orphan check
400 * Case ii: Our child is in a different pgrp
401 * than we are, and it was the only connection
402 * outside, so the child pgrp is now orphaned.
404 if ((p->pgrp != current->pgrp) &&
405 (p->session == current->session)) {
406 int pgrp = p->pgrp;
408 write_unlock_irq(&tasklist_lock);
409 if (is_orphaned_pgrp(pgrp) && has_stopped_jobs(pgrp)) {
410 kill_pg(pgrp,SIGHUP,1);
411 kill_pg(pgrp,SIGCONT,1);
413 write_lock_irq(&tasklist_lock);
416 write_unlock_irq(&tasklist_lock);
418 if (current->leader)
419 disassociate_ctty(1);
422 NORET_TYPE void do_exit(long code)
424 struct task_struct *tsk = current;
426 if (in_interrupt())
427 printk("Aiee, killing interrupt handler\n");
428 if (!tsk->pid)
429 panic("Attempted to kill the idle task!");
430 if (tsk->pid == 1)
431 panic("Attempted to kill init!");
432 tsk->flags |= PF_EXITING;
433 del_timer_sync(&tsk->real_timer);
435 fake_volatile:
436 #ifdef CONFIG_BSD_PROCESS_ACCT
437 acct_process(code);
438 #endif
439 lock_kernel();
440 sem_exit();
441 __exit_mm(tsk);
442 __exit_files(tsk);
443 __exit_fs(tsk);
444 exit_sighand(tsk);
445 exit_thread();
446 tsk->state = TASK_ZOMBIE;
447 tsk->exit_code = code;
448 exit_notify();
449 put_exec_domain(tsk->exec_domain);
450 if (tsk->binfmt && tsk->binfmt->module)
451 __MOD_DEC_USE_COUNT(tsk->binfmt->module);
452 schedule();
454 * In order to get rid of the "volatile function does return" message
455 * I did this little loop that confuses gcc to think do_exit really
456 * is volatile. In fact it's schedule() that is volatile in some
457 * circumstances: when current->state = ZOMBIE, schedule() never
458 * returns.
460 * In fact the natural way to do all this is to have the label and the
461 * goto right after each other, but I put the fake_volatile label at
462 * the start of the function just in case something /really/ bad
463 * happens, and the schedule returns. This way we can try again. I'm
464 * not paranoid: it's just that everybody is out to get me.
466 goto fake_volatile;
469 asmlinkage long sys_exit(int error_code)
471 do_exit((error_code&0xff)<<8);
474 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
476 int flag, retval;
477 DECLARE_WAITQUEUE(wait, current);
478 struct task_struct *tsk;
480 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
481 return -EINVAL;
483 add_wait_queue(&current->wait_chldexit,&wait);
484 repeat:
485 flag = 0;
486 current->state = TASK_INTERRUPTIBLE;
487 read_lock(&tasklist_lock);
488 tsk = current;
489 do {
490 struct task_struct *p;
491 for (p = tsk->p_cptr ; p ; p = p->p_osptr) {
492 if (pid>0) {
493 if (p->pid != pid)
494 continue;
495 } else if (!pid) {
496 if (p->pgrp != current->pgrp)
497 continue;
498 } else if (pid != -1) {
499 if (p->pgrp != -pid)
500 continue;
502 /* Wait for all children (clone and not) if __WALL is set;
503 * otherwise, wait for clone children *only* if __WCLONE is
504 * set; otherwise, wait for non-clone children *only*. (Note:
505 * A "clone" child here is one that reports to its parent
506 * using a signal other than SIGCHLD.) */
507 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
508 && !(options & __WALL))
509 continue;
510 flag = 1;
511 switch (p->state) {
512 case TASK_STOPPED:
513 if (!p->exit_code)
514 continue;
515 if (!(options & WUNTRACED) && !(p->ptrace & PT_PTRACED))
516 continue;
517 read_unlock(&tasklist_lock);
518 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
519 if (!retval && stat_addr)
520 retval = put_user((p->exit_code << 8) | 0x7f, stat_addr);
521 if (!retval) {
522 p->exit_code = 0;
523 retval = p->pid;
525 goto end_wait4;
526 case TASK_ZOMBIE:
527 current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime;
528 current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime;
529 read_unlock(&tasklist_lock);
530 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
531 if (!retval && stat_addr)
532 retval = put_user(p->exit_code, stat_addr);
533 if (retval)
534 goto end_wait4;
535 retval = p->pid;
536 if (p->p_opptr != p->p_pptr) {
537 write_lock_irq(&tasklist_lock);
538 REMOVE_LINKS(p);
539 p->p_pptr = p->p_opptr;
540 SET_LINKS(p);
541 do_notify_parent(p, SIGCHLD);
542 write_unlock_irq(&tasklist_lock);
543 } else
544 release(p);
545 goto end_wait4;
546 default:
547 continue;
550 if (options & __WNOTHREAD)
551 break;
552 tsk = next_thread(tsk);
553 } while (tsk != current);
554 read_unlock(&tasklist_lock);
555 if (flag) {
556 retval = 0;
557 if (options & WNOHANG)
558 goto end_wait4;
559 retval = -ERESTARTSYS;
560 if (signal_pending(current))
561 goto end_wait4;
562 schedule();
563 goto repeat;
565 retval = -ECHILD;
566 end_wait4:
567 current->state = TASK_RUNNING;
568 remove_wait_queue(&current->wait_chldexit,&wait);
569 return retval;
572 #if !defined(__alpha__) && !defined(__ia64__)
575 * sys_waitpid() remains for compatibility. waitpid() should be
576 * implemented by calling sys_wait4() from libc.a.
578 asmlinkage long sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
580 return sys_wait4(pid, stat_addr, options, NULL);
583 #endif