Import 2.1.122
[davej-history.git] / kernel / exit.c
blob72ecd910df3cae2fa83c6a218e2f3ff443895251
1 /*
2 * linux/kernel/exit.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/wait.h>
9 #include <linux/errno.h>
10 #include <linux/signal.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/resource.h>
14 #include <linux/mm.h>
15 #include <linux/tty.h>
16 #include <linux/malloc.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #ifdef CONFIG_BSD_PROCESS_ACCT
24 #include <linux/acct.h>
25 #endif
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/mmu_context.h>
31 extern void sem_exit (void);
33 int getrusage(struct task_struct *, int, struct rusage *);
35 static void release(struct task_struct * p)
37 if (p != current) {
38 #ifdef __SMP__
40 * Wait to make sure the process isn't active on any
41 * other CPU
43 for (;;) {
44 int has_cpu;
45 spin_lock(&scheduler_lock);
46 has_cpu = p->has_cpu;
47 spin_unlock(&scheduler_lock);
48 if (!has_cpu)
49 break;
50 do {
51 barrier();
52 } while (p->has_cpu);
54 #endif
55 free_uid(p);
56 nr_tasks--;
57 add_free_taskslot(p->tarray_ptr);
59 write_lock_irq(&tasklist_lock);
60 unhash_pid(p);
61 REMOVE_LINKS(p);
62 write_unlock_irq(&tasklist_lock);
64 release_thread(p);
65 current->cmin_flt += p->min_flt + p->cmin_flt;
66 current->cmaj_flt += p->maj_flt + p->cmaj_flt;
67 current->cnswap += p->nswap + p->cnswap;
68 free_task_struct(p);
69 } else {
70 printk("task releasing itself\n");
75 * This checks not only the pgrp, but falls back on the pid if no
76 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
77 * without this...
79 int session_of_pgrp(int pgrp)
81 struct task_struct *p;
82 int fallback;
84 fallback = -1;
85 read_lock(&tasklist_lock);
86 for_each_task(p) {
87 if (p->session <= 0)
88 continue;
89 if (p->pgrp == pgrp) {
90 fallback = p->session;
91 break;
93 if (p->pid == pgrp)
94 fallback = p->session;
96 read_unlock(&tasklist_lock);
97 return fallback;
101 * Determine if a process group is "orphaned", according to the POSIX
102 * definition in 2.2.2.52. Orphaned process groups are not to be affected
103 * by terminal-generated stop signals. Newly orphaned process groups are
104 * to receive a SIGHUP and a SIGCONT.
106 * "I ask you, have you ever known what it is to be an orphan?"
108 static int will_become_orphaned_pgrp(int pgrp, struct task_struct * ignored_task)
110 struct task_struct *p;
112 read_lock(&tasklist_lock);
113 for_each_task(p) {
114 if ((p == ignored_task) || (p->pgrp != pgrp) ||
115 (p->state == TASK_ZOMBIE) ||
116 (p->p_pptr->pid == 1))
117 continue;
118 if ((p->p_pptr->pgrp != pgrp) &&
119 (p->p_pptr->session == p->session)) {
120 read_unlock(&tasklist_lock);
121 return 0;
124 read_unlock(&tasklist_lock);
125 return 1; /* (sighing) "Often!" */
128 int is_orphaned_pgrp(int pgrp)
130 return will_become_orphaned_pgrp(pgrp, 0);
133 static inline int has_stopped_jobs(int pgrp)
135 int retval = 0;
136 struct task_struct * p;
138 read_lock(&tasklist_lock);
139 for_each_task(p) {
140 if (p->pgrp != pgrp)
141 continue;
142 if (p->state != TASK_STOPPED)
143 continue;
144 retval = 1;
145 break;
147 read_unlock(&tasklist_lock);
148 return retval;
151 static inline void forget_original_parent(struct task_struct * father)
153 struct task_struct * p;
155 read_lock(&tasklist_lock);
156 for_each_task(p) {
157 if (p->p_opptr == father) {
158 p->exit_signal = SIGCHLD;
159 p->p_opptr = task[smp_num_cpus] ? : task[0]; /* init */
160 if (p->pdeath_signal) send_sig(p->pdeath_signal, p, 0);
163 read_unlock(&tasklist_lock);
166 static inline void close_files(struct files_struct * files)
168 int i, j;
170 j = 0;
171 for (;;) {
172 unsigned long set = files->open_fds.fds_bits[j];
173 i = j * __NFDBITS;
174 j++;
175 if (i >= files->max_fds)
176 break;
177 while (set) {
178 if (set & 1) {
179 struct file * file = files->fd[i];
180 if (file) {
181 files->fd[i] = NULL;
182 close_fp(file, files);
185 i++;
186 set >>= 1;
191 extern kmem_cache_t *files_cachep;
193 static inline void __exit_files(struct task_struct *tsk)
195 struct files_struct * files = tsk->files;
197 if (files) {
198 tsk->files = NULL;
199 if (atomic_dec_and_test(&files->count)) {
200 close_files(files);
202 * Free the fd array as appropriate ...
204 if (NR_OPEN * sizeof(struct file *) == PAGE_SIZE)
205 free_page((unsigned long) files->fd);
206 else
207 kfree(files->fd);
208 kmem_cache_free(files_cachep, files);
213 void exit_files(struct task_struct *tsk)
215 __exit_files(tsk);
218 static inline void __exit_fs(struct task_struct *tsk)
220 struct fs_struct * fs = tsk->fs;
222 if (fs) {
223 tsk->fs = NULL;
224 if (atomic_dec_and_test(&fs->count)) {
225 dput(fs->root);
226 dput(fs->pwd);
227 kfree(fs);
232 void exit_fs(struct task_struct *tsk)
234 __exit_fs(tsk);
237 static inline void __exit_sighand(struct task_struct *tsk)
239 struct signal_struct * sig = tsk->sig;
241 if (sig) {
242 unsigned long flags;
244 spin_lock_irqsave(&tsk->sigmask_lock, flags);
245 tsk->sig = NULL;
246 spin_unlock_irqrestore(&tsk->sigmask_lock, flags);
247 if (atomic_dec_and_test(&sig->count))
248 kfree(sig);
251 flush_signals(tsk);
254 void exit_sighand(struct task_struct *tsk)
256 __exit_sighand(tsk);
259 static inline void __exit_mm(struct task_struct * tsk)
261 struct mm_struct * mm = tsk->mm;
263 /* Set us up to use the kernel mm state */
264 if (mm != &init_mm) {
265 flush_cache_mm(mm);
266 flush_tlb_mm(mm);
267 destroy_context(mm);
268 tsk->mm = &init_mm;
269 tsk->swappable = 0;
270 SET_PAGE_DIR(tsk, swapper_pg_dir);
271 mmput(mm);
275 void exit_mm(struct task_struct *tsk)
277 __exit_mm(tsk);
281 * Send signals to all our closest relatives so that they know
282 * to properly mourn us..
284 static void exit_notify(void)
286 struct task_struct * p;
288 forget_original_parent(current);
290 * Check to see if any process groups have become orphaned
291 * as a result of our exiting, and if they have any stopped
292 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
294 * Case i: Our father is in a different pgrp than we are
295 * and we were the only connection outside, so our pgrp
296 * is about to become orphaned.
298 if ((current->p_pptr->pgrp != current->pgrp) &&
299 (current->p_pptr->session == current->session) &&
300 will_become_orphaned_pgrp(current->pgrp, current) &&
301 has_stopped_jobs(current->pgrp)) {
302 kill_pg(current->pgrp,SIGHUP,1);
303 kill_pg(current->pgrp,SIGCONT,1);
305 /* Let father know we died */
306 notify_parent(current, current->exit_signal);
309 * This loop does two things:
311 * A. Make init inherit all the child processes
312 * B. Check to see if any process groups have become orphaned
313 * as a result of our exiting, and if they have any stopped
314 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
317 write_lock_irq(&tasklist_lock);
318 while (current->p_cptr != NULL) {
319 p = current->p_cptr;
320 current->p_cptr = p->p_osptr;
321 p->p_ysptr = NULL;
322 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
324 p->p_pptr = p->p_opptr;
325 p->p_osptr = p->p_pptr->p_cptr;
326 if (p->p_osptr)
327 p->p_osptr->p_ysptr = p;
328 p->p_pptr->p_cptr = p;
329 if (p->state == TASK_ZOMBIE)
330 notify_parent(p, p->exit_signal);
332 * process group orphan check
333 * Case ii: Our child is in a different pgrp
334 * than we are, and it was the only connection
335 * outside, so the child pgrp is now orphaned.
337 if ((p->pgrp != current->pgrp) &&
338 (p->session == current->session)) {
339 int pgrp = p->pgrp;
341 write_unlock_irq(&tasklist_lock);
342 if (is_orphaned_pgrp(pgrp) && has_stopped_jobs(pgrp)) {
343 kill_pg(pgrp,SIGHUP,1);
344 kill_pg(pgrp,SIGCONT,1);
346 write_lock_irq(&tasklist_lock);
349 write_unlock_irq(&tasklist_lock);
351 if (current->leader)
352 disassociate_ctty(1);
355 NORET_TYPE void do_exit(long code)
357 struct task_struct *tsk = current;
359 if (in_interrupt())
360 printk("Aiee, killing interrupt handler\n");
361 if (!tsk->pid)
362 panic("Attempted to kill the idle task!");
363 tsk->flags |= PF_EXITING;
364 del_timer(&tsk->real_timer);
366 lock_kernel();
367 fake_volatile:
368 #ifdef CONFIG_BSD_PROCESS_ACCT
369 acct_process(code);
370 #endif
371 sem_exit();
372 __exit_mm(tsk);
373 #if CONFIG_AP1000
374 exit_msc(tsk);
375 #endif
376 __exit_files(tsk);
377 __exit_fs(tsk);
378 __exit_sighand(tsk);
379 exit_thread();
380 tsk->state = TASK_ZOMBIE;
381 tsk->exit_code = code;
382 exit_notify();
383 #ifdef DEBUG_PROC_TREE
384 audit_ptree();
385 #endif
386 if (tsk->exec_domain && tsk->exec_domain->module)
387 __MOD_DEC_USE_COUNT(tsk->exec_domain->module);
388 if (tsk->binfmt && tsk->binfmt->module)
389 __MOD_DEC_USE_COUNT(tsk->binfmt->module);
390 schedule();
392 * In order to get rid of the "volatile function does return" message
393 * I did this little loop that confuses gcc to think do_exit really
394 * is volatile. In fact it's schedule() that is volatile in some
395 * circumstances: when current->state = ZOMBIE, schedule() never
396 * returns.
398 * In fact the natural way to do all this is to have the label and the
399 * goto right after each other, but I put the fake_volatile label at
400 * the start of the function just in case something /really/ bad
401 * happens, and the schedule returns. This way we can try again. I'm
402 * not paranoid: it's just that everybody is out to get me.
404 goto fake_volatile;
407 asmlinkage int sys_exit(int error_code)
409 do_exit((error_code&0xff)<<8);
412 asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
414 int flag, retval;
415 struct wait_queue wait = { current, NULL };
416 struct task_struct *p;
418 if (stat_addr) {
419 if(verify_area(VERIFY_WRITE, stat_addr, sizeof(*stat_addr)))
420 return -EFAULT;
422 if (ru) {
423 if(verify_area(VERIFY_WRITE, ru, sizeof(*ru)))
424 return -EFAULT;
427 if (options & ~(WNOHANG|WUNTRACED|__WCLONE))
428 return -EINVAL;
430 add_wait_queue(&current->wait_chldexit,&wait);
431 repeat:
432 flag = 0;
433 read_lock(&tasklist_lock);
434 for (p = current->p_cptr ; p ; p = p->p_osptr) {
435 if (pid>0) {
436 if (p->pid != pid)
437 continue;
438 } else if (!pid) {
439 if (p->pgrp != current->pgrp)
440 continue;
441 } else if (pid != -1) {
442 if (p->pgrp != -pid)
443 continue;
445 /* wait for cloned processes iff the __WCLONE flag is set */
446 if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
447 continue;
448 flag = 1;
449 switch (p->state) {
450 case TASK_STOPPED:
451 if (!p->exit_code)
452 continue;
453 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
454 continue;
455 read_unlock(&tasklist_lock);
456 if (ru != NULL)
457 getrusage(p, RUSAGE_BOTH, ru);
458 if (stat_addr)
459 __put_user((p->exit_code << 8) | 0x7f, stat_addr);
460 p->exit_code = 0;
461 retval = p->pid;
462 goto end_wait4;
463 case TASK_ZOMBIE:
464 current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime;
465 current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime;
466 read_unlock(&tasklist_lock);
467 if (ru != NULL)
468 getrusage(p, RUSAGE_BOTH, ru);
469 if (stat_addr)
470 __put_user(p->exit_code, stat_addr);
471 retval = p->pid;
472 if (p->p_opptr != p->p_pptr) {
473 write_lock_irq(&tasklist_lock);
474 REMOVE_LINKS(p);
475 p->p_pptr = p->p_opptr;
476 SET_LINKS(p);
477 write_unlock_irq(&tasklist_lock);
478 notify_parent(p, SIGCHLD);
479 } else
480 release(p);
481 #ifdef DEBUG_PROC_TREE
482 audit_ptree();
483 #endif
484 goto end_wait4;
485 default:
486 continue;
489 read_unlock(&tasklist_lock);
490 if (flag) {
491 retval = 0;
492 if (options & WNOHANG)
493 goto end_wait4;
494 retval = -ERESTARTSYS;
495 if (signal_pending(current))
496 goto end_wait4;
497 current->state=TASK_INTERRUPTIBLE;
498 schedule();
499 goto repeat;
501 retval = -ECHILD;
502 end_wait4:
503 remove_wait_queue(&current->wait_chldexit,&wait);
504 return retval;
507 #ifndef __alpha__
510 * sys_waitpid() remains for compatibility. waitpid() should be
511 * implemented by calling sys_wait4() from libc.a.
513 asmlinkage int sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
515 return sys_wait4(pid, stat_addr, options, NULL);
518 #endif