4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/smp_lock.h>
11 #include <linux/module.h>
12 #include <linux/capability.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/tracehook.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/taskstats_kern.h>
30 #include <linux/delayacct.h>
31 #include <linux/cpuset.h>
32 #include <linux/syscalls.h>
33 #include <linux/signal.h>
34 #include <linux/posix-timers.h>
35 #include <linux/cn_proc.h>
36 #include <linux/mutex.h>
37 #include <linux/futex.h>
38 #include <linux/compat.h>
39 #include <linux/pipe_fs_i.h>
40 #include <linux/audit.h> /* for audit_free() */
41 #include <linux/resource.h>
42 #include <linux/vs_base.h>
43 #include <linux/vs_context.h>
44 #include <linux/vs_network.h>
45 #include <linux/vs_limit.h>
47 #include <asm/uaccess.h>
48 #include <asm/unistd.h>
49 #include <asm/pgtable.h>
50 #include <asm/mmu_context.h>
52 extern void sem_exit (void);
53 extern struct task_struct *child_reaper;
55 static void exit_mm(struct task_struct * tsk);
57 static void __unhash_process(struct task_struct *p)
60 detach_pid(p, PIDTYPE_PID);
61 if (thread_group_leader(p)) {
62 detach_pid(p, PIDTYPE_PGID);
63 detach_pid(p, PIDTYPE_SID);
65 list_del_rcu(&p->tasks);
66 __get_cpu_var(process_counts)--;
68 list_del_rcu(&p->thread_group);
73 * This function expects the tasklist_lock write-locked.
75 static void __exit_signal(struct task_struct *tsk)
77 struct signal_struct *sig = tsk->signal;
78 struct sighand_struct *sighand;
81 BUG_ON(!atomic_read(&sig->count));
84 sighand = rcu_dereference(tsk->sighand);
85 spin_lock(&sighand->siglock);
87 posix_cpu_timers_exit(tsk);
88 if (atomic_dec_and_test(&sig->count))
89 posix_cpu_timers_exit_group(tsk);
92 * If there is any task waiting for the group exit
95 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
96 wake_up_process(sig->group_exit_task);
97 sig->group_exit_task = NULL;
99 if (tsk == sig->curr_target)
100 sig->curr_target = next_thread(tsk);
102 * Accumulate here the counters for all threads but the
103 * group leader as they die, so they can be added into
104 * the process-wide totals when those are taken.
105 * The group leader stays around as a zombie as long
106 * as there are other threads. When it gets reaped,
107 * the exit.c code will add its counts into these totals.
108 * We won't ever get here for the group leader, since it
109 * will have been the last reference on the signal_struct.
111 sig->utime = cputime_add(sig->utime, tsk->utime);
112 sig->stime = cputime_add(sig->stime, tsk->stime);
113 sig->min_flt += tsk->min_flt;
114 sig->maj_flt += tsk->maj_flt;
115 sig->nvcsw += tsk->nvcsw;
116 sig->nivcsw += tsk->nivcsw;
117 sig->sched_time += tsk->sched_time;
118 sig = NULL; /* Marker for below. */
121 __unhash_process(tsk);
125 spin_unlock(&sighand->siglock);
128 __cleanup_sighand(sighand);
129 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
130 flush_sigqueue(&tsk->pending);
132 flush_sigqueue(&sig->shared_pending);
133 __cleanup_signal(sig);
137 static void delayed_put_task_struct(struct rcu_head *rhp)
139 put_task_struct(container_of(rhp, struct task_struct, rcu));
142 void release_task(struct task_struct * p)
144 struct task_struct *leader;
147 tracehook_release_task(p);
148 atomic_dec(&p->user->processes);
149 write_lock_irq(&tasklist_lock);
150 BUG_ON(tracehook_check_released(p));
154 * If we are the last non-leader member of the thread
155 * group, and the leader is zombie, then notify the
156 * group leader's parent process. (if it wants notification.)
159 leader = p->group_leader;
160 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
161 BUG_ON(leader->exit_signal == -1);
162 do_notify_parent(leader, leader->exit_signal);
164 * If we were the last child thread and the leader has
165 * exited already, and the leader's parent ignores SIGCHLD,
166 * then we are the one who should release the leader.
168 * do_notify_parent() will have marked it self-reaping in
171 zap_leader = (leader->exit_signal == -1);
175 write_unlock_irq(&tasklist_lock);
178 call_rcu(&p->rcu, delayed_put_task_struct);
181 if (unlikely(zap_leader))
186 * This checks not only the pgrp, but falls back on the pid if no
187 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
190 int session_of_pgrp(int pgrp)
192 struct task_struct *p;
195 read_lock(&tasklist_lock);
196 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
197 if (p->signal->session > 0) {
198 sid = p->signal->session;
201 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
202 p = find_task_by_pid(pgrp);
204 sid = p->signal->session;
206 read_unlock(&tasklist_lock);
212 * Determine if a process group is "orphaned", according to the POSIX
213 * definition in 2.2.2.52. Orphaned process groups are not to be affected
214 * by terminal-generated stop signals. Newly orphaned process groups are
215 * to receive a SIGHUP and a SIGCONT.
217 * "I ask you, have you ever known what it is to be an orphan?"
219 static int will_become_orphaned_pgrp(int pgrp, struct task_struct *ignored_task)
221 struct task_struct *p;
224 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
225 if (p == ignored_task
227 || p->parent->pid == 1)
229 if (process_group(p->parent) != pgrp
230 && p->parent->signal->session == p->signal->session) {
234 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
235 return ret; /* (sighing) "Often!" */
238 int is_orphaned_pgrp(int pgrp)
242 read_lock(&tasklist_lock);
243 retval = will_become_orphaned_pgrp(pgrp, NULL);
244 read_unlock(&tasklist_lock);
249 static int has_stopped_jobs(int pgrp)
252 struct task_struct *p;
254 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
255 if (p->state != TASK_STOPPED)
260 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
265 * reparent_to_init - Reparent the calling kernel thread to the init task.
267 * If a kernel thread is launched as a result of a system call, or if
268 * it ever exits, it should generally reparent itself to init so that
269 * it is correctly cleaned up on exit.
271 * The various task state such as scheduling policy and priority may have
272 * been inherited from a user process, so we reset them to sane values here.
274 * NOTE that reparent_to_init() gives the caller full capabilities.
276 static void reparent_to_init(void)
278 write_lock_irq(&tasklist_lock);
280 /* Reparent to init */
281 remove_parent(current);
282 current->parent = child_reaper;
285 /* Set the exit signal to SIGCHLD so we signal init on exit */
286 current->exit_signal = SIGCHLD;
288 if ((current->policy == SCHED_NORMAL ||
289 current->policy == SCHED_BATCH)
290 && (task_nice(current) < 0))
291 set_user_nice(current, 0);
295 security_task_reparent_to_init(current);
296 memcpy(current->signal->rlim, init_task.signal->rlim,
297 sizeof(current->signal->rlim));
298 atomic_inc(&(INIT_USER->__count));
299 write_unlock_irq(&tasklist_lock);
300 switch_uid(INIT_USER);
303 void __set_special_pids(pid_t session, pid_t pgrp)
305 struct task_struct *curr = current->group_leader;
307 if (curr->signal->session != session) {
308 detach_pid(curr, PIDTYPE_SID);
309 curr->signal->session = session;
310 attach_pid(curr, PIDTYPE_SID, session);
312 if (process_group(curr) != pgrp) {
313 detach_pid(curr, PIDTYPE_PGID);
314 curr->signal->pgrp = pgrp;
315 attach_pid(curr, PIDTYPE_PGID, pgrp);
319 void set_special_pids(pid_t session, pid_t pgrp)
321 write_lock_irq(&tasklist_lock);
322 __set_special_pids(session, pgrp);
323 write_unlock_irq(&tasklist_lock);
327 * Let kernel threads use this to say that they
328 * allow a certain signal (since daemonize() will
329 * have disabled all of them by default).
331 int allow_signal(int sig)
333 if (!valid_signal(sig) || sig < 1)
336 spin_lock_irq(¤t->sighand->siglock);
337 sigdelset(¤t->blocked, sig);
339 /* Kernel threads handle their own signals.
340 Let the signal code know it'll be handled, so
341 that they don't get converted to SIGKILL or
342 just silently dropped */
343 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
346 spin_unlock_irq(¤t->sighand->siglock);
350 EXPORT_SYMBOL(allow_signal);
352 int disallow_signal(int sig)
354 if (!valid_signal(sig) || sig < 1)
357 spin_lock_irq(¤t->sighand->siglock);
358 sigaddset(¤t->blocked, sig);
360 spin_unlock_irq(¤t->sighand->siglock);
364 EXPORT_SYMBOL(disallow_signal);
367 * Put all the gunge required to become a kernel thread without
368 * attached user resources in one place where it belongs.
371 void daemonize(const char *name, ...)
374 struct fs_struct *fs;
377 va_start(args, name);
378 vsnprintf(current->comm, sizeof(current->comm), name, args);
382 * If we were started as result of loading a module, close all of the
383 * user space pages. We don't need them, and if we didn't close them
384 * they would be locked into memory.
388 set_special_pids(1, 1);
389 proc_clear_tty(current);
391 /* Block and flush all signals */
392 sigfillset(&blocked);
393 sigprocmask(SIG_BLOCK, &blocked, NULL);
394 flush_signals(current);
396 /* Become as one with the init task */
398 exit_fs(current); /* current->fs->count--; */
401 atomic_inc(&fs->count);
402 exit_namespace(current);
403 current->namespace = init_task.namespace;
404 get_namespace(current->namespace);
406 current->files = init_task.files;
407 atomic_inc(¤t->files->count);
412 EXPORT_SYMBOL(daemonize);
414 static void close_files(struct files_struct * files)
422 * It is safe to dereference the fd table without RCU or
423 * ->file_lock because this is the last reference to the
426 fdt = files_fdtable(files);
430 if (i >= fdt->max_fdset || i >= fdt->max_fds)
432 set = fdt->open_fds->fds_bits[j++];
435 struct file * file = xchg(&fdt->fd[i], NULL);
437 filp_close(file, files);
447 struct files_struct *get_files_struct(struct task_struct *task)
449 struct files_struct *files;
454 atomic_inc(&files->count);
460 void fastcall put_files_struct(struct files_struct *files)
464 if (atomic_dec_and_test(&files->count)) {
467 * Free the fd and fdset arrays if we expanded them.
468 * If the fdtable was embedded, pass files for freeing
469 * at the end of the RCU grace period. Otherwise,
470 * you can free files immediately.
472 fdt = files_fdtable(files);
473 if (fdt == &files->fdtab)
474 fdt->free_files = files;
476 kmem_cache_free(files_cachep, files);
481 EXPORT_SYMBOL(put_files_struct);
483 static inline void __exit_files(struct task_struct *tsk)
485 struct files_struct * files = tsk->files;
491 put_files_struct(files);
495 void exit_files(struct task_struct *tsk)
500 static inline void __put_fs_struct(struct fs_struct *fs)
502 /* No need to hold fs->lock if we are killing it */
503 if (atomic_dec_and_test(&fs->count)) {
510 mntput(fs->altrootmnt);
512 kmem_cache_free(fs_cachep, fs);
516 void put_fs_struct(struct fs_struct *fs)
521 static inline void __exit_fs(struct task_struct *tsk)
523 struct fs_struct * fs = tsk->fs;
533 void exit_fs(struct task_struct *tsk)
538 EXPORT_SYMBOL_GPL(exit_fs);
541 * Turn us into a lazy TLB process if we
544 static void exit_mm(struct task_struct * tsk)
546 struct mm_struct *mm = tsk->mm;
552 * Serialize with any possible pending coredump.
553 * We must hold mmap_sem around checking core_waiters
554 * and clearing tsk->mm. The core-inducing thread
555 * will increment core_waiters for each thread in the
556 * group with ->mm != NULL.
558 down_read(&mm->mmap_sem);
559 if (mm->core_waiters) {
560 up_read(&mm->mmap_sem);
561 down_write(&mm->mmap_sem);
562 if (!--mm->core_waiters)
563 complete(mm->core_startup_done);
564 up_write(&mm->mmap_sem);
566 wait_for_completion(&mm->core_done);
567 down_read(&mm->mmap_sem);
569 atomic_inc(&mm->mm_count);
570 BUG_ON(mm != tsk->active_mm);
571 /* more a memory barrier than a real lock */
574 up_read(&mm->mmap_sem);
575 enter_lazy_tlb(mm, current);
581 choose_new_parent(struct task_struct *p, struct task_struct *reaper)
583 /* check for reaper context */
584 vxwprintk((p->xid != reaper->xid) && (reaper != child_reaper),
585 "rogue reaper: %p[%d,#%u] <> %p[%d,#%u]",
586 p, p->pid, p->xid, reaper, reaper->pid, reaper->xid);
589 * Make sure we're not reparenting to ourselves and that
590 * the parent is not a zombie.
592 BUG_ON(p == reaper || reaper->exit_state);
597 reparent_thread(struct task_struct *p, struct task_struct *father)
599 /* We don't want people slaying init. */
600 if (p->exit_signal != -1)
601 p->exit_signal = SIGCHLD;
603 if (p->pdeath_signal)
604 /* We already hold the tasklist_lock here. */
605 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
607 /* Move the child from its dying parent to the new one. */
608 list_move_tail(&p->sibling, &p->parent->children);
610 /* If we'd notified the old parent about this child's death,
611 * also notify the new parent.
613 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
614 thread_group_empty(p))
615 do_notify_parent(p, p->exit_signal);
618 * process group orphan check
619 * Case ii: Our child is in a different pgrp
620 * than we are, and it was the only connection
621 * outside, so the child pgrp is now orphaned.
623 if ((process_group(p) != process_group(father)) &&
624 (p->signal->session == father->signal->session)) {
625 int pgrp = process_group(p);
627 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
628 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, pgrp);
629 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, pgrp);
635 * When we die, we re-parent all our children.
636 * Try to give them to another thread in our thread
637 * group, and if no such member exists, give it to
638 * the global child reaper process (ie "init")
641 forget_original_parent(struct task_struct *father)
643 struct task_struct *p, *reaper = father;
644 struct list_head *_p, *_n;
647 reaper = next_thread(reaper);
648 if (reaper == father) {
649 reaper = vx_child_reaper(father);
652 } while (reaper->exit_state);
654 list_for_each_safe(_p, _n, &father->children) {
655 p = list_entry(_p, struct task_struct, sibling);
656 choose_new_parent(p, vx_child_reaper(p));
657 reparent_thread(p, father);
662 * Send signals to all our closest relatives so that they know
663 * to properly mourn us..
665 static void exit_notify(struct task_struct *tsk)
668 struct task_struct *t;
672 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
673 && !thread_group_empty(tsk)) {
675 * This occurs when there was a race between our exit
676 * syscall and a group signal choosing us as the one to
677 * wake up. It could be that we are the only thread
678 * alerted to check for pending signals, but another thread
679 * should be woken now to take the signal since we will not.
680 * Now we'll wake all the threads in the group just to make
681 * sure someone gets all the pending signals.
683 read_lock(&tasklist_lock);
684 spin_lock_irq(&tsk->sighand->siglock);
685 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
686 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
687 recalc_sigpending_tsk(t);
688 if (signal_pending(t))
689 signal_wake_up(t, 0);
691 spin_unlock_irq(&tsk->sighand->siglock);
692 read_unlock(&tasklist_lock);
695 write_lock_irq(&tasklist_lock);
698 * This does two things:
700 * A. Make init inherit all the child processes
701 * B. Check to see if any process groups have become orphaned
702 * as a result of our exiting, and if they have any stopped
703 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
706 forget_original_parent(tsk);
707 BUG_ON(!list_empty(&tsk->children));
710 * Check to see if any process groups have become orphaned
711 * as a result of our exiting, and if they have any stopped
712 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
714 * Case i: Our father is in a different pgrp than we are
715 * and we were the only connection outside, so our pgrp
716 * is about to become orphaned.
721 if ((process_group(t) != process_group(tsk)) &&
722 (t->signal->session == tsk->signal->session) &&
723 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
724 has_stopped_jobs(process_group(tsk))) {
725 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, process_group(tsk));
726 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, process_group(tsk));
729 /* Let father know we died
731 * Thread signals are configurable, but you aren't going to use
732 * that to send signals to arbitary processes.
733 * That stops right now.
735 * If the parent exec id doesn't match the exec id we saved
736 * when we started then we know the parent has changed security
739 * If our self_exec id doesn't match our parent_exec_id then
740 * we have changed execution domain as these two values started
741 * the same after a fork.
745 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
746 ( tsk->parent_exec_id != t->self_exec_id ||
747 tsk->self_exec_id != tsk->parent_exec_id)
748 && !capable(CAP_KILL))
749 tsk->exit_signal = SIGCHLD;
751 if (!tracehook_notify_death(tsk, &noreap, &cookie)
752 && tsk->exit_signal != -1 && thread_group_empty(tsk))
753 do_notify_parent(tsk, tsk->exit_signal);
756 if (tsk->exit_signal == -1 && !noreap)
758 tsk->exit_state = state;
760 write_unlock_irq(&tasklist_lock);
762 tracehook_report_death(tsk, state, cookie);
764 /* If the process is dead, release it - nobody will wait for it */
765 if (state == EXIT_DEAD)
769 fastcall NORET_TYPE void do_exit(long code)
771 struct task_struct *tsk = current;
772 struct taskstats *tidstats;
776 profile_task_exit(tsk);
778 WARN_ON(atomic_read(&tsk->fs_excl));
780 if (unlikely(in_interrupt()))
781 panic("Aiee, killing interrupt handler!");
782 if (unlikely(!tsk->pid))
783 panic("Attempted to kill the idle task!");
784 if (unlikely(tsk == child_reaper))
785 panic("Attempted to kill init!");
787 tracehook_report_exit(&code);
790 * We're taking recursive faults here in do_exit. Safest is to just
791 * leave this task alone and wait for reboot.
793 if (unlikely(tsk->flags & PF_EXITING)) {
795 "Fixing recursive fault but reboot is needed!\n");
798 set_current_state(TASK_UNINTERRUPTIBLE);
802 tsk->flags |= PF_EXITING;
806 if (unlikely(in_atomic()))
807 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
808 current->comm, current->pid,
811 taskstats_exit_alloc(&tidstats, &mycpu);
813 acct_update_integrals(tsk);
815 update_hiwater_rss(tsk->mm);
816 update_hiwater_vm(tsk->mm);
818 group_dead = atomic_dec_and_test(&tsk->signal->live);
820 hrtimer_cancel(&tsk->signal->real_timer);
821 exit_itimers(tsk->signal);
824 if (current->tux_info) {
825 #ifdef CONFIG_TUX_DEBUG
826 printk("Possibly unexpected TUX-thread exit(%ld) at %p?\n",
827 code, __builtin_return_address(0));
832 acct_collect(code, group_dead);
833 if (unlikely(tsk->robust_list))
834 exit_robust_list(tsk);
835 #if defined(CONFIG_FUTEX) && defined(CONFIG_COMPAT)
836 if (unlikely(tsk->compat_robust_list))
837 compat_exit_robust_list(tsk);
839 if (unlikely(tsk->audit_context))
841 taskstats_exit_send(tsk, tidstats, group_dead, mycpu);
842 taskstats_exit_free(tidstats);
856 if (group_dead && tsk->signal->leader)
857 disassociate_ctty(1);
859 module_put(task_thread_info(tsk)->exec_domain->module);
861 module_put(tsk->binfmt->module);
863 tsk->exit_code = code;
864 proc_exit_connector(tsk);
865 /* needs to stay before exit_notify() */
866 exit_vx_info_early(tsk, code);
869 mpol_free(tsk->mempolicy);
870 tsk->mempolicy = NULL;
873 * This must happen late, after the PID is not
876 if (unlikely(!list_empty(&tsk->pi_state_list)))
877 exit_pi_state_list(tsk);
878 if (unlikely(current->pi_state_cache))
879 kfree(current->pi_state_cache);
881 * Make sure we are holding no locks:
883 debug_check_no_locks_held(tsk);
888 if (tsk->splice_pipe)
889 __free_pipe_info(tsk->splice_pipe);
891 /* needs to stay after exit_notify() */
892 exit_vx_info(tsk, code);
895 /* PF_DEAD causes final put_task_struct after we schedule. */
897 BUG_ON(tsk->flags & PF_DEAD);
898 tsk->flags |= PF_DEAD;
902 /* Avoid "noreturn function does return". */
906 EXPORT_SYMBOL_GPL(do_exit);
908 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
916 EXPORT_SYMBOL(complete_and_exit);
918 asmlinkage long sys_exit(int error_code)
920 do_exit((error_code&0xff)<<8);
924 * Take down every thread in the group. This is called by fatal signals
925 * as well as by sys_exit_group (below).
928 do_group_exit(int exit_code)
930 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
932 if (current->signal->flags & SIGNAL_GROUP_EXIT)
933 exit_code = current->signal->group_exit_code;
934 else if (!thread_group_empty(current)) {
935 struct signal_struct *const sig = current->signal;
936 struct sighand_struct *const sighand = current->sighand;
937 spin_lock_irq(&sighand->siglock);
938 if (sig->flags & SIGNAL_GROUP_EXIT)
939 /* Another thread got here before we took the lock. */
940 exit_code = sig->group_exit_code;
942 sig->group_exit_code = exit_code;
943 zap_other_threads(current);
945 spin_unlock_irq(&sighand->siglock);
953 * this kills every thread in the thread group. Note that any externally
954 * wait4()-ing process will get the correct exit code - even if this
955 * thread is not the thread group leader.
957 asmlinkage void sys_exit_group(int error_code)
959 do_group_exit((error_code & 0xff) << 8);
962 static int eligible_child(pid_t pid, int options, struct task_struct *p)
968 if (process_group(p) != process_group(current))
970 } else if (pid != -1) {
971 if (process_group(p) != -pid)
976 * Do not consider detached threads.
978 if (p->exit_signal == -1)
981 /* Wait for all children (clone and not) if __WALL is set;
982 * otherwise, wait for clone children *only* if __WCLONE is
983 * set; otherwise, wait for non-clone children *only*. (Note:
984 * A "clone" child here is one that reports to its parent
985 * using a signal other than SIGCHLD.) */
986 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
987 && !(options & __WALL))
990 * Do not consider thread group leaders that are
991 * in a non-empty thread group:
993 if (delay_group_leader(p))
996 if (security_task_wait(p))
1002 static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1003 int why, int status,
1004 struct siginfo __user *infop,
1005 struct rusage __user *rusagep)
1007 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1011 retval = put_user(SIGCHLD, &infop->si_signo);
1013 retval = put_user(0, &infop->si_errno);
1015 retval = put_user((short)why, &infop->si_code);
1017 retval = put_user(pid, &infop->si_pid);
1019 retval = put_user(uid, &infop->si_uid);
1021 retval = put_user(status, &infop->si_status);
1028 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1029 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1030 * the lock and this task is uninteresting. If we return nonzero, we have
1031 * released the lock and the system call should return.
1033 static int wait_task_zombie(struct task_struct *p, int noreap,
1034 struct siginfo __user *infop,
1035 int __user *stat_addr, struct rusage __user *ru)
1037 unsigned long state;
1041 if (unlikely(noreap)) {
1044 int exit_code = p->exit_code;
1047 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1049 if (unlikely(p->exit_signal == -1))
1052 read_unlock(&tasklist_lock);
1053 if ((exit_code & 0x7f) == 0) {
1055 status = exit_code >> 8;
1057 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1058 status = exit_code & 0x7f;
1060 return wait_noreap_copyout(p, pid, uid, why,
1065 * Try to move the task's state to DEAD
1066 * only one thread is allowed to do this:
1068 state = xchg(&p->exit_state, EXIT_DEAD);
1069 if (state != EXIT_ZOMBIE) {
1070 BUG_ON(state != EXIT_DEAD);
1073 BUG_ON(p->exit_signal == -1);
1075 if (likely(p->signal)) {
1076 struct signal_struct *psig;
1077 struct signal_struct *sig;
1080 * The resource counters for the group leader are in its
1081 * own task_struct. Those for dead threads in the group
1082 * are in its signal_struct, as are those for the child
1083 * processes it has previously reaped. All these
1084 * accumulate in the parent's signal_struct c* fields.
1086 * We don't bother to take a lock here to protect these
1087 * p->signal fields, because they are only touched by
1088 * __exit_signal, which runs with tasklist_lock
1089 * write-locked anyway, and so is excluded here. We do
1090 * need to protect the access to p->parent->signal fields,
1091 * as other threads in the parent group can be right
1092 * here reaping other children at the same time.
1094 spin_lock_irq(&p->parent->sighand->siglock);
1095 psig = p->parent->signal;
1098 cputime_add(psig->cutime,
1099 cputime_add(p->utime,
1100 cputime_add(sig->utime,
1103 cputime_add(psig->cstime,
1104 cputime_add(p->stime,
1105 cputime_add(sig->stime,
1108 p->min_flt + sig->min_flt + sig->cmin_flt;
1110 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1112 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1114 p->nivcsw + sig->nivcsw + sig->cnivcsw;
1115 spin_unlock_irq(&p->parent->sighand->siglock);
1119 * Now we are sure this task is interesting, and no other
1120 * thread can reap it because we set its state to EXIT_DEAD.
1122 read_unlock(&tasklist_lock);
1124 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1125 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1126 ? p->signal->group_exit_code : p->exit_code;
1127 if (!retval && stat_addr)
1128 retval = put_user(status, stat_addr);
1129 if (!retval && infop)
1130 retval = put_user(SIGCHLD, &infop->si_signo);
1131 if (!retval && infop)
1132 retval = put_user(0, &infop->si_errno);
1133 if (!retval && infop) {
1136 if ((status & 0x7f) == 0) {
1140 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1143 retval = put_user((short)why, &infop->si_code);
1145 retval = put_user(status, &infop->si_status);
1147 if (!retval && infop)
1148 retval = put_user(p->pid, &infop->si_pid);
1149 if (!retval && infop)
1150 retval = put_user(p->uid, &infop->si_uid);
1152 // TODO: is this safe?
1153 p->exit_state = EXIT_ZOMBIE;
1164 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1165 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1166 * the lock and this task is uninteresting. If we return nonzero, we have
1167 * released the lock and the system call should return.
1169 static int wait_task_stopped(struct task_struct *p, int delayed_group_leader,
1170 int noreap, struct siginfo __user *infop,
1171 int __user *stat_addr, struct rusage __user *ru)
1173 int retval, exit_code;
1177 if (delayed_group_leader &&
1178 p->signal && p->signal->group_stop_count > 0)
1180 * A group stop is in progress and this is the group leader.
1181 * We won't report until all threads have stopped.
1186 * Now we are pretty sure this task is interesting.
1187 * Make sure it doesn't get reaped out from under us while we
1188 * give up the lock and then examine it below. We don't want to
1189 * keep holding onto the tasklist_lock while we call getrusage and
1190 * possibly take page faults for user memory.
1193 read_unlock(&tasklist_lock);
1195 if (unlikely(noreap)) {
1199 exit_code = p->exit_code;
1200 if (unlikely(!exit_code) ||
1201 unlikely(p->state & TASK_TRACED))
1203 return wait_noreap_copyout(p, pid, uid, CLD_STOPPED,
1204 (exit_code << 8) | 0x7f,
1208 write_lock_irq(&tasklist_lock);
1211 * This uses xchg to be atomic with the thread resuming and setting
1212 * it. It must also be done with the write lock held to prevent a
1213 * race with the EXIT_ZOMBIE case.
1215 exit_code = xchg(&p->exit_code, 0);
1216 if (unlikely(p->exit_state)) {
1218 * The task resumed and then died. Let the next iteration
1219 * catch it in EXIT_ZOMBIE. Note that exit_code might
1220 * already be zero here if it resumed and did _exit(0).
1221 * The task itself is dead and won't touch exit_code again;
1222 * other processors in this function are locked out.
1224 p->exit_code = exit_code;
1227 if (unlikely(exit_code == 0)) {
1229 * Another thread in this function got to it first, or it
1230 * resumed, or it resumed and then died.
1232 write_unlock_irq(&tasklist_lock);
1236 * We are returning to the wait loop without having successfully
1237 * removed the process and having released the lock. We cannot
1238 * continue, since the "p" task pointer is potentially stale.
1240 * Return -EAGAIN, and do_wait() will restart the loop from the
1241 * beginning. Do _not_ re-acquire the lock.
1246 /* move to end of parent's list to avoid starvation */
1250 write_unlock_irq(&tasklist_lock);
1252 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1253 if (!retval && stat_addr)
1254 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1255 if (!retval && infop)
1256 retval = put_user(SIGCHLD, &infop->si_signo);
1257 if (!retval && infop)
1258 retval = put_user(0, &infop->si_errno);
1259 if (!retval && infop)
1260 retval = put_user((short)CLD_STOPPED, &infop->si_code);
1261 if (!retval && infop)
1262 retval = put_user(exit_code, &infop->si_status);
1263 if (!retval && infop)
1264 retval = put_user(p->pid, &infop->si_pid);
1265 if (!retval && infop)
1266 retval = put_user(p->uid, &infop->si_uid);
1276 * Handle do_wait work for one task in a live, non-stopped state.
1277 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1278 * the lock and this task is uninteresting. If we return nonzero, we have
1279 * released the lock and the system call should return.
1281 static int wait_task_continued(struct task_struct *p, int noreap,
1282 struct siginfo __user *infop,
1283 int __user *stat_addr, struct rusage __user *ru)
1289 if (unlikely(!p->signal))
1292 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1295 spin_lock_irq(&p->sighand->siglock);
1296 /* Re-check with the lock held. */
1297 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1298 spin_unlock_irq(&p->sighand->siglock);
1302 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1303 spin_unlock_irq(&p->sighand->siglock);
1308 read_unlock(&tasklist_lock);
1311 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1313 if (!retval && stat_addr)
1314 retval = put_user(0xffff, stat_addr);
1318 retval = wait_noreap_copyout(p, pid, uid,
1319 CLD_CONTINUED, SIGCONT,
1321 BUG_ON(retval == 0);
1328 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1329 int __user *stat_addr, struct rusage __user *ru)
1331 DECLARE_WAITQUEUE(wait, current);
1332 struct task_struct *tsk;
1335 add_wait_queue(¤t->signal->wait_chldexit,&wait);
1338 * We will set this flag if we see any child that might later
1339 * match our criteria, even if we are not able to reap it yet.
1342 current->state = TASK_INTERRUPTIBLE;
1343 read_lock(&tasklist_lock);
1346 struct task_struct *p;
1347 struct list_head *_p;
1350 list_for_each(_p,&tsk->children) {
1351 p = list_entry(_p, struct task_struct, sibling);
1353 ret = eligible_child(pid, options, p);
1363 * It's stopped now, so it might later
1364 * continue, exit, or stop again.
1367 if (!(options & WUNTRACED))
1369 if (tracehook_inhibit_wait_stopped(p))
1371 retval = wait_task_stopped(p, ret == 2,
1372 (options & WNOWAIT),
1375 if (retval == -EAGAIN)
1377 if (retval != 0) /* He released the lock. */
1382 if (p->exit_state == EXIT_DEAD)
1384 // case EXIT_ZOMBIE:
1385 if (p->exit_state == EXIT_ZOMBIE) {
1387 * Eligible but we cannot release
1391 goto check_continued;
1392 if (!likely(options & WEXITED))
1394 if (tracehook_inhibit_wait_zombie(p)) {
1398 retval = wait_task_zombie(
1399 p, (options & WNOWAIT),
1400 infop, stat_addr, ru);
1401 /* He released the lock. */
1408 * It's running now, so it might later
1409 * exit, stop, or stop and then continue.
1412 if (!unlikely(options & WCONTINUED))
1414 if (tracehook_inhibit_wait_continued(p))
1416 retval = wait_task_continued(
1417 p, (options & WNOWAIT),
1418 infop, stat_addr, ru);
1419 if (retval != 0) /* He released the lock. */
1425 retval = ptrace_do_wait(tsk, pid, options,
1426 infop, stat_addr, ru);
1427 if (retval != -ECHILD) {
1429 if (retval != 0) /* He released the lock. */
1433 if (options & __WNOTHREAD)
1435 tsk = next_thread(tsk);
1436 BUG_ON(tsk->signal != current->signal);
1437 } while (tsk != current);
1439 read_unlock(&tasklist_lock);
1442 if (options & WNOHANG)
1444 retval = -ERESTARTSYS;
1445 if (signal_pending(current))
1452 current->state = TASK_RUNNING;
1453 remove_wait_queue(¤t->signal->wait_chldexit,&wait);
1459 * For a WNOHANG return, clear out all the fields
1460 * we would set so the user can easily tell the
1464 retval = put_user(0, &infop->si_signo);
1466 retval = put_user(0, &infop->si_errno);
1468 retval = put_user(0, &infop->si_code);
1470 retval = put_user(0, &infop->si_pid);
1472 retval = put_user(0, &infop->si_uid);
1474 retval = put_user(0, &infop->si_status);
1480 asmlinkage long sys_waitid(int which, pid_t pid,
1481 struct siginfo __user *infop, int options,
1482 struct rusage __user *ru)
1486 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1488 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1508 ret = do_wait(pid, options, infop, NULL, ru);
1510 /* avoid REGPARM breakage on x86: */
1511 prevent_tail_call(ret);
1515 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1516 int options, struct rusage __user *ru)
1520 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1521 __WNOTHREAD|__WCLONE|__WALL))
1523 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1525 /* avoid REGPARM breakage on x86: */
1526 prevent_tail_call(ret);
1530 #ifdef __ARCH_WANT_SYS_WAITPID
1533 * sys_waitpid() remains for compatibility. waitpid() should be
1534 * implemented by calling sys_wait4() from libc.a.
1536 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1538 return sys_wait4(pid, stat_addr, options, NULL);