[MMC] Clean up wbsd detection handling
[linux-2.6.22.y-op.git] / kernel / exit.c
blob6d2089a1bce74614009acfb18f1e3b95458e90cf
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);
76 * Note that the fastpath in sys_times depends on __exit_signal having
77 * updated the counters before a task is removed from the tasklist of
78 * the process by __unhash_process.
80 __unhash_process(p);
83 * If we are the last non-leader member of the thread
84 * group, and the leader is zombie, then notify the
85 * group leader's parent process. (if it wants notification.)
87 zap_leader = 0;
88 leader = p->group_leader;
89 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
90 BUG_ON(leader->exit_signal == -1);
91 do_notify_parent(leader, leader->exit_signal);
93 * If we were the last child thread and the leader has
94 * exited already, and the leader's parent ignores SIGCHLD,
95 * then we are the one who should release the leader.
97 * do_notify_parent() will have marked it self-reaping in
98 * that case.
100 zap_leader = (leader->exit_signal == -1);
103 sched_exit(p);
104 write_unlock_irq(&tasklist_lock);
105 spin_unlock(&p->proc_lock);
106 proc_pid_flush(proc_dentry);
107 release_thread(p);
108 put_task_struct(p);
110 p = leader;
111 if (unlikely(zap_leader))
112 goto repeat;
115 /* we are using it only for SMP init */
117 void unhash_process(struct task_struct *p)
119 struct dentry *proc_dentry;
121 spin_lock(&p->proc_lock);
122 proc_dentry = proc_pid_unhash(p);
123 write_lock_irq(&tasklist_lock);
124 __unhash_process(p);
125 write_unlock_irq(&tasklist_lock);
126 spin_unlock(&p->proc_lock);
127 proc_pid_flush(proc_dentry);
131 * This checks not only the pgrp, but falls back on the pid if no
132 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
133 * without this...
135 int session_of_pgrp(int pgrp)
137 struct task_struct *p;
138 int sid = -1;
140 read_lock(&tasklist_lock);
141 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
142 if (p->signal->session > 0) {
143 sid = p->signal->session;
144 goto out;
146 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
147 p = find_task_by_pid(pgrp);
148 if (p)
149 sid = p->signal->session;
150 out:
151 read_unlock(&tasklist_lock);
153 return sid;
157 * Determine if a process group is "orphaned", according to the POSIX
158 * definition in 2.2.2.52. Orphaned process groups are not to be affected
159 * by terminal-generated stop signals. Newly orphaned process groups are
160 * to receive a SIGHUP and a SIGCONT.
162 * "I ask you, have you ever known what it is to be an orphan?"
164 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
166 struct task_struct *p;
167 int ret = 1;
169 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
170 if (p == ignored_task
171 || p->exit_state
172 || p->real_parent->pid == 1)
173 continue;
174 if (process_group(p->real_parent) != pgrp
175 && p->real_parent->signal->session == p->signal->session) {
176 ret = 0;
177 break;
179 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
180 return ret; /* (sighing) "Often!" */
183 int is_orphaned_pgrp(int pgrp)
185 int retval;
187 read_lock(&tasklist_lock);
188 retval = will_become_orphaned_pgrp(pgrp, NULL);
189 read_unlock(&tasklist_lock);
191 return retval;
194 static inline int has_stopped_jobs(int pgrp)
196 int retval = 0;
197 struct task_struct *p;
199 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
200 if (p->state != TASK_STOPPED)
201 continue;
203 /* If p is stopped by a debugger on a signal that won't
204 stop it, then don't count p as stopped. This isn't
205 perfect but it's a good approximation. */
206 if (unlikely (p->ptrace)
207 && p->exit_code != SIGSTOP
208 && p->exit_code != SIGTSTP
209 && p->exit_code != SIGTTOU
210 && p->exit_code != SIGTTIN)
211 continue;
213 retval = 1;
214 break;
215 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
216 return retval;
220 * reparent_to_init - Reparent the calling kernel thread to the init task.
222 * If a kernel thread is launched as a result of a system call, or if
223 * it ever exits, it should generally reparent itself to init so that
224 * it is correctly cleaned up on exit.
226 * The various task state such as scheduling policy and priority may have
227 * been inherited from a user process, so we reset them to sane values here.
229 * NOTE that reparent_to_init() gives the caller full capabilities.
231 static inline void reparent_to_init(void)
233 write_lock_irq(&tasklist_lock);
235 ptrace_unlink(current);
236 /* Reparent to init */
237 REMOVE_LINKS(current);
238 current->parent = child_reaper;
239 current->real_parent = child_reaper;
240 SET_LINKS(current);
242 /* Set the exit signal to SIGCHLD so we signal init on exit */
243 current->exit_signal = SIGCHLD;
245 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
246 set_user_nice(current, 0);
247 /* cpus_allowed? */
248 /* rt_priority? */
249 /* signals? */
250 security_task_reparent_to_init(current);
251 memcpy(current->signal->rlim, init_task.signal->rlim,
252 sizeof(current->signal->rlim));
253 atomic_inc(&(INIT_USER->__count));
254 write_unlock_irq(&tasklist_lock);
255 switch_uid(INIT_USER);
258 void __set_special_pids(pid_t session, pid_t pgrp)
260 struct task_struct *curr = current;
262 if (curr->signal->session != session) {
263 detach_pid(curr, PIDTYPE_SID);
264 curr->signal->session = session;
265 attach_pid(curr, PIDTYPE_SID, session);
267 if (process_group(curr) != pgrp) {
268 detach_pid(curr, PIDTYPE_PGID);
269 curr->signal->pgrp = pgrp;
270 attach_pid(curr, PIDTYPE_PGID, pgrp);
274 void set_special_pids(pid_t session, pid_t pgrp)
276 write_lock_irq(&tasklist_lock);
277 __set_special_pids(session, pgrp);
278 write_unlock_irq(&tasklist_lock);
282 * Let kernel threads use this to say that they
283 * allow a certain signal (since daemonize() will
284 * have disabled all of them by default).
286 int allow_signal(int sig)
288 if (!valid_signal(sig) || sig < 1)
289 return -EINVAL;
291 spin_lock_irq(&current->sighand->siglock);
292 sigdelset(&current->blocked, sig);
293 if (!current->mm) {
294 /* Kernel threads handle their own signals.
295 Let the signal code know it'll be handled, so
296 that they don't get converted to SIGKILL or
297 just silently dropped */
298 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
300 recalc_sigpending();
301 spin_unlock_irq(&current->sighand->siglock);
302 return 0;
305 EXPORT_SYMBOL(allow_signal);
307 int disallow_signal(int sig)
309 if (!valid_signal(sig) || sig < 1)
310 return -EINVAL;
312 spin_lock_irq(&current->sighand->siglock);
313 sigaddset(&current->blocked, sig);
314 recalc_sigpending();
315 spin_unlock_irq(&current->sighand->siglock);
316 return 0;
319 EXPORT_SYMBOL(disallow_signal);
322 * Put all the gunge required to become a kernel thread without
323 * attached user resources in one place where it belongs.
326 void daemonize(const char *name, ...)
328 va_list args;
329 struct fs_struct *fs;
330 sigset_t blocked;
332 va_start(args, name);
333 vsnprintf(current->comm, sizeof(current->comm), name, args);
334 va_end(args);
337 * If we were started as result of loading a module, close all of the
338 * user space pages. We don't need them, and if we didn't close them
339 * they would be locked into memory.
341 exit_mm(current);
343 set_special_pids(1, 1);
344 down(&tty_sem);
345 current->signal->tty = NULL;
346 up(&tty_sem);
348 /* Block and flush all signals */
349 sigfillset(&blocked);
350 sigprocmask(SIG_BLOCK, &blocked, NULL);
351 flush_signals(current);
353 /* Become as one with the init task */
355 exit_fs(current); /* current->fs->count--; */
356 fs = init_task.fs;
357 current->fs = fs;
358 atomic_inc(&fs->count);
359 exit_files(current);
360 current->files = init_task.files;
361 atomic_inc(&current->files->count);
363 reparent_to_init();
366 EXPORT_SYMBOL(daemonize);
368 static inline void close_files(struct files_struct * files)
370 int i, j;
371 struct fdtable *fdt;
373 j = 0;
374 fdt = files_fdtable(files);
375 for (;;) {
376 unsigned long set;
377 i = j * __NFDBITS;
378 if (i >= fdt->max_fdset || i >= fdt->max_fds)
379 break;
380 set = fdt->open_fds->fds_bits[j++];
381 while (set) {
382 if (set & 1) {
383 struct file * file = xchg(&fdt->fd[i], NULL);
384 if (file)
385 filp_close(file, files);
387 i++;
388 set >>= 1;
393 struct files_struct *get_files_struct(struct task_struct *task)
395 struct files_struct *files;
397 task_lock(task);
398 files = task->files;
399 if (files)
400 atomic_inc(&files->count);
401 task_unlock(task);
403 return files;
406 void fastcall put_files_struct(struct files_struct *files)
408 struct fdtable *fdt;
410 if (atomic_dec_and_test(&files->count)) {
411 close_files(files);
413 * Free the fd and fdset arrays if we expanded them.
414 * If the fdtable was embedded, pass files for freeing
415 * at the end of the RCU grace period. Otherwise,
416 * you can free files immediately.
418 fdt = files_fdtable(files);
419 if (fdt == &files->fdtab)
420 fdt->free_files = files;
421 else
422 kmem_cache_free(files_cachep, files);
423 free_fdtable(fdt);
427 EXPORT_SYMBOL(put_files_struct);
429 static inline void __exit_files(struct task_struct *tsk)
431 struct files_struct * files = tsk->files;
433 if (files) {
434 task_lock(tsk);
435 tsk->files = NULL;
436 task_unlock(tsk);
437 put_files_struct(files);
441 void exit_files(struct task_struct *tsk)
443 __exit_files(tsk);
446 static inline void __put_fs_struct(struct fs_struct *fs)
448 /* No need to hold fs->lock if we are killing it */
449 if (atomic_dec_and_test(&fs->count)) {
450 dput(fs->root);
451 mntput(fs->rootmnt);
452 dput(fs->pwd);
453 mntput(fs->pwdmnt);
454 if (fs->altroot) {
455 dput(fs->altroot);
456 mntput(fs->altrootmnt);
458 kmem_cache_free(fs_cachep, fs);
462 void put_fs_struct(struct fs_struct *fs)
464 __put_fs_struct(fs);
467 static inline void __exit_fs(struct task_struct *tsk)
469 struct fs_struct * fs = tsk->fs;
471 if (fs) {
472 task_lock(tsk);
473 tsk->fs = NULL;
474 task_unlock(tsk);
475 __put_fs_struct(fs);
479 void exit_fs(struct task_struct *tsk)
481 __exit_fs(tsk);
484 EXPORT_SYMBOL_GPL(exit_fs);
487 * Turn us into a lazy TLB process if we
488 * aren't already..
490 static void exit_mm(struct task_struct * tsk)
492 struct mm_struct *mm = tsk->mm;
494 mm_release(tsk, mm);
495 if (!mm)
496 return;
498 * Serialize with any possible pending coredump.
499 * We must hold mmap_sem around checking core_waiters
500 * and clearing tsk->mm. The core-inducing thread
501 * will increment core_waiters for each thread in the
502 * group with ->mm != NULL.
504 down_read(&mm->mmap_sem);
505 if (mm->core_waiters) {
506 up_read(&mm->mmap_sem);
507 down_write(&mm->mmap_sem);
508 if (!--mm->core_waiters)
509 complete(mm->core_startup_done);
510 up_write(&mm->mmap_sem);
512 wait_for_completion(&mm->core_done);
513 down_read(&mm->mmap_sem);
515 atomic_inc(&mm->mm_count);
516 if (mm != tsk->active_mm) BUG();
517 /* more a memory barrier than a real lock */
518 task_lock(tsk);
519 tsk->mm = NULL;
520 up_read(&mm->mmap_sem);
521 enter_lazy_tlb(mm, current);
522 task_unlock(tsk);
523 mmput(mm);
526 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
529 * Make sure we're not reparenting to ourselves and that
530 * the parent is not a zombie.
532 BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE);
533 p->real_parent = reaper;
536 static inline void reparent_thread(task_t *p, task_t *father, int traced)
538 /* We don't want people slaying init. */
539 if (p->exit_signal != -1)
540 p->exit_signal = SIGCHLD;
542 if (p->pdeath_signal)
543 /* We already hold the tasklist_lock here. */
544 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
546 /* Move the child from its dying parent to the new one. */
547 if (unlikely(traced)) {
548 /* Preserve ptrace links if someone else is tracing this child. */
549 list_del_init(&p->ptrace_list);
550 if (p->parent != p->real_parent)
551 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
552 } else {
553 /* If this child is being traced, then we're the one tracing it
554 * anyway, so let go of it.
556 p->ptrace = 0;
557 list_del_init(&p->sibling);
558 p->parent = p->real_parent;
559 list_add_tail(&p->sibling, &p->parent->children);
561 /* If we'd notified the old parent about this child's death,
562 * also notify the new parent.
564 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
565 thread_group_empty(p))
566 do_notify_parent(p, p->exit_signal);
567 else if (p->state == TASK_TRACED) {
569 * If it was at a trace stop, turn it into
570 * a normal stop since it's no longer being
571 * traced.
573 ptrace_untrace(p);
578 * process group orphan check
579 * Case ii: Our child is in a different pgrp
580 * than we are, and it was the only connection
581 * outside, so the child pgrp is now orphaned.
583 if ((process_group(p) != process_group(father)) &&
584 (p->signal->session == father->signal->session)) {
585 int pgrp = process_group(p);
587 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
588 __kill_pg_info(SIGHUP, (void *)1, pgrp);
589 __kill_pg_info(SIGCONT, (void *)1, pgrp);
595 * When we die, we re-parent all our children.
596 * Try to give them to another thread in our thread
597 * group, and if no such member exists, give it to
598 * the global child reaper process (ie "init")
600 static inline void forget_original_parent(struct task_struct * father,
601 struct list_head *to_release)
603 struct task_struct *p, *reaper = father;
604 struct list_head *_p, *_n;
606 do {
607 reaper = next_thread(reaper);
608 if (reaper == father) {
609 reaper = child_reaper;
610 break;
612 } while (reaper->exit_state);
615 * There are only two places where our children can be:
617 * - in our child list
618 * - in our ptraced child list
620 * Search them and reparent children.
622 list_for_each_safe(_p, _n, &father->children) {
623 int ptrace;
624 p = list_entry(_p,struct task_struct,sibling);
626 ptrace = p->ptrace;
628 /* if father isn't the real parent, then ptrace must be enabled */
629 BUG_ON(father != p->real_parent && !ptrace);
631 if (father == p->real_parent) {
632 /* reparent with a reaper, real father it's us */
633 choose_new_parent(p, reaper, child_reaper);
634 reparent_thread(p, father, 0);
635 } else {
636 /* reparent ptraced task to its real parent */
637 __ptrace_unlink (p);
638 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
639 thread_group_empty(p))
640 do_notify_parent(p, p->exit_signal);
644 * if the ptraced child is a zombie with exit_signal == -1
645 * we must collect it before we exit, or it will remain
646 * zombie forever since we prevented it from self-reap itself
647 * while it was being traced by us, to be able to see it in wait4.
649 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
650 list_add(&p->ptrace_list, to_release);
652 list_for_each_safe(_p, _n, &father->ptrace_children) {
653 p = list_entry(_p,struct task_struct,ptrace_list);
654 choose_new_parent(p, reaper, child_reaper);
655 reparent_thread(p, father, 1);
660 * Send signals to all our closest relatives so that they know
661 * to properly mourn us..
663 static void exit_notify(struct task_struct *tsk)
665 int state;
666 struct task_struct *t;
667 struct list_head ptrace_dead, *_p, *_n;
669 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
670 && !thread_group_empty(tsk)) {
672 * This occurs when there was a race between our exit
673 * syscall and a group signal choosing us as the one to
674 * wake up. It could be that we are the only thread
675 * alerted to check for pending signals, but another thread
676 * should be woken now to take the signal since we will not.
677 * Now we'll wake all the threads in the group just to make
678 * sure someone gets all the pending signals.
680 read_lock(&tasklist_lock);
681 spin_lock_irq(&tsk->sighand->siglock);
682 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
683 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
684 recalc_sigpending_tsk(t);
685 if (signal_pending(t))
686 signal_wake_up(t, 0);
688 spin_unlock_irq(&tsk->sighand->siglock);
689 read_unlock(&tasklist_lock);
692 write_lock_irq(&tasklist_lock);
695 * This does two things:
697 * A. Make init inherit all the child processes
698 * B. 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)
703 INIT_LIST_HEAD(&ptrace_dead);
704 forget_original_parent(tsk, &ptrace_dead);
705 BUG_ON(!list_empty(&tsk->children));
706 BUG_ON(!list_empty(&tsk->ptrace_children));
709 * Check to see if any process groups have become orphaned
710 * as a result of our exiting, and if they have any stopped
711 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
713 * Case i: Our father is in a different pgrp than we are
714 * and we were the only connection outside, so our pgrp
715 * is about to become orphaned.
718 t = tsk->real_parent;
720 if ((process_group(t) != process_group(tsk)) &&
721 (t->signal->session == tsk->signal->session) &&
722 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
723 has_stopped_jobs(process_group(tsk))) {
724 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
725 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
728 /* Let father know we died
730 * Thread signals are configurable, but you aren't going to use
731 * that to send signals to arbitary processes.
732 * That stops right now.
734 * If the parent exec id doesn't match the exec id we saved
735 * when we started then we know the parent has changed security
736 * domain.
738 * If our self_exec id doesn't match our parent_exec_id then
739 * we have changed execution domain as these two values started
740 * the same after a fork.
744 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
745 ( tsk->parent_exec_id != t->self_exec_id ||
746 tsk->self_exec_id != tsk->parent_exec_id)
747 && !capable(CAP_KILL))
748 tsk->exit_signal = SIGCHLD;
751 /* If something other than our normal parent is ptracing us, then
752 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
753 * only has special meaning to our real parent.
755 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
756 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
757 do_notify_parent(tsk, signal);
758 } else if (tsk->ptrace) {
759 do_notify_parent(tsk, SIGCHLD);
762 state = EXIT_ZOMBIE;
763 if (tsk->exit_signal == -1 &&
764 (likely(tsk->ptrace == 0) ||
765 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
766 state = EXIT_DEAD;
767 tsk->exit_state = state;
769 write_unlock_irq(&tasklist_lock);
771 list_for_each_safe(_p, _n, &ptrace_dead) {
772 list_del_init(_p);
773 t = list_entry(_p,struct task_struct,ptrace_list);
774 release_task(t);
777 /* If the process is dead, release it - nobody will wait for it */
778 if (state == EXIT_DEAD)
779 release_task(tsk);
781 /* PF_DEAD causes final put_task_struct after we schedule. */
782 preempt_disable();
783 tsk->flags |= PF_DEAD;
786 fastcall NORET_TYPE void do_exit(long code)
788 struct task_struct *tsk = current;
789 int group_dead;
791 profile_task_exit(tsk);
793 WARN_ON(atomic_read(&tsk->fs_excl));
795 if (unlikely(in_interrupt()))
796 panic("Aiee, killing interrupt handler!");
797 if (unlikely(!tsk->pid))
798 panic("Attempted to kill the idle task!");
799 if (unlikely(tsk->pid == 1))
800 panic("Attempted to kill init!");
801 if (tsk->io_context)
802 exit_io_context();
804 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
805 current->ptrace_message = code;
806 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
810 * We're taking recursive faults here in do_exit. Safest is to just
811 * leave this task alone and wait for reboot.
813 if (unlikely(tsk->flags & PF_EXITING)) {
814 printk(KERN_ALERT
815 "Fixing recursive fault but reboot is needed!\n");
816 set_current_state(TASK_UNINTERRUPTIBLE);
817 schedule();
820 tsk->flags |= PF_EXITING;
823 * Make sure we don't try to process any timer firings
824 * while we are already exiting.
826 tsk->it_virt_expires = cputime_zero;
827 tsk->it_prof_expires = cputime_zero;
828 tsk->it_sched_expires = 0;
830 if (unlikely(in_atomic()))
831 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
832 current->comm, current->pid,
833 preempt_count());
835 acct_update_integrals(tsk);
836 update_mem_hiwater(tsk);
837 group_dead = atomic_dec_and_test(&tsk->signal->live);
838 if (group_dead) {
839 del_timer_sync(&tsk->signal->real_timer);
840 acct_process(code);
842 exit_mm(tsk);
844 exit_sem(tsk);
845 __exit_files(tsk);
846 __exit_fs(tsk);
847 exit_namespace(tsk);
848 exit_thread();
849 cpuset_exit(tsk);
850 exit_keys(tsk);
852 if (group_dead && tsk->signal->leader)
853 disassociate_ctty(1);
855 module_put(tsk->thread_info->exec_domain->module);
856 if (tsk->binfmt)
857 module_put(tsk->binfmt->module);
859 tsk->exit_code = code;
860 exit_notify(tsk);
861 #ifdef CONFIG_NUMA
862 mpol_free(tsk->mempolicy);
863 tsk->mempolicy = NULL;
864 #endif
866 BUG_ON(!(current->flags & PF_DEAD));
867 schedule();
868 BUG();
869 /* Avoid "noreturn function does return". */
870 for (;;) ;
873 EXPORT_SYMBOL_GPL(do_exit);
875 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
877 if (comp)
878 complete(comp);
880 do_exit(code);
883 EXPORT_SYMBOL(complete_and_exit);
885 asmlinkage long sys_exit(int error_code)
887 do_exit((error_code&0xff)<<8);
890 task_t fastcall *next_thread(const task_t *p)
892 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
895 EXPORT_SYMBOL(next_thread);
898 * Take down every thread in the group. This is called by fatal signals
899 * as well as by sys_exit_group (below).
901 NORET_TYPE void
902 do_group_exit(int exit_code)
904 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
906 if (current->signal->flags & SIGNAL_GROUP_EXIT)
907 exit_code = current->signal->group_exit_code;
908 else if (!thread_group_empty(current)) {
909 struct signal_struct *const sig = current->signal;
910 struct sighand_struct *const sighand = current->sighand;
911 read_lock(&tasklist_lock);
912 spin_lock_irq(&sighand->siglock);
913 if (sig->flags & SIGNAL_GROUP_EXIT)
914 /* Another thread got here before we took the lock. */
915 exit_code = sig->group_exit_code;
916 else {
917 sig->flags = SIGNAL_GROUP_EXIT;
918 sig->group_exit_code = exit_code;
919 zap_other_threads(current);
921 spin_unlock_irq(&sighand->siglock);
922 read_unlock(&tasklist_lock);
925 do_exit(exit_code);
926 /* NOTREACHED */
930 * this kills every thread in the thread group. Note that any externally
931 * wait4()-ing process will get the correct exit code - even if this
932 * thread is not the thread group leader.
934 asmlinkage void sys_exit_group(int error_code)
936 do_group_exit((error_code & 0xff) << 8);
939 static int eligible_child(pid_t pid, int options, task_t *p)
941 if (pid > 0) {
942 if (p->pid != pid)
943 return 0;
944 } else if (!pid) {
945 if (process_group(p) != process_group(current))
946 return 0;
947 } else if (pid != -1) {
948 if (process_group(p) != -pid)
949 return 0;
953 * Do not consider detached threads that are
954 * not ptraced:
956 if (p->exit_signal == -1 && !p->ptrace)
957 return 0;
959 /* Wait for all children (clone and not) if __WALL is set;
960 * otherwise, wait for clone children *only* if __WCLONE is
961 * set; otherwise, wait for non-clone children *only*. (Note:
962 * A "clone" child here is one that reports to its parent
963 * using a signal other than SIGCHLD.) */
964 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
965 && !(options & __WALL))
966 return 0;
968 * Do not consider thread group leaders that are
969 * in a non-empty thread group:
971 if (current->tgid != p->tgid && delay_group_leader(p))
972 return 2;
974 if (security_task_wait(p))
975 return 0;
977 return 1;
980 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
981 int why, int status,
982 struct siginfo __user *infop,
983 struct rusage __user *rusagep)
985 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
986 put_task_struct(p);
987 if (!retval)
988 retval = put_user(SIGCHLD, &infop->si_signo);
989 if (!retval)
990 retval = put_user(0, &infop->si_errno);
991 if (!retval)
992 retval = put_user((short)why, &infop->si_code);
993 if (!retval)
994 retval = put_user(pid, &infop->si_pid);
995 if (!retval)
996 retval = put_user(uid, &infop->si_uid);
997 if (!retval)
998 retval = put_user(status, &infop->si_status);
999 if (!retval)
1000 retval = pid;
1001 return retval;
1005 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1006 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1007 * the lock and this task is uninteresting. If we return nonzero, we have
1008 * released the lock and the system call should return.
1010 static int wait_task_zombie(task_t *p, int noreap,
1011 struct siginfo __user *infop,
1012 int __user *stat_addr, struct rusage __user *ru)
1014 unsigned long state;
1015 int retval;
1016 int status;
1018 if (unlikely(noreap)) {
1019 pid_t pid = p->pid;
1020 uid_t uid = p->uid;
1021 int exit_code = p->exit_code;
1022 int why, status;
1024 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1025 return 0;
1026 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1027 return 0;
1028 get_task_struct(p);
1029 read_unlock(&tasklist_lock);
1030 if ((exit_code & 0x7f) == 0) {
1031 why = CLD_EXITED;
1032 status = exit_code >> 8;
1033 } else {
1034 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1035 status = exit_code & 0x7f;
1037 return wait_noreap_copyout(p, pid, uid, why,
1038 status, infop, ru);
1042 * Try to move the task's state to DEAD
1043 * only one thread is allowed to do this:
1045 state = xchg(&p->exit_state, EXIT_DEAD);
1046 if (state != EXIT_ZOMBIE) {
1047 BUG_ON(state != EXIT_DEAD);
1048 return 0;
1050 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1052 * This can only happen in a race with a ptraced thread
1053 * dying on another processor.
1055 return 0;
1058 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1060 * The resource counters for the group leader are in its
1061 * own task_struct. Those for dead threads in the group
1062 * are in its signal_struct, as are those for the child
1063 * processes it has previously reaped. All these
1064 * accumulate in the parent's signal_struct c* fields.
1066 * We don't bother to take a lock here to protect these
1067 * p->signal fields, because they are only touched by
1068 * __exit_signal, which runs with tasklist_lock
1069 * write-locked anyway, and so is excluded here. We do
1070 * need to protect the access to p->parent->signal fields,
1071 * as other threads in the parent group can be right
1072 * here reaping other children at the same time.
1074 spin_lock_irq(&p->parent->sighand->siglock);
1075 p->parent->signal->cutime =
1076 cputime_add(p->parent->signal->cutime,
1077 cputime_add(p->utime,
1078 cputime_add(p->signal->utime,
1079 p->signal->cutime)));
1080 p->parent->signal->cstime =
1081 cputime_add(p->parent->signal->cstime,
1082 cputime_add(p->stime,
1083 cputime_add(p->signal->stime,
1084 p->signal->cstime)));
1085 p->parent->signal->cmin_flt +=
1086 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1087 p->parent->signal->cmaj_flt +=
1088 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1089 p->parent->signal->cnvcsw +=
1090 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1091 p->parent->signal->cnivcsw +=
1092 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1093 spin_unlock_irq(&p->parent->sighand->siglock);
1097 * Now we are sure this task is interesting, and no other
1098 * thread can reap it because we set its state to EXIT_DEAD.
1100 read_unlock(&tasklist_lock);
1102 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1103 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1104 ? p->signal->group_exit_code : p->exit_code;
1105 if (!retval && stat_addr)
1106 retval = put_user(status, stat_addr);
1107 if (!retval && infop)
1108 retval = put_user(SIGCHLD, &infop->si_signo);
1109 if (!retval && infop)
1110 retval = put_user(0, &infop->si_errno);
1111 if (!retval && infop) {
1112 int why;
1114 if ((status & 0x7f) == 0) {
1115 why = CLD_EXITED;
1116 status >>= 8;
1117 } else {
1118 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1119 status &= 0x7f;
1121 retval = put_user((short)why, &infop->si_code);
1122 if (!retval)
1123 retval = put_user(status, &infop->si_status);
1125 if (!retval && infop)
1126 retval = put_user(p->pid, &infop->si_pid);
1127 if (!retval && infop)
1128 retval = put_user(p->uid, &infop->si_uid);
1129 if (retval) {
1130 // TODO: is this safe?
1131 p->exit_state = EXIT_ZOMBIE;
1132 return retval;
1134 retval = p->pid;
1135 if (p->real_parent != p->parent) {
1136 write_lock_irq(&tasklist_lock);
1137 /* Double-check with lock held. */
1138 if (p->real_parent != p->parent) {
1139 __ptrace_unlink(p);
1140 // TODO: is this safe?
1141 p->exit_state = EXIT_ZOMBIE;
1143 * If this is not a detached task, notify the parent.
1144 * If it's still not detached after that, don't release
1145 * it now.
1147 if (p->exit_signal != -1) {
1148 do_notify_parent(p, p->exit_signal);
1149 if (p->exit_signal != -1)
1150 p = NULL;
1153 write_unlock_irq(&tasklist_lock);
1155 if (p != NULL)
1156 release_task(p);
1157 BUG_ON(!retval);
1158 return retval;
1162 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1163 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1164 * the lock and this task is uninteresting. If we return nonzero, we have
1165 * released the lock and the system call should return.
1167 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1168 struct siginfo __user *infop,
1169 int __user *stat_addr, struct rusage __user *ru)
1171 int retval, exit_code;
1173 if (!p->exit_code)
1174 return 0;
1175 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1176 p->signal && p->signal->group_stop_count > 0)
1178 * A group stop is in progress and this is the group leader.
1179 * We won't report until all threads have stopped.
1181 return 0;
1184 * Now we are pretty sure this task is interesting.
1185 * Make sure it doesn't get reaped out from under us while we
1186 * give up the lock and then examine it below. We don't want to
1187 * keep holding onto the tasklist_lock while we call getrusage and
1188 * possibly take page faults for user memory.
1190 get_task_struct(p);
1191 read_unlock(&tasklist_lock);
1193 if (unlikely(noreap)) {
1194 pid_t pid = p->pid;
1195 uid_t uid = p->uid;
1196 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1198 exit_code = p->exit_code;
1199 if (unlikely(!exit_code) ||
1200 unlikely(p->state > TASK_STOPPED))
1201 goto bail_ref;
1202 return wait_noreap_copyout(p, pid, uid,
1203 why, (exit_code << 8) | 0x7f,
1204 infop, ru);
1207 write_lock_irq(&tasklist_lock);
1210 * This uses xchg to be atomic with the thread resuming and setting
1211 * it. It must also be done with the write lock held to prevent a
1212 * race with the EXIT_ZOMBIE case.
1214 exit_code = xchg(&p->exit_code, 0);
1215 if (unlikely(p->exit_state)) {
1217 * The task resumed and then died. Let the next iteration
1218 * catch it in EXIT_ZOMBIE. Note that exit_code might
1219 * already be zero here if it resumed and did _exit(0).
1220 * The task itself is dead and won't touch exit_code again;
1221 * other processors in this function are locked out.
1223 p->exit_code = exit_code;
1224 exit_code = 0;
1226 if (unlikely(exit_code == 0)) {
1228 * Another thread in this function got to it first, or it
1229 * resumed, or it resumed and then died.
1231 write_unlock_irq(&tasklist_lock);
1232 bail_ref:
1233 put_task_struct(p);
1235 * We are returning to the wait loop without having successfully
1236 * removed the process and having released the lock. We cannot
1237 * continue, since the "p" task pointer is potentially stale.
1239 * Return -EAGAIN, and do_wait() will restart the loop from the
1240 * beginning. Do _not_ re-acquire the lock.
1242 return -EAGAIN;
1245 /* move to end of parent's list to avoid starvation */
1246 remove_parent(p);
1247 add_parent(p, p->parent);
1249 write_unlock_irq(&tasklist_lock);
1251 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1252 if (!retval && stat_addr)
1253 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1254 if (!retval && infop)
1255 retval = put_user(SIGCHLD, &infop->si_signo);
1256 if (!retval && infop)
1257 retval = put_user(0, &infop->si_errno);
1258 if (!retval && infop)
1259 retval = put_user((short)((p->ptrace & PT_PTRACED)
1260 ? CLD_TRAPPED : CLD_STOPPED),
1261 &infop->si_code);
1262 if (!retval && infop)
1263 retval = put_user(exit_code, &infop->si_status);
1264 if (!retval && infop)
1265 retval = put_user(p->pid, &infop->si_pid);
1266 if (!retval && infop)
1267 retval = put_user(p->uid, &infop->si_uid);
1268 if (!retval)
1269 retval = p->pid;
1270 put_task_struct(p);
1272 BUG_ON(!retval);
1273 return retval;
1277 * Handle do_wait work for one task in a live, non-stopped state.
1278 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1279 * the lock and this task is uninteresting. If we return nonzero, we have
1280 * released the lock and the system call should return.
1282 static int wait_task_continued(task_t *p, int noreap,
1283 struct siginfo __user *infop,
1284 int __user *stat_addr, struct rusage __user *ru)
1286 int retval;
1287 pid_t pid;
1288 uid_t uid;
1290 if (unlikely(!p->signal))
1291 return 0;
1293 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1294 return 0;
1296 spin_lock_irq(&p->sighand->siglock);
1297 /* Re-check with the lock held. */
1298 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1299 spin_unlock_irq(&p->sighand->siglock);
1300 return 0;
1302 if (!noreap)
1303 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1304 spin_unlock_irq(&p->sighand->siglock);
1306 pid = p->pid;
1307 uid = p->uid;
1308 get_task_struct(p);
1309 read_unlock(&tasklist_lock);
1311 if (!infop) {
1312 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1313 put_task_struct(p);
1314 if (!retval && stat_addr)
1315 retval = put_user(0xffff, stat_addr);
1316 if (!retval)
1317 retval = p->pid;
1318 } else {
1319 retval = wait_noreap_copyout(p, pid, uid,
1320 CLD_CONTINUED, SIGCONT,
1321 infop, ru);
1322 BUG_ON(retval == 0);
1325 return retval;
1329 static inline int my_ptrace_child(struct task_struct *p)
1331 if (!(p->ptrace & PT_PTRACED))
1332 return 0;
1333 if (!(p->ptrace & PT_ATTACHED))
1334 return 1;
1336 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1337 * we are the attacher. If we are the real parent, this is a race
1338 * inside ptrace_attach. It is waiting for the tasklist_lock,
1339 * which we have to switch the parent links, but has already set
1340 * the flags in p->ptrace.
1342 return (p->parent != p->real_parent);
1345 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1346 int __user *stat_addr, struct rusage __user *ru)
1348 DECLARE_WAITQUEUE(wait, current);
1349 struct task_struct *tsk;
1350 int flag, retval;
1352 add_wait_queue(&current->signal->wait_chldexit,&wait);
1353 repeat:
1355 * We will set this flag if we see any child that might later
1356 * match our criteria, even if we are not able to reap it yet.
1358 flag = 0;
1359 current->state = TASK_INTERRUPTIBLE;
1360 read_lock(&tasklist_lock);
1361 tsk = current;
1362 do {
1363 struct task_struct *p;
1364 struct list_head *_p;
1365 int ret;
1367 list_for_each(_p,&tsk->children) {
1368 p = list_entry(_p,struct task_struct,sibling);
1370 ret = eligible_child(pid, options, p);
1371 if (!ret)
1372 continue;
1374 switch (p->state) {
1375 case TASK_TRACED:
1376 if (!my_ptrace_child(p))
1377 continue;
1378 /*FALLTHROUGH*/
1379 case TASK_STOPPED:
1381 * It's stopped now, so it might later
1382 * continue, exit, or stop again.
1384 flag = 1;
1385 if (!(options & WUNTRACED) &&
1386 !my_ptrace_child(p))
1387 continue;
1388 retval = wait_task_stopped(p, ret == 2,
1389 (options & WNOWAIT),
1390 infop,
1391 stat_addr, ru);
1392 if (retval == -EAGAIN)
1393 goto repeat;
1394 if (retval != 0) /* He released the lock. */
1395 goto end;
1396 break;
1397 default:
1398 // case EXIT_DEAD:
1399 if (p->exit_state == EXIT_DEAD)
1400 continue;
1401 // case EXIT_ZOMBIE:
1402 if (p->exit_state == EXIT_ZOMBIE) {
1404 * Eligible but we cannot release
1405 * it yet:
1407 if (ret == 2)
1408 goto check_continued;
1409 if (!likely(options & WEXITED))
1410 continue;
1411 retval = wait_task_zombie(
1412 p, (options & WNOWAIT),
1413 infop, stat_addr, ru);
1414 /* He released the lock. */
1415 if (retval != 0)
1416 goto end;
1417 break;
1419 check_continued:
1421 * It's running now, so it might later
1422 * exit, stop, or stop and then continue.
1424 flag = 1;
1425 if (!unlikely(options & WCONTINUED))
1426 continue;
1427 retval = wait_task_continued(
1428 p, (options & WNOWAIT),
1429 infop, stat_addr, ru);
1430 if (retval != 0) /* He released the lock. */
1431 goto end;
1432 break;
1435 if (!flag) {
1436 list_for_each(_p, &tsk->ptrace_children) {
1437 p = list_entry(_p, struct task_struct,
1438 ptrace_list);
1439 if (!eligible_child(pid, options, p))
1440 continue;
1441 flag = 1;
1442 break;
1445 if (options & __WNOTHREAD)
1446 break;
1447 tsk = next_thread(tsk);
1448 if (tsk->signal != current->signal)
1449 BUG();
1450 } while (tsk != current);
1452 read_unlock(&tasklist_lock);
1453 if (flag) {
1454 retval = 0;
1455 if (options & WNOHANG)
1456 goto end;
1457 retval = -ERESTARTSYS;
1458 if (signal_pending(current))
1459 goto end;
1460 schedule();
1461 goto repeat;
1463 retval = -ECHILD;
1464 end:
1465 current->state = TASK_RUNNING;
1466 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1467 if (infop) {
1468 if (retval > 0)
1469 retval = 0;
1470 else {
1472 * For a WNOHANG return, clear out all the fields
1473 * we would set so the user can easily tell the
1474 * difference.
1476 if (!retval)
1477 retval = put_user(0, &infop->si_signo);
1478 if (!retval)
1479 retval = put_user(0, &infop->si_errno);
1480 if (!retval)
1481 retval = put_user(0, &infop->si_code);
1482 if (!retval)
1483 retval = put_user(0, &infop->si_pid);
1484 if (!retval)
1485 retval = put_user(0, &infop->si_uid);
1486 if (!retval)
1487 retval = put_user(0, &infop->si_status);
1490 return retval;
1493 asmlinkage long sys_waitid(int which, pid_t pid,
1494 struct siginfo __user *infop, int options,
1495 struct rusage __user *ru)
1497 long ret;
1499 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1500 return -EINVAL;
1501 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1502 return -EINVAL;
1504 switch (which) {
1505 case P_ALL:
1506 pid = -1;
1507 break;
1508 case P_PID:
1509 if (pid <= 0)
1510 return -EINVAL;
1511 break;
1512 case P_PGID:
1513 if (pid <= 0)
1514 return -EINVAL;
1515 pid = -pid;
1516 break;
1517 default:
1518 return -EINVAL;
1521 ret = do_wait(pid, options, infop, NULL, ru);
1523 /* avoid REGPARM breakage on x86: */
1524 prevent_tail_call(ret);
1525 return ret;
1528 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1529 int options, struct rusage __user *ru)
1531 long ret;
1533 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1534 __WNOTHREAD|__WCLONE|__WALL))
1535 return -EINVAL;
1536 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1538 /* avoid REGPARM breakage on x86: */
1539 prevent_tail_call(ret);
1540 return ret;
1543 #ifdef __ARCH_WANT_SYS_WAITPID
1546 * sys_waitpid() remains for compatibility. waitpid() should be
1547 * implemented by calling sys_wait4() from libc.a.
1549 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1551 return sys_wait4(pid, stat_addr, options, NULL);
1554 #endif