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/mnt_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/tsacct_kern.h>
22 #include <linux/file.h>
23 #include <linux/binfmts.h>
24 #include <linux/nsproxy.h>
25 #include <linux/tracehook.h>
26 #include <linux/pid_namespace.h>
27 #include <linux/ptrace.h>
28 #include <linux/profile.h>
29 #include <linux/mount.h>
30 #include <linux/proc_fs.h>
31 #include <linux/mempolicy.h>
32 #include <linux/taskstats_kern.h>
33 #include <linux/delayacct.h>
34 #include <linux/cpuset.h>
35 #include <linux/syscalls.h>
36 #include <linux/signal.h>
37 #include <linux/posix-timers.h>
38 #include <linux/cn_proc.h>
39 #include <linux/mutex.h>
40 #include <linux/futex.h>
41 #include <linux/compat.h>
42 #include <linux/pipe_fs_i.h>
43 #include <linux/audit.h> /* for audit_free() */
44 #include <linux/resource.h>
45 #include <linux/blkdev.h>
46 #include <linux/vs_limit.h>
47 #include <linux/vs_context.h>
48 #include <linux/vs_network.h>
49 #include <linux/vs_pid.h>
50 #include <linux/vserver/global.h>
52 #include <asm/uaccess.h>
53 #include <asm/unistd.h>
54 #include <asm/pgtable.h>
55 #include <asm/mmu_context.h>
57 extern void sem_exit (void);
59 static void exit_mm(struct task_struct * tsk);
61 static void __unhash_process(struct task_struct *p)
64 detach_pid(p, PIDTYPE_PID);
65 if (thread_group_leader(p)) {
66 detach_pid(p, PIDTYPE_PGID);
67 detach_pid(p, PIDTYPE_SID);
69 list_del_rcu(&p->tasks);
70 __get_cpu_var(process_counts)--;
72 list_del_rcu(&p->thread_group);
77 * This function expects the tasklist_lock write-locked.
79 static void __exit_signal(struct task_struct *tsk)
81 struct signal_struct *sig = tsk->signal;
82 struct sighand_struct *sighand;
85 BUG_ON(!atomic_read(&sig->count));
88 sighand = rcu_dereference(tsk->sighand);
89 spin_lock(&sighand->siglock);
91 posix_cpu_timers_exit(tsk);
92 if (atomic_dec_and_test(&sig->count))
93 posix_cpu_timers_exit_group(tsk);
96 * If there is any task waiting for the group exit
99 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
100 wake_up_process(sig->group_exit_task);
101 sig->group_exit_task = NULL;
103 if (tsk == sig->curr_target)
104 sig->curr_target = next_thread(tsk);
106 * Accumulate here the counters for all threads but the
107 * group leader as they die, so they can be added into
108 * the process-wide totals when those are taken.
109 * The group leader stays around as a zombie as long
110 * as there are other threads. When it gets reaped,
111 * the exit.c code will add its counts into these totals.
112 * We won't ever get here for the group leader, since it
113 * will have been the last reference on the signal_struct.
115 sig->utime = cputime_add(sig->utime, tsk->utime);
116 sig->stime = cputime_add(sig->stime, tsk->stime);
117 sig->min_flt += tsk->min_flt;
118 sig->maj_flt += tsk->maj_flt;
119 sig->nvcsw += tsk->nvcsw;
120 sig->nivcsw += tsk->nivcsw;
121 sig->sched_time += tsk->sched_time;
122 sig = NULL; /* Marker for below. */
125 __unhash_process(tsk);
129 spin_unlock(&sighand->siglock);
132 __cleanup_sighand(sighand);
133 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
134 flush_sigqueue(&tsk->pending);
136 flush_sigqueue(&sig->shared_pending);
137 taskstats_tgid_free(sig);
138 __cleanup_signal(sig);
142 static void delayed_put_task_struct(struct rcu_head *rhp)
144 put_task_struct(container_of(rhp, struct task_struct, rcu));
147 void release_task(struct task_struct * p)
149 struct task_struct *leader;
151 int inhibited_leader;
153 tracehook_release_task(p);
154 atomic_dec(&p->user->processes);
155 write_lock_irq(&tasklist_lock);
156 BUG_ON(tracehook_check_released(p));
160 * If we are the last non-leader member of the thread
161 * group, and the leader is zombie, then notify the
162 * group leader's parent process. (if it wants notification.)
165 inhibited_leader = 0;
166 leader = p->group_leader;
167 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
168 BUG_ON(leader->exit_signal == -1);
169 if (tracehook_inhibit_wait_zombie(leader))
170 inhibited_leader = 1;
172 do_notify_parent(leader, leader->exit_signal);
174 * If we were the last child thread and the leader has
175 * exited already, and the leader's parent ignores SIGCHLD,
176 * then we are the one who should release the leader.
178 * do_notify_parent() will have marked it self-reaping in
181 zap_leader = (leader->exit_signal == -1);
185 write_unlock_irq(&tasklist_lock);
188 call_rcu(&p->rcu, delayed_put_task_struct);
191 if (unlikely(zap_leader))
195 * If tracing usurps normal reaping of the leader, tracing needs
196 * to be notified it would normally be reapable now.
198 if (unlikely(inhibited_leader))
199 tracehook_report_delayed_group_leader(leader);
203 * This checks not only the pgrp, but falls back on the pid if no
204 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
207 int session_of_pgrp(int pgrp)
209 struct task_struct *p;
212 read_lock(&tasklist_lock);
214 p = find_task_by_pid_type(PIDTYPE_PGID, pgrp);
216 p = find_task_by_pid(pgrp);
218 sid = process_session(p);
220 read_unlock(&tasklist_lock);
226 * Determine if a process group is "orphaned", according to the POSIX
227 * definition in 2.2.2.52. Orphaned process groups are not to be affected
228 * by terminal-generated stop signals. Newly orphaned process groups are
229 * to receive a SIGHUP and a SIGCONT.
231 * "I ask you, have you ever known what it is to be an orphan?"
233 static int will_become_orphaned_pgrp(int pgrp, struct task_struct *ignored_task)
235 struct task_struct *p;
238 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
239 if (p == ignored_task
241 || is_init(p->parent))
243 if (process_group(p->parent) != pgrp &&
244 process_session(p->parent) == process_session(p)) {
248 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
249 return ret; /* (sighing) "Often!" */
252 int is_orphaned_pgrp(int pgrp)
256 read_lock(&tasklist_lock);
257 retval = will_become_orphaned_pgrp(pgrp, NULL);
258 read_unlock(&tasklist_lock);
263 static int has_stopped_jobs(int pgrp)
266 struct task_struct *p;
268 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
269 if (p->state != TASK_STOPPED)
273 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
278 * reparent_to_init - Reparent the calling kernel thread to the init task
279 * of the pid space that the thread belongs to.
281 * If a kernel thread is launched as a result of a system call, or if
282 * it ever exits, it should generally reparent itself to init so that
283 * it is correctly cleaned up on exit.
285 * The various task state such as scheduling policy and priority may have
286 * been inherited from a user process, so we reset them to sane values here.
288 * NOTE that reparent_to_init() gives the caller full capabilities.
290 static void reparent_to_init(void)
292 write_lock_irq(&tasklist_lock);
294 /* Reparent to init */
295 remove_parent(current);
296 current->parent = child_reaper(current);
299 /* Set the exit signal to SIGCHLD so we signal init on exit */
300 current->exit_signal = SIGCHLD;
302 if (!has_rt_policy(current) && (task_nice(current) < 0))
303 set_user_nice(current, 0);
307 security_task_reparent_to_init(current);
308 memcpy(current->signal->rlim, init_task.signal->rlim,
309 sizeof(current->signal->rlim));
310 atomic_inc(&(INIT_USER->__count));
311 write_unlock_irq(&tasklist_lock);
312 switch_uid(INIT_USER);
315 void __set_special_pids(pid_t session, pid_t pgrp)
317 struct task_struct *curr = current->group_leader;
319 if (process_session(curr) != session) {
320 detach_pid(curr, PIDTYPE_SID);
321 set_signal_session(curr->signal, session);
322 attach_pid(curr, PIDTYPE_SID, session);
324 if (process_group(curr) != pgrp) {
325 detach_pid(curr, PIDTYPE_PGID);
326 curr->signal->pgrp = pgrp;
327 attach_pid(curr, PIDTYPE_PGID, pgrp);
331 static void set_special_pids(pid_t session, pid_t pgrp)
333 write_lock_irq(&tasklist_lock);
334 __set_special_pids(session, pgrp);
335 write_unlock_irq(&tasklist_lock);
339 * Let kernel threads use this to say that they
340 * allow a certain signal (since daemonize() will
341 * have disabled all of them by default).
343 int allow_signal(int sig)
345 if (!valid_signal(sig) || sig < 1)
348 spin_lock_irq(¤t->sighand->siglock);
349 sigdelset(¤t->blocked, sig);
351 /* Kernel threads handle their own signals.
352 Let the signal code know it'll be handled, so
353 that they don't get converted to SIGKILL or
354 just silently dropped */
355 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
358 spin_unlock_irq(¤t->sighand->siglock);
362 EXPORT_SYMBOL(allow_signal);
364 int disallow_signal(int sig)
366 if (!valid_signal(sig) || sig < 1)
369 spin_lock_irq(¤t->sighand->siglock);
370 sigaddset(¤t->blocked, sig);
372 spin_unlock_irq(¤t->sighand->siglock);
376 EXPORT_SYMBOL(disallow_signal);
379 * Put all the gunge required to become a kernel thread without
380 * attached user resources in one place where it belongs.
383 void daemonize(const char *name, ...)
386 struct fs_struct *fs;
389 va_start(args, name);
390 vsnprintf(current->comm, sizeof(current->comm), name, args);
394 * If we were started as result of loading a module, close all of the
395 * user space pages. We don't need them, and if we didn't close them
396 * they would be locked into memory.
400 set_special_pids(1, 1);
401 proc_clear_tty(current);
403 /* Block and flush all signals */
404 sigfillset(&blocked);
405 sigprocmask(SIG_BLOCK, &blocked, NULL);
406 flush_signals(current);
408 /* Become as one with the init task */
410 exit_fs(current); /* current->fs->count--; */
413 atomic_inc(&fs->count);
415 exit_task_namespaces(current);
416 current->nsproxy = init_task.nsproxy;
417 get_task_namespaces(current);
420 current->files = init_task.files;
421 atomic_inc(¤t->files->count);
426 EXPORT_SYMBOL(daemonize);
428 static void close_files(struct files_struct * files)
436 * It is safe to dereference the fd table without RCU or
437 * ->file_lock because this is the last reference to the
440 fdt = files_fdtable(files);
444 if (i >= fdt->max_fds)
446 set = fdt->open_fds->fds_bits[j++];
449 struct file * file = xchg(&fdt->fd[i], NULL);
451 filp_close(file, files);
461 struct files_struct *get_files_struct(struct task_struct *task)
463 struct files_struct *files;
468 atomic_inc(&files->count);
474 void fastcall put_files_struct(struct files_struct *files)
478 if (atomic_dec_and_test(&files->count)) {
481 * Free the fd and fdset arrays if we expanded them.
482 * If the fdtable was embedded, pass files for freeing
483 * at the end of the RCU grace period. Otherwise,
484 * you can free files immediately.
486 fdt = files_fdtable(files);
487 if (fdt != &files->fdtab)
488 kmem_cache_free(files_cachep, files);
493 EXPORT_SYMBOL(put_files_struct);
495 void reset_files_struct(struct task_struct *tsk, struct files_struct *files)
497 struct files_struct *old;
503 put_files_struct(old);
505 EXPORT_SYMBOL(reset_files_struct);
507 static inline void __exit_files(struct task_struct *tsk)
509 struct files_struct * files = tsk->files;
515 put_files_struct(files);
519 void exit_files(struct task_struct *tsk)
524 static inline void __put_fs_struct(struct fs_struct *fs)
526 /* No need to hold fs->lock if we are killing it */
527 if (atomic_dec_and_test(&fs->count)) {
534 mntput(fs->altrootmnt);
536 atomic_dec(&vs_global_fs);
537 kmem_cache_free(fs_cachep, fs);
541 void put_fs_struct(struct fs_struct *fs)
546 static inline void __exit_fs(struct task_struct *tsk)
548 struct fs_struct * fs = tsk->fs;
558 void exit_fs(struct task_struct *tsk)
563 EXPORT_SYMBOL_GPL(exit_fs);
566 * Turn us into a lazy TLB process if we
569 static void exit_mm(struct task_struct * tsk)
571 struct mm_struct *mm = tsk->mm;
577 * Serialize with any possible pending coredump.
578 * We must hold mmap_sem around checking core_waiters
579 * and clearing tsk->mm. The core-inducing thread
580 * will increment core_waiters for each thread in the
581 * group with ->mm != NULL.
583 down_read(&mm->mmap_sem);
584 if (mm->core_waiters) {
585 up_read(&mm->mmap_sem);
586 down_write(&mm->mmap_sem);
587 if (!--mm->core_waiters)
588 complete(mm->core_startup_done);
589 up_write(&mm->mmap_sem);
591 wait_for_completion(&mm->core_done);
592 down_read(&mm->mmap_sem);
594 atomic_inc(&mm->mm_count);
595 BUG_ON(mm != tsk->active_mm);
596 /* more a memory barrier than a real lock */
599 up_read(&mm->mmap_sem);
600 enter_lazy_tlb(mm, current);
606 choose_new_parent(struct task_struct *p, struct task_struct *reaper)
608 /* check for reaper context */
609 vxwprintk((p->xid != reaper->xid) && (reaper != child_reaper(p)),
610 "rogue reaper: %p[%d,#%u] <> %p[%d,#%u]",
611 p, p->pid, p->xid, reaper, reaper->pid, reaper->xid);
614 reaper = vx_child_reaper(p);
617 * Make sure we're not reparenting to ourselves and that
618 * the parent is not a zombie.
620 BUG_ON(p == reaper || reaper->exit_state);
625 reparent_thread(struct task_struct *p, struct task_struct *father)
627 if (p->pdeath_signal)
628 /* We already hold the tasklist_lock here. */
629 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
631 /* Move the child from its dying parent to the new one. */
632 list_move_tail(&p->sibling, &p->parent->children);
634 /* If this is a threaded reparent there is no need to
635 * notify anyone anything has happened.
637 if (p->parent->group_leader == father->group_leader)
640 /* We don't want people slaying init. */
641 if (p->exit_signal != -1)
642 p->exit_signal = SIGCHLD;
644 /* If we'd notified the old parent about this child's death,
645 * also notify the new parent.
647 if (!tracehook_inhibit_wait_zombie(p) &&
648 p->exit_state == EXIT_ZOMBIE &&
649 p->exit_signal != -1 && thread_group_empty(p))
650 do_notify_parent(p, p->exit_signal);
653 * process group orphan check
654 * Case ii: Our child is in a different pgrp
655 * than we are, and it was the only connection
656 * outside, so the child pgrp is now orphaned.
658 if ((process_group(p) != process_group(father)) &&
659 (process_session(p) == process_session(father))) {
660 int pgrp = process_group(p);
662 if (will_become_orphaned_pgrp(pgrp, NULL) &&
663 has_stopped_jobs(pgrp)) {
664 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, pgrp);
665 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, pgrp);
671 * When we die, we re-parent all our children.
672 * Try to give them to another thread in our thread
673 * group, and if no such member exists, give it to
674 * the child reaper process (ie "init") in our pid
678 forget_original_parent(struct task_struct *father)
680 struct task_struct *p, *reaper = father;
681 struct list_head *_p, *_n;
684 reaper = next_thread(reaper);
685 if (reaper == father) {
686 // reaper = child_reaper(father);
687 reaper = vx_child_reaper(father);
690 } while (reaper->exit_state);
692 list_for_each_safe(_p, _n, &father->children) {
693 p = list_entry(_p, struct task_struct, sibling);
694 choose_new_parent(p, reaper);
695 reparent_thread(p, father);
700 * Send signals to all our closest relatives so that they know
701 * to properly mourn us..
703 static void exit_notify(struct task_struct *tsk)
706 struct task_struct *t;
710 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
711 && !thread_group_empty(tsk)) {
713 * This occurs when there was a race between our exit
714 * syscall and a group signal choosing us as the one to
715 * wake up. It could be that we are the only thread
716 * alerted to check for pending signals, but another thread
717 * should be woken now to take the signal since we will not.
718 * Now we'll wake all the threads in the group just to make
719 * sure someone gets all the pending signals.
721 read_lock(&tasklist_lock);
722 spin_lock_irq(&tsk->sighand->siglock);
723 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
724 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
725 recalc_sigpending_tsk(t);
726 if (signal_pending(t))
727 signal_wake_up(t, 0);
729 spin_unlock_irq(&tsk->sighand->siglock);
730 read_unlock(&tasklist_lock);
733 write_lock_irq(&tasklist_lock);
736 * This does two things:
738 * A. Make init inherit all the child processes
739 * B. Check to see if any process groups have become orphaned
740 * as a result of our exiting, and if they have any stopped
741 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
744 forget_original_parent(tsk);
745 BUG_ON(!list_empty(&tsk->children));
748 * Check to see if any process groups have become orphaned
749 * as a result of our exiting, and if they have any stopped
750 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
752 * Case i: Our father is in a different pgrp than we are
753 * and we were the only connection outside, so our pgrp
754 * is about to become orphaned.
759 if ((process_group(t) != process_group(tsk)) &&
760 (process_session(t) == process_session(tsk)) &&
761 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
762 has_stopped_jobs(process_group(tsk))) {
763 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, process_group(tsk));
764 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, process_group(tsk));
767 /* Let father know we died
769 * Thread signals are configurable, but you aren't going to use
770 * that to send signals to arbitary processes.
771 * That stops right now.
773 * If the parent exec id doesn't match the exec id we saved
774 * when we started then we know the parent has changed security
777 * If our self_exec id doesn't match our parent_exec_id then
778 * we have changed execution domain as these two values started
779 * the same after a fork.
783 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
784 ( tsk->parent_exec_id != t->self_exec_id ||
785 tsk->self_exec_id != tsk->parent_exec_id)
786 && !capable(CAP_KILL))
787 tsk->exit_signal = SIGCHLD;
789 if (!tracehook_notify_death(tsk, &noreap, &cookie)
790 && tsk->exit_signal != -1 && thread_group_empty(tsk))
791 do_notify_parent(tsk, tsk->exit_signal);
794 if (tsk->exit_signal == -1 && !noreap)
796 tsk->exit_state = state;
798 write_unlock_irq(&tasklist_lock);
800 tracehook_report_death(tsk, state, cookie);
802 /* If the process is dead, release it - nobody will wait for it */
803 if (state == EXIT_DEAD)
807 fastcall NORET_TYPE void do_exit(long code)
809 struct task_struct *tsk = current;
812 profile_task_exit(tsk);
814 WARN_ON(atomic_read(&tsk->fs_excl));
816 if (unlikely(in_interrupt()))
817 panic("Aiee, killing interrupt handler!");
818 if (unlikely(!tsk->pid))
819 panic("Attempted to kill the idle task!");
820 if (unlikely(tsk == child_reaper(tsk))) {
821 if (tsk->nsproxy->pid_ns != &init_pid_ns)
822 tsk->nsproxy->pid_ns->child_reaper = init_pid_ns.child_reaper;
824 panic("Attempted to kill init!");
828 tracehook_report_exit(&code);
831 * We're taking recursive faults here in do_exit. Safest is to just
832 * leave this task alone and wait for reboot.
834 if (unlikely(tsk->flags & PF_EXITING)) {
836 "Fixing recursive fault but reboot is needed!\n");
839 set_current_state(TASK_UNINTERRUPTIBLE);
843 tsk->flags |= PF_EXITING;
847 if (unlikely(in_atomic()))
848 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
849 current->comm, current->pid,
852 acct_update_integrals(tsk);
854 update_hiwater_rss(tsk->mm);
855 update_hiwater_vm(tsk->mm);
857 group_dead = atomic_dec_and_test(&tsk->signal->live);
859 hrtimer_cancel(&tsk->signal->real_timer);
860 exit_itimers(tsk->signal);
862 acct_collect(code, group_dead);
863 if (unlikely(tsk->robust_list))
864 exit_robust_list(tsk);
865 #if defined(CONFIG_FUTEX) && defined(CONFIG_COMPAT)
866 if (unlikely(tsk->compat_robust_list))
867 compat_exit_robust_list(tsk);
869 if (unlikely(tsk->audit_context))
872 taskstats_exit(tsk, group_dead);
885 if (group_dead && tsk->signal->leader)
886 disassociate_ctty(1);
888 module_put(task_thread_info(tsk)->exec_domain->module);
890 module_put(tsk->binfmt->module);
892 tsk->exit_code = code;
893 proc_exit_connector(tsk);
894 exit_task_namespaces(tsk);
895 // ns = exit_task_namespaces_early(tsk);
896 /* needs to stay before exit_notify() */
897 exit_vx_info_early(tsk, code);
899 // exit_task_namespaces(tsk, ns);
901 mpol_free(tsk->mempolicy);
902 tsk->mempolicy = NULL;
905 * This must happen late, after the PID is not
908 if (unlikely(!list_empty(&tsk->pi_state_list)))
909 exit_pi_state_list(tsk);
910 if (unlikely(current->pi_state_cache))
911 kfree(current->pi_state_cache);
913 * Make sure we are holding no locks:
915 debug_check_no_locks_held(tsk);
920 if (tsk->splice_pipe)
921 __free_pipe_info(tsk->splice_pipe);
923 /* needs to stay after exit_notify() */
924 exit_vx_info(tsk, code);
928 /* causes final put_task_struct in finish_task_switch(). */
929 tsk->state = TASK_DEAD;
933 /* Avoid "noreturn function does return". */
935 cpu_relax(); /* For when BUG is null */
938 EXPORT_SYMBOL_GPL(do_exit);
940 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
948 EXPORT_SYMBOL(complete_and_exit);
950 asmlinkage long sys_exit(int error_code)
952 do_exit((error_code&0xff)<<8);
956 * Take down every thread in the group. This is called by fatal signals
957 * as well as by sys_exit_group (below).
960 do_group_exit(int exit_code)
962 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
964 if (current->signal->flags & SIGNAL_GROUP_EXIT)
965 exit_code = current->signal->group_exit_code;
966 else if (!thread_group_empty(current)) {
967 struct signal_struct *const sig = current->signal;
968 struct sighand_struct *const sighand = current->sighand;
969 spin_lock_irq(&sighand->siglock);
970 if (sig->flags & SIGNAL_GROUP_EXIT)
971 /* Another thread got here before we took the lock. */
972 exit_code = sig->group_exit_code;
974 sig->group_exit_code = exit_code;
975 zap_other_threads(current);
977 spin_unlock_irq(&sighand->siglock);
985 * this kills every thread in the thread group. Note that any externally
986 * wait4()-ing process will get the correct exit code - even if this
987 * thread is not the thread group leader.
989 asmlinkage void sys_exit_group(int error_code)
991 do_group_exit((error_code & 0xff) << 8);
994 static int eligible_child(pid_t pid, int options, struct task_struct *p)
1000 if (process_group(p) != process_group(current))
1002 } else if (pid != -1) {
1003 if (process_group(p) != -pid)
1008 * Do not consider detached threads.
1010 if (p->exit_signal == -1)
1013 /* Wait for all children (clone and not) if __WALL is set;
1014 * otherwise, wait for clone children *only* if __WCLONE is
1015 * set; otherwise, wait for non-clone children *only*. (Note:
1016 * A "clone" child here is one that reports to its parent
1017 * using a signal other than SIGCHLD.) */
1018 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
1019 && !(options & __WALL))
1022 * Do not consider thread group leaders that are
1023 * in a non-empty thread group:
1025 if (delay_group_leader(p))
1028 if (security_task_wait(p))
1034 static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1035 int why, int status,
1036 struct siginfo __user *infop,
1037 struct rusage __user *rusagep)
1039 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1043 retval = put_user(SIGCHLD, &infop->si_signo);
1045 retval = put_user(0, &infop->si_errno);
1047 retval = put_user((short)why, &infop->si_code);
1049 retval = put_user(pid, &infop->si_pid);
1051 retval = put_user(uid, &infop->si_uid);
1053 retval = put_user(status, &infop->si_status);
1060 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1061 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1062 * the lock and this task is uninteresting. If we return nonzero, we have
1063 * released the lock and the system call should return.
1065 static int wait_task_zombie(struct task_struct *p, int noreap,
1066 struct siginfo __user *infop,
1067 int __user *stat_addr, struct rusage __user *ru)
1069 unsigned long state;
1073 if (unlikely(noreap)) {
1076 int exit_code = p->exit_code;
1079 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1081 if (unlikely(p->exit_signal == -1))
1084 read_unlock(&tasklist_lock);
1085 if ((exit_code & 0x7f) == 0) {
1087 status = exit_code >> 8;
1089 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1090 status = exit_code & 0x7f;
1092 return wait_noreap_copyout(p, pid, uid, why,
1097 * Try to move the task's state to DEAD
1098 * only one thread is allowed to do this:
1100 state = xchg(&p->exit_state, EXIT_DEAD);
1101 if (state != EXIT_ZOMBIE) {
1102 BUG_ON(state != EXIT_DEAD);
1105 BUG_ON(p->exit_signal == -1);
1107 if (likely(p->signal)) {
1108 struct signal_struct *psig;
1109 struct signal_struct *sig;
1112 * The resource counters for the group leader are in its
1113 * own task_struct. Those for dead threads in the group
1114 * are in its signal_struct, as are those for the child
1115 * processes it has previously reaped. All these
1116 * accumulate in the parent's signal_struct c* fields.
1118 * We don't bother to take a lock here to protect these
1119 * p->signal fields, because they are only touched by
1120 * __exit_signal, which runs with tasklist_lock
1121 * write-locked anyway, and so is excluded here. We do
1122 * need to protect the access to p->parent->signal fields,
1123 * as other threads in the parent group can be right
1124 * here reaping other children at the same time.
1126 spin_lock_irq(&p->parent->sighand->siglock);
1127 psig = p->parent->signal;
1130 cputime_add(psig->cutime,
1131 cputime_add(p->utime,
1132 cputime_add(sig->utime,
1135 cputime_add(psig->cstime,
1136 cputime_add(p->stime,
1137 cputime_add(sig->stime,
1140 p->min_flt + sig->min_flt + sig->cmin_flt;
1142 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1144 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1146 p->nivcsw + sig->nivcsw + sig->cnivcsw;
1147 spin_unlock_irq(&p->parent->sighand->siglock);
1151 * Now we are sure this task is interesting, and no other
1152 * thread can reap it because we set its state to EXIT_DEAD.
1154 read_unlock(&tasklist_lock);
1156 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1157 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1158 ? p->signal->group_exit_code : p->exit_code;
1159 if (!retval && stat_addr)
1160 retval = put_user(status, stat_addr);
1161 if (!retval && infop)
1162 retval = put_user(SIGCHLD, &infop->si_signo);
1163 if (!retval && infop)
1164 retval = put_user(0, &infop->si_errno);
1165 if (!retval && infop) {
1168 if ((status & 0x7f) == 0) {
1172 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1175 retval = put_user((short)why, &infop->si_code);
1177 retval = put_user(status, &infop->si_status);
1179 if (!retval && infop)
1180 retval = put_user(p->pid, &infop->si_pid);
1181 if (!retval && infop)
1182 retval = put_user(p->uid, &infop->si_uid);
1184 // TODO: is this safe?
1185 p->exit_state = EXIT_ZOMBIE;
1196 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1197 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1198 * the lock and this task is uninteresting. If we return nonzero, we have
1199 * released the lock and the system call should return.
1201 static int wait_task_stopped(struct task_struct *p, int delayed_group_leader,
1202 int noreap, struct siginfo __user *infop,
1203 int __user *stat_addr, struct rusage __user *ru)
1205 int retval, exit_code;
1209 if (delayed_group_leader &&
1210 p->signal && p->signal->group_stop_count > 0)
1212 * A group stop is in progress and this is the group leader.
1213 * We won't report until all threads have stopped.
1218 * Now we are pretty sure this task is interesting.
1219 * Make sure it doesn't get reaped out from under us while we
1220 * give up the lock and then examine it below. We don't want to
1221 * keep holding onto the tasklist_lock while we call getrusage and
1222 * possibly take page faults for user memory.
1225 read_unlock(&tasklist_lock);
1227 if (unlikely(noreap)) {
1231 exit_code = p->exit_code;
1232 if (unlikely(!exit_code) ||
1233 unlikely(p->state & TASK_TRACED))
1235 return wait_noreap_copyout(p, pid, uid, CLD_STOPPED,
1236 (exit_code << 8) | 0x7f,
1240 write_lock_irq(&tasklist_lock);
1243 * This uses xchg to be atomic with the thread resuming and setting
1244 * it. It must also be done with the write lock held to prevent a
1245 * race with the EXIT_ZOMBIE case.
1247 exit_code = xchg(&p->exit_code, 0);
1248 if (unlikely(p->exit_state)) {
1250 * The task resumed and then died. Let the next iteration
1251 * catch it in EXIT_ZOMBIE. Note that exit_code might
1252 * already be zero here if it resumed and did _exit(0).
1253 * The task itself is dead and won't touch exit_code again;
1254 * other processors in this function are locked out.
1256 p->exit_code = exit_code;
1259 if (unlikely(exit_code == 0)) {
1261 * Another thread in this function got to it first, or it
1262 * resumed, or it resumed and then died.
1264 write_unlock_irq(&tasklist_lock);
1268 * We are returning to the wait loop without having successfully
1269 * removed the process and having released the lock. We cannot
1270 * continue, since the "p" task pointer is potentially stale.
1272 * Return -EAGAIN, and do_wait() will restart the loop from the
1273 * beginning. Do _not_ re-acquire the lock.
1278 /* move to end of parent's list to avoid starvation */
1282 write_unlock_irq(&tasklist_lock);
1284 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1285 if (!retval && stat_addr)
1286 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1287 if (!retval && infop)
1288 retval = put_user(SIGCHLD, &infop->si_signo);
1289 if (!retval && infop)
1290 retval = put_user(0, &infop->si_errno);
1291 if (!retval && infop)
1292 retval = put_user((short)CLD_STOPPED, &infop->si_code);
1293 if (!retval && infop)
1294 retval = put_user(exit_code, &infop->si_status);
1295 if (!retval && infop)
1296 retval = put_user(p->pid, &infop->si_pid);
1297 if (!retval && infop)
1298 retval = put_user(p->uid, &infop->si_uid);
1308 * Handle do_wait work for one task in a live, non-stopped state.
1309 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1310 * the lock and this task is uninteresting. If we return nonzero, we have
1311 * released the lock and the system call should return.
1313 static int wait_task_continued(struct task_struct *p, int noreap,
1314 struct siginfo __user *infop,
1315 int __user *stat_addr, struct rusage __user *ru)
1321 if (unlikely(!p->signal))
1324 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1327 spin_lock_irq(&p->sighand->siglock);
1328 /* Re-check with the lock held. */
1329 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1330 spin_unlock_irq(&p->sighand->siglock);
1334 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1335 spin_unlock_irq(&p->sighand->siglock);
1340 read_unlock(&tasklist_lock);
1343 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1345 if (!retval && stat_addr)
1346 retval = put_user(0xffff, stat_addr);
1350 retval = wait_noreap_copyout(p, pid, uid,
1351 CLD_CONTINUED, SIGCONT,
1353 BUG_ON(retval == 0);
1360 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1361 int __user *stat_addr, struct rusage __user *ru)
1363 DECLARE_WAITQUEUE(wait, current);
1364 struct task_struct *tsk;
1367 add_wait_queue(¤t->signal->wait_chldexit,&wait);
1370 * We will set this flag if we see any child that might later
1371 * match our criteria, even if we are not able to reap it yet.
1374 current->state = TASK_INTERRUPTIBLE;
1375 read_lock(&tasklist_lock);
1378 struct task_struct *p;
1379 struct list_head *_p;
1382 list_for_each(_p,&tsk->children) {
1383 p = list_entry(_p, struct task_struct, sibling);
1385 ret = eligible_child(pid, options, p);
1395 * It's stopped now, so it might later
1396 * continue, exit, or stop again.
1399 if (!(options & WUNTRACED))
1401 if (tracehook_inhibit_wait_stopped(p))
1403 retval = wait_task_stopped(p, ret == 2,
1404 (options & WNOWAIT),
1407 if (retval == -EAGAIN)
1409 if (retval != 0) /* He released the lock. */
1414 if (p->exit_state == EXIT_DEAD)
1416 // case EXIT_ZOMBIE:
1417 if (p->exit_state == EXIT_ZOMBIE) {
1419 * Eligible but we cannot release
1423 goto check_continued;
1424 if (!likely(options & WEXITED))
1426 if (tracehook_inhibit_wait_zombie(p)) {
1430 retval = wait_task_zombie(
1431 p, (options & WNOWAIT),
1432 infop, stat_addr, ru);
1433 /* He released the lock. */
1440 * It's running now, so it might later
1441 * exit, stop, or stop and then continue.
1444 if (!unlikely(options & WCONTINUED))
1446 if (tracehook_inhibit_wait_continued(p))
1448 retval = wait_task_continued(
1449 p, (options & WNOWAIT),
1450 infop, stat_addr, ru);
1451 if (retval != 0) /* He released the lock. */
1457 retval = ptrace_do_wait(tsk, pid, options,
1458 infop, stat_addr, ru);
1459 if (retval != -ECHILD) {
1461 if (retval != 0) /* He released the lock. */
1465 if (options & __WNOTHREAD)
1467 tsk = next_thread(tsk);
1468 BUG_ON(tsk->signal != current->signal);
1469 } while (tsk != current);
1471 read_unlock(&tasklist_lock);
1474 if (options & WNOHANG)
1476 retval = -ERESTARTSYS;
1477 if (signal_pending(current))
1484 current->state = TASK_RUNNING;
1485 remove_wait_queue(¤t->signal->wait_chldexit,&wait);
1491 * For a WNOHANG return, clear out all the fields
1492 * we would set so the user can easily tell the
1496 retval = put_user(0, &infop->si_signo);
1498 retval = put_user(0, &infop->si_errno);
1500 retval = put_user(0, &infop->si_code);
1502 retval = put_user(0, &infop->si_pid);
1504 retval = put_user(0, &infop->si_uid);
1506 retval = put_user(0, &infop->si_status);
1512 asmlinkage long sys_waitid(int which, pid_t pid,
1513 struct siginfo __user *infop, int options,
1514 struct rusage __user *ru)
1518 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1520 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1540 ret = do_wait(pid, options, infop, NULL, ru);
1542 /* avoid REGPARM breakage on x86: */
1543 prevent_tail_call(ret);
1547 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1548 int options, struct rusage __user *ru)
1552 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1553 __WNOTHREAD|__WCLONE|__WALL))
1555 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1557 /* avoid REGPARM breakage on x86: */
1558 prevent_tail_call(ret);
1562 #ifdef __ARCH_WANT_SYS_WAITPID
1565 * sys_waitpid() remains for compatibility. waitpid() should be
1566 * implemented by calling sys_wait4() from libc.a.
1568 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1570 return sys_wait4(pid, stat_addr, options, NULL);