Merge with 2.3.48.
[linux-2.6/linux-mips.git] / kernel / exit.c
blob9dd09f05087879635b624bc79c341349e7983fe1
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 void release(struct task_struct * p)
27 if (p != current) {
28 #ifdef __SMP__
29 int has_cpu;
32 * Wait to make sure the process isn't on the
33 * runqueue (active on some other CPU still)
35 do {
36 spin_lock_irq(&runqueue_lock);
37 has_cpu = p->has_cpu;
38 spin_unlock_irq(&runqueue_lock);
39 } while (has_cpu);
40 #endif
41 free_uid(p);
42 unhash_process(p);
44 release_thread(p);
45 current->cmin_flt += p->min_flt + p->cmin_flt;
46 current->cmaj_flt += p->maj_flt + p->cmaj_flt;
47 current->cnswap += p->nswap + p->cnswap;
48 free_task_struct(p);
49 } else {
50 printk("task releasing itself\n");
55 * This checks not only the pgrp, but falls back on the pid if no
56 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
57 * without this...
59 int session_of_pgrp(int pgrp)
61 struct task_struct *p;
62 int fallback;
64 fallback = -1;
65 read_lock(&tasklist_lock);
66 for_each_task(p) {
67 if (p->session <= 0)
68 continue;
69 if (p->pgrp == pgrp) {
70 fallback = p->session;
71 break;
73 if (p->pid == pgrp)
74 fallback = p->session;
76 read_unlock(&tasklist_lock);
77 return fallback;
81 * Determine if a process group is "orphaned", according to the POSIX
82 * definition in 2.2.2.52. Orphaned process groups are not to be affected
83 * by terminal-generated stop signals. Newly orphaned process groups are
84 * to receive a SIGHUP and a SIGCONT.
86 * "I ask you, have you ever known what it is to be an orphan?"
88 static int will_become_orphaned_pgrp(int pgrp, struct task_struct * ignored_task)
90 struct task_struct *p;
92 read_lock(&tasklist_lock);
93 for_each_task(p) {
94 if ((p == ignored_task) || (p->pgrp != pgrp) ||
95 (p->state == TASK_ZOMBIE) ||
96 (p->p_pptr->pid == 1))
97 continue;
98 if ((p->p_pptr->pgrp != pgrp) &&
99 (p->p_pptr->session == p->session)) {
100 read_unlock(&tasklist_lock);
101 return 0;
104 read_unlock(&tasklist_lock);
105 return 1; /* (sighing) "Often!" */
108 int is_orphaned_pgrp(int pgrp)
110 return will_become_orphaned_pgrp(pgrp, 0);
113 static inline int has_stopped_jobs(int pgrp)
115 int retval = 0;
116 struct task_struct * p;
118 read_lock(&tasklist_lock);
119 for_each_task(p) {
120 if (p->pgrp != pgrp)
121 continue;
122 if (p->state != TASK_STOPPED)
123 continue;
124 retval = 1;
125 break;
127 read_unlock(&tasklist_lock);
128 return retval;
131 static inline void forget_original_parent(struct task_struct * father)
133 struct task_struct * p;
135 read_lock(&tasklist_lock);
136 for_each_task(p) {
137 if (p->p_opptr == father) {
138 /* We dont want people slaying init */
139 p->exit_signal = SIGCHLD;
140 p->self_exec_id++;
141 p->p_opptr = child_reaper; /* init */
142 if (p->pdeath_signal) send_sig(p->pdeath_signal, p, 0);
145 read_unlock(&tasklist_lock);
148 static inline void close_files(struct files_struct * files)
150 int i, j;
152 j = 0;
153 for (;;) {
154 unsigned long set;
155 i = j * __NFDBITS;
156 if (i >= files->max_fdset || i >= files->max_fds)
157 break;
158 set = files->open_fds->fds_bits[j++];
159 while (set) {
160 if (set & 1) {
161 struct file * file = xchg(&files->fd[i], NULL);
162 if (file)
163 filp_close(file, files);
165 i++;
166 set >>= 1;
171 extern kmem_cache_t *files_cachep;
173 static inline void __exit_files(struct task_struct *tsk)
175 struct files_struct * files = xchg(&tsk->files, NULL);
177 if (files) {
178 if (atomic_dec_and_test(&files->count)) {
179 close_files(files);
181 * Free the fd and fdset arrays if we expanded them.
183 if (files->fd != &files->fd_array[0])
184 free_fd_array(files->fd, files->max_fds);
185 if (files->max_fdset > __FD_SETSIZE) {
186 free_fdset(files->open_fds, files->max_fdset);
187 free_fdset(files->close_on_exec, files->max_fdset);
189 kmem_cache_free(files_cachep, files);
194 void exit_files(struct task_struct *tsk)
196 __exit_files(tsk);
199 static inline void __exit_fs(struct task_struct *tsk)
201 struct fs_struct * fs = tsk->fs;
203 if (fs) {
204 tsk->fs = NULL;
205 if (atomic_dec_and_test(&fs->count)) {
206 dput(fs->root);
207 dput(fs->pwd);
208 kfree(fs);
213 void exit_fs(struct task_struct *tsk)
215 __exit_fs(tsk);
218 static inline void __exit_sighand(struct task_struct *tsk)
220 struct signal_struct * sig = tsk->sig;
222 if (sig) {
223 unsigned long flags;
225 spin_lock_irqsave(&tsk->sigmask_lock, flags);
226 tsk->sig = NULL;
227 spin_unlock_irqrestore(&tsk->sigmask_lock, flags);
228 if (atomic_dec_and_test(&sig->count))
229 kfree(sig);
232 flush_signals(tsk);
235 void exit_sighand(struct task_struct *tsk)
237 __exit_sighand(tsk);
241 * We can use these to temporarily drop into
242 * "lazy TLB" mode and back.
244 struct mm_struct * start_lazy_tlb(void)
246 struct mm_struct *mm = current->mm;
247 current->mm = NULL;
248 /* active_mm is still 'mm' */
249 atomic_inc(&mm->mm_count);
250 enter_lazy_tlb(mm, current, smp_processor_id());
251 return mm;
254 void end_lazy_tlb(struct mm_struct *mm)
256 struct mm_struct *active_mm = current->active_mm;
258 current->mm = mm;
259 if (mm != active_mm) {
260 current->active_mm = mm;
261 activate_mm(active_mm, mm);
263 mmdrop(active_mm);
267 * Turn us into a lazy TLB process if we
268 * aren't already..
270 static inline void __exit_mm(struct task_struct * tsk)
272 struct mm_struct * mm = tsk->mm;
274 if (mm) {
275 atomic_inc(&mm->mm_count);
276 mm_release();
277 if (mm != tsk->active_mm) BUG();
278 tsk->mm = NULL;
279 enter_lazy_tlb(mm, current, smp_processor_id());
280 mmput(mm);
284 void exit_mm(struct task_struct *tsk)
286 __exit_mm(tsk);
290 * Send signals to all our closest relatives so that they know
291 * to properly mourn us..
293 static void exit_notify(void)
295 struct task_struct * p, *t;
297 forget_original_parent(current);
299 * Check to see if any process groups have become orphaned
300 * as a result of our exiting, and if they have any stopped
301 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
303 * Case i: Our father is in a different pgrp than we are
304 * and we were the only connection outside, so our pgrp
305 * is about to become orphaned.
308 t = current->p_pptr;
310 if ((t->pgrp != current->pgrp) &&
311 (t->session == current->session) &&
312 will_become_orphaned_pgrp(current->pgrp, current) &&
313 has_stopped_jobs(current->pgrp)) {
314 kill_pg(current->pgrp,SIGHUP,1);
315 kill_pg(current->pgrp,SIGCONT,1);
318 /* Let father know we died
320 * Thread signals are configurable, but you aren't going to use
321 * that to send signals to arbitary processes.
322 * That stops right now.
324 * If the parent exec id doesn't match the exec id we saved
325 * when we started then we know the parent has changed security
326 * domain.
328 * If our self_exec id doesn't match our parent_exec_id then
329 * we have changed execution domain as these two values started
330 * the same after a fork.
334 if(current->exit_signal != SIGCHLD &&
335 ( current->parent_exec_id != t->self_exec_id ||
336 current->self_exec_id != current->parent_exec_id)
337 && !capable(CAP_KILL))
338 current->exit_signal = SIGCHLD;
340 notify_parent(current, current->exit_signal);
343 * This loop does two things:
345 * A. Make init inherit all the child processes
346 * B. Check to see if any process groups have become orphaned
347 * as a result of our exiting, and if they have any stopped
348 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
351 write_lock_irq(&tasklist_lock);
352 while (current->p_cptr != NULL) {
353 p = current->p_cptr;
354 current->p_cptr = p->p_osptr;
355 p->p_ysptr = NULL;
356 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
358 p->p_pptr = p->p_opptr;
359 p->p_osptr = p->p_pptr->p_cptr;
360 if (p->p_osptr)
361 p->p_osptr->p_ysptr = p;
362 p->p_pptr->p_cptr = p;
363 if (p->state == TASK_ZOMBIE)
364 notify_parent(p, p->exit_signal);
366 * process group orphan check
367 * Case ii: Our child is in a different pgrp
368 * than we are, and it was the only connection
369 * outside, so the child pgrp is now orphaned.
371 if ((p->pgrp != current->pgrp) &&
372 (p->session == current->session)) {
373 int pgrp = p->pgrp;
375 write_unlock_irq(&tasklist_lock);
376 if (is_orphaned_pgrp(pgrp) && has_stopped_jobs(pgrp)) {
377 kill_pg(pgrp,SIGHUP,1);
378 kill_pg(pgrp,SIGCONT,1);
380 write_lock_irq(&tasklist_lock);
383 write_unlock_irq(&tasklist_lock);
385 if (current->leader)
386 disassociate_ctty(1);
389 NORET_TYPE void do_exit(long code)
391 struct task_struct *tsk = current;
393 if (in_interrupt())
394 printk("Aiee, killing interrupt handler\n");
395 if (!tsk->pid)
396 panic("Attempted to kill the idle task!");
397 tsk->flags |= PF_EXITING;
398 del_timer_sync(&tsk->real_timer);
400 lock_kernel();
401 fake_volatile:
402 #ifdef CONFIG_BSD_PROCESS_ACCT
403 acct_process(code);
404 #endif
405 task_lock(tsk);
406 sem_exit();
407 __exit_mm(tsk);
408 __exit_files(tsk);
409 __exit_fs(tsk);
410 __exit_sighand(tsk);
411 exit_thread();
412 tsk->state = TASK_ZOMBIE;
413 tsk->exit_code = code;
414 exit_notify();
415 task_unlock(tsk);
416 if (tsk->exec_domain && tsk->exec_domain->module)
417 __MOD_DEC_USE_COUNT(tsk->exec_domain->module);
418 if (tsk->binfmt && tsk->binfmt->module)
419 __MOD_DEC_USE_COUNT(tsk->binfmt->module);
420 schedule();
422 * In order to get rid of the "volatile function does return" message
423 * I did this little loop that confuses gcc to think do_exit really
424 * is volatile. In fact it's schedule() that is volatile in some
425 * circumstances: when current->state = ZOMBIE, schedule() never
426 * returns.
428 * In fact the natural way to do all this is to have the label and the
429 * goto right after each other, but I put the fake_volatile label at
430 * the start of the function just in case something /really/ bad
431 * happens, and the schedule returns. This way we can try again. I'm
432 * not paranoid: it's just that everybody is out to get me.
434 goto fake_volatile;
437 asmlinkage long sys_exit(int error_code)
439 do_exit((error_code&0xff)<<8);
442 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
444 int flag, retval;
445 DECLARE_WAITQUEUE(wait, current);
446 struct task_struct *p;
448 if (options & ~(WNOHANG|WUNTRACED|__WCLONE|__WALL))
449 return -EINVAL;
451 add_wait_queue(&current->wait_chldexit,&wait);
452 repeat:
453 flag = 0;
454 current->state = TASK_INTERRUPTIBLE;
455 read_lock(&tasklist_lock);
456 for (p = current->p_cptr ; p ; p = p->p_osptr) {
457 if (pid>0) {
458 if (p->pid != pid)
459 continue;
460 } else if (!pid) {
461 if (p->pgrp != current->pgrp)
462 continue;
463 } else if (pid != -1) {
464 if (p->pgrp != -pid)
465 continue;
467 /* Wait for all children (clone and not) if __WALL is set;
468 * otherwise, wait for clone children *only* if __WCLONE is
469 * set; otherwise, wait for non-clone children *only*. (Note:
470 * A "clone" child here is one that reports to its parent
471 * using a signal other than SIGCHLD.) */
472 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
473 && !(options & __WALL))
474 continue;
475 flag = 1;
476 switch (p->state) {
477 case TASK_STOPPED:
478 if (!p->exit_code)
479 continue;
480 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
481 continue;
482 read_unlock(&tasklist_lock);
483 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
484 if (!retval && stat_addr)
485 retval = put_user((p->exit_code << 8) | 0x7f, stat_addr);
486 if (!retval) {
487 p->exit_code = 0;
488 retval = p->pid;
490 goto end_wait4;
491 case TASK_ZOMBIE:
492 current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime;
493 current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime;
494 read_unlock(&tasklist_lock);
495 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
496 if (!retval && stat_addr)
497 retval = put_user(p->exit_code, stat_addr);
498 if (retval)
499 goto end_wait4;
500 retval = p->pid;
501 if (p->p_opptr != p->p_pptr) {
502 write_lock_irq(&tasklist_lock);
503 REMOVE_LINKS(p);
504 p->p_pptr = p->p_opptr;
505 SET_LINKS(p);
506 write_unlock_irq(&tasklist_lock);
507 notify_parent(p, SIGCHLD);
508 } else
509 release(p);
510 goto end_wait4;
511 default:
512 continue;
515 read_unlock(&tasklist_lock);
516 if (flag) {
517 retval = 0;
518 if (options & WNOHANG)
519 goto end_wait4;
520 retval = -ERESTARTSYS;
521 if (signal_pending(current))
522 goto end_wait4;
523 schedule();
524 goto repeat;
526 retval = -ECHILD;
527 end_wait4:
528 current->state = TASK_RUNNING;
529 remove_wait_queue(&current->wait_chldexit,&wait);
530 return retval;
533 #if !defined(__alpha__) && !defined(__ia64__)
536 * sys_waitpid() remains for compatibility. waitpid() should be
537 * implemented by calling sys_wait4() from libc.a.
539 asmlinkage long sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
541 return sys_wait4(pid, stat_addr, options, NULL);
544 #endif