MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / kernel / exit.c
blob6ec1f96fa92b6e4a3daaf1e698ae6ff30a7b2051
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/security.h>
18 #include <linux/cpu.h>
19 #include <linux/acct.h>
20 #include <linux/file.h>
21 #include <linux/binfmts.h>
22 #include <linux/ptrace.h>
23 #include <linux/profile.h>
24 #include <linux/mount.h>
25 #include <linux/proc_fs.h>
26 #include <linux/mempolicy.h>
28 #include <asm/uaccess.h>
29 #include <asm/unistd.h>
30 #include <asm/pgtable.h>
31 #include <asm/mmu_context.h>
33 extern void sem_exit (void);
34 extern struct task_struct *child_reaper;
36 int getrusage(struct task_struct *, int, struct rusage __user *);
38 static void __unhash_process(struct task_struct *p)
40 nr_threads--;
41 detach_pid(p, PIDTYPE_PID);
42 detach_pid(p, PIDTYPE_TGID);
43 if (thread_group_leader(p)) {
44 detach_pid(p, PIDTYPE_PGID);
45 detach_pid(p, PIDTYPE_SID);
46 if (p->pid)
47 __get_cpu_var(process_counts)--;
50 REMOVE_LINKS(p);
53 void release_task(struct task_struct * p)
55 int zap_leader;
56 task_t *leader;
57 struct dentry *proc_dentry;
59 repeat:
60 atomic_dec(&p->user->processes);
61 spin_lock(&p->proc_lock);
62 proc_dentry = proc_pid_unhash(p);
63 write_lock_irq(&tasklist_lock);
64 if (unlikely(p->ptrace))
65 __ptrace_unlink(p);
66 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
67 __exit_signal(p);
68 __exit_sighand(p);
69 __unhash_process(p);
72 * If we are the last non-leader member of the thread
73 * group, and the leader is zombie, then notify the
74 * group leader's parent process. (if it wants notification.)
76 zap_leader = 0;
77 leader = p->group_leader;
78 if (leader != p && thread_group_empty(leader) && leader->state == TASK_ZOMBIE) {
79 BUG_ON(leader->exit_signal == -1);
80 do_notify_parent(leader, leader->exit_signal);
82 * If we were the last child thread and the leader has
83 * exited already, and the leader's parent ignores SIGCHLD,
84 * then we are the one who should release the leader.
86 * do_notify_parent() will have marked it self-reaping in
87 * that case.
89 zap_leader = (leader->exit_signal == -1);
92 sched_exit(p);
93 write_unlock_irq(&tasklist_lock);
94 spin_unlock(&p->proc_lock);
95 proc_pid_flush(proc_dentry);
96 release_thread(p);
97 put_task_struct(p);
99 p = leader;
100 if (unlikely(zap_leader))
101 goto repeat;
104 /* we are using it only for SMP init */
106 void unhash_process(struct task_struct *p)
108 struct dentry *proc_dentry;
110 spin_lock(&p->proc_lock);
111 proc_dentry = proc_pid_unhash(p);
112 write_lock_irq(&tasklist_lock);
113 __unhash_process(p);
114 write_unlock_irq(&tasklist_lock);
115 spin_unlock(&p->proc_lock);
116 proc_pid_flush(proc_dentry);
120 * This checks not only the pgrp, but falls back on the pid if no
121 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
122 * without this...
124 int session_of_pgrp(int pgrp)
126 struct task_struct *p;
127 int sid = -1;
129 read_lock(&tasklist_lock);
130 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
131 if (p->signal->session > 0) {
132 sid = p->signal->session;
133 goto out;
135 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
136 p = find_task_by_pid(pgrp);
137 if (p)
138 sid = p->signal->session;
139 out:
140 read_unlock(&tasklist_lock);
142 return sid;
146 * Determine if a process group is "orphaned", according to the POSIX
147 * definition in 2.2.2.52. Orphaned process groups are not to be affected
148 * by terminal-generated stop signals. Newly orphaned process groups are
149 * to receive a SIGHUP and a SIGCONT.
151 * "I ask you, have you ever known what it is to be an orphan?"
153 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
155 struct task_struct *p;
156 int ret = 1;
158 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
159 if (p == ignored_task
160 || p->state >= TASK_ZOMBIE
161 || p->real_parent->pid == 1)
162 continue;
163 if (process_group(p->real_parent) != pgrp
164 && p->real_parent->signal->session == p->signal->session) {
165 ret = 0;
166 break;
168 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
169 return ret; /* (sighing) "Often!" */
172 int is_orphaned_pgrp(int pgrp)
174 int retval;
176 read_lock(&tasklist_lock);
177 retval = will_become_orphaned_pgrp(pgrp, NULL);
178 read_unlock(&tasklist_lock);
180 return retval;
183 static inline int has_stopped_jobs(int pgrp)
185 int retval = 0;
186 struct task_struct *p;
188 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
189 if (p->state != TASK_STOPPED)
190 continue;
192 /* If p is stopped by a debugger on a signal that won't
193 stop it, then don't count p as stopped. This isn't
194 perfect but it's a good approximation. */
195 if (unlikely (p->ptrace)
196 && p->exit_code != SIGSTOP
197 && p->exit_code != SIGTSTP
198 && p->exit_code != SIGTTOU
199 && p->exit_code != SIGTTIN)
200 continue;
202 retval = 1;
203 break;
204 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
205 return retval;
209 * reparent_to_init() - Reparent the calling kernel thread to the init task.
211 * If a kernel thread is launched as a result of a system call, or if
212 * it ever exits, it should generally reparent itself to init so that
213 * it is correctly cleaned up on exit.
215 * The various task state such as scheduling policy and priority may have
216 * been inherited from a user process, so we reset them to sane values here.
218 * NOTE that reparent_to_init() gives the caller full capabilities.
220 void reparent_to_init(void)
222 write_lock_irq(&tasklist_lock);
224 ptrace_unlink(current);
225 /* Reparent to init */
226 REMOVE_LINKS(current);
227 current->parent = child_reaper;
228 current->real_parent = child_reaper;
229 SET_LINKS(current);
231 /* Set the exit signal to SIGCHLD so we signal init on exit */
232 current->exit_signal = SIGCHLD;
234 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
235 set_user_nice(current, 0);
236 /* cpus_allowed? */
237 /* rt_priority? */
238 /* signals? */
239 security_task_reparent_to_init(current);
240 memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
241 atomic_inc(&(INIT_USER->__count));
242 switch_uid(INIT_USER);
244 write_unlock_irq(&tasklist_lock);
247 void __set_special_pids(pid_t session, pid_t pgrp)
249 struct task_struct *curr = current;
251 if (curr->signal->session != session) {
252 detach_pid(curr, PIDTYPE_SID);
253 curr->signal->session = session;
254 attach_pid(curr, PIDTYPE_SID, session);
256 if (process_group(curr) != pgrp) {
257 detach_pid(curr, PIDTYPE_PGID);
258 curr->signal->pgrp = pgrp;
259 attach_pid(curr, PIDTYPE_PGID, pgrp);
263 void set_special_pids(pid_t session, pid_t pgrp)
265 write_lock_irq(&tasklist_lock);
266 __set_special_pids(session, pgrp);
267 write_unlock_irq(&tasklist_lock);
271 * Let kernel threads use this to say that they
272 * allow a certain signal (since daemonize() will
273 * have disabled all of them by default).
275 int allow_signal(int sig)
277 if (sig < 1 || sig > _NSIG)
278 return -EINVAL;
280 spin_lock_irq(&current->sighand->siglock);
281 sigdelset(&current->blocked, sig);
282 if (!current->mm) {
283 /* Kernel threads handle their own signals.
284 Let the signal code know it'll be handled, so
285 that they don't get converted to SIGKILL or
286 just silently dropped */
287 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
289 recalc_sigpending();
290 spin_unlock_irq(&current->sighand->siglock);
291 return 0;
294 EXPORT_SYMBOL(allow_signal);
296 int disallow_signal(int sig)
298 if (sig < 1 || sig > _NSIG)
299 return -EINVAL;
301 spin_lock_irq(&current->sighand->siglock);
302 sigaddset(&current->blocked, sig);
303 recalc_sigpending();
304 spin_unlock_irq(&current->sighand->siglock);
305 return 0;
308 EXPORT_SYMBOL(disallow_signal);
311 * Put all the gunge required to become a kernel thread without
312 * attached user resources in one place where it belongs.
315 void daemonize(const char *name, ...)
317 va_list args;
318 struct fs_struct *fs;
319 sigset_t blocked;
321 va_start(args, name);
322 vsnprintf(current->comm, sizeof(current->comm), name, args);
323 va_end(args);
326 * If we were started as result of loading a module, close all of the
327 * user space pages. We don't need them, and if we didn't close them
328 * they would be locked into memory.
330 exit_mm(current);
332 set_special_pids(1, 1);
333 current->signal->tty = NULL;
335 /* Block and flush all signals */
336 sigfillset(&blocked);
337 sigprocmask(SIG_BLOCK, &blocked, NULL);
338 flush_signals(current);
340 /* Become as one with the init task */
342 exit_fs(current); /* current->fs->count--; */
343 fs = init_task.fs;
344 current->fs = fs;
345 atomic_inc(&fs->count);
346 exit_files(current);
347 current->files = init_task.files;
348 atomic_inc(&current->files->count);
350 reparent_to_init();
353 EXPORT_SYMBOL(daemonize);
355 static inline void close_files(struct files_struct * files)
357 int i, j;
359 j = 0;
360 for (;;) {
361 unsigned long set;
362 i = j * __NFDBITS;
363 if (i >= files->max_fdset || i >= files->max_fds)
364 break;
365 set = files->open_fds->fds_bits[j++];
366 while (set) {
367 if (set & 1) {
368 struct file * file = xchg(&files->fd[i], NULL);
369 if (file)
370 filp_close(file, files);
372 i++;
373 set >>= 1;
378 struct files_struct *get_files_struct(struct task_struct *task)
380 struct files_struct *files;
382 task_lock(task);
383 files = task->files;
384 if (files)
385 atomic_inc(&files->count);
386 task_unlock(task);
388 return files;
391 void fastcall put_files_struct(struct files_struct *files)
393 if (atomic_dec_and_test(&files->count)) {
394 close_files(files);
396 * Free the fd and fdset arrays if we expanded them.
398 if (files->fd != &files->fd_array[0])
399 free_fd_array(files->fd, files->max_fds);
400 if (files->max_fdset > __FD_SETSIZE) {
401 free_fdset(files->open_fds, files->max_fdset);
402 free_fdset(files->close_on_exec, files->max_fdset);
404 kmem_cache_free(files_cachep, files);
408 EXPORT_SYMBOL(put_files_struct);
410 static inline void __exit_files(struct task_struct *tsk)
412 struct files_struct * files = tsk->files;
414 if (files) {
415 task_lock(tsk);
416 tsk->files = NULL;
417 task_unlock(tsk);
418 put_files_struct(files);
422 void exit_files(struct task_struct *tsk)
424 __exit_files(tsk);
427 static inline void __put_fs_struct(struct fs_struct *fs)
429 /* No need to hold fs->lock if we are killing it */
430 if (atomic_dec_and_test(&fs->count)) {
431 dput(fs->root);
432 mntput(fs->rootmnt);
433 dput(fs->pwd);
434 mntput(fs->pwdmnt);
435 if (fs->altroot) {
436 dput(fs->altroot);
437 mntput(fs->altrootmnt);
439 kmem_cache_free(fs_cachep, fs);
443 void put_fs_struct(struct fs_struct *fs)
445 __put_fs_struct(fs);
448 static inline void __exit_fs(struct task_struct *tsk)
450 struct fs_struct * fs = tsk->fs;
452 if (fs) {
453 task_lock(tsk);
454 tsk->fs = NULL;
455 task_unlock(tsk);
456 __put_fs_struct(fs);
460 void exit_fs(struct task_struct *tsk)
462 __exit_fs(tsk);
465 EXPORT_SYMBOL_GPL(exit_fs);
468 * Turn us into a lazy TLB process if we
469 * aren't already..
471 static inline void __exit_mm(struct task_struct * tsk)
473 struct mm_struct *mm = tsk->mm;
475 mm_release(tsk, mm);
476 if (!mm)
477 return;
479 * Serialize with any possible pending coredump.
480 * We must hold mmap_sem around checking core_waiters
481 * and clearing tsk->mm. The core-inducing thread
482 * will increment core_waiters for each thread in the
483 * group with ->mm != NULL.
485 down_read(&mm->mmap_sem);
486 if (mm->core_waiters) {
487 up_read(&mm->mmap_sem);
488 down_write(&mm->mmap_sem);
489 if (!--mm->core_waiters)
490 complete(mm->core_startup_done);
491 up_write(&mm->mmap_sem);
493 wait_for_completion(&mm->core_done);
494 down_read(&mm->mmap_sem);
496 atomic_inc(&mm->mm_count);
497 if (mm != tsk->active_mm) BUG();
498 /* more a memory barrier than a real lock */
499 task_lock(tsk);
500 tsk->mm = NULL;
501 up_read(&mm->mmap_sem);
502 enter_lazy_tlb(mm, current);
503 task_unlock(tsk);
504 mmput(mm);
507 void exit_mm(struct task_struct *tsk)
509 __exit_mm(tsk);
512 EXPORT_SYMBOL(exit_mm);
514 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
517 * Make sure we're not reparenting to ourselves and that
518 * the parent is not a zombie.
520 BUG_ON(p == reaper || reaper->state >= TASK_ZOMBIE);
521 p->real_parent = reaper;
522 if (p->parent == p->real_parent)
523 BUG();
526 static inline void reparent_thread(task_t *p, task_t *father, int traced)
528 /* We don't want people slaying init. */
529 if (p->exit_signal != -1)
530 p->exit_signal = SIGCHLD;
531 p->self_exec_id++;
533 if (p->pdeath_signal)
534 /* We already hold the tasklist_lock here. */
535 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
537 /* Move the child from its dying parent to the new one. */
538 if (unlikely(traced)) {
539 /* Preserve ptrace links if someone else is tracing this child. */
540 list_del_init(&p->ptrace_list);
541 if (p->parent != p->real_parent)
542 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
543 } else {
544 /* If this child is being traced, then we're the one tracing it
545 * anyway, so let go of it.
547 p->ptrace = 0;
548 list_del_init(&p->sibling);
549 p->parent = p->real_parent;
550 list_add_tail(&p->sibling, &p->parent->children);
552 /* If we'd notified the old parent about this child's death,
553 * also notify the new parent.
555 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
556 thread_group_empty(p))
557 do_notify_parent(p, p->exit_signal);
558 else if (p->state == TASK_TRACED) {
560 * If it was at a trace stop, turn it into
561 * a normal stop since it's no longer being
562 * traced.
564 p->state = TASK_STOPPED;
569 * process group orphan check
570 * Case ii: Our child is in a different pgrp
571 * than we are, and it was the only connection
572 * outside, so the child pgrp is now orphaned.
574 if ((process_group(p) != process_group(father)) &&
575 (p->signal->session == father->signal->session)) {
576 int pgrp = process_group(p);
578 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
579 __kill_pg_info(SIGHUP, (void *)1, pgrp);
580 __kill_pg_info(SIGCONT, (void *)1, pgrp);
586 * When we die, we re-parent all our children.
587 * Try to give them to another thread in our thread
588 * group, and if no such member exists, give it to
589 * the global child reaper process (ie "init")
591 static inline void forget_original_parent(struct task_struct * father,
592 struct list_head *to_release)
594 struct task_struct *p, *reaper = father;
595 struct list_head *_p, *_n;
597 do {
598 reaper = next_thread(reaper);
599 if (reaper == father) {
600 reaper = child_reaper;
601 break;
603 } while (reaper->state >= TASK_ZOMBIE);
606 * There are only two places where our children can be:
608 * - in our child list
609 * - in our ptraced child list
611 * Search them and reparent children.
613 list_for_each_safe(_p, _n, &father->children) {
614 int ptrace;
615 p = list_entry(_p,struct task_struct,sibling);
617 ptrace = p->ptrace;
619 /* if father isn't the real parent, then ptrace must be enabled */
620 BUG_ON(father != p->real_parent && !ptrace);
622 if (father == p->real_parent) {
623 /* reparent with a reaper, real father it's us */
624 choose_new_parent(p, reaper, child_reaper);
625 reparent_thread(p, father, 0);
626 } else {
627 /* reparent ptraced task to its real parent */
628 __ptrace_unlink (p);
629 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
630 thread_group_empty(p))
631 do_notify_parent(p, p->exit_signal);
635 * if the ptraced child is a zombie with exit_signal == -1
636 * we must collect it before we exit, or it will remain
637 * zombie forever since we prevented it from self-reap itself
638 * while it was being traced by us, to be able to see it in wait4.
640 if (unlikely(ptrace && p->state == TASK_ZOMBIE && p->exit_signal == -1))
641 list_add(&p->ptrace_list, to_release);
643 list_for_each_safe(_p, _n, &father->ptrace_children) {
644 p = list_entry(_p,struct task_struct,ptrace_list);
645 choose_new_parent(p, reaper, child_reaper);
646 reparent_thread(p, father, 1);
651 * Send signals to all our closest relatives so that they know
652 * to properly mourn us..
654 static void exit_notify(struct task_struct *tsk)
656 int state;
657 struct task_struct *t;
658 struct list_head ptrace_dead, *_p, *_n;
660 if (signal_pending(tsk) && !tsk->signal->group_exit
661 && !thread_group_empty(tsk)) {
663 * This occurs when there was a race between our exit
664 * syscall and a group signal choosing us as the one to
665 * wake up. It could be that we are the only thread
666 * alerted to check for pending signals, but another thread
667 * should be woken now to take the signal since we will not.
668 * Now we'll wake all the threads in the group just to make
669 * sure someone gets all the pending signals.
671 read_lock(&tasklist_lock);
672 spin_lock_irq(&tsk->sighand->siglock);
673 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
674 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
675 recalc_sigpending_tsk(t);
676 if (signal_pending(t))
677 signal_wake_up(t, 0);
679 spin_unlock_irq(&tsk->sighand->siglock);
680 read_unlock(&tasklist_lock);
683 write_lock_irq(&tasklist_lock);
686 * This does two things:
688 * A. Make init inherit all the child processes
689 * B. Check to see if any process groups have become orphaned
690 * as a result of our exiting, and if they have any stopped
691 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
694 INIT_LIST_HEAD(&ptrace_dead);
695 forget_original_parent(tsk, &ptrace_dead);
696 BUG_ON(!list_empty(&tsk->children));
697 BUG_ON(!list_empty(&tsk->ptrace_children));
700 * Check to see if any process groups have become orphaned
701 * as a result of our exiting, and if they have any stopped
702 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
704 * Case i: Our father is in a different pgrp than we are
705 * and we were the only connection outside, so our pgrp
706 * is about to become orphaned.
709 t = tsk->real_parent;
711 if ((process_group(t) != process_group(tsk)) &&
712 (t->signal->session == tsk->signal->session) &&
713 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
714 has_stopped_jobs(process_group(tsk))) {
715 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
716 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
719 /* Let father know we died
721 * Thread signals are configurable, but you aren't going to use
722 * that to send signals to arbitary processes.
723 * That stops right now.
725 * If the parent exec id doesn't match the exec id we saved
726 * when we started then we know the parent has changed security
727 * domain.
729 * If our self_exec id doesn't match our parent_exec_id then
730 * we have changed execution domain as these two values started
731 * the same after a fork.
735 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
736 ( tsk->parent_exec_id != t->self_exec_id ||
737 tsk->self_exec_id != tsk->parent_exec_id)
738 && !capable(CAP_KILL))
739 tsk->exit_signal = SIGCHLD;
742 /* If something other than our normal parent is ptracing us, then
743 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
744 * only has special meaning to our real parent.
746 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
747 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
748 do_notify_parent(tsk, signal);
749 } else if (tsk->ptrace) {
750 do_notify_parent(tsk, SIGCHLD);
753 state = TASK_ZOMBIE;
754 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
755 state = TASK_DEAD;
756 tsk->state = state;
759 * Clear these here so that update_process_times() won't try to deliver
760 * itimer, profile or rlimit signals to this task while it is in late exit.
762 tsk->it_virt_value = 0;
763 tsk->it_prof_value = 0;
764 tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
766 write_unlock_irq(&tasklist_lock);
768 list_for_each_safe(_p, _n, &ptrace_dead) {
769 list_del_init(_p);
770 t = list_entry(_p,struct task_struct,ptrace_list);
771 release_task(t);
774 /* If the process is dead, release it - nobody will wait for it */
775 if (state == TASK_DEAD)
776 release_task(tsk);
778 /* PF_DEAD causes final put_task_struct after we schedule. */
779 preempt_disable();
780 tsk->flags |= PF_DEAD;
783 asmlinkage NORET_TYPE void do_exit(long code)
785 struct task_struct *tsk = current;
787 profile_task_exit(tsk);
789 if (unlikely(in_interrupt()))
790 panic("Aiee, killing interrupt handler!");
791 if (unlikely(!tsk->pid))
792 panic("Attempted to kill the idle task!");
793 if (unlikely(tsk->pid == 1))
794 panic("Attempted to kill init!");
795 if (tsk->io_context)
796 exit_io_context();
797 tsk->flags |= PF_EXITING;
798 del_timer_sync(&tsk->real_timer);
800 if (unlikely(in_atomic()))
801 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
802 current->comm, current->pid,
803 preempt_count());
805 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
806 current->ptrace_message = code;
807 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
810 acct_process(code);
811 __exit_mm(tsk);
813 exit_sem(tsk);
814 __exit_files(tsk);
815 __exit_fs(tsk);
816 exit_namespace(tsk);
817 exit_thread();
819 if (tsk->signal->leader)
820 disassociate_ctty(1);
822 module_put(tsk->thread_info->exec_domain->module);
823 if (tsk->binfmt)
824 module_put(tsk->binfmt->module);
826 tsk->exit_code = code;
827 exit_notify(tsk);
828 #ifdef CONFIG_NUMA
829 mpol_free(tsk->mempolicy);
830 tsk->mempolicy = NULL;
831 #endif
832 schedule();
833 BUG();
834 /* Avoid "noreturn function does return". */
835 for (;;) ;
838 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
840 if (comp)
841 complete(comp);
843 do_exit(code);
846 EXPORT_SYMBOL(complete_and_exit);
848 asmlinkage long sys_exit(int error_code)
850 do_exit((error_code&0xff)<<8);
853 task_t fastcall *next_thread(const task_t *p)
855 #ifdef CONFIG_SMP
856 if (!p->sighand)
857 BUG();
858 if (!spin_is_locked(&p->sighand->siglock) &&
859 !rwlock_is_locked(&tasklist_lock))
860 BUG();
861 #endif
862 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
865 EXPORT_SYMBOL(next_thread);
868 * Take down every thread in the group. This is called by fatal signals
869 * as well as by sys_exit_group (below).
871 NORET_TYPE void
872 do_group_exit(int exit_code)
874 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
876 if (current->signal->group_exit)
877 exit_code = current->signal->group_exit_code;
878 else if (!thread_group_empty(current)) {
879 struct signal_struct *const sig = current->signal;
880 struct sighand_struct *const sighand = current->sighand;
881 read_lock(&tasklist_lock);
882 spin_lock_irq(&sighand->siglock);
883 if (sig->group_exit)
884 /* Another thread got here before we took the lock. */
885 exit_code = sig->group_exit_code;
886 else {
887 sig->group_exit = 1;
888 sig->group_exit_code = exit_code;
889 zap_other_threads(current);
891 spin_unlock_irq(&sighand->siglock);
892 read_unlock(&tasklist_lock);
895 do_exit(exit_code);
896 /* NOTREACHED */
900 * this kills every thread in the thread group. Note that any externally
901 * wait4()-ing process will get the correct exit code - even if this
902 * thread is not the thread group leader.
904 asmlinkage void sys_exit_group(int error_code)
906 do_group_exit((error_code & 0xff) << 8);
909 static int eligible_child(pid_t pid, int options, task_t *p)
911 if (pid > 0) {
912 if (p->pid != pid)
913 return 0;
914 } else if (!pid) {
915 if (process_group(p) != process_group(current))
916 return 0;
917 } else if (pid != -1) {
918 if (process_group(p) != -pid)
919 return 0;
923 * Do not consider detached threads that are
924 * not ptraced:
926 if (p->exit_signal == -1 && !p->ptrace)
927 return 0;
929 /* Wait for all children (clone and not) if __WALL is set;
930 * otherwise, wait for clone children *only* if __WCLONE is
931 * set; otherwise, wait for non-clone children *only*. (Note:
932 * A "clone" child here is one that reports to its parent
933 * using a signal other than SIGCHLD.) */
934 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
935 && !(options & __WALL))
936 return 0;
938 * Do not consider thread group leaders that are
939 * in a non-empty thread group:
941 if (current->tgid != p->tgid && delay_group_leader(p))
942 return 2;
944 if (security_task_wait(p))
945 return 0;
947 return 1;
950 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
951 int why, int status,
952 struct siginfo __user *infop,
953 struct rusage __user *rusagep)
955 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
956 put_task_struct(p);
957 if (!retval)
958 retval = put_user(SIGCHLD, &infop->si_signo);
959 if (!retval)
960 retval = put_user(0, &infop->si_errno);
961 if (!retval)
962 retval = put_user((short)why, &infop->si_code);
963 if (!retval)
964 retval = put_user(pid, &infop->si_pid);
965 if (!retval)
966 retval = put_user(uid, &infop->si_uid);
967 if (!retval)
968 retval = put_user(status, &infop->si_status);
969 if (!retval)
970 retval = pid;
971 return retval;
975 * Handle sys_wait4 work for one task in state TASK_ZOMBIE. We hold
976 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
977 * the lock and this task is uninteresting. If we return nonzero, we have
978 * released the lock and the system call should return.
980 static int wait_task_zombie(task_t *p, int noreap,
981 struct siginfo __user *infop,
982 int __user *stat_addr, struct rusage __user *ru)
984 unsigned long state;
985 int retval;
986 int status;
988 if (unlikely(noreap)) {
989 pid_t pid = p->pid;
990 uid_t uid = p->uid;
991 int exit_code = p->exit_code;
992 int why, status;
994 if (unlikely(p->state != TASK_ZOMBIE))
995 return 0;
996 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
997 return 0;
998 get_task_struct(p);
999 read_unlock(&tasklist_lock);
1000 if ((exit_code & 0x7f) == 0) {
1001 why = CLD_EXITED;
1002 status = exit_code >> 8;
1003 } else {
1004 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1005 status = exit_code & 0x7f;
1007 return wait_noreap_copyout(p, pid, uid, why,
1008 status, infop, ru);
1012 * Try to move the task's state to DEAD
1013 * only one thread is allowed to do this:
1015 state = xchg(&p->state, TASK_DEAD);
1016 if (state != TASK_ZOMBIE) {
1017 BUG_ON(state != TASK_DEAD);
1018 return 0;
1020 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1022 * This can only happen in a race with a ptraced thread
1023 * dying on another processor.
1025 return 0;
1028 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1030 * The resource counters for the group leader are in its
1031 * own task_struct. Those for dead threads in the group
1032 * are in its signal_struct, as are those for the child
1033 * processes it has previously reaped. All these
1034 * accumulate in the parent's signal_struct c* fields.
1036 * We don't bother to take a lock here to protect these
1037 * p->signal fields, because they are only touched by
1038 * __exit_signal, which runs with tasklist_lock
1039 * write-locked anyway, and so is excluded here. We do
1040 * need to protect the access to p->parent->signal fields,
1041 * as other threads in the parent group can be right
1042 * here reaping other children at the same time.
1044 spin_lock_irq(&p->parent->sighand->siglock);
1045 p->parent->signal->cutime +=
1046 p->utime + p->signal->utime + p->signal->cutime;
1047 p->parent->signal->cstime +=
1048 p->stime + p->signal->stime + p->signal->cstime;
1049 p->parent->signal->cmin_flt +=
1050 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1051 p->parent->signal->cmaj_flt +=
1052 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1053 p->parent->signal->cnvcsw +=
1054 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1055 p->parent->signal->cnivcsw +=
1056 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1057 spin_unlock_irq(&p->parent->sighand->siglock);
1061 * Now we are sure this task is interesting, and no other
1062 * thread can reap it because we set its state to TASK_DEAD.
1064 read_unlock(&tasklist_lock);
1066 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1067 status = p->signal->group_exit
1068 ? p->signal->group_exit_code : p->exit_code;
1069 if (!retval && stat_addr)
1070 retval = put_user(status, stat_addr);
1071 if (!retval && infop)
1072 retval = put_user(SIGCHLD, &infop->si_signo);
1073 if (!retval && infop)
1074 retval = put_user(0, &infop->si_errno);
1075 if (!retval && infop) {
1076 int why;
1078 if ((status & 0x7f) == 0) {
1079 why = CLD_EXITED;
1080 status >>= 8;
1081 } else {
1082 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1083 status &= 0x7f;
1085 retval = put_user((short)why, &infop->si_code);
1086 if (!retval)
1087 retval = put_user(status, &infop->si_status);
1089 if (!retval && infop)
1090 retval = put_user(p->pid, &infop->si_pid);
1091 if (!retval && infop)
1092 retval = put_user(p->uid, &infop->si_uid);
1093 if (retval) {
1094 p->state = TASK_ZOMBIE;
1095 return retval;
1097 retval = p->pid;
1098 if (p->real_parent != p->parent) {
1099 write_lock_irq(&tasklist_lock);
1100 /* Double-check with lock held. */
1101 if (p->real_parent != p->parent) {
1102 __ptrace_unlink(p);
1103 p->state = TASK_ZOMBIE;
1105 * If this is not a detached task, notify the parent.
1106 * If it's still not detached after that, don't release
1107 * it now.
1109 if (p->exit_signal != -1) {
1110 do_notify_parent(p, p->exit_signal);
1111 if (p->exit_signal != -1)
1112 p = NULL;
1115 write_unlock_irq(&tasklist_lock);
1117 if (p != NULL)
1118 release_task(p);
1119 BUG_ON(!retval);
1120 return retval;
1124 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1125 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1126 * the lock and this task is uninteresting. If we return nonzero, we have
1127 * released the lock and the system call should return.
1129 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1130 struct siginfo __user *infop,
1131 int __user *stat_addr, struct rusage __user *ru)
1133 int retval, exit_code;
1135 if (!p->exit_code)
1136 return 0;
1137 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1138 p->signal && p->signal->group_stop_count > 0)
1140 * A group stop is in progress and this is the group leader.
1141 * We won't report until all threads have stopped.
1143 return 0;
1146 * Now we are pretty sure this task is interesting.
1147 * Make sure it doesn't get reaped out from under us while we
1148 * give up the lock and then examine it below. We don't want to
1149 * keep holding onto the tasklist_lock while we call getrusage and
1150 * possibly take page faults for user memory.
1152 get_task_struct(p);
1153 read_unlock(&tasklist_lock);
1155 if (unlikely(noreap)) {
1156 pid_t pid = p->pid;
1157 uid_t uid = p->uid;
1158 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1160 exit_code = p->exit_code;
1161 if (unlikely(!exit_code) ||
1162 unlikely(p->state > TASK_STOPPED))
1163 goto bail_ref;
1164 return wait_noreap_copyout(p, pid, uid,
1165 why, (exit_code << 8) | 0x7f,
1166 infop, ru);
1169 write_lock_irq(&tasklist_lock);
1172 * This uses xchg to be atomic with the thread resuming and setting
1173 * it. It must also be done with the write lock held to prevent a
1174 * race with the TASK_ZOMBIE case.
1176 exit_code = xchg(&p->exit_code, 0);
1177 if (unlikely(p->state >= TASK_ZOMBIE)) {
1179 * The task resumed and then died. Let the next iteration
1180 * catch it in TASK_ZOMBIE. Note that exit_code might
1181 * already be zero here if it resumed and did _exit(0).
1182 * The task itself is dead and won't touch exit_code again;
1183 * other processors in this function are locked out.
1185 p->exit_code = exit_code;
1186 exit_code = 0;
1188 if (unlikely(exit_code == 0)) {
1190 * Another thread in this function got to it first, or it
1191 * resumed, or it resumed and then died.
1193 write_unlock_irq(&tasklist_lock);
1194 bail_ref:
1195 put_task_struct(p);
1196 read_lock(&tasklist_lock);
1197 return 0;
1200 /* move to end of parent's list to avoid starvation */
1201 remove_parent(p);
1202 add_parent(p, p->parent);
1204 write_unlock_irq(&tasklist_lock);
1206 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1207 if (!retval && stat_addr)
1208 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1209 if (!retval && infop)
1210 retval = put_user(SIGCHLD, &infop->si_signo);
1211 if (!retval && infop)
1212 retval = put_user(0, &infop->si_errno);
1213 if (!retval && infop)
1214 retval = put_user((short)((p->ptrace & PT_PTRACED)
1215 ? CLD_TRAPPED : CLD_STOPPED),
1216 &infop->si_code);
1217 if (!retval && infop)
1218 retval = put_user(exit_code, &infop->si_status);
1219 if (!retval && infop)
1220 retval = put_user(p->pid, &infop->si_pid);
1221 if (!retval && infop)
1222 retval = put_user(p->uid, &infop->si_uid);
1223 if (!retval)
1224 retval = p->pid;
1225 put_task_struct(p);
1227 BUG_ON(!retval);
1228 return retval;
1231 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1232 int __user *stat_addr, struct rusage __user *ru)
1234 DECLARE_WAITQUEUE(wait, current);
1235 struct task_struct *tsk;
1236 int flag, retval;
1238 add_wait_queue(&current->wait_chldexit,&wait);
1239 repeat:
1240 flag = 0;
1241 current->state = TASK_INTERRUPTIBLE;
1242 read_lock(&tasklist_lock);
1243 tsk = current;
1244 do {
1245 struct task_struct *p;
1246 struct list_head *_p;
1247 int ret;
1249 list_for_each(_p,&tsk->children) {
1250 p = list_entry(_p,struct task_struct,sibling);
1252 ret = eligible_child(pid, options, p);
1253 if (!ret)
1254 continue;
1255 flag = 1;
1257 switch (p->state) {
1258 case TASK_TRACED:
1259 if (!(p->ptrace & PT_PTRACED))
1260 continue;
1261 /*FALLTHROUGH*/
1262 case TASK_STOPPED:
1263 if (!(options & WUNTRACED) &&
1264 !(p->ptrace & PT_PTRACED))
1265 continue;
1266 retval = wait_task_stopped(p, ret == 2,
1267 (options & WNOWAIT),
1268 infop,
1269 stat_addr, ru);
1270 if (retval != 0) /* He released the lock. */
1271 goto end;
1272 break;
1273 case TASK_ZOMBIE:
1275 * Eligible but we cannot release it yet:
1277 if (ret == 2)
1278 goto check_continued;
1279 if (!likely(options & WEXITED))
1280 continue;
1281 retval = wait_task_zombie(
1282 p, (options & WNOWAIT),
1283 infop, stat_addr, ru);
1284 if (retval != 0) /* He released the lock. */
1285 goto end;
1286 break;
1287 case TASK_DEAD:
1288 continue;
1289 default:
1290 check_continued:
1291 if (!unlikely(options & WCONTINUED))
1292 continue;
1293 if (unlikely(!p->signal))
1294 continue;
1295 spin_lock_irq(&p->sighand->siglock);
1296 if (p->signal->stop_state < 0) {
1297 pid_t pid;
1298 uid_t uid;
1300 if (!(options & WNOWAIT))
1301 p->signal->stop_state = 0;
1302 spin_unlock_irq(&p->sighand->siglock);
1303 pid = p->pid;
1304 uid = p->uid;
1305 get_task_struct(p);
1306 read_unlock(&tasklist_lock);
1307 retval = wait_noreap_copyout(p, pid,
1308 uid, CLD_CONTINUED,
1309 SIGCONT, infop, ru);
1310 BUG_ON(retval == 0);
1311 goto end;
1313 spin_unlock_irq(&p->sighand->siglock);
1314 break;
1317 if (!flag) {
1318 list_for_each(_p, &tsk->ptrace_children) {
1319 p = list_entry(_p, struct task_struct,
1320 ptrace_list);
1321 if (!eligible_child(pid, options, p))
1322 continue;
1323 flag = 1;
1324 break;
1327 if (options & __WNOTHREAD)
1328 break;
1329 tsk = next_thread(tsk);
1330 if (tsk->signal != current->signal)
1331 BUG();
1332 } while (tsk != current);
1334 read_unlock(&tasklist_lock);
1335 if (flag) {
1336 retval = 0;
1337 if (options & WNOHANG)
1338 goto end;
1339 retval = -ERESTARTSYS;
1340 if (signal_pending(current))
1341 goto end;
1342 schedule();
1343 goto repeat;
1345 retval = -ECHILD;
1346 end:
1347 current->state = TASK_RUNNING;
1348 remove_wait_queue(&current->wait_chldexit,&wait);
1349 if (infop) {
1350 if (retval > 0)
1351 retval = 0;
1352 else {
1354 * For a WNOHANG return, clear out all the fields
1355 * we would set so the user can easily tell the
1356 * difference.
1358 if (!retval)
1359 retval = put_user(0, &infop->si_signo);
1360 if (!retval)
1361 retval = put_user(0, &infop->si_errno);
1362 if (!retval)
1363 retval = put_user(0, &infop->si_code);
1364 if (!retval)
1365 retval = put_user(0, &infop->si_pid);
1366 if (!retval)
1367 retval = put_user(0, &infop->si_uid);
1368 if (!retval)
1369 retval = put_user(0, &infop->si_status);
1372 return retval;
1375 asmlinkage long sys_waitid(int which, pid_t pid,
1376 struct siginfo __user *infop, int options,
1377 struct rusage __user *ru)
1379 long ret;
1381 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1382 return -EINVAL;
1383 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1384 return -EINVAL;
1386 switch (which) {
1387 case P_ALL:
1388 pid = -1;
1389 break;
1390 case P_PID:
1391 if (pid <= 0)
1392 return -EINVAL;
1393 break;
1394 case P_PGID:
1395 if (pid <= 0)
1396 return -EINVAL;
1397 pid = -pid;
1398 break;
1399 default:
1400 return -EINVAL;
1403 ret = do_wait(pid, options, infop, NULL, ru);
1405 /* avoid REGPARM breakage on x86: */
1406 prevent_tail_call(ret);
1407 return ret;
1410 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1411 int options, struct rusage __user *ru)
1413 long ret;
1415 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
1416 return -EINVAL;
1417 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1419 /* avoid REGPARM breakage on x86: */
1420 prevent_tail_call(ret);
1421 return ret;
1424 #ifdef __ARCH_WANT_SYS_WAITPID
1427 * sys_waitpid() remains for compatibility. waitpid() should be
1428 * implemented by calling sys_wait4() from libc.a.
1430 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1432 return sys_wait4(pid, stat_addr, options, NULL);
1435 #endif