Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / exit.c
blobedaa50b5bbfaecc36f1e492e28d9b80ef55b5dae
1 /*
2 * linux/kernel/exit.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/mm.h>
9 #include <linux/slab.h>
10 #include <linux/interrupt.h>
11 #include <linux/smp_lock.h>
12 #include <linux/module.h>
13 #include <linux/completion.h>
14 #include <linux/personality.h>
15 #include <linux/tty.h>
16 #include <linux/namespace.h>
17 #include <linux/key.h>
18 #include <linux/security.h>
19 #include <linux/cpu.h>
20 #include <linux/acct.h>
21 #include <linux/file.h>
22 #include <linux/binfmts.h>
23 #include <linux/ptrace.h>
24 #include <linux/profile.h>
25 #include <linux/mount.h>
26 #include <linux/proc_fs.h>
27 #include <linux/mempolicy.h>
28 #include <linux/cpuset.h>
29 #include <linux/syscalls.h>
30 #include <linux/signal.h>
32 #include <asm/uaccess.h>
33 #include <asm/unistd.h>
34 #include <asm/pgtable.h>
35 #include <asm/mmu_context.h>
37 extern void sem_exit (void);
38 extern struct task_struct *child_reaper;
40 int getrusage(struct task_struct *, int, struct rusage __user *);
42 static void exit_mm(struct task_struct * tsk);
44 static void __unhash_process(struct task_struct *p)
46 nr_threads--;
47 detach_pid(p, PIDTYPE_PID);
48 detach_pid(p, PIDTYPE_TGID);
49 if (thread_group_leader(p)) {
50 detach_pid(p, PIDTYPE_PGID);
51 detach_pid(p, PIDTYPE_SID);
52 if (p->pid)
53 __get_cpu_var(process_counts)--;
56 REMOVE_LINKS(p);
59 void release_task(struct task_struct * p)
61 int zap_leader;
62 task_t *leader;
63 struct dentry *proc_dentry;
65 repeat:
66 atomic_dec(&p->user->processes);
67 spin_lock(&p->proc_lock);
68 proc_dentry = proc_pid_unhash(p);
69 write_lock_irq(&tasklist_lock);
70 if (unlikely(p->ptrace))
71 __ptrace_unlink(p);
72 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
73 __exit_signal(p);
74 __exit_sighand(p);
75 __unhash_process(p);
78 * If we are the last non-leader member of the thread
79 * group, and the leader is zombie, then notify the
80 * group leader's parent process. (if it wants notification.)
82 zap_leader = 0;
83 leader = p->group_leader;
84 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
85 BUG_ON(leader->exit_signal == -1);
86 do_notify_parent(leader, leader->exit_signal);
88 * If we were the last child thread and the leader has
89 * exited already, and the leader's parent ignores SIGCHLD,
90 * then we are the one who should release the leader.
92 * do_notify_parent() will have marked it self-reaping in
93 * that case.
95 zap_leader = (leader->exit_signal == -1);
98 sched_exit(p);
99 write_unlock_irq(&tasklist_lock);
100 spin_unlock(&p->proc_lock);
101 proc_pid_flush(proc_dentry);
102 release_thread(p);
103 put_task_struct(p);
105 p = leader;
106 if (unlikely(zap_leader))
107 goto repeat;
110 /* we are using it only for SMP init */
112 void unhash_process(struct task_struct *p)
114 struct dentry *proc_dentry;
116 spin_lock(&p->proc_lock);
117 proc_dentry = proc_pid_unhash(p);
118 write_lock_irq(&tasklist_lock);
119 __unhash_process(p);
120 write_unlock_irq(&tasklist_lock);
121 spin_unlock(&p->proc_lock);
122 proc_pid_flush(proc_dentry);
126 * This checks not only the pgrp, but falls back on the pid if no
127 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
128 * without this...
130 int session_of_pgrp(int pgrp)
132 struct task_struct *p;
133 int sid = -1;
135 read_lock(&tasklist_lock);
136 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
137 if (p->signal->session > 0) {
138 sid = p->signal->session;
139 goto out;
141 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
142 p = find_task_by_pid(pgrp);
143 if (p)
144 sid = p->signal->session;
145 out:
146 read_unlock(&tasklist_lock);
148 return sid;
152 * Determine if a process group is "orphaned", according to the POSIX
153 * definition in 2.2.2.52. Orphaned process groups are not to be affected
154 * by terminal-generated stop signals. Newly orphaned process groups are
155 * to receive a SIGHUP and a SIGCONT.
157 * "I ask you, have you ever known what it is to be an orphan?"
159 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
161 struct task_struct *p;
162 int ret = 1;
164 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
165 if (p == ignored_task
166 || p->exit_state
167 || p->real_parent->pid == 1)
168 continue;
169 if (process_group(p->real_parent) != pgrp
170 && p->real_parent->signal->session == p->signal->session) {
171 ret = 0;
172 break;
174 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
175 return ret; /* (sighing) "Often!" */
178 int is_orphaned_pgrp(int pgrp)
180 int retval;
182 read_lock(&tasklist_lock);
183 retval = will_become_orphaned_pgrp(pgrp, NULL);
184 read_unlock(&tasklist_lock);
186 return retval;
189 static inline int has_stopped_jobs(int pgrp)
191 int retval = 0;
192 struct task_struct *p;
194 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
195 if (p->state != TASK_STOPPED)
196 continue;
198 /* If p is stopped by a debugger on a signal that won't
199 stop it, then don't count p as stopped. This isn't
200 perfect but it's a good approximation. */
201 if (unlikely (p->ptrace)
202 && p->exit_code != SIGSTOP
203 && p->exit_code != SIGTSTP
204 && p->exit_code != SIGTTOU
205 && p->exit_code != SIGTTIN)
206 continue;
208 retval = 1;
209 break;
210 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
211 return retval;
215 * reparent_to_init - Reparent the calling kernel thread to the init task.
217 * If a kernel thread is launched as a result of a system call, or if
218 * it ever exits, it should generally reparent itself to init so that
219 * it is correctly cleaned up on exit.
221 * The various task state such as scheduling policy and priority may have
222 * been inherited from a user process, so we reset them to sane values here.
224 * NOTE that reparent_to_init() gives the caller full capabilities.
226 static inline void reparent_to_init(void)
228 write_lock_irq(&tasklist_lock);
230 ptrace_unlink(current);
231 /* Reparent to init */
232 REMOVE_LINKS(current);
233 current->parent = child_reaper;
234 current->real_parent = child_reaper;
235 SET_LINKS(current);
237 /* Set the exit signal to SIGCHLD so we signal init on exit */
238 current->exit_signal = SIGCHLD;
240 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
241 set_user_nice(current, 0);
242 /* cpus_allowed? */
243 /* rt_priority? */
244 /* signals? */
245 security_task_reparent_to_init(current);
246 memcpy(current->signal->rlim, init_task.signal->rlim,
247 sizeof(current->signal->rlim));
248 atomic_inc(&(INIT_USER->__count));
249 write_unlock_irq(&tasklist_lock);
250 switch_uid(INIT_USER);
253 void __set_special_pids(pid_t session, pid_t pgrp)
255 struct task_struct *curr = current;
257 if (curr->signal->session != session) {
258 detach_pid(curr, PIDTYPE_SID);
259 curr->signal->session = session;
260 attach_pid(curr, PIDTYPE_SID, session);
262 if (process_group(curr) != pgrp) {
263 detach_pid(curr, PIDTYPE_PGID);
264 curr->signal->pgrp = pgrp;
265 attach_pid(curr, PIDTYPE_PGID, pgrp);
269 void set_special_pids(pid_t session, pid_t pgrp)
271 write_lock_irq(&tasklist_lock);
272 __set_special_pids(session, pgrp);
273 write_unlock_irq(&tasklist_lock);
277 * Let kernel threads use this to say that they
278 * allow a certain signal (since daemonize() will
279 * have disabled all of them by default).
281 int allow_signal(int sig)
283 if (!valid_signal(sig) || sig < 1)
284 return -EINVAL;
286 spin_lock_irq(&current->sighand->siglock);
287 sigdelset(&current->blocked, sig);
288 if (!current->mm) {
289 /* Kernel threads handle their own signals.
290 Let the signal code know it'll be handled, so
291 that they don't get converted to SIGKILL or
292 just silently dropped */
293 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
295 recalc_sigpending();
296 spin_unlock_irq(&current->sighand->siglock);
297 return 0;
300 EXPORT_SYMBOL(allow_signal);
302 int disallow_signal(int sig)
304 if (!valid_signal(sig) || sig < 1)
305 return -EINVAL;
307 spin_lock_irq(&current->sighand->siglock);
308 sigaddset(&current->blocked, sig);
309 recalc_sigpending();
310 spin_unlock_irq(&current->sighand->siglock);
311 return 0;
314 EXPORT_SYMBOL(disallow_signal);
317 * Put all the gunge required to become a kernel thread without
318 * attached user resources in one place where it belongs.
321 void daemonize(const char *name, ...)
323 va_list args;
324 struct fs_struct *fs;
325 sigset_t blocked;
327 va_start(args, name);
328 vsnprintf(current->comm, sizeof(current->comm), name, args);
329 va_end(args);
332 * If we were started as result of loading a module, close all of the
333 * user space pages. We don't need them, and if we didn't close them
334 * they would be locked into memory.
336 exit_mm(current);
338 set_special_pids(1, 1);
339 down(&tty_sem);
340 current->signal->tty = NULL;
341 up(&tty_sem);
343 /* Block and flush all signals */
344 sigfillset(&blocked);
345 sigprocmask(SIG_BLOCK, &blocked, NULL);
346 flush_signals(current);
348 /* Become as one with the init task */
350 exit_fs(current); /* current->fs->count--; */
351 fs = init_task.fs;
352 current->fs = fs;
353 atomic_inc(&fs->count);
354 exit_files(current);
355 current->files = init_task.files;
356 atomic_inc(&current->files->count);
358 reparent_to_init();
361 EXPORT_SYMBOL(daemonize);
363 static inline void close_files(struct files_struct * files)
365 int i, j;
367 j = 0;
368 for (;;) {
369 unsigned long set;
370 i = j * __NFDBITS;
371 if (i >= files->max_fdset || i >= files->max_fds)
372 break;
373 set = files->open_fds->fds_bits[j++];
374 while (set) {
375 if (set & 1) {
376 struct file * file = xchg(&files->fd[i], NULL);
377 if (file)
378 filp_close(file, files);
380 i++;
381 set >>= 1;
386 struct files_struct *get_files_struct(struct task_struct *task)
388 struct files_struct *files;
390 task_lock(task);
391 files = task->files;
392 if (files)
393 atomic_inc(&files->count);
394 task_unlock(task);
396 return files;
399 void fastcall put_files_struct(struct files_struct *files)
401 if (atomic_dec_and_test(&files->count)) {
402 close_files(files);
404 * Free the fd and fdset arrays if we expanded them.
406 if (files->fd != &files->fd_array[0])
407 free_fd_array(files->fd, files->max_fds);
408 if (files->max_fdset > __FD_SETSIZE) {
409 free_fdset(files->open_fds, files->max_fdset);
410 free_fdset(files->close_on_exec, files->max_fdset);
412 kmem_cache_free(files_cachep, files);
416 EXPORT_SYMBOL(put_files_struct);
418 static inline void __exit_files(struct task_struct *tsk)
420 struct files_struct * files = tsk->files;
422 if (files) {
423 task_lock(tsk);
424 tsk->files = NULL;
425 task_unlock(tsk);
426 put_files_struct(files);
430 void exit_files(struct task_struct *tsk)
432 __exit_files(tsk);
435 static inline void __put_fs_struct(struct fs_struct *fs)
437 /* No need to hold fs->lock if we are killing it */
438 if (atomic_dec_and_test(&fs->count)) {
439 dput(fs->root);
440 mntput(fs->rootmnt);
441 dput(fs->pwd);
442 mntput(fs->pwdmnt);
443 if (fs->altroot) {
444 dput(fs->altroot);
445 mntput(fs->altrootmnt);
447 kmem_cache_free(fs_cachep, fs);
451 void put_fs_struct(struct fs_struct *fs)
453 __put_fs_struct(fs);
456 static inline void __exit_fs(struct task_struct *tsk)
458 struct fs_struct * fs = tsk->fs;
460 if (fs) {
461 task_lock(tsk);
462 tsk->fs = NULL;
463 task_unlock(tsk);
464 __put_fs_struct(fs);
468 void exit_fs(struct task_struct *tsk)
470 __exit_fs(tsk);
473 EXPORT_SYMBOL_GPL(exit_fs);
476 * Turn us into a lazy TLB process if we
477 * aren't already..
479 static void exit_mm(struct task_struct * tsk)
481 struct mm_struct *mm = tsk->mm;
483 mm_release(tsk, mm);
484 if (!mm)
485 return;
487 * Serialize with any possible pending coredump.
488 * We must hold mmap_sem around checking core_waiters
489 * and clearing tsk->mm. The core-inducing thread
490 * will increment core_waiters for each thread in the
491 * group with ->mm != NULL.
493 down_read(&mm->mmap_sem);
494 if (mm->core_waiters) {
495 up_read(&mm->mmap_sem);
496 down_write(&mm->mmap_sem);
497 if (!--mm->core_waiters)
498 complete(mm->core_startup_done);
499 up_write(&mm->mmap_sem);
501 wait_for_completion(&mm->core_done);
502 down_read(&mm->mmap_sem);
504 atomic_inc(&mm->mm_count);
505 if (mm != tsk->active_mm) BUG();
506 /* more a memory barrier than a real lock */
507 task_lock(tsk);
508 tsk->mm = NULL;
509 up_read(&mm->mmap_sem);
510 enter_lazy_tlb(mm, current);
511 task_unlock(tsk);
512 mmput(mm);
515 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
518 * Make sure we're not reparenting to ourselves and that
519 * the parent is not a zombie.
521 BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE);
522 p->real_parent = reaper;
525 static inline void reparent_thread(task_t *p, task_t *father, int traced)
527 /* We don't want people slaying init. */
528 if (p->exit_signal != -1)
529 p->exit_signal = SIGCHLD;
531 if (p->pdeath_signal)
532 /* We already hold the tasklist_lock here. */
533 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
535 /* Move the child from its dying parent to the new one. */
536 if (unlikely(traced)) {
537 /* Preserve ptrace links if someone else is tracing this child. */
538 list_del_init(&p->ptrace_list);
539 if (p->parent != p->real_parent)
540 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
541 } else {
542 /* If this child is being traced, then we're the one tracing it
543 * anyway, so let go of it.
545 p->ptrace = 0;
546 list_del_init(&p->sibling);
547 p->parent = p->real_parent;
548 list_add_tail(&p->sibling, &p->parent->children);
550 /* If we'd notified the old parent about this child's death,
551 * also notify the new parent.
553 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
554 thread_group_empty(p))
555 do_notify_parent(p, p->exit_signal);
556 else if (p->state == TASK_TRACED) {
558 * If it was at a trace stop, turn it into
559 * a normal stop since it's no longer being
560 * traced.
562 ptrace_untrace(p);
567 * process group orphan check
568 * Case ii: Our child is in a different pgrp
569 * than we are, and it was the only connection
570 * outside, so the child pgrp is now orphaned.
572 if ((process_group(p) != process_group(father)) &&
573 (p->signal->session == father->signal->session)) {
574 int pgrp = process_group(p);
576 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
577 __kill_pg_info(SIGHUP, (void *)1, pgrp);
578 __kill_pg_info(SIGCONT, (void *)1, pgrp);
584 * When we die, we re-parent all our children.
585 * Try to give them to another thread in our thread
586 * group, and if no such member exists, give it to
587 * the global child reaper process (ie "init")
589 static inline void forget_original_parent(struct task_struct * father,
590 struct list_head *to_release)
592 struct task_struct *p, *reaper = father;
593 struct list_head *_p, *_n;
595 do {
596 reaper = next_thread(reaper);
597 if (reaper == father) {
598 reaper = child_reaper;
599 break;
601 } while (reaper->exit_state);
604 * There are only two places where our children can be:
606 * - in our child list
607 * - in our ptraced child list
609 * Search them and reparent children.
611 list_for_each_safe(_p, _n, &father->children) {
612 int ptrace;
613 p = list_entry(_p,struct task_struct,sibling);
615 ptrace = p->ptrace;
617 /* if father isn't the real parent, then ptrace must be enabled */
618 BUG_ON(father != p->real_parent && !ptrace);
620 if (father == p->real_parent) {
621 /* reparent with a reaper, real father it's us */
622 choose_new_parent(p, reaper, child_reaper);
623 reparent_thread(p, father, 0);
624 } else {
625 /* reparent ptraced task to its real parent */
626 __ptrace_unlink (p);
627 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
628 thread_group_empty(p))
629 do_notify_parent(p, p->exit_signal);
633 * if the ptraced child is a zombie with exit_signal == -1
634 * we must collect it before we exit, or it will remain
635 * zombie forever since we prevented it from self-reap itself
636 * while it was being traced by us, to be able to see it in wait4.
638 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
639 list_add(&p->ptrace_list, to_release);
641 list_for_each_safe(_p, _n, &father->ptrace_children) {
642 p = list_entry(_p,struct task_struct,ptrace_list);
643 choose_new_parent(p, reaper, child_reaper);
644 reparent_thread(p, father, 1);
649 * Send signals to all our closest relatives so that they know
650 * to properly mourn us..
652 static void exit_notify(struct task_struct *tsk)
654 int state;
655 struct task_struct *t;
656 struct list_head ptrace_dead, *_p, *_n;
658 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
659 && !thread_group_empty(tsk)) {
661 * This occurs when there was a race between our exit
662 * syscall and a group signal choosing us as the one to
663 * wake up. It could be that we are the only thread
664 * alerted to check for pending signals, but another thread
665 * should be woken now to take the signal since we will not.
666 * Now we'll wake all the threads in the group just to make
667 * sure someone gets all the pending signals.
669 read_lock(&tasklist_lock);
670 spin_lock_irq(&tsk->sighand->siglock);
671 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
672 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
673 recalc_sigpending_tsk(t);
674 if (signal_pending(t))
675 signal_wake_up(t, 0);
677 spin_unlock_irq(&tsk->sighand->siglock);
678 read_unlock(&tasklist_lock);
681 write_lock_irq(&tasklist_lock);
684 * This does two things:
686 * A. Make init inherit all the child processes
687 * B. Check to see if any process groups have become orphaned
688 * as a result of our exiting, and if they have any stopped
689 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
692 INIT_LIST_HEAD(&ptrace_dead);
693 forget_original_parent(tsk, &ptrace_dead);
694 BUG_ON(!list_empty(&tsk->children));
695 BUG_ON(!list_empty(&tsk->ptrace_children));
698 * Check to see if any process groups have become orphaned
699 * as a result of our exiting, and if they have any stopped
700 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
702 * Case i: Our father is in a different pgrp than we are
703 * and we were the only connection outside, so our pgrp
704 * is about to become orphaned.
707 t = tsk->real_parent;
709 if ((process_group(t) != process_group(tsk)) &&
710 (t->signal->session == tsk->signal->session) &&
711 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
712 has_stopped_jobs(process_group(tsk))) {
713 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
714 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
717 /* Let father know we died
719 * Thread signals are configurable, but you aren't going to use
720 * that to send signals to arbitary processes.
721 * That stops right now.
723 * If the parent exec id doesn't match the exec id we saved
724 * when we started then we know the parent has changed security
725 * domain.
727 * If our self_exec id doesn't match our parent_exec_id then
728 * we have changed execution domain as these two values started
729 * the same after a fork.
733 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
734 ( tsk->parent_exec_id != t->self_exec_id ||
735 tsk->self_exec_id != tsk->parent_exec_id)
736 && !capable(CAP_KILL))
737 tsk->exit_signal = SIGCHLD;
740 /* If something other than our normal parent is ptracing us, then
741 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
742 * only has special meaning to our real parent.
744 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
745 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
746 do_notify_parent(tsk, signal);
747 } else if (tsk->ptrace) {
748 do_notify_parent(tsk, SIGCHLD);
751 state = EXIT_ZOMBIE;
752 if (tsk->exit_signal == -1 &&
753 (likely(tsk->ptrace == 0) ||
754 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
755 state = EXIT_DEAD;
756 tsk->exit_state = state;
758 write_unlock_irq(&tasklist_lock);
760 list_for_each_safe(_p, _n, &ptrace_dead) {
761 list_del_init(_p);
762 t = list_entry(_p,struct task_struct,ptrace_list);
763 release_task(t);
766 /* If the process is dead, release it - nobody will wait for it */
767 if (state == EXIT_DEAD)
768 release_task(tsk);
770 /* PF_DEAD causes final put_task_struct after we schedule. */
771 preempt_disable();
772 tsk->flags |= PF_DEAD;
775 fastcall NORET_TYPE void do_exit(long code)
777 struct task_struct *tsk = current;
778 int group_dead;
780 profile_task_exit(tsk);
782 if (unlikely(in_interrupt()))
783 panic("Aiee, killing interrupt handler!");
784 if (unlikely(!tsk->pid))
785 panic("Attempted to kill the idle task!");
786 if (unlikely(tsk->pid == 1))
787 panic("Attempted to kill init!");
788 if (tsk->io_context)
789 exit_io_context();
791 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
792 current->ptrace_message = code;
793 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
796 tsk->flags |= PF_EXITING;
799 * Make sure we don't try to process any timer firings
800 * while we are already exiting.
802 tsk->it_virt_expires = cputime_zero;
803 tsk->it_prof_expires = cputime_zero;
804 tsk->it_sched_expires = 0;
806 if (unlikely(in_atomic()))
807 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
808 current->comm, current->pid,
809 preempt_count());
811 acct_update_integrals(tsk);
812 update_mem_hiwater(tsk);
813 group_dead = atomic_dec_and_test(&tsk->signal->live);
814 if (group_dead) {
815 del_timer_sync(&tsk->signal->real_timer);
816 acct_process(code);
818 exit_mm(tsk);
820 exit_sem(tsk);
821 __exit_files(tsk);
822 __exit_fs(tsk);
823 exit_namespace(tsk);
824 exit_thread();
825 cpuset_exit(tsk);
826 exit_keys(tsk);
828 if (group_dead && tsk->signal->leader)
829 disassociate_ctty(1);
831 module_put(tsk->thread_info->exec_domain->module);
832 if (tsk->binfmt)
833 module_put(tsk->binfmt->module);
835 tsk->exit_code = code;
836 exit_notify(tsk);
837 #ifdef CONFIG_NUMA
838 mpol_free(tsk->mempolicy);
839 tsk->mempolicy = NULL;
840 #endif
842 BUG_ON(!(current->flags & PF_DEAD));
843 schedule();
844 BUG();
845 /* Avoid "noreturn function does return". */
846 for (;;) ;
849 EXPORT_SYMBOL_GPL(do_exit);
851 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
853 if (comp)
854 complete(comp);
856 do_exit(code);
859 EXPORT_SYMBOL(complete_and_exit);
861 asmlinkage long sys_exit(int error_code)
863 do_exit((error_code&0xff)<<8);
866 task_t fastcall *next_thread(const task_t *p)
868 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
871 EXPORT_SYMBOL(next_thread);
874 * Take down every thread in the group. This is called by fatal signals
875 * as well as by sys_exit_group (below).
877 NORET_TYPE void
878 do_group_exit(int exit_code)
880 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
882 if (current->signal->flags & SIGNAL_GROUP_EXIT)
883 exit_code = current->signal->group_exit_code;
884 else if (!thread_group_empty(current)) {
885 struct signal_struct *const sig = current->signal;
886 struct sighand_struct *const sighand = current->sighand;
887 read_lock(&tasklist_lock);
888 spin_lock_irq(&sighand->siglock);
889 if (sig->flags & SIGNAL_GROUP_EXIT)
890 /* Another thread got here before we took the lock. */
891 exit_code = sig->group_exit_code;
892 else {
893 sig->flags = SIGNAL_GROUP_EXIT;
894 sig->group_exit_code = exit_code;
895 zap_other_threads(current);
897 spin_unlock_irq(&sighand->siglock);
898 read_unlock(&tasklist_lock);
901 do_exit(exit_code);
902 /* NOTREACHED */
906 * this kills every thread in the thread group. Note that any externally
907 * wait4()-ing process will get the correct exit code - even if this
908 * thread is not the thread group leader.
910 asmlinkage void sys_exit_group(int error_code)
912 do_group_exit((error_code & 0xff) << 8);
915 static int eligible_child(pid_t pid, int options, task_t *p)
917 if (pid > 0) {
918 if (p->pid != pid)
919 return 0;
920 } else if (!pid) {
921 if (process_group(p) != process_group(current))
922 return 0;
923 } else if (pid != -1) {
924 if (process_group(p) != -pid)
925 return 0;
929 * Do not consider detached threads that are
930 * not ptraced:
932 if (p->exit_signal == -1 && !p->ptrace)
933 return 0;
935 /* Wait for all children (clone and not) if __WALL is set;
936 * otherwise, wait for clone children *only* if __WCLONE is
937 * set; otherwise, wait for non-clone children *only*. (Note:
938 * A "clone" child here is one that reports to its parent
939 * using a signal other than SIGCHLD.) */
940 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
941 && !(options & __WALL))
942 return 0;
944 * Do not consider thread group leaders that are
945 * in a non-empty thread group:
947 if (current->tgid != p->tgid && delay_group_leader(p))
948 return 2;
950 if (security_task_wait(p))
951 return 0;
953 return 1;
956 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
957 int why, int status,
958 struct siginfo __user *infop,
959 struct rusage __user *rusagep)
961 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
962 put_task_struct(p);
963 if (!retval)
964 retval = put_user(SIGCHLD, &infop->si_signo);
965 if (!retval)
966 retval = put_user(0, &infop->si_errno);
967 if (!retval)
968 retval = put_user((short)why, &infop->si_code);
969 if (!retval)
970 retval = put_user(pid, &infop->si_pid);
971 if (!retval)
972 retval = put_user(uid, &infop->si_uid);
973 if (!retval)
974 retval = put_user(status, &infop->si_status);
975 if (!retval)
976 retval = pid;
977 return retval;
981 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
982 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
983 * the lock and this task is uninteresting. If we return nonzero, we have
984 * released the lock and the system call should return.
986 static int wait_task_zombie(task_t *p, int noreap,
987 struct siginfo __user *infop,
988 int __user *stat_addr, struct rusage __user *ru)
990 unsigned long state;
991 int retval;
992 int status;
994 if (unlikely(noreap)) {
995 pid_t pid = p->pid;
996 uid_t uid = p->uid;
997 int exit_code = p->exit_code;
998 int why, status;
1000 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1001 return 0;
1002 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1003 return 0;
1004 get_task_struct(p);
1005 read_unlock(&tasklist_lock);
1006 if ((exit_code & 0x7f) == 0) {
1007 why = CLD_EXITED;
1008 status = exit_code >> 8;
1009 } else {
1010 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1011 status = exit_code & 0x7f;
1013 return wait_noreap_copyout(p, pid, uid, why,
1014 status, infop, ru);
1018 * Try to move the task's state to DEAD
1019 * only one thread is allowed to do this:
1021 state = xchg(&p->exit_state, EXIT_DEAD);
1022 if (state != EXIT_ZOMBIE) {
1023 BUG_ON(state != EXIT_DEAD);
1024 return 0;
1026 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1028 * This can only happen in a race with a ptraced thread
1029 * dying on another processor.
1031 return 0;
1034 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1036 * The resource counters for the group leader are in its
1037 * own task_struct. Those for dead threads in the group
1038 * are in its signal_struct, as are those for the child
1039 * processes it has previously reaped. All these
1040 * accumulate in the parent's signal_struct c* fields.
1042 * We don't bother to take a lock here to protect these
1043 * p->signal fields, because they are only touched by
1044 * __exit_signal, which runs with tasklist_lock
1045 * write-locked anyway, and so is excluded here. We do
1046 * need to protect the access to p->parent->signal fields,
1047 * as other threads in the parent group can be right
1048 * here reaping other children at the same time.
1050 spin_lock_irq(&p->parent->sighand->siglock);
1051 p->parent->signal->cutime =
1052 cputime_add(p->parent->signal->cutime,
1053 cputime_add(p->utime,
1054 cputime_add(p->signal->utime,
1055 p->signal->cutime)));
1056 p->parent->signal->cstime =
1057 cputime_add(p->parent->signal->cstime,
1058 cputime_add(p->stime,
1059 cputime_add(p->signal->stime,
1060 p->signal->cstime)));
1061 p->parent->signal->cmin_flt +=
1062 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1063 p->parent->signal->cmaj_flt +=
1064 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1065 p->parent->signal->cnvcsw +=
1066 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1067 p->parent->signal->cnivcsw +=
1068 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1069 spin_unlock_irq(&p->parent->sighand->siglock);
1073 * Now we are sure this task is interesting, and no other
1074 * thread can reap it because we set its state to EXIT_DEAD.
1076 read_unlock(&tasklist_lock);
1078 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1079 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1080 ? p->signal->group_exit_code : p->exit_code;
1081 if (!retval && stat_addr)
1082 retval = put_user(status, stat_addr);
1083 if (!retval && infop)
1084 retval = put_user(SIGCHLD, &infop->si_signo);
1085 if (!retval && infop)
1086 retval = put_user(0, &infop->si_errno);
1087 if (!retval && infop) {
1088 int why;
1090 if ((status & 0x7f) == 0) {
1091 why = CLD_EXITED;
1092 status >>= 8;
1093 } else {
1094 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1095 status &= 0x7f;
1097 retval = put_user((short)why, &infop->si_code);
1098 if (!retval)
1099 retval = put_user(status, &infop->si_status);
1101 if (!retval && infop)
1102 retval = put_user(p->pid, &infop->si_pid);
1103 if (!retval && infop)
1104 retval = put_user(p->uid, &infop->si_uid);
1105 if (retval) {
1106 // TODO: is this safe?
1107 p->exit_state = EXIT_ZOMBIE;
1108 return retval;
1110 retval = p->pid;
1111 if (p->real_parent != p->parent) {
1112 write_lock_irq(&tasklist_lock);
1113 /* Double-check with lock held. */
1114 if (p->real_parent != p->parent) {
1115 __ptrace_unlink(p);
1116 // TODO: is this safe?
1117 p->exit_state = EXIT_ZOMBIE;
1119 * If this is not a detached task, notify the parent.
1120 * If it's still not detached after that, don't release
1121 * it now.
1123 if (p->exit_signal != -1) {
1124 do_notify_parent(p, p->exit_signal);
1125 if (p->exit_signal != -1)
1126 p = NULL;
1129 write_unlock_irq(&tasklist_lock);
1131 if (p != NULL)
1132 release_task(p);
1133 BUG_ON(!retval);
1134 return retval;
1138 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1139 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1140 * the lock and this task is uninteresting. If we return nonzero, we have
1141 * released the lock and the system call should return.
1143 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1144 struct siginfo __user *infop,
1145 int __user *stat_addr, struct rusage __user *ru)
1147 int retval, exit_code;
1149 if (!p->exit_code)
1150 return 0;
1151 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1152 p->signal && p->signal->group_stop_count > 0)
1154 * A group stop is in progress and this is the group leader.
1155 * We won't report until all threads have stopped.
1157 return 0;
1160 * Now we are pretty sure this task is interesting.
1161 * Make sure it doesn't get reaped out from under us while we
1162 * give up the lock and then examine it below. We don't want to
1163 * keep holding onto the tasklist_lock while we call getrusage and
1164 * possibly take page faults for user memory.
1166 get_task_struct(p);
1167 read_unlock(&tasklist_lock);
1169 if (unlikely(noreap)) {
1170 pid_t pid = p->pid;
1171 uid_t uid = p->uid;
1172 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1174 exit_code = p->exit_code;
1175 if (unlikely(!exit_code) ||
1176 unlikely(p->state > TASK_STOPPED))
1177 goto bail_ref;
1178 return wait_noreap_copyout(p, pid, uid,
1179 why, (exit_code << 8) | 0x7f,
1180 infop, ru);
1183 write_lock_irq(&tasklist_lock);
1186 * This uses xchg to be atomic with the thread resuming and setting
1187 * it. It must also be done with the write lock held to prevent a
1188 * race with the EXIT_ZOMBIE case.
1190 exit_code = xchg(&p->exit_code, 0);
1191 if (unlikely(p->exit_state)) {
1193 * The task resumed and then died. Let the next iteration
1194 * catch it in EXIT_ZOMBIE. Note that exit_code might
1195 * already be zero here if it resumed and did _exit(0).
1196 * The task itself is dead and won't touch exit_code again;
1197 * other processors in this function are locked out.
1199 p->exit_code = exit_code;
1200 exit_code = 0;
1202 if (unlikely(exit_code == 0)) {
1204 * Another thread in this function got to it first, or it
1205 * resumed, or it resumed and then died.
1207 write_unlock_irq(&tasklist_lock);
1208 bail_ref:
1209 put_task_struct(p);
1211 * We are returning to the wait loop without having successfully
1212 * removed the process and having released the lock. We cannot
1213 * continue, since the "p" task pointer is potentially stale.
1215 * Return -EAGAIN, and do_wait() will restart the loop from the
1216 * beginning. Do _not_ re-acquire the lock.
1218 return -EAGAIN;
1221 /* move to end of parent's list to avoid starvation */
1222 remove_parent(p);
1223 add_parent(p, p->parent);
1225 write_unlock_irq(&tasklist_lock);
1227 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1228 if (!retval && stat_addr)
1229 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1230 if (!retval && infop)
1231 retval = put_user(SIGCHLD, &infop->si_signo);
1232 if (!retval && infop)
1233 retval = put_user(0, &infop->si_errno);
1234 if (!retval && infop)
1235 retval = put_user((short)((p->ptrace & PT_PTRACED)
1236 ? CLD_TRAPPED : CLD_STOPPED),
1237 &infop->si_code);
1238 if (!retval && infop)
1239 retval = put_user(exit_code, &infop->si_status);
1240 if (!retval && infop)
1241 retval = put_user(p->pid, &infop->si_pid);
1242 if (!retval && infop)
1243 retval = put_user(p->uid, &infop->si_uid);
1244 if (!retval)
1245 retval = p->pid;
1246 put_task_struct(p);
1248 BUG_ON(!retval);
1249 return retval;
1253 * Handle do_wait work for one task in a live, non-stopped state.
1254 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1255 * the lock and this task is uninteresting. If we return nonzero, we have
1256 * released the lock and the system call should return.
1258 static int wait_task_continued(task_t *p, int noreap,
1259 struct siginfo __user *infop,
1260 int __user *stat_addr, struct rusage __user *ru)
1262 int retval;
1263 pid_t pid;
1264 uid_t uid;
1266 if (unlikely(!p->signal))
1267 return 0;
1269 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1270 return 0;
1272 spin_lock_irq(&p->sighand->siglock);
1273 /* Re-check with the lock held. */
1274 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1275 spin_unlock_irq(&p->sighand->siglock);
1276 return 0;
1278 if (!noreap)
1279 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1280 spin_unlock_irq(&p->sighand->siglock);
1282 pid = p->pid;
1283 uid = p->uid;
1284 get_task_struct(p);
1285 read_unlock(&tasklist_lock);
1287 if (!infop) {
1288 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1289 put_task_struct(p);
1290 if (!retval && stat_addr)
1291 retval = put_user(0xffff, stat_addr);
1292 if (!retval)
1293 retval = p->pid;
1294 } else {
1295 retval = wait_noreap_copyout(p, pid, uid,
1296 CLD_CONTINUED, SIGCONT,
1297 infop, ru);
1298 BUG_ON(retval == 0);
1301 return retval;
1305 static inline int my_ptrace_child(struct task_struct *p)
1307 if (!(p->ptrace & PT_PTRACED))
1308 return 0;
1309 if (!(p->ptrace & PT_ATTACHED))
1310 return 1;
1312 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1313 * we are the attacher. If we are the real parent, this is a race
1314 * inside ptrace_attach. It is waiting for the tasklist_lock,
1315 * which we have to switch the parent links, but has already set
1316 * the flags in p->ptrace.
1318 return (p->parent != p->real_parent);
1321 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1322 int __user *stat_addr, struct rusage __user *ru)
1324 DECLARE_WAITQUEUE(wait, current);
1325 struct task_struct *tsk;
1326 int flag, retval;
1328 add_wait_queue(&current->signal->wait_chldexit,&wait);
1329 repeat:
1331 * We will set this flag if we see any child that might later
1332 * match our criteria, even if we are not able to reap it yet.
1334 flag = 0;
1335 current->state = TASK_INTERRUPTIBLE;
1336 read_lock(&tasklist_lock);
1337 tsk = current;
1338 do {
1339 struct task_struct *p;
1340 struct list_head *_p;
1341 int ret;
1343 list_for_each(_p,&tsk->children) {
1344 p = list_entry(_p,struct task_struct,sibling);
1346 ret = eligible_child(pid, options, p);
1347 if (!ret)
1348 continue;
1350 switch (p->state) {
1351 case TASK_TRACED:
1352 if (!my_ptrace_child(p))
1353 continue;
1354 /*FALLTHROUGH*/
1355 case TASK_STOPPED:
1357 * It's stopped now, so it might later
1358 * continue, exit, or stop again.
1360 flag = 1;
1361 if (!(options & WUNTRACED) &&
1362 !my_ptrace_child(p))
1363 continue;
1364 retval = wait_task_stopped(p, ret == 2,
1365 (options & WNOWAIT),
1366 infop,
1367 stat_addr, ru);
1368 if (retval == -EAGAIN)
1369 goto repeat;
1370 if (retval != 0) /* He released the lock. */
1371 goto end;
1372 break;
1373 default:
1374 // case EXIT_DEAD:
1375 if (p->exit_state == EXIT_DEAD)
1376 continue;
1377 // case EXIT_ZOMBIE:
1378 if (p->exit_state == EXIT_ZOMBIE) {
1380 * Eligible but we cannot release
1381 * it yet:
1383 if (ret == 2)
1384 goto check_continued;
1385 if (!likely(options & WEXITED))
1386 continue;
1387 retval = wait_task_zombie(
1388 p, (options & WNOWAIT),
1389 infop, stat_addr, ru);
1390 /* He released the lock. */
1391 if (retval != 0)
1392 goto end;
1393 break;
1395 check_continued:
1397 * It's running now, so it might later
1398 * exit, stop, or stop and then continue.
1400 flag = 1;
1401 if (!unlikely(options & WCONTINUED))
1402 continue;
1403 retval = wait_task_continued(
1404 p, (options & WNOWAIT),
1405 infop, stat_addr, ru);
1406 if (retval != 0) /* He released the lock. */
1407 goto end;
1408 break;
1411 if (!flag) {
1412 list_for_each(_p, &tsk->ptrace_children) {
1413 p = list_entry(_p, struct task_struct,
1414 ptrace_list);
1415 if (!eligible_child(pid, options, p))
1416 continue;
1417 flag = 1;
1418 break;
1421 if (options & __WNOTHREAD)
1422 break;
1423 tsk = next_thread(tsk);
1424 if (tsk->signal != current->signal)
1425 BUG();
1426 } while (tsk != current);
1428 read_unlock(&tasklist_lock);
1429 if (flag) {
1430 retval = 0;
1431 if (options & WNOHANG)
1432 goto end;
1433 retval = -ERESTARTSYS;
1434 if (signal_pending(current))
1435 goto end;
1436 schedule();
1437 goto repeat;
1439 retval = -ECHILD;
1440 end:
1441 current->state = TASK_RUNNING;
1442 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1443 if (infop) {
1444 if (retval > 0)
1445 retval = 0;
1446 else {
1448 * For a WNOHANG return, clear out all the fields
1449 * we would set so the user can easily tell the
1450 * difference.
1452 if (!retval)
1453 retval = put_user(0, &infop->si_signo);
1454 if (!retval)
1455 retval = put_user(0, &infop->si_errno);
1456 if (!retval)
1457 retval = put_user(0, &infop->si_code);
1458 if (!retval)
1459 retval = put_user(0, &infop->si_pid);
1460 if (!retval)
1461 retval = put_user(0, &infop->si_uid);
1462 if (!retval)
1463 retval = put_user(0, &infop->si_status);
1466 return retval;
1469 asmlinkage long sys_waitid(int which, pid_t pid,
1470 struct siginfo __user *infop, int options,
1471 struct rusage __user *ru)
1473 long ret;
1475 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1476 return -EINVAL;
1477 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1478 return -EINVAL;
1480 switch (which) {
1481 case P_ALL:
1482 pid = -1;
1483 break;
1484 case P_PID:
1485 if (pid <= 0)
1486 return -EINVAL;
1487 break;
1488 case P_PGID:
1489 if (pid <= 0)
1490 return -EINVAL;
1491 pid = -pid;
1492 break;
1493 default:
1494 return -EINVAL;
1497 ret = do_wait(pid, options, infop, NULL, ru);
1499 /* avoid REGPARM breakage on x86: */
1500 prevent_tail_call(ret);
1501 return ret;
1504 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1505 int options, struct rusage __user *ru)
1507 long ret;
1509 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1510 __WNOTHREAD|__WCLONE|__WALL))
1511 return -EINVAL;
1512 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1514 /* avoid REGPARM breakage on x86: */
1515 prevent_tail_call(ret);
1516 return ret;
1519 #ifdef __ARCH_WANT_SYS_WAITPID
1522 * sys_waitpid() remains for compatibility. waitpid() should be
1523 * implemented by calling sys_wait4() from libc.a.
1525 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1527 return sys_wait4(pid, stat_addr, options, NULL);
1530 #endif