Import 2.1.116pre2
[davej-history.git] / kernel / exit.c
blob6f6b281ea5825376ac9da8f9dba40453bedead33
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 tsk->sig = NULL;
243 if (atomic_dec_and_test(&sig->count))
244 kfree(sig);
247 flush_signals(tsk);
250 void exit_sighand(struct task_struct *tsk)
252 __exit_sighand(tsk);
255 static inline void __exit_mm(struct task_struct * tsk)
257 struct mm_struct * mm = tsk->mm;
259 /* Set us up to use the kernel mm state */
260 if (mm != &init_mm) {
261 flush_cache_mm(mm);
262 flush_tlb_mm(mm);
263 destroy_context(mm);
264 tsk->mm = &init_mm;
265 tsk->swappable = 0;
266 SET_PAGE_DIR(tsk, swapper_pg_dir);
267 mmput(mm);
271 void exit_mm(struct task_struct *tsk)
273 __exit_mm(tsk);
277 * Send signals to all our closest relatives so that they know
278 * to properly mourn us..
280 static void exit_notify(void)
282 struct task_struct * p;
284 forget_original_parent(current);
286 * Check to see if any process groups have become orphaned
287 * as a result of our exiting, and if they have any stopped
288 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
290 * Case i: Our father is in a different pgrp than we are
291 * and we were the only connection outside, so our pgrp
292 * is about to become orphaned.
294 if ((current->p_pptr->pgrp != current->pgrp) &&
295 (current->p_pptr->session == current->session) &&
296 will_become_orphaned_pgrp(current->pgrp, current) &&
297 has_stopped_jobs(current->pgrp)) {
298 kill_pg(current->pgrp,SIGHUP,1);
299 kill_pg(current->pgrp,SIGCONT,1);
301 /* Let father know we died */
302 notify_parent(current, current->exit_signal);
305 * This loop does two things:
307 * A. Make init inherit all the child processes
308 * B. Check to see if any process groups have become orphaned
309 * as a result of our exiting, and if they have any stopped
310 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
313 write_lock_irq(&tasklist_lock);
314 while (current->p_cptr != NULL) {
315 p = current->p_cptr;
316 current->p_cptr = p->p_osptr;
317 p->p_ysptr = NULL;
318 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
320 p->p_pptr = p->p_opptr;
321 p->p_osptr = p->p_pptr->p_cptr;
322 if (p->p_osptr)
323 p->p_osptr->p_ysptr = p;
324 p->p_pptr->p_cptr = p;
325 if (p->state == TASK_ZOMBIE)
326 notify_parent(p, p->exit_signal);
328 * process group orphan check
329 * Case ii: Our child is in a different pgrp
330 * than we are, and it was the only connection
331 * outside, so the child pgrp is now orphaned.
333 if ((p->pgrp != current->pgrp) &&
334 (p->session == current->session)) {
335 int pgrp = p->pgrp;
337 write_unlock_irq(&tasklist_lock);
338 if (is_orphaned_pgrp(pgrp) && has_stopped_jobs(pgrp)) {
339 kill_pg(pgrp,SIGHUP,1);
340 kill_pg(pgrp,SIGCONT,1);
342 write_lock_irq(&tasklist_lock);
345 write_unlock_irq(&tasklist_lock);
347 if (current->leader)
348 disassociate_ctty(1);
351 NORET_TYPE void do_exit(long code)
353 struct task_struct *tsk = current;
355 if (in_interrupt())
356 printk("Aiee, killing interrupt handler\n");
357 if (!tsk->pid)
358 panic("Attempted to kill the idle task!");
359 tsk->flags |= PF_EXITING;
360 del_timer(&tsk->real_timer);
362 lock_kernel();
363 fake_volatile:
364 #ifdef CONFIG_BSD_PROCESS_ACCT
365 acct_process(code);
366 #endif
367 sem_exit();
368 __exit_mm(tsk);
369 #if CONFIG_AP1000
370 exit_msc(tsk);
371 #endif
372 __exit_files(tsk);
373 __exit_fs(tsk);
374 __exit_sighand(tsk);
375 exit_thread();
376 tsk->state = TASK_ZOMBIE;
377 tsk->exit_code = code;
378 exit_notify();
379 #ifdef DEBUG_PROC_TREE
380 audit_ptree();
381 #endif
382 if (tsk->exec_domain && tsk->exec_domain->module)
383 __MOD_DEC_USE_COUNT(tsk->exec_domain->module);
384 if (tsk->binfmt && tsk->binfmt->module)
385 __MOD_DEC_USE_COUNT(tsk->binfmt->module);
386 schedule();
388 * In order to get rid of the "volatile function does return" message
389 * I did this little loop that confuses gcc to think do_exit really
390 * is volatile. In fact it's schedule() that is volatile in some
391 * circumstances: when current->state = ZOMBIE, schedule() never
392 * returns.
394 * In fact the natural way to do all this is to have the label and the
395 * goto right after each other, but I put the fake_volatile label at
396 * the start of the function just in case something /really/ bad
397 * happens, and the schedule returns. This way we can try again. I'm
398 * not paranoid: it's just that everybody is out to get me.
400 goto fake_volatile;
403 asmlinkage int sys_exit(int error_code)
405 do_exit((error_code&0xff)<<8);
408 asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
410 int flag, retval;
411 struct wait_queue wait = { current, NULL };
412 struct task_struct *p;
414 if (stat_addr) {
415 if(verify_area(VERIFY_WRITE, stat_addr, sizeof(*stat_addr)))
416 return -EFAULT;
418 if (ru) {
419 if(verify_area(VERIFY_WRITE, ru, sizeof(*ru)))
420 return -EFAULT;
423 if (options & ~(WNOHANG|WUNTRACED|__WCLONE))
424 return -EINVAL;
426 add_wait_queue(&current->wait_chldexit,&wait);
427 repeat:
428 flag = 0;
429 read_lock(&tasklist_lock);
430 for (p = current->p_cptr ; p ; p = p->p_osptr) {
431 if (pid>0) {
432 if (p->pid != pid)
433 continue;
434 } else if (!pid) {
435 if (p->pgrp != current->pgrp)
436 continue;
437 } else if (pid != -1) {
438 if (p->pgrp != -pid)
439 continue;
441 /* wait for cloned processes iff the __WCLONE flag is set */
442 if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
443 continue;
444 flag = 1;
445 switch (p->state) {
446 case TASK_STOPPED:
447 if (!p->exit_code)
448 continue;
449 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
450 continue;
451 read_unlock(&tasklist_lock);
452 if (ru != NULL)
453 getrusage(p, RUSAGE_BOTH, ru);
454 if (stat_addr)
455 __put_user((p->exit_code << 8) | 0x7f, stat_addr);
456 p->exit_code = 0;
457 retval = p->pid;
458 goto end_wait4;
459 case TASK_ZOMBIE:
460 current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime;
461 current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime;
462 read_unlock(&tasklist_lock);
463 if (ru != NULL)
464 getrusage(p, RUSAGE_BOTH, ru);
465 if (stat_addr)
466 __put_user(p->exit_code, stat_addr);
467 retval = p->pid;
468 if (p->p_opptr != p->p_pptr) {
469 write_lock_irq(&tasklist_lock);
470 REMOVE_LINKS(p);
471 p->p_pptr = p->p_opptr;
472 SET_LINKS(p);
473 write_unlock_irq(&tasklist_lock);
474 notify_parent(p, SIGCHLD);
475 } else
476 release(p);
477 #ifdef DEBUG_PROC_TREE
478 audit_ptree();
479 #endif
480 goto end_wait4;
481 default:
482 continue;
485 read_unlock(&tasklist_lock);
486 if (flag) {
487 retval = 0;
488 if (options & WNOHANG)
489 goto end_wait4;
490 retval = -ERESTARTSYS;
491 if (signal_pending(current))
492 goto end_wait4;
493 current->state=TASK_INTERRUPTIBLE;
494 schedule();
495 goto repeat;
497 retval = -ECHILD;
498 end_wait4:
499 remove_wait_queue(&current->wait_chldexit,&wait);
500 return retval;
503 #ifndef __alpha__
506 * sys_waitpid() remains for compatibility. waitpid() should be
507 * implemented by calling sys_wait4() from libc.a.
509 asmlinkage int sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
511 return sys_wait4(pid, stat_addr, options, NULL);
514 #endif