4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.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/capability.h>
14 #include <linux/completion.h>
15 #include <linux/personality.h>
16 #include <linux/tty.h>
17 #include <linux/namespace.h>
18 #include <linux/key.h>
19 #include <linux/security.h>
20 #include <linux/cpu.h>
21 #include <linux/acct.h>
22 #include <linux/file.h>
23 #include <linux/binfmts.h>
24 #include <linux/ptrace.h>
25 #include <linux/profile.h>
26 #include <linux/mount.h>
27 #include <linux/proc_fs.h>
28 #include <linux/mempolicy.h>
29 #include <linux/cpuset.h>
30 #include <linux/syscalls.h>
31 #include <linux/signal.h>
32 #include <linux/posix-timers.h>
33 #include <linux/cn_proc.h>
34 #include <linux/mutex.h>
35 #include <linux/futex.h>
36 #include <linux/compat.h>
38 #include <asm/uaccess.h>
39 #include <asm/unistd.h>
40 #include <asm/pgtable.h>
41 #include <asm/mmu_context.h>
43 extern void sem_exit (void);
44 extern struct task_struct *child_reaper;
46 int getrusage(struct task_struct *, int, struct rusage __user *);
48 static void exit_mm(struct task_struct * tsk);
50 static void __unhash_process(struct task_struct *p)
53 detach_pid(p, PIDTYPE_PID);
54 if (thread_group_leader(p)) {
55 detach_pid(p, PIDTYPE_PGID);
56 detach_pid(p, PIDTYPE_SID);
58 list_del_init(&p->tasks);
59 __get_cpu_var(process_counts)--;
61 list_del_rcu(&p->thread_group);
66 * This function expects the tasklist_lock write-locked.
68 static void __exit_signal(struct task_struct *tsk)
70 struct signal_struct *sig = tsk->signal;
71 struct sighand_struct *sighand;
74 BUG_ON(!atomic_read(&sig->count));
77 sighand = rcu_dereference(tsk->sighand);
78 spin_lock(&sighand->siglock);
80 posix_cpu_timers_exit(tsk);
81 if (atomic_dec_and_test(&sig->count))
82 posix_cpu_timers_exit_group(tsk);
85 * If there is any task waiting for the group exit
88 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
89 wake_up_process(sig->group_exit_task);
90 sig->group_exit_task = NULL;
92 if (tsk == sig->curr_target)
93 sig->curr_target = next_thread(tsk);
95 * Accumulate here the counters for all threads but the
96 * group leader as they die, so they can be added into
97 * the process-wide totals when those are taken.
98 * The group leader stays around as a zombie as long
99 * as there are other threads. When it gets reaped,
100 * the exit.c code will add its counts into these totals.
101 * We won't ever get here for the group leader, since it
102 * will have been the last reference on the signal_struct.
104 sig->utime = cputime_add(sig->utime, tsk->utime);
105 sig->stime = cputime_add(sig->stime, tsk->stime);
106 sig->min_flt += tsk->min_flt;
107 sig->maj_flt += tsk->maj_flt;
108 sig->nvcsw += tsk->nvcsw;
109 sig->nivcsw += tsk->nivcsw;
110 sig->sched_time += tsk->sched_time;
111 sig = NULL; /* Marker for below. */
114 __unhash_process(tsk);
117 cleanup_sighand(tsk);
118 spin_unlock(&sighand->siglock);
121 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
122 flush_sigqueue(&tsk->pending);
124 flush_sigqueue(&sig->shared_pending);
125 __cleanup_signal(sig);
129 void release_task(struct task_struct * p)
133 struct dentry *proc_dentry;
136 atomic_dec(&p->user->processes);
137 spin_lock(&p->proc_lock);
138 proc_dentry = proc_pid_unhash(p);
139 write_lock_irq(&tasklist_lock);
141 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
145 * If we are the last non-leader member of the thread
146 * group, and the leader is zombie, then notify the
147 * group leader's parent process. (if it wants notification.)
150 leader = p->group_leader;
151 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
152 BUG_ON(leader->exit_signal == -1);
153 do_notify_parent(leader, leader->exit_signal);
155 * If we were the last child thread and the leader has
156 * exited already, and the leader's parent ignores SIGCHLD,
157 * then we are the one who should release the leader.
159 * do_notify_parent() will have marked it self-reaping in
162 zap_leader = (leader->exit_signal == -1);
166 write_unlock_irq(&tasklist_lock);
167 spin_unlock(&p->proc_lock);
168 proc_pid_flush(proc_dentry);
173 if (unlikely(zap_leader))
178 * This checks not only the pgrp, but falls back on the pid if no
179 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
182 int session_of_pgrp(int pgrp)
184 struct task_struct *p;
187 read_lock(&tasklist_lock);
188 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
189 if (p->signal->session > 0) {
190 sid = p->signal->session;
193 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
194 p = find_task_by_pid(pgrp);
196 sid = p->signal->session;
198 read_unlock(&tasklist_lock);
204 * Determine if a process group is "orphaned", according to the POSIX
205 * definition in 2.2.2.52. Orphaned process groups are not to be affected
206 * by terminal-generated stop signals. Newly orphaned process groups are
207 * to receive a SIGHUP and a SIGCONT.
209 * "I ask you, have you ever known what it is to be an orphan?"
211 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
213 struct task_struct *p;
216 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
217 if (p == ignored_task
219 || p->real_parent->pid == 1)
221 if (process_group(p->real_parent) != pgrp
222 && p->real_parent->signal->session == p->signal->session) {
226 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
227 return ret; /* (sighing) "Often!" */
230 int is_orphaned_pgrp(int pgrp)
234 read_lock(&tasklist_lock);
235 retval = will_become_orphaned_pgrp(pgrp, NULL);
236 read_unlock(&tasklist_lock);
241 static int has_stopped_jobs(int pgrp)
244 struct task_struct *p;
246 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
247 if (p->state != TASK_STOPPED)
250 /* If p is stopped by a debugger on a signal that won't
251 stop it, then don't count p as stopped. This isn't
252 perfect but it's a good approximation. */
253 if (unlikely (p->ptrace)
254 && p->exit_code != SIGSTOP
255 && p->exit_code != SIGTSTP
256 && p->exit_code != SIGTTOU
257 && p->exit_code != SIGTTIN)
262 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
267 * reparent_to_init - Reparent the calling kernel thread to the init task.
269 * If a kernel thread is launched as a result of a system call, or if
270 * it ever exits, it should generally reparent itself to init so that
271 * it is correctly cleaned up on exit.
273 * The various task state such as scheduling policy and priority may have
274 * been inherited from a user process, so we reset them to sane values here.
276 * NOTE that reparent_to_init() gives the caller full capabilities.
278 static void reparent_to_init(void)
280 write_lock_irq(&tasklist_lock);
282 ptrace_unlink(current);
283 /* Reparent to init */
284 remove_parent(current);
285 current->parent = child_reaper;
286 current->real_parent = child_reaper;
289 /* Set the exit signal to SIGCHLD so we signal init on exit */
290 current->exit_signal = SIGCHLD;
292 if ((current->policy == SCHED_NORMAL ||
293 current->policy == SCHED_BATCH)
294 && (task_nice(current) < 0))
295 set_user_nice(current, 0);
299 security_task_reparent_to_init(current);
300 memcpy(current->signal->rlim, init_task.signal->rlim,
301 sizeof(current->signal->rlim));
302 atomic_inc(&(INIT_USER->__count));
303 write_unlock_irq(&tasklist_lock);
304 switch_uid(INIT_USER);
307 void __set_special_pids(pid_t session, pid_t pgrp)
309 struct task_struct *curr = current->group_leader;
311 if (curr->signal->session != session) {
312 detach_pid(curr, PIDTYPE_SID);
313 curr->signal->session = session;
314 attach_pid(curr, PIDTYPE_SID, session);
316 if (process_group(curr) != pgrp) {
317 detach_pid(curr, PIDTYPE_PGID);
318 curr->signal->pgrp = pgrp;
319 attach_pid(curr, PIDTYPE_PGID, pgrp);
323 void set_special_pids(pid_t session, pid_t pgrp)
325 write_lock_irq(&tasklist_lock);
326 __set_special_pids(session, pgrp);
327 write_unlock_irq(&tasklist_lock);
331 * Let kernel threads use this to say that they
332 * allow a certain signal (since daemonize() will
333 * have disabled all of them by default).
335 int allow_signal(int sig)
337 if (!valid_signal(sig) || sig < 1)
340 spin_lock_irq(¤t->sighand->siglock);
341 sigdelset(¤t->blocked, sig);
343 /* Kernel threads handle their own signals.
344 Let the signal code know it'll be handled, so
345 that they don't get converted to SIGKILL or
346 just silently dropped */
347 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
350 spin_unlock_irq(¤t->sighand->siglock);
354 EXPORT_SYMBOL(allow_signal);
356 int disallow_signal(int sig)
358 if (!valid_signal(sig) || sig < 1)
361 spin_lock_irq(¤t->sighand->siglock);
362 sigaddset(¤t->blocked, sig);
364 spin_unlock_irq(¤t->sighand->siglock);
368 EXPORT_SYMBOL(disallow_signal);
371 * Put all the gunge required to become a kernel thread without
372 * attached user resources in one place where it belongs.
375 void daemonize(const char *name, ...)
378 struct fs_struct *fs;
381 va_start(args, name);
382 vsnprintf(current->comm, sizeof(current->comm), name, args);
386 * If we were started as result of loading a module, close all of the
387 * user space pages. We don't need them, and if we didn't close them
388 * they would be locked into memory.
392 set_special_pids(1, 1);
393 mutex_lock(&tty_mutex);
394 current->signal->tty = NULL;
395 mutex_unlock(&tty_mutex);
397 /* Block and flush all signals */
398 sigfillset(&blocked);
399 sigprocmask(SIG_BLOCK, &blocked, NULL);
400 flush_signals(current);
402 /* Become as one with the init task */
404 exit_fs(current); /* current->fs->count--; */
407 atomic_inc(&fs->count);
408 exit_namespace(current);
409 current->namespace = init_task.namespace;
410 get_namespace(current->namespace);
412 current->files = init_task.files;
413 atomic_inc(¤t->files->count);
418 EXPORT_SYMBOL(daemonize);
420 static void close_files(struct files_struct * files)
428 * It is safe to dereference the fd table without RCU or
429 * ->file_lock because this is the last reference to the
432 fdt = files_fdtable(files);
436 if (i >= fdt->max_fdset || i >= fdt->max_fds)
438 set = fdt->open_fds->fds_bits[j++];
441 struct file * file = xchg(&fdt->fd[i], NULL);
443 filp_close(file, files);
451 struct files_struct *get_files_struct(struct task_struct *task)
453 struct files_struct *files;
458 atomic_inc(&files->count);
464 void fastcall put_files_struct(struct files_struct *files)
468 if (atomic_dec_and_test(&files->count)) {
471 * Free the fd and fdset arrays if we expanded them.
472 * If the fdtable was embedded, pass files for freeing
473 * at the end of the RCU grace period. Otherwise,
474 * you can free files immediately.
476 fdt = files_fdtable(files);
477 if (fdt == &files->fdtab)
478 fdt->free_files = files;
480 kmem_cache_free(files_cachep, files);
485 EXPORT_SYMBOL(put_files_struct);
487 static inline void __exit_files(struct task_struct *tsk)
489 struct files_struct * files = tsk->files;
495 put_files_struct(files);
499 void exit_files(struct task_struct *tsk)
504 static inline void __put_fs_struct(struct fs_struct *fs)
506 /* No need to hold fs->lock if we are killing it */
507 if (atomic_dec_and_test(&fs->count)) {
514 mntput(fs->altrootmnt);
516 kmem_cache_free(fs_cachep, fs);
520 void put_fs_struct(struct fs_struct *fs)
525 static inline void __exit_fs(struct task_struct *tsk)
527 struct fs_struct * fs = tsk->fs;
537 void exit_fs(struct task_struct *tsk)
542 EXPORT_SYMBOL_GPL(exit_fs);
545 * Turn us into a lazy TLB process if we
548 static void exit_mm(struct task_struct * tsk)
550 struct mm_struct *mm = tsk->mm;
556 * Serialize with any possible pending coredump.
557 * We must hold mmap_sem around checking core_waiters
558 * and clearing tsk->mm. The core-inducing thread
559 * will increment core_waiters for each thread in the
560 * group with ->mm != NULL.
562 down_read(&mm->mmap_sem);
563 if (mm->core_waiters) {
564 up_read(&mm->mmap_sem);
565 down_write(&mm->mmap_sem);
566 if (!--mm->core_waiters)
567 complete(mm->core_startup_done);
568 up_write(&mm->mmap_sem);
570 wait_for_completion(&mm->core_done);
571 down_read(&mm->mmap_sem);
573 atomic_inc(&mm->mm_count);
574 if (mm != tsk->active_mm) BUG();
575 /* more a memory barrier than a real lock */
578 up_read(&mm->mmap_sem);
579 enter_lazy_tlb(mm, current);
584 static inline void choose_new_parent(task_t *p, task_t *reaper)
587 * Make sure we're not reparenting to ourselves and that
588 * the parent is not a zombie.
590 BUG_ON(p == reaper || reaper->exit_state);
591 p->real_parent = reaper;
594 static void reparent_thread(task_t *p, task_t *father, int traced)
596 /* We don't want people slaying init. */
597 if (p->exit_signal != -1)
598 p->exit_signal = SIGCHLD;
600 if (p->pdeath_signal)
601 /* We already hold the tasklist_lock here. */
602 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
604 /* Move the child from its dying parent to the new one. */
605 if (unlikely(traced)) {
606 /* Preserve ptrace links if someone else is tracing this child. */
607 list_del_init(&p->ptrace_list);
608 if (p->parent != p->real_parent)
609 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
611 /* If this child is being traced, then we're the one tracing it
612 * anyway, so let go of it.
616 p->parent = p->real_parent;
619 /* If we'd notified the old parent about this child's death,
620 * also notify the new parent.
622 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
623 thread_group_empty(p))
624 do_notify_parent(p, p->exit_signal);
625 else if (p->state == TASK_TRACED) {
627 * If it was at a trace stop, turn it into
628 * a normal stop since it's no longer being
636 * process group orphan check
637 * Case ii: Our child is in a different pgrp
638 * than we are, and it was the only connection
639 * outside, so the child pgrp is now orphaned.
641 if ((process_group(p) != process_group(father)) &&
642 (p->signal->session == father->signal->session)) {
643 int pgrp = process_group(p);
645 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
646 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, pgrp);
647 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, pgrp);
653 * When we die, we re-parent all our children.
654 * Try to give them to another thread in our thread
655 * group, and if no such member exists, give it to
656 * the global child reaper process (ie "init")
658 static void forget_original_parent(struct task_struct * father,
659 struct list_head *to_release)
661 struct task_struct *p, *reaper = father;
662 struct list_head *_p, *_n;
665 reaper = next_thread(reaper);
666 if (reaper == father) {
667 reaper = child_reaper;
670 } while (reaper->exit_state);
673 * There are only two places where our children can be:
675 * - in our child list
676 * - in our ptraced child list
678 * Search them and reparent children.
680 list_for_each_safe(_p, _n, &father->children) {
682 p = list_entry(_p,struct task_struct,sibling);
686 /* if father isn't the real parent, then ptrace must be enabled */
687 BUG_ON(father != p->real_parent && !ptrace);
689 if (father == p->real_parent) {
690 /* reparent with a reaper, real father it's us */
691 choose_new_parent(p, reaper);
692 reparent_thread(p, father, 0);
694 /* reparent ptraced task to its real parent */
696 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
697 thread_group_empty(p))
698 do_notify_parent(p, p->exit_signal);
702 * if the ptraced child is a zombie with exit_signal == -1
703 * we must collect it before we exit, or it will remain
704 * zombie forever since we prevented it from self-reap itself
705 * while it was being traced by us, to be able to see it in wait4.
707 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
708 list_add(&p->ptrace_list, to_release);
710 list_for_each_safe(_p, _n, &father->ptrace_children) {
711 p = list_entry(_p,struct task_struct,ptrace_list);
712 choose_new_parent(p, reaper);
713 reparent_thread(p, father, 1);
718 * Send signals to all our closest relatives so that they know
719 * to properly mourn us..
721 static void exit_notify(struct task_struct *tsk)
724 struct task_struct *t;
725 struct list_head ptrace_dead, *_p, *_n;
727 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
728 && !thread_group_empty(tsk)) {
730 * This occurs when there was a race between our exit
731 * syscall and a group signal choosing us as the one to
732 * wake up. It could be that we are the only thread
733 * alerted to check for pending signals, but another thread
734 * should be woken now to take the signal since we will not.
735 * Now we'll wake all the threads in the group just to make
736 * sure someone gets all the pending signals.
738 read_lock(&tasklist_lock);
739 spin_lock_irq(&tsk->sighand->siglock);
740 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
741 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
742 recalc_sigpending_tsk(t);
743 if (signal_pending(t))
744 signal_wake_up(t, 0);
746 spin_unlock_irq(&tsk->sighand->siglock);
747 read_unlock(&tasklist_lock);
750 write_lock_irq(&tasklist_lock);
753 * This does two things:
755 * A. Make init inherit all the child processes
756 * B. Check to see if any process groups have become orphaned
757 * as a result of our exiting, and if they have any stopped
758 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
761 INIT_LIST_HEAD(&ptrace_dead);
762 forget_original_parent(tsk, &ptrace_dead);
763 BUG_ON(!list_empty(&tsk->children));
764 BUG_ON(!list_empty(&tsk->ptrace_children));
767 * Check to see if any process groups have become orphaned
768 * as a result of our exiting, and if they have any stopped
769 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
771 * Case i: Our father is in a different pgrp than we are
772 * and we were the only connection outside, so our pgrp
773 * is about to become orphaned.
776 t = tsk->real_parent;
778 if ((process_group(t) != process_group(tsk)) &&
779 (t->signal->session == tsk->signal->session) &&
780 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
781 has_stopped_jobs(process_group(tsk))) {
782 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, process_group(tsk));
783 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, process_group(tsk));
786 /* Let father know we died
788 * Thread signals are configurable, but you aren't going to use
789 * that to send signals to arbitary processes.
790 * That stops right now.
792 * If the parent exec id doesn't match the exec id we saved
793 * when we started then we know the parent has changed security
796 * If our self_exec id doesn't match our parent_exec_id then
797 * we have changed execution domain as these two values started
798 * the same after a fork.
802 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
803 ( tsk->parent_exec_id != t->self_exec_id ||
804 tsk->self_exec_id != tsk->parent_exec_id)
805 && !capable(CAP_KILL))
806 tsk->exit_signal = SIGCHLD;
809 /* If something other than our normal parent is ptracing us, then
810 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
811 * only has special meaning to our real parent.
813 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
814 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
815 do_notify_parent(tsk, signal);
816 } else if (tsk->ptrace) {
817 do_notify_parent(tsk, SIGCHLD);
821 if (tsk->exit_signal == -1 &&
822 (likely(tsk->ptrace == 0) ||
823 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
825 tsk->exit_state = state;
827 write_unlock_irq(&tasklist_lock);
829 list_for_each_safe(_p, _n, &ptrace_dead) {
831 t = list_entry(_p,struct task_struct,ptrace_list);
835 /* If the process is dead, release it - nobody will wait for it */
836 if (state == EXIT_DEAD)
840 fastcall NORET_TYPE void do_exit(long code)
842 struct task_struct *tsk = current;
845 profile_task_exit(tsk);
847 WARN_ON(atomic_read(&tsk->fs_excl));
849 if (unlikely(in_interrupt()))
850 panic("Aiee, killing interrupt handler!");
851 if (unlikely(!tsk->pid))
852 panic("Attempted to kill the idle task!");
853 if (unlikely(tsk == child_reaper))
854 panic("Attempted to kill init!");
856 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
857 current->ptrace_message = code;
858 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
862 * We're taking recursive faults here in do_exit. Safest is to just
863 * leave this task alone and wait for reboot.
865 if (unlikely(tsk->flags & PF_EXITING)) {
867 "Fixing recursive fault but reboot is needed!\n");
870 set_current_state(TASK_UNINTERRUPTIBLE);
874 tsk->flags |= PF_EXITING;
877 * Make sure we don't try to process any timer firings
878 * while we are already exiting.
880 tsk->it_virt_expires = cputime_zero;
881 tsk->it_prof_expires = cputime_zero;
882 tsk->it_sched_expires = 0;
884 if (unlikely(in_atomic()))
885 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
886 current->comm, current->pid,
889 acct_update_integrals(tsk);
891 update_hiwater_rss(tsk->mm);
892 update_hiwater_vm(tsk->mm);
894 group_dead = atomic_dec_and_test(&tsk->signal->live);
896 hrtimer_cancel(&tsk->signal->real_timer);
897 exit_itimers(tsk->signal);
900 if (unlikely(tsk->robust_list))
901 exit_robust_list(tsk);
903 if (unlikely(tsk->compat_robust_list))
904 compat_exit_robust_list(tsk);
916 if (group_dead && tsk->signal->leader)
917 disassociate_ctty(1);
919 module_put(task_thread_info(tsk)->exec_domain->module);
921 module_put(tsk->binfmt->module);
923 tsk->exit_code = code;
924 proc_exit_connector(tsk);
927 mpol_free(tsk->mempolicy);
928 tsk->mempolicy = NULL;
931 * If DEBUG_MUTEXES is on, make sure we are holding no locks:
933 mutex_debug_check_no_locks_held(tsk);
938 /* PF_DEAD causes final put_task_struct after we schedule. */
940 BUG_ON(tsk->flags & PF_DEAD);
941 tsk->flags |= PF_DEAD;
945 /* Avoid "noreturn function does return". */
949 EXPORT_SYMBOL_GPL(do_exit);
951 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
959 EXPORT_SYMBOL(complete_and_exit);
961 asmlinkage long sys_exit(int error_code)
963 do_exit((error_code&0xff)<<8);
967 * Take down every thread in the group. This is called by fatal signals
968 * as well as by sys_exit_group (below).
971 do_group_exit(int exit_code)
973 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
975 if (current->signal->flags & SIGNAL_GROUP_EXIT)
976 exit_code = current->signal->group_exit_code;
977 else if (!thread_group_empty(current)) {
978 struct signal_struct *const sig = current->signal;
979 struct sighand_struct *const sighand = current->sighand;
980 spin_lock_irq(&sighand->siglock);
981 if (sig->flags & SIGNAL_GROUP_EXIT)
982 /* Another thread got here before we took the lock. */
983 exit_code = sig->group_exit_code;
985 sig->group_exit_code = exit_code;
986 zap_other_threads(current);
988 spin_unlock_irq(&sighand->siglock);
996 * this kills every thread in the thread group. Note that any externally
997 * wait4()-ing process will get the correct exit code - even if this
998 * thread is not the thread group leader.
1000 asmlinkage void sys_exit_group(int error_code)
1002 do_group_exit((error_code & 0xff) << 8);
1005 static int eligible_child(pid_t pid, int options, task_t *p)
1011 if (process_group(p) != process_group(current))
1013 } else if (pid != -1) {
1014 if (process_group(p) != -pid)
1019 * Do not consider detached threads that are
1022 if (p->exit_signal == -1 && !p->ptrace)
1025 /* Wait for all children (clone and not) if __WALL is set;
1026 * otherwise, wait for clone children *only* if __WCLONE is
1027 * set; otherwise, wait for non-clone children *only*. (Note:
1028 * A "clone" child here is one that reports to its parent
1029 * using a signal other than SIGCHLD.) */
1030 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
1031 && !(options & __WALL))
1034 * Do not consider thread group leaders that are
1035 * in a non-empty thread group:
1037 if (current->tgid != p->tgid && delay_group_leader(p))
1040 if (security_task_wait(p))
1046 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
1047 int why, int status,
1048 struct siginfo __user *infop,
1049 struct rusage __user *rusagep)
1051 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1054 retval = put_user(SIGCHLD, &infop->si_signo);
1056 retval = put_user(0, &infop->si_errno);
1058 retval = put_user((short)why, &infop->si_code);
1060 retval = put_user(pid, &infop->si_pid);
1062 retval = put_user(uid, &infop->si_uid);
1064 retval = put_user(status, &infop->si_status);
1071 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1072 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1073 * the lock and this task is uninteresting. If we return nonzero, we have
1074 * released the lock and the system call should return.
1076 static int wait_task_zombie(task_t *p, int noreap,
1077 struct siginfo __user *infop,
1078 int __user *stat_addr, struct rusage __user *ru)
1080 unsigned long state;
1084 if (unlikely(noreap)) {
1087 int exit_code = p->exit_code;
1090 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1092 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1095 read_unlock(&tasklist_lock);
1096 if ((exit_code & 0x7f) == 0) {
1098 status = exit_code >> 8;
1100 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1101 status = exit_code & 0x7f;
1103 return wait_noreap_copyout(p, pid, uid, why,
1108 * Try to move the task's state to DEAD
1109 * only one thread is allowed to do this:
1111 state = xchg(&p->exit_state, EXIT_DEAD);
1112 if (state != EXIT_ZOMBIE) {
1113 BUG_ON(state != EXIT_DEAD);
1116 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1118 * This can only happen in a race with a ptraced thread
1119 * dying on another processor.
1124 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1125 struct signal_struct *psig;
1126 struct signal_struct *sig;
1129 * The resource counters for the group leader are in its
1130 * own task_struct. Those for dead threads in the group
1131 * are in its signal_struct, as are those for the child
1132 * processes it has previously reaped. All these
1133 * accumulate in the parent's signal_struct c* fields.
1135 * We don't bother to take a lock here to protect these
1136 * p->signal fields, because they are only touched by
1137 * __exit_signal, which runs with tasklist_lock
1138 * write-locked anyway, and so is excluded here. We do
1139 * need to protect the access to p->parent->signal fields,
1140 * as other threads in the parent group can be right
1141 * here reaping other children at the same time.
1143 spin_lock_irq(&p->parent->sighand->siglock);
1144 psig = p->parent->signal;
1147 cputime_add(psig->cutime,
1148 cputime_add(p->utime,
1149 cputime_add(sig->utime,
1152 cputime_add(psig->cstime,
1153 cputime_add(p->stime,
1154 cputime_add(sig->stime,
1157 p->min_flt + sig->min_flt + sig->cmin_flt;
1159 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1161 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1163 p->nivcsw + sig->nivcsw + sig->cnivcsw;
1164 spin_unlock_irq(&p->parent->sighand->siglock);
1168 * Now we are sure this task is interesting, and no other
1169 * thread can reap it because we set its state to EXIT_DEAD.
1171 read_unlock(&tasklist_lock);
1173 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1174 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1175 ? p->signal->group_exit_code : p->exit_code;
1176 if (!retval && stat_addr)
1177 retval = put_user(status, stat_addr);
1178 if (!retval && infop)
1179 retval = put_user(SIGCHLD, &infop->si_signo);
1180 if (!retval && infop)
1181 retval = put_user(0, &infop->si_errno);
1182 if (!retval && infop) {
1185 if ((status & 0x7f) == 0) {
1189 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1192 retval = put_user((short)why, &infop->si_code);
1194 retval = put_user(status, &infop->si_status);
1196 if (!retval && infop)
1197 retval = put_user(p->pid, &infop->si_pid);
1198 if (!retval && infop)
1199 retval = put_user(p->uid, &infop->si_uid);
1201 // TODO: is this safe?
1202 p->exit_state = EXIT_ZOMBIE;
1206 if (p->real_parent != p->parent) {
1207 write_lock_irq(&tasklist_lock);
1208 /* Double-check with lock held. */
1209 if (p->real_parent != p->parent) {
1211 // TODO: is this safe?
1212 p->exit_state = EXIT_ZOMBIE;
1214 * If this is not a detached task, notify the parent.
1215 * If it's still not detached after that, don't release
1218 if (p->exit_signal != -1) {
1219 do_notify_parent(p, p->exit_signal);
1220 if (p->exit_signal != -1)
1224 write_unlock_irq(&tasklist_lock);
1233 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1234 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1235 * the lock and this task is uninteresting. If we return nonzero, we have
1236 * released the lock and the system call should return.
1238 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1239 struct siginfo __user *infop,
1240 int __user *stat_addr, struct rusage __user *ru)
1242 int retval, exit_code;
1246 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1247 p->signal && p->signal->group_stop_count > 0)
1249 * A group stop is in progress and this is the group leader.
1250 * We won't report until all threads have stopped.
1255 * Now we are pretty sure this task is interesting.
1256 * Make sure it doesn't get reaped out from under us while we
1257 * give up the lock and then examine it below. We don't want to
1258 * keep holding onto the tasklist_lock while we call getrusage and
1259 * possibly take page faults for user memory.
1262 read_unlock(&tasklist_lock);
1264 if (unlikely(noreap)) {
1267 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1269 exit_code = p->exit_code;
1270 if (unlikely(!exit_code) ||
1271 unlikely(p->state & TASK_TRACED))
1273 return wait_noreap_copyout(p, pid, uid,
1274 why, (exit_code << 8) | 0x7f,
1278 write_lock_irq(&tasklist_lock);
1281 * This uses xchg to be atomic with the thread resuming and setting
1282 * it. It must also be done with the write lock held to prevent a
1283 * race with the EXIT_ZOMBIE case.
1285 exit_code = xchg(&p->exit_code, 0);
1286 if (unlikely(p->exit_state)) {
1288 * The task resumed and then died. Let the next iteration
1289 * catch it in EXIT_ZOMBIE. Note that exit_code might
1290 * already be zero here if it resumed and did _exit(0).
1291 * The task itself is dead and won't touch exit_code again;
1292 * other processors in this function are locked out.
1294 p->exit_code = exit_code;
1297 if (unlikely(exit_code == 0)) {
1299 * Another thread in this function got to it first, or it
1300 * resumed, or it resumed and then died.
1302 write_unlock_irq(&tasklist_lock);
1306 * We are returning to the wait loop without having successfully
1307 * removed the process and having released the lock. We cannot
1308 * continue, since the "p" task pointer is potentially stale.
1310 * Return -EAGAIN, and do_wait() will restart the loop from the
1311 * beginning. Do _not_ re-acquire the lock.
1316 /* move to end of parent's list to avoid starvation */
1320 write_unlock_irq(&tasklist_lock);
1322 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1323 if (!retval && stat_addr)
1324 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1325 if (!retval && infop)
1326 retval = put_user(SIGCHLD, &infop->si_signo);
1327 if (!retval && infop)
1328 retval = put_user(0, &infop->si_errno);
1329 if (!retval && infop)
1330 retval = put_user((short)((p->ptrace & PT_PTRACED)
1331 ? CLD_TRAPPED : CLD_STOPPED),
1333 if (!retval && infop)
1334 retval = put_user(exit_code, &infop->si_status);
1335 if (!retval && infop)
1336 retval = put_user(p->pid, &infop->si_pid);
1337 if (!retval && infop)
1338 retval = put_user(p->uid, &infop->si_uid);
1348 * Handle do_wait work for one task in a live, non-stopped state.
1349 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1350 * the lock and this task is uninteresting. If we return nonzero, we have
1351 * released the lock and the system call should return.
1353 static int wait_task_continued(task_t *p, int noreap,
1354 struct siginfo __user *infop,
1355 int __user *stat_addr, struct rusage __user *ru)
1361 if (unlikely(!p->signal))
1364 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1367 spin_lock_irq(&p->sighand->siglock);
1368 /* Re-check with the lock held. */
1369 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1370 spin_unlock_irq(&p->sighand->siglock);
1374 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1375 spin_unlock_irq(&p->sighand->siglock);
1380 read_unlock(&tasklist_lock);
1383 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1385 if (!retval && stat_addr)
1386 retval = put_user(0xffff, stat_addr);
1390 retval = wait_noreap_copyout(p, pid, uid,
1391 CLD_CONTINUED, SIGCONT,
1393 BUG_ON(retval == 0);
1400 static inline int my_ptrace_child(struct task_struct *p)
1402 if (!(p->ptrace & PT_PTRACED))
1404 if (!(p->ptrace & PT_ATTACHED))
1407 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1408 * we are the attacher. If we are the real parent, this is a race
1409 * inside ptrace_attach. It is waiting for the tasklist_lock,
1410 * which we have to switch the parent links, but has already set
1411 * the flags in p->ptrace.
1413 return (p->parent != p->real_parent);
1416 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1417 int __user *stat_addr, struct rusage __user *ru)
1419 DECLARE_WAITQUEUE(wait, current);
1420 struct task_struct *tsk;
1423 add_wait_queue(¤t->signal->wait_chldexit,&wait);
1426 * We will set this flag if we see any child that might later
1427 * match our criteria, even if we are not able to reap it yet.
1430 current->state = TASK_INTERRUPTIBLE;
1431 read_lock(&tasklist_lock);
1434 struct task_struct *p;
1435 struct list_head *_p;
1438 list_for_each(_p,&tsk->children) {
1439 p = list_entry(_p,struct task_struct,sibling);
1441 ret = eligible_child(pid, options, p);
1448 * When we hit the race with PTRACE_ATTACH,
1449 * we will not report this child. But the
1450 * race means it has not yet been moved to
1451 * our ptrace_children list, so we need to
1452 * set the flag here to avoid a spurious ECHILD
1453 * when the race happens with the only child.
1456 if (!my_ptrace_child(p))
1461 * It's stopped now, so it might later
1462 * continue, exit, or stop again.
1465 if (!(options & WUNTRACED) &&
1466 !my_ptrace_child(p))
1468 retval = wait_task_stopped(p, ret == 2,
1469 (options & WNOWAIT),
1472 if (retval == -EAGAIN)
1474 if (retval != 0) /* He released the lock. */
1479 if (p->exit_state == EXIT_DEAD)
1481 // case EXIT_ZOMBIE:
1482 if (p->exit_state == EXIT_ZOMBIE) {
1484 * Eligible but we cannot release
1488 goto check_continued;
1489 if (!likely(options & WEXITED))
1491 retval = wait_task_zombie(
1492 p, (options & WNOWAIT),
1493 infop, stat_addr, ru);
1494 /* He released the lock. */
1501 * It's running now, so it might later
1502 * exit, stop, or stop and then continue.
1505 if (!unlikely(options & WCONTINUED))
1507 retval = wait_task_continued(
1508 p, (options & WNOWAIT),
1509 infop, stat_addr, ru);
1510 if (retval != 0) /* He released the lock. */
1516 list_for_each(_p, &tsk->ptrace_children) {
1517 p = list_entry(_p, struct task_struct,
1519 if (!eligible_child(pid, options, p))
1525 if (options & __WNOTHREAD)
1527 tsk = next_thread(tsk);
1528 if (tsk->signal != current->signal)
1530 } while (tsk != current);
1532 read_unlock(&tasklist_lock);
1535 if (options & WNOHANG)
1537 retval = -ERESTARTSYS;
1538 if (signal_pending(current))
1545 current->state = TASK_RUNNING;
1546 remove_wait_queue(¤t->signal->wait_chldexit,&wait);
1552 * For a WNOHANG return, clear out all the fields
1553 * we would set so the user can easily tell the
1557 retval = put_user(0, &infop->si_signo);
1559 retval = put_user(0, &infop->si_errno);
1561 retval = put_user(0, &infop->si_code);
1563 retval = put_user(0, &infop->si_pid);
1565 retval = put_user(0, &infop->si_uid);
1567 retval = put_user(0, &infop->si_status);
1573 asmlinkage long sys_waitid(int which, pid_t pid,
1574 struct siginfo __user *infop, int options,
1575 struct rusage __user *ru)
1579 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1581 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1601 ret = do_wait(pid, options, infop, NULL, ru);
1603 /* avoid REGPARM breakage on x86: */
1604 prevent_tail_call(ret);
1608 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1609 int options, struct rusage __user *ru)
1613 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1614 __WNOTHREAD|__WCLONE|__WALL))
1616 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1618 /* avoid REGPARM breakage on x86: */
1619 prevent_tail_call(ret);
1623 #ifdef __ARCH_WANT_SYS_WAITPID
1626 * sys_waitpid() remains for compatibility. waitpid() should be
1627 * implemented by calling sys_wait4() from libc.a.
1629 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1631 return sys_wait4(pid, stat_addr, options, NULL);