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/completion.h>
14 #include <linux/personality.h>
15 #include <linux/tty.h>
16 #include <linux/namespace.h>
17 #include <linux/key.h>
18 #include <linux/security.h>
19 #include <linux/cpu.h>
20 #include <linux/acct.h>
21 #include <linux/file.h>
22 #include <linux/binfmts.h>
23 #include <linux/ptrace.h>
24 #include <linux/profile.h>
25 #include <linux/mount.h>
26 #include <linux/proc_fs.h>
27 #include <linux/mempolicy.h>
28 #include <linux/cpuset.h>
29 #include <linux/syscalls.h>
30 #include <linux/signal.h>
31 #include <linux/cn_proc.h>
33 #include <asm/uaccess.h>
34 #include <asm/unistd.h>
35 #include <asm/pgtable.h>
36 #include <asm/mmu_context.h>
38 extern void sem_exit (void);
39 extern struct task_struct *child_reaper;
41 int getrusage(struct task_struct *, int, struct rusage __user *);
43 static void exit_mm(struct task_struct * tsk);
45 static void __unhash_process(struct task_struct *p)
48 detach_pid(p, PIDTYPE_PID);
49 detach_pid(p, PIDTYPE_TGID);
50 if (thread_group_leader(p)) {
51 detach_pid(p, PIDTYPE_PGID);
52 detach_pid(p, PIDTYPE_SID);
54 __get_cpu_var(process_counts)--;
60 void release_task(struct task_struct * p)
64 struct dentry *proc_dentry;
67 atomic_dec(&p->user->processes);
68 spin_lock(&p->proc_lock);
69 proc_dentry = proc_pid_unhash(p);
70 write_lock_irq(&tasklist_lock);
71 if (unlikely(p->ptrace))
73 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
77 * Note that the fastpath in sys_times depends on __exit_signal having
78 * updated the counters before a task is removed from the tasklist of
79 * the process by __unhash_process.
84 * If we are the last non-leader member of the thread
85 * group, and the leader is zombie, then notify the
86 * group leader's parent process. (if it wants notification.)
89 leader = p->group_leader;
90 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
91 BUG_ON(leader->exit_signal == -1);
92 do_notify_parent(leader, leader->exit_signal);
94 * If we were the last child thread and the leader has
95 * exited already, and the leader's parent ignores SIGCHLD,
96 * then we are the one who should release the leader.
98 * do_notify_parent() will have marked it self-reaping in
101 zap_leader = (leader->exit_signal == -1);
105 write_unlock_irq(&tasklist_lock);
106 spin_unlock(&p->proc_lock);
107 proc_pid_flush(proc_dentry);
112 if (unlikely(zap_leader))
116 /* we are using it only for SMP init */
118 void unhash_process(struct task_struct *p)
120 struct dentry *proc_dentry;
122 spin_lock(&p->proc_lock);
123 proc_dentry = proc_pid_unhash(p);
124 write_lock_irq(&tasklist_lock);
126 write_unlock_irq(&tasklist_lock);
127 spin_unlock(&p->proc_lock);
128 proc_pid_flush(proc_dentry);
132 * This checks not only the pgrp, but falls back on the pid if no
133 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
136 int session_of_pgrp(int pgrp)
138 struct task_struct *p;
141 read_lock(&tasklist_lock);
142 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
143 if (p->signal->session > 0) {
144 sid = p->signal->session;
147 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
148 p = find_task_by_pid(pgrp);
150 sid = p->signal->session;
152 read_unlock(&tasklist_lock);
158 * Determine if a process group is "orphaned", according to the POSIX
159 * definition in 2.2.2.52. Orphaned process groups are not to be affected
160 * by terminal-generated stop signals. Newly orphaned process groups are
161 * to receive a SIGHUP and a SIGCONT.
163 * "I ask you, have you ever known what it is to be an orphan?"
165 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
167 struct task_struct *p;
170 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
171 if (p == ignored_task
173 || p->real_parent->pid == 1)
175 if (process_group(p->real_parent) != pgrp
176 && p->real_parent->signal->session == p->signal->session) {
180 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
181 return ret; /* (sighing) "Often!" */
184 int is_orphaned_pgrp(int pgrp)
188 read_lock(&tasklist_lock);
189 retval = will_become_orphaned_pgrp(pgrp, NULL);
190 read_unlock(&tasklist_lock);
195 static inline int has_stopped_jobs(int pgrp)
198 struct task_struct *p;
200 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
201 if (p->state != TASK_STOPPED)
204 /* If p is stopped by a debugger on a signal that won't
205 stop it, then don't count p as stopped. This isn't
206 perfect but it's a good approximation. */
207 if (unlikely (p->ptrace)
208 && p->exit_code != SIGSTOP
209 && p->exit_code != SIGTSTP
210 && p->exit_code != SIGTTOU
211 && p->exit_code != SIGTTIN)
216 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
221 * reparent_to_init - Reparent the calling kernel thread to the init task.
223 * If a kernel thread is launched as a result of a system call, or if
224 * it ever exits, it should generally reparent itself to init so that
225 * it is correctly cleaned up on exit.
227 * The various task state such as scheduling policy and priority may have
228 * been inherited from a user process, so we reset them to sane values here.
230 * NOTE that reparent_to_init() gives the caller full capabilities.
232 static inline void reparent_to_init(void)
234 write_lock_irq(&tasklist_lock);
236 ptrace_unlink(current);
237 /* Reparent to init */
238 REMOVE_LINKS(current);
239 current->parent = child_reaper;
240 current->real_parent = child_reaper;
243 /* Set the exit signal to SIGCHLD so we signal init on exit */
244 current->exit_signal = SIGCHLD;
246 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
247 set_user_nice(current, 0);
251 security_task_reparent_to_init(current);
252 memcpy(current->signal->rlim, init_task.signal->rlim,
253 sizeof(current->signal->rlim));
254 atomic_inc(&(INIT_USER->__count));
255 write_unlock_irq(&tasklist_lock);
256 switch_uid(INIT_USER);
259 void __set_special_pids(pid_t session, pid_t pgrp)
261 struct task_struct *curr = current;
263 if (curr->signal->session != session) {
264 detach_pid(curr, PIDTYPE_SID);
265 curr->signal->session = session;
266 attach_pid(curr, PIDTYPE_SID, session);
268 if (process_group(curr) != pgrp) {
269 detach_pid(curr, PIDTYPE_PGID);
270 curr->signal->pgrp = pgrp;
271 attach_pid(curr, PIDTYPE_PGID, pgrp);
275 void set_special_pids(pid_t session, pid_t pgrp)
277 write_lock_irq(&tasklist_lock);
278 __set_special_pids(session, pgrp);
279 write_unlock_irq(&tasklist_lock);
283 * Let kernel threads use this to say that they
284 * allow a certain signal (since daemonize() will
285 * have disabled all of them by default).
287 int allow_signal(int sig)
289 if (!valid_signal(sig) || sig < 1)
292 spin_lock_irq(¤t->sighand->siglock);
293 sigdelset(¤t->blocked, sig);
295 /* Kernel threads handle their own signals.
296 Let the signal code know it'll be handled, so
297 that they don't get converted to SIGKILL or
298 just silently dropped */
299 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
302 spin_unlock_irq(¤t->sighand->siglock);
306 EXPORT_SYMBOL(allow_signal);
308 int disallow_signal(int sig)
310 if (!valid_signal(sig) || sig < 1)
313 spin_lock_irq(¤t->sighand->siglock);
314 sigaddset(¤t->blocked, sig);
316 spin_unlock_irq(¤t->sighand->siglock);
320 EXPORT_SYMBOL(disallow_signal);
323 * Put all the gunge required to become a kernel thread without
324 * attached user resources in one place where it belongs.
327 void daemonize(const char *name, ...)
330 struct fs_struct *fs;
333 va_start(args, name);
334 vsnprintf(current->comm, sizeof(current->comm), name, args);
338 * If we were started as result of loading a module, close all of the
339 * user space pages. We don't need them, and if we didn't close them
340 * they would be locked into memory.
344 set_special_pids(1, 1);
346 current->signal->tty = NULL;
349 /* Block and flush all signals */
350 sigfillset(&blocked);
351 sigprocmask(SIG_BLOCK, &blocked, NULL);
352 flush_signals(current);
354 /* Become as one with the init task */
356 exit_fs(current); /* current->fs->count--; */
359 atomic_inc(&fs->count);
361 current->files = init_task.files;
362 atomic_inc(¤t->files->count);
367 EXPORT_SYMBOL(daemonize);
369 static inline void close_files(struct files_struct * files)
377 * It is safe to dereference the fd table without RCU or
378 * ->file_lock because this is the last reference to the
381 fdt = files_fdtable(files);
385 if (i >= fdt->max_fdset || i >= fdt->max_fds)
387 set = fdt->open_fds->fds_bits[j++];
390 struct file * file = xchg(&fdt->fd[i], NULL);
392 filp_close(file, files);
400 struct files_struct *get_files_struct(struct task_struct *task)
402 struct files_struct *files;
407 atomic_inc(&files->count);
413 void fastcall put_files_struct(struct files_struct *files)
417 if (atomic_dec_and_test(&files->count)) {
420 * Free the fd and fdset arrays if we expanded them.
421 * If the fdtable was embedded, pass files for freeing
422 * at the end of the RCU grace period. Otherwise,
423 * you can free files immediately.
425 fdt = files_fdtable(files);
426 if (fdt == &files->fdtab)
427 fdt->free_files = files;
429 kmem_cache_free(files_cachep, files);
434 EXPORT_SYMBOL(put_files_struct);
436 static inline void __exit_files(struct task_struct *tsk)
438 struct files_struct * files = tsk->files;
444 put_files_struct(files);
448 void exit_files(struct task_struct *tsk)
453 static inline void __put_fs_struct(struct fs_struct *fs)
455 /* No need to hold fs->lock if we are killing it */
456 if (atomic_dec_and_test(&fs->count)) {
463 mntput(fs->altrootmnt);
465 kmem_cache_free(fs_cachep, fs);
469 void put_fs_struct(struct fs_struct *fs)
474 static inline void __exit_fs(struct task_struct *tsk)
476 struct fs_struct * fs = tsk->fs;
486 void exit_fs(struct task_struct *tsk)
491 EXPORT_SYMBOL_GPL(exit_fs);
494 * Turn us into a lazy TLB process if we
497 static void exit_mm(struct task_struct * tsk)
499 struct mm_struct *mm = tsk->mm;
505 * Serialize with any possible pending coredump.
506 * We must hold mmap_sem around checking core_waiters
507 * and clearing tsk->mm. The core-inducing thread
508 * will increment core_waiters for each thread in the
509 * group with ->mm != NULL.
511 down_read(&mm->mmap_sem);
512 if (mm->core_waiters) {
513 up_read(&mm->mmap_sem);
514 down_write(&mm->mmap_sem);
515 if (!--mm->core_waiters)
516 complete(mm->core_startup_done);
517 up_write(&mm->mmap_sem);
519 wait_for_completion(&mm->core_done);
520 down_read(&mm->mmap_sem);
522 atomic_inc(&mm->mm_count);
523 if (mm != tsk->active_mm) BUG();
524 /* more a memory barrier than a real lock */
527 up_read(&mm->mmap_sem);
528 enter_lazy_tlb(mm, current);
533 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
536 * Make sure we're not reparenting to ourselves and that
537 * the parent is not a zombie.
539 BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE);
540 p->real_parent = reaper;
543 static inline void reparent_thread(task_t *p, task_t *father, int traced)
545 /* We don't want people slaying init. */
546 if (p->exit_signal != -1)
547 p->exit_signal = SIGCHLD;
549 if (p->pdeath_signal)
550 /* We already hold the tasklist_lock here. */
551 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
553 /* Move the child from its dying parent to the new one. */
554 if (unlikely(traced)) {
555 /* Preserve ptrace links if someone else is tracing this child. */
556 list_del_init(&p->ptrace_list);
557 if (p->parent != p->real_parent)
558 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
560 /* If this child is being traced, then we're the one tracing it
561 * anyway, so let go of it.
564 list_del_init(&p->sibling);
565 p->parent = p->real_parent;
566 list_add_tail(&p->sibling, &p->parent->children);
568 /* If we'd notified the old parent about this child's death,
569 * also notify the new parent.
571 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
572 thread_group_empty(p))
573 do_notify_parent(p, p->exit_signal);
574 else if (p->state == TASK_TRACED) {
576 * If it was at a trace stop, turn it into
577 * a normal stop since it's no longer being
585 * process group orphan check
586 * Case ii: Our child is in a different pgrp
587 * than we are, and it was the only connection
588 * outside, so the child pgrp is now orphaned.
590 if ((process_group(p) != process_group(father)) &&
591 (p->signal->session == father->signal->session)) {
592 int pgrp = process_group(p);
594 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
595 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, pgrp);
596 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, pgrp);
602 * When we die, we re-parent all our children.
603 * Try to give them to another thread in our thread
604 * group, and if no such member exists, give it to
605 * the global child reaper process (ie "init")
607 static inline void forget_original_parent(struct task_struct * father,
608 struct list_head *to_release)
610 struct task_struct *p, *reaper = father;
611 struct list_head *_p, *_n;
614 reaper = next_thread(reaper);
615 if (reaper == father) {
616 reaper = child_reaper;
619 } while (reaper->exit_state);
622 * There are only two places where our children can be:
624 * - in our child list
625 * - in our ptraced child list
627 * Search them and reparent children.
629 list_for_each_safe(_p, _n, &father->children) {
631 p = list_entry(_p,struct task_struct,sibling);
635 /* if father isn't the real parent, then ptrace must be enabled */
636 BUG_ON(father != p->real_parent && !ptrace);
638 if (father == p->real_parent) {
639 /* reparent with a reaper, real father it's us */
640 choose_new_parent(p, reaper, child_reaper);
641 reparent_thread(p, father, 0);
643 /* reparent ptraced task to its real parent */
645 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
646 thread_group_empty(p))
647 do_notify_parent(p, p->exit_signal);
651 * if the ptraced child is a zombie with exit_signal == -1
652 * we must collect it before we exit, or it will remain
653 * zombie forever since we prevented it from self-reap itself
654 * while it was being traced by us, to be able to see it in wait4.
656 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
657 list_add(&p->ptrace_list, to_release);
659 list_for_each_safe(_p, _n, &father->ptrace_children) {
660 p = list_entry(_p,struct task_struct,ptrace_list);
661 choose_new_parent(p, reaper, child_reaper);
662 reparent_thread(p, father, 1);
667 * Send signals to all our closest relatives so that they know
668 * to properly mourn us..
670 static void exit_notify(struct task_struct *tsk)
673 struct task_struct *t;
674 struct list_head ptrace_dead, *_p, *_n;
676 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
677 && !thread_group_empty(tsk)) {
679 * This occurs when there was a race between our exit
680 * syscall and a group signal choosing us as the one to
681 * wake up. It could be that we are the only thread
682 * alerted to check for pending signals, but another thread
683 * should be woken now to take the signal since we will not.
684 * Now we'll wake all the threads in the group just to make
685 * sure someone gets all the pending signals.
687 read_lock(&tasklist_lock);
688 spin_lock_irq(&tsk->sighand->siglock);
689 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
690 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
691 recalc_sigpending_tsk(t);
692 if (signal_pending(t))
693 signal_wake_up(t, 0);
695 spin_unlock_irq(&tsk->sighand->siglock);
696 read_unlock(&tasklist_lock);
699 write_lock_irq(&tasklist_lock);
702 * This does two things:
704 * A. Make init inherit all the child processes
705 * B. Check to see if any process groups have become orphaned
706 * as a result of our exiting, and if they have any stopped
707 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
710 INIT_LIST_HEAD(&ptrace_dead);
711 forget_original_parent(tsk, &ptrace_dead);
712 BUG_ON(!list_empty(&tsk->children));
713 BUG_ON(!list_empty(&tsk->ptrace_children));
716 * Check to see if any process groups have become orphaned
717 * as a result of our exiting, and if they have any stopped
718 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
720 * Case i: Our father is in a different pgrp than we are
721 * and we were the only connection outside, so our pgrp
722 * is about to become orphaned.
725 t = tsk->real_parent;
727 if ((process_group(t) != process_group(tsk)) &&
728 (t->signal->session == tsk->signal->session) &&
729 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
730 has_stopped_jobs(process_group(tsk))) {
731 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, process_group(tsk));
732 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, process_group(tsk));
735 /* Let father know we died
737 * Thread signals are configurable, but you aren't going to use
738 * that to send signals to arbitary processes.
739 * That stops right now.
741 * If the parent exec id doesn't match the exec id we saved
742 * when we started then we know the parent has changed security
745 * If our self_exec id doesn't match our parent_exec_id then
746 * we have changed execution domain as these two values started
747 * the same after a fork.
751 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
752 ( tsk->parent_exec_id != t->self_exec_id ||
753 tsk->self_exec_id != tsk->parent_exec_id)
754 && !capable(CAP_KILL))
755 tsk->exit_signal = SIGCHLD;
758 /* If something other than our normal parent is ptracing us, then
759 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
760 * only has special meaning to our real parent.
762 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
763 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
764 do_notify_parent(tsk, signal);
765 } else if (tsk->ptrace) {
766 do_notify_parent(tsk, SIGCHLD);
770 if (tsk->exit_signal == -1 &&
771 (likely(tsk->ptrace == 0) ||
772 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
774 tsk->exit_state = state;
776 write_unlock_irq(&tasklist_lock);
778 list_for_each_safe(_p, _n, &ptrace_dead) {
780 t = list_entry(_p,struct task_struct,ptrace_list);
784 /* If the process is dead, release it - nobody will wait for it */
785 if (state == EXIT_DEAD)
789 fastcall NORET_TYPE void do_exit(long code)
791 struct task_struct *tsk = current;
794 profile_task_exit(tsk);
796 WARN_ON(atomic_read(&tsk->fs_excl));
798 if (unlikely(in_interrupt()))
799 panic("Aiee, killing interrupt handler!");
800 if (unlikely(!tsk->pid))
801 panic("Attempted to kill the idle task!");
802 if (unlikely(tsk->pid == 1))
803 panic("Attempted to kill init!");
807 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
808 current->ptrace_message = code;
809 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
813 * We're taking recursive faults here in do_exit. Safest is to just
814 * leave this task alone and wait for reboot.
816 if (unlikely(tsk->flags & PF_EXITING)) {
818 "Fixing recursive fault but reboot is needed!\n");
819 set_current_state(TASK_UNINTERRUPTIBLE);
823 tsk->flags |= PF_EXITING;
826 * Make sure we don't try to process any timer firings
827 * while we are already exiting.
829 tsk->it_virt_expires = cputime_zero;
830 tsk->it_prof_expires = cputime_zero;
831 tsk->it_sched_expires = 0;
833 if (unlikely(in_atomic()))
834 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
835 current->comm, current->pid,
838 acct_update_integrals(tsk);
840 update_hiwater_rss(tsk->mm);
841 update_hiwater_vm(tsk->mm);
843 group_dead = atomic_dec_and_test(&tsk->signal->live);
845 del_timer_sync(&tsk->signal->real_timer);
846 exit_itimers(tsk->signal);
859 if (group_dead && tsk->signal->leader)
860 disassociate_ctty(1);
862 module_put(task_thread_info(tsk)->exec_domain->module);
864 module_put(tsk->binfmt->module);
866 tsk->exit_code = code;
867 proc_exit_connector(tsk);
870 mpol_free(tsk->mempolicy);
871 tsk->mempolicy = NULL;
874 /* PF_DEAD causes final put_task_struct after we schedule. */
876 BUG_ON(tsk->flags & PF_DEAD);
877 tsk->flags |= PF_DEAD;
881 /* Avoid "noreturn function does return". */
885 EXPORT_SYMBOL_GPL(do_exit);
887 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
895 EXPORT_SYMBOL(complete_and_exit);
897 asmlinkage long sys_exit(int error_code)
899 do_exit((error_code&0xff)<<8);
902 task_t fastcall *next_thread(const task_t *p)
904 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
907 EXPORT_SYMBOL(next_thread);
910 * Take down every thread in the group. This is called by fatal signals
911 * as well as by sys_exit_group (below).
914 do_group_exit(int exit_code)
916 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
918 if (current->signal->flags & SIGNAL_GROUP_EXIT)
919 exit_code = current->signal->group_exit_code;
920 else if (!thread_group_empty(current)) {
921 struct signal_struct *const sig = current->signal;
922 struct sighand_struct *const sighand = current->sighand;
923 read_lock(&tasklist_lock);
924 spin_lock_irq(&sighand->siglock);
925 if (sig->flags & SIGNAL_GROUP_EXIT)
926 /* Another thread got here before we took the lock. */
927 exit_code = sig->group_exit_code;
929 sig->flags = SIGNAL_GROUP_EXIT;
930 sig->group_exit_code = exit_code;
931 zap_other_threads(current);
933 spin_unlock_irq(&sighand->siglock);
934 read_unlock(&tasklist_lock);
942 * this kills every thread in the thread group. Note that any externally
943 * wait4()-ing process will get the correct exit code - even if this
944 * thread is not the thread group leader.
946 asmlinkage void sys_exit_group(int error_code)
948 do_group_exit((error_code & 0xff) << 8);
951 static int eligible_child(pid_t pid, int options, task_t *p)
957 if (process_group(p) != process_group(current))
959 } else if (pid != -1) {
960 if (process_group(p) != -pid)
965 * Do not consider detached threads that are
968 if (p->exit_signal == -1 && !p->ptrace)
971 /* Wait for all children (clone and not) if __WALL is set;
972 * otherwise, wait for clone children *only* if __WCLONE is
973 * set; otherwise, wait for non-clone children *only*. (Note:
974 * A "clone" child here is one that reports to its parent
975 * using a signal other than SIGCHLD.) */
976 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
977 && !(options & __WALL))
980 * Do not consider thread group leaders that are
981 * in a non-empty thread group:
983 if (current->tgid != p->tgid && delay_group_leader(p))
986 if (security_task_wait(p))
992 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
994 struct siginfo __user *infop,
995 struct rusage __user *rusagep)
997 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1000 retval = put_user(SIGCHLD, &infop->si_signo);
1002 retval = put_user(0, &infop->si_errno);
1004 retval = put_user((short)why, &infop->si_code);
1006 retval = put_user(pid, &infop->si_pid);
1008 retval = put_user(uid, &infop->si_uid);
1010 retval = put_user(status, &infop->si_status);
1017 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1018 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1019 * the lock and this task is uninteresting. If we return nonzero, we have
1020 * released the lock and the system call should return.
1022 static int wait_task_zombie(task_t *p, int noreap,
1023 struct siginfo __user *infop,
1024 int __user *stat_addr, struct rusage __user *ru)
1026 unsigned long state;
1030 if (unlikely(noreap)) {
1033 int exit_code = p->exit_code;
1036 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1038 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1041 read_unlock(&tasklist_lock);
1042 if ((exit_code & 0x7f) == 0) {
1044 status = exit_code >> 8;
1046 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1047 status = exit_code & 0x7f;
1049 return wait_noreap_copyout(p, pid, uid, why,
1054 * Try to move the task's state to DEAD
1055 * only one thread is allowed to do this:
1057 state = xchg(&p->exit_state, EXIT_DEAD);
1058 if (state != EXIT_ZOMBIE) {
1059 BUG_ON(state != EXIT_DEAD);
1062 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1064 * This can only happen in a race with a ptraced thread
1065 * dying on another processor.
1070 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1072 * The resource counters for the group leader are in its
1073 * own task_struct. Those for dead threads in the group
1074 * are in its signal_struct, as are those for the child
1075 * processes it has previously reaped. All these
1076 * accumulate in the parent's signal_struct c* fields.
1078 * We don't bother to take a lock here to protect these
1079 * p->signal fields, because they are only touched by
1080 * __exit_signal, which runs with tasklist_lock
1081 * write-locked anyway, and so is excluded here. We do
1082 * need to protect the access to p->parent->signal fields,
1083 * as other threads in the parent group can be right
1084 * here reaping other children at the same time.
1086 spin_lock_irq(&p->parent->sighand->siglock);
1087 p->parent->signal->cutime =
1088 cputime_add(p->parent->signal->cutime,
1089 cputime_add(p->utime,
1090 cputime_add(p->signal->utime,
1091 p->signal->cutime)));
1092 p->parent->signal->cstime =
1093 cputime_add(p->parent->signal->cstime,
1094 cputime_add(p->stime,
1095 cputime_add(p->signal->stime,
1096 p->signal->cstime)));
1097 p->parent->signal->cmin_flt +=
1098 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1099 p->parent->signal->cmaj_flt +=
1100 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1101 p->parent->signal->cnvcsw +=
1102 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1103 p->parent->signal->cnivcsw +=
1104 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1105 spin_unlock_irq(&p->parent->sighand->siglock);
1109 * Now we are sure this task is interesting, and no other
1110 * thread can reap it because we set its state to EXIT_DEAD.
1112 read_unlock(&tasklist_lock);
1114 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1115 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1116 ? p->signal->group_exit_code : p->exit_code;
1117 if (!retval && stat_addr)
1118 retval = put_user(status, stat_addr);
1119 if (!retval && infop)
1120 retval = put_user(SIGCHLD, &infop->si_signo);
1121 if (!retval && infop)
1122 retval = put_user(0, &infop->si_errno);
1123 if (!retval && infop) {
1126 if ((status & 0x7f) == 0) {
1130 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1133 retval = put_user((short)why, &infop->si_code);
1135 retval = put_user(status, &infop->si_status);
1137 if (!retval && infop)
1138 retval = put_user(p->pid, &infop->si_pid);
1139 if (!retval && infop)
1140 retval = put_user(p->uid, &infop->si_uid);
1142 // TODO: is this safe?
1143 p->exit_state = EXIT_ZOMBIE;
1147 if (p->real_parent != p->parent) {
1148 write_lock_irq(&tasklist_lock);
1149 /* Double-check with lock held. */
1150 if (p->real_parent != p->parent) {
1152 // TODO: is this safe?
1153 p->exit_state = EXIT_ZOMBIE;
1155 * If this is not a detached task, notify the parent.
1156 * If it's still not detached after that, don't release
1159 if (p->exit_signal != -1) {
1160 do_notify_parent(p, p->exit_signal);
1161 if (p->exit_signal != -1)
1165 write_unlock_irq(&tasklist_lock);
1174 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1175 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1176 * the lock and this task is uninteresting. If we return nonzero, we have
1177 * released the lock and the system call should return.
1179 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1180 struct siginfo __user *infop,
1181 int __user *stat_addr, struct rusage __user *ru)
1183 int retval, exit_code;
1187 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1188 p->signal && p->signal->group_stop_count > 0)
1190 * A group stop is in progress and this is the group leader.
1191 * We won't report until all threads have stopped.
1196 * Now we are pretty sure this task is interesting.
1197 * Make sure it doesn't get reaped out from under us while we
1198 * give up the lock and then examine it below. We don't want to
1199 * keep holding onto the tasklist_lock while we call getrusage and
1200 * possibly take page faults for user memory.
1203 read_unlock(&tasklist_lock);
1205 if (unlikely(noreap)) {
1208 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1210 exit_code = p->exit_code;
1211 if (unlikely(!exit_code) ||
1212 unlikely(p->state & TASK_TRACED))
1214 return wait_noreap_copyout(p, pid, uid,
1215 why, (exit_code << 8) | 0x7f,
1219 write_lock_irq(&tasklist_lock);
1222 * This uses xchg to be atomic with the thread resuming and setting
1223 * it. It must also be done with the write lock held to prevent a
1224 * race with the EXIT_ZOMBIE case.
1226 exit_code = xchg(&p->exit_code, 0);
1227 if (unlikely(p->exit_state)) {
1229 * The task resumed and then died. Let the next iteration
1230 * catch it in EXIT_ZOMBIE. Note that exit_code might
1231 * already be zero here if it resumed and did _exit(0).
1232 * The task itself is dead and won't touch exit_code again;
1233 * other processors in this function are locked out.
1235 p->exit_code = exit_code;
1238 if (unlikely(exit_code == 0)) {
1240 * Another thread in this function got to it first, or it
1241 * resumed, or it resumed and then died.
1243 write_unlock_irq(&tasklist_lock);
1247 * We are returning to the wait loop without having successfully
1248 * removed the process and having released the lock. We cannot
1249 * continue, since the "p" task pointer is potentially stale.
1251 * Return -EAGAIN, and do_wait() will restart the loop from the
1252 * beginning. Do _not_ re-acquire the lock.
1257 /* move to end of parent's list to avoid starvation */
1259 add_parent(p, p->parent);
1261 write_unlock_irq(&tasklist_lock);
1263 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1264 if (!retval && stat_addr)
1265 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1266 if (!retval && infop)
1267 retval = put_user(SIGCHLD, &infop->si_signo);
1268 if (!retval && infop)
1269 retval = put_user(0, &infop->si_errno);
1270 if (!retval && infop)
1271 retval = put_user((short)((p->ptrace & PT_PTRACED)
1272 ? CLD_TRAPPED : CLD_STOPPED),
1274 if (!retval && infop)
1275 retval = put_user(exit_code, &infop->si_status);
1276 if (!retval && infop)
1277 retval = put_user(p->pid, &infop->si_pid);
1278 if (!retval && infop)
1279 retval = put_user(p->uid, &infop->si_uid);
1289 * Handle do_wait work for one task in a live, non-stopped state.
1290 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1291 * the lock and this task is uninteresting. If we return nonzero, we have
1292 * released the lock and the system call should return.
1294 static int wait_task_continued(task_t *p, int noreap,
1295 struct siginfo __user *infop,
1296 int __user *stat_addr, struct rusage __user *ru)
1302 if (unlikely(!p->signal))
1305 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1308 spin_lock_irq(&p->sighand->siglock);
1309 /* Re-check with the lock held. */
1310 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1311 spin_unlock_irq(&p->sighand->siglock);
1315 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1316 spin_unlock_irq(&p->sighand->siglock);
1321 read_unlock(&tasklist_lock);
1324 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1326 if (!retval && stat_addr)
1327 retval = put_user(0xffff, stat_addr);
1331 retval = wait_noreap_copyout(p, pid, uid,
1332 CLD_CONTINUED, SIGCONT,
1334 BUG_ON(retval == 0);
1341 static inline int my_ptrace_child(struct task_struct *p)
1343 if (!(p->ptrace & PT_PTRACED))
1345 if (!(p->ptrace & PT_ATTACHED))
1348 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1349 * we are the attacher. If we are the real parent, this is a race
1350 * inside ptrace_attach. It is waiting for the tasklist_lock,
1351 * which we have to switch the parent links, but has already set
1352 * the flags in p->ptrace.
1354 return (p->parent != p->real_parent);
1357 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1358 int __user *stat_addr, struct rusage __user *ru)
1360 DECLARE_WAITQUEUE(wait, current);
1361 struct task_struct *tsk;
1364 add_wait_queue(¤t->signal->wait_chldexit,&wait);
1367 * We will set this flag if we see any child that might later
1368 * match our criteria, even if we are not able to reap it yet.
1371 current->state = TASK_INTERRUPTIBLE;
1372 read_lock(&tasklist_lock);
1375 struct task_struct *p;
1376 struct list_head *_p;
1379 list_for_each(_p,&tsk->children) {
1380 p = list_entry(_p,struct task_struct,sibling);
1382 ret = eligible_child(pid, options, p);
1389 * When we hit the race with PTRACE_ATTACH,
1390 * we will not report this child. But the
1391 * race means it has not yet been moved to
1392 * our ptrace_children list, so we need to
1393 * set the flag here to avoid a spurious ECHILD
1394 * when the race happens with the only child.
1397 if (!my_ptrace_child(p))
1402 * It's stopped now, so it might later
1403 * continue, exit, or stop again.
1406 if (!(options & WUNTRACED) &&
1407 !my_ptrace_child(p))
1409 retval = wait_task_stopped(p, ret == 2,
1410 (options & WNOWAIT),
1413 if (retval == -EAGAIN)
1415 if (retval != 0) /* He released the lock. */
1420 if (p->exit_state == EXIT_DEAD)
1422 // case EXIT_ZOMBIE:
1423 if (p->exit_state == EXIT_ZOMBIE) {
1425 * Eligible but we cannot release
1429 goto check_continued;
1430 if (!likely(options & WEXITED))
1432 retval = wait_task_zombie(
1433 p, (options & WNOWAIT),
1434 infop, stat_addr, ru);
1435 /* He released the lock. */
1442 * It's running now, so it might later
1443 * exit, stop, or stop and then continue.
1446 if (!unlikely(options & WCONTINUED))
1448 retval = wait_task_continued(
1449 p, (options & WNOWAIT),
1450 infop, stat_addr, ru);
1451 if (retval != 0) /* He released the lock. */
1457 list_for_each(_p, &tsk->ptrace_children) {
1458 p = list_entry(_p, struct task_struct,
1460 if (!eligible_child(pid, options, p))
1466 if (options & __WNOTHREAD)
1468 tsk = next_thread(tsk);
1469 if (tsk->signal != current->signal)
1471 } while (tsk != current);
1473 read_unlock(&tasklist_lock);
1476 if (options & WNOHANG)
1478 retval = -ERESTARTSYS;
1479 if (signal_pending(current))
1486 current->state = TASK_RUNNING;
1487 remove_wait_queue(¤t->signal->wait_chldexit,&wait);
1493 * For a WNOHANG return, clear out all the fields
1494 * we would set so the user can easily tell the
1498 retval = put_user(0, &infop->si_signo);
1500 retval = put_user(0, &infop->si_errno);
1502 retval = put_user(0, &infop->si_code);
1504 retval = put_user(0, &infop->si_pid);
1506 retval = put_user(0, &infop->si_uid);
1508 retval = put_user(0, &infop->si_status);
1514 asmlinkage long sys_waitid(int which, pid_t pid,
1515 struct siginfo __user *infop, int options,
1516 struct rusage __user *ru)
1520 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1522 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1542 ret = do_wait(pid, options, infop, NULL, ru);
1544 /* avoid REGPARM breakage on x86: */
1545 prevent_tail_call(ret);
1549 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1550 int options, struct rusage __user *ru)
1554 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1555 __WNOTHREAD|__WCLONE|__WALL))
1557 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1559 /* avoid REGPARM breakage on x86: */
1560 prevent_tail_call(ret);
1564 #ifdef __ARCH_WANT_SYS_WAITPID
1567 * sys_waitpid() remains for compatibility. waitpid() should be
1568 * implemented by calling sys_wait4() from libc.a.
1570 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1572 return sys_wait4(pid, stat_addr, options, NULL);