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/security.h>
18 #include <linux/acct.h>
19 #include <linux/file.h>
20 #include <linux/binfmts.h>
21 #include <linux/ptrace.h>
22 #include <linux/profile.h>
23 #include <linux/mount.h>
24 #include <linux/proc_fs.h>
25 #include <linux/mempolicy.h>
26 #include <linux/ckrm.h>
27 #include <linux/ckrm_tsk.h>
28 #include <linux/vs_limit.h>
30 #include <asm/uaccess.h>
31 #include <asm/unistd.h>
32 #include <asm/pgtable.h>
33 #include <asm/mmu_context.h>
35 extern void sem_exit (void);
36 extern struct task_struct *child_reaper;
38 int getrusage(struct task_struct *, int, struct rusage __user *);
40 static void __unhash_process(struct task_struct *p)
43 /* tasklist_lock is held, is this sufficient? */
45 atomic_dec(&p->vx_info->cacct.nr_threads);
46 vx_nproc_dec(p->vx_info);
47 // atomic_dec(&p->vx_info->limit.res[RLIMIT_NPROC]);
49 detach_pid(p, PIDTYPE_PID);
50 detach_pid(p, PIDTYPE_TGID);
51 if (thread_group_leader(p)) {
52 detach_pid(p, PIDTYPE_PGID);
53 detach_pid(p, PIDTYPE_SID);
55 __get_cpu_var(process_counts)--;
61 void release_task(struct task_struct * p)
65 struct dentry *proc_dentry;
68 BUG_ON(p->state < TASK_ZOMBIE);
70 atomic_dec(&p->user->processes);
71 spin_lock(&p->proc_lock);
72 proc_dentry = proc_pid_unhash(p);
73 write_lock_irq(&tasklist_lock);
74 if (unlikely(p->ptrace))
76 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
82 * If we are the last non-leader member of the thread
83 * group, and the leader is zombie, then notify the
84 * group leader's parent process. (if it wants notification.)
87 leader = p->group_leader;
88 if (leader != p && thread_group_empty(leader) && leader->state == TASK_ZOMBIE) {
89 BUG_ON(leader->exit_signal == -1);
90 do_notify_parent(leader, leader->exit_signal);
92 * If we were the last child thread and the leader has
93 * exited already, and the leader's parent ignores SIGCHLD,
94 * then we are the one who should release the leader.
96 * do_notify_parent() will have marked it self-reaping in
99 zap_leader = (leader->exit_signal == -1);
102 p->parent->cutime += p->utime + p->cutime;
103 p->parent->cstime += p->stime + p->cstime;
104 p->parent->cmin_flt += p->min_flt + p->cmin_flt;
105 p->parent->cmaj_flt += p->maj_flt + p->cmaj_flt;
106 p->parent->cnvcsw += p->nvcsw + p->cnvcsw;
107 p->parent->cnivcsw += p->nivcsw + p->cnivcsw;
109 write_unlock_irq(&tasklist_lock);
110 spin_unlock(&p->proc_lock);
116 if (unlikely(zap_leader))
120 /* we are using it only for SMP init */
122 void unhash_process(struct task_struct *p)
124 struct dentry *proc_dentry;
126 spin_lock(&p->proc_lock);
127 proc_dentry = proc_pid_unhash(p);
128 write_lock_irq(&tasklist_lock);
130 write_unlock_irq(&tasklist_lock);
131 spin_unlock(&p->proc_lock);
132 proc_pid_flush(proc_dentry);
136 * This checks not only the pgrp, but falls back on the pid if no
137 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
140 int session_of_pgrp(int pgrp)
142 struct task_struct *p;
147 read_lock(&tasklist_lock);
148 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid)
149 if (p->signal->session > 0) {
150 sid = p->signal->session;
153 p = find_task_by_pid(pgrp);
155 sid = p->signal->session;
157 read_unlock(&tasklist_lock);
163 * Determine if a process group is "orphaned", according to the POSIX
164 * definition in 2.2.2.52. Orphaned process groups are not to be affected
165 * by terminal-generated stop signals. Newly orphaned process groups are
166 * to receive a SIGHUP and a SIGCONT.
168 * "I ask you, have you ever known what it is to be an orphan?"
170 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
172 struct task_struct *p;
177 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
178 if (p == ignored_task
179 || p->state >= TASK_ZOMBIE
180 || p->real_parent->pid == 1)
182 if (process_group(p->real_parent) != pgrp
183 && p->real_parent->signal->session == p->signal->session) {
188 return ret; /* (sighing) "Often!" */
191 int is_orphaned_pgrp(int pgrp)
195 read_lock(&tasklist_lock);
196 retval = will_become_orphaned_pgrp(pgrp, NULL);
197 read_unlock(&tasklist_lock);
202 static inline int has_stopped_jobs(int pgrp)
205 struct task_struct *p;
209 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
210 if (p->state != TASK_STOPPED)
213 /* If p is stopped by a debugger on a signal that won't
214 stop it, then don't count p as stopped. This isn't
215 perfect but it's a good approximation. */
216 if (unlikely (p->ptrace)
217 && p->exit_code != SIGSTOP
218 && p->exit_code != SIGTSTP
219 && p->exit_code != SIGTTOU
220 && p->exit_code != SIGTTIN)
230 * reparent_to_init() - Reparent the calling kernel thread to the init task.
232 * If a kernel thread is launched as a result of a system call, or if
233 * it ever exits, it should generally reparent itself to init so that
234 * it is correctly cleaned up on exit.
236 * The various task state such as scheduling policy and priority may have
237 * been inherited from a user process, so we reset them to sane values here.
239 * NOTE that reparent_to_init() gives the caller full capabilities.
241 void reparent_to_init(void)
243 write_lock_irq(&tasklist_lock);
245 ptrace_unlink(current);
246 /* Reparent to init */
247 REMOVE_LINKS(current);
248 /* FIXME handle vchild_reaper/initpid */
249 current->parent = child_reaper;
250 current->real_parent = child_reaper;
253 /* Set the exit signal to SIGCHLD so we signal init on exit */
254 current->exit_signal = SIGCHLD;
256 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
257 set_user_nice(current, 0);
261 security_task_reparent_to_init(current);
262 memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
263 atomic_inc(&(INIT_USER->__count));
264 switch_uid(INIT_USER);
266 write_unlock_irq(&tasklist_lock);
269 void __set_special_pids(pid_t session, pid_t pgrp)
271 struct task_struct *curr = current;
273 if (curr->signal->session != session) {
274 detach_pid(curr, PIDTYPE_SID);
275 curr->signal->session = session;
276 attach_pid(curr, PIDTYPE_SID, session);
278 if (process_group(curr) != pgrp) {
279 detach_pid(curr, PIDTYPE_PGID);
280 curr->signal->pgrp = pgrp;
281 attach_pid(curr, PIDTYPE_PGID, pgrp);
285 void set_special_pids(pid_t session, pid_t pgrp)
287 write_lock_irq(&tasklist_lock);
288 __set_special_pids(session, pgrp);
289 write_unlock_irq(&tasklist_lock);
293 * Let kernel threads use this to say that they
294 * allow a certain signal (since daemonize() will
295 * have disabled all of them by default).
297 int allow_signal(int sig)
299 if (sig < 1 || sig > _NSIG)
302 spin_lock_irq(¤t->sighand->siglock);
303 sigdelset(¤t->blocked, sig);
305 /* Kernel threads handle their own signals.
306 Let the signal code know it'll be handled, so
307 that they don't get converted to SIGKILL or
308 just silently dropped */
309 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
312 spin_unlock_irq(¤t->sighand->siglock);
316 EXPORT_SYMBOL(allow_signal);
318 int disallow_signal(int sig)
320 if (sig < 1 || sig > _NSIG)
323 spin_lock_irq(¤t->sighand->siglock);
324 sigaddset(¤t->blocked, sig);
326 spin_unlock_irq(¤t->sighand->siglock);
330 EXPORT_SYMBOL(disallow_signal);
333 * Put all the gunge required to become a kernel thread without
334 * attached user resources in one place where it belongs.
337 void daemonize(const char *name, ...)
340 struct fs_struct *fs;
343 va_start(args, name);
344 vsnprintf(current->comm, sizeof(current->comm), name, args);
348 * If we were started as result of loading a module, close all of the
349 * user space pages. We don't need them, and if we didn't close them
350 * they would be locked into memory.
354 set_special_pids(1, 1);
355 current->signal->tty = NULL;
357 /* Block and flush all signals */
358 sigfillset(&blocked);
359 sigprocmask(SIG_BLOCK, &blocked, NULL);
360 flush_signals(current);
362 /* Become as one with the init task */
364 exit_fs(current); /* current->fs->count--; */
367 atomic_inc(&fs->count);
369 current->files = init_task.files;
370 atomic_inc(¤t->files->count);
375 EXPORT_SYMBOL(daemonize);
377 static inline void close_files(struct files_struct * files)
385 if (i >= files->max_fdset || i >= files->max_fds)
387 set = files->open_fds->fds_bits[j++];
390 struct file * file = xchg(&files->fd[i], NULL);
392 filp_close(file, files);
403 struct files_struct *get_files_struct(struct task_struct *task)
405 struct files_struct *files;
410 atomic_inc(&files->count);
416 void fastcall put_files_struct(struct files_struct *files)
418 if (atomic_dec_and_test(&files->count)) {
421 * Free the fd and fdset arrays if we expanded them.
423 if (files->fd != &files->fd_array[0])
424 free_fd_array(files->fd, files->max_fds);
425 if (files->max_fdset > __FD_SETSIZE) {
426 free_fdset(files->open_fds, files->max_fdset);
427 free_fdset(files->close_on_exec, files->max_fdset);
429 kmem_cache_free(files_cachep, files);
433 EXPORT_SYMBOL(put_files_struct);
435 static inline void __exit_files(struct task_struct *tsk)
437 struct files_struct * files = tsk->files;
443 put_files_struct(files);
447 void exit_files(struct task_struct *tsk)
452 static inline void __put_fs_struct(struct fs_struct *fs)
454 /* No need to hold fs->lock if we are killing it */
455 if (atomic_dec_and_test(&fs->count)) {
462 mntput(fs->altrootmnt);
464 kmem_cache_free(fs_cachep, fs);
468 void put_fs_struct(struct fs_struct *fs)
473 static inline void __exit_fs(struct task_struct *tsk)
475 struct fs_struct * fs = tsk->fs;
485 void exit_fs(struct task_struct *tsk)
490 EXPORT_SYMBOL_GPL(exit_fs);
493 * Turn us into a lazy TLB process if we
496 static inline void __exit_mm(struct task_struct * tsk)
498 struct mm_struct *mm = tsk->mm;
504 * Serialize with any possible pending coredump.
505 * We must hold mmap_sem around checking core_waiters
506 * and clearing tsk->mm. The core-inducing thread
507 * will increment core_waiters for each thread in the
508 * group with ->mm != NULL.
510 down_read(&mm->mmap_sem);
511 if (mm->core_waiters) {
512 up_read(&mm->mmap_sem);
513 down_write(&mm->mmap_sem);
514 if (!--mm->core_waiters)
515 complete(mm->core_startup_done);
516 up_write(&mm->mmap_sem);
518 wait_for_completion(&mm->core_done);
519 down_read(&mm->mmap_sem);
521 atomic_inc(&mm->mm_count);
522 if (mm != tsk->active_mm) BUG();
523 /* more a memory barrier than a real lock */
526 up_read(&mm->mmap_sem);
527 enter_lazy_tlb(mm, current);
532 void exit_mm(struct task_struct *tsk)
537 EXPORT_SYMBOL(exit_mm);
539 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
542 * Make sure we're not reparenting to ourselves and that
543 * the parent is not a zombie.
545 if (p == reaper || reaper->state >= TASK_ZOMBIE)
546 p->real_parent = child_reaper;
548 p->real_parent = reaper;
549 if (p->parent == p->real_parent)
553 static inline void reparent_thread(task_t *p, task_t *father, int traced)
555 /* We don't want people slaying init. */
556 if (p->exit_signal != -1)
557 p->exit_signal = SIGCHLD;
560 if (p->pdeath_signal)
561 /* We already hold the tasklist_lock here. */
562 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
564 /* Move the child from its dying parent to the new one. */
565 if (unlikely(traced)) {
566 /* Preserve ptrace links if someone else is tracing this child. */
567 list_del_init(&p->ptrace_list);
568 if (p->parent != p->real_parent)
569 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
571 /* If this child is being traced, then we're the one tracing it
572 * anyway, so let go of it.
575 list_del_init(&p->sibling);
576 p->parent = p->real_parent;
577 list_add_tail(&p->sibling, &p->parent->children);
579 /* If we'd notified the old parent about this child's death,
580 * also notify the new parent.
582 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
583 thread_group_empty(p))
584 do_notify_parent(p, p->exit_signal);
588 * process group orphan check
589 * Case ii: Our child is in a different pgrp
590 * than we are, and it was the only connection
591 * outside, so the child pgrp is now orphaned.
593 if ((process_group(p) != process_group(father)) &&
594 (p->signal->session == father->signal->session)) {
595 int pgrp = process_group(p);
597 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
598 __kill_pg_info(SIGHUP, (void *)1, pgrp);
599 __kill_pg_info(SIGCONT, (void *)1, pgrp);
605 * When we die, we re-parent all our children.
606 * Try to give them to another thread in our thread
607 * group, and if no such member exists, give it to
608 * the global child reaper process (ie "init")
610 static inline void forget_original_parent(struct task_struct * father,
611 struct list_head *to_release)
613 struct task_struct *p, *reaper = father;
614 struct list_head *_p, *_n;
616 /* FIXME handle vchild_reaper/initpid */
617 reaper = father->group_leader;
618 if (reaper == father)
619 reaper = child_reaper;
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->state == TASK_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->state == TASK_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;
678 if (signal_pending(tsk) && !tsk->signal->group_exit
679 && !thread_group_empty(tsk)) {
681 * This occurs when there was a race between our exit
682 * syscall and a group signal choosing us as the one to
683 * wake up. It could be that we are the only thread
684 * alerted to check for pending signals, but another thread
685 * should be woken now to take the signal since we will not.
686 * Now we'll wake all the threads in the group just to make
687 * sure someone gets all the pending signals.
689 read_lock(&tasklist_lock);
690 spin_lock_irq(&tsk->sighand->siglock);
691 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
692 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
693 recalc_sigpending_tsk(t);
694 if (signal_pending(t))
695 signal_wake_up(t, 0);
697 spin_unlock_irq(&tsk->sighand->siglock);
698 read_unlock(&tasklist_lock);
701 write_lock_irq(&tasklist_lock);
704 * This does two things:
706 * A. Make init inherit all the child processes
707 * B. Check to see if any process groups have become orphaned
708 * as a result of our exiting, and if they have any stopped
709 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
712 INIT_LIST_HEAD(&ptrace_dead);
713 forget_original_parent(tsk, &ptrace_dead);
714 BUG_ON(!list_empty(&tsk->children));
715 BUG_ON(!list_empty(&tsk->ptrace_children));
718 * Check to see if any process groups have become orphaned
719 * as a result of our exiting, and if they have any stopped
720 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
722 * Case i: Our father is in a different pgrp than we are
723 * and we were the only connection outside, so our pgrp
724 * is about to become orphaned.
727 t = tsk->real_parent;
729 if ((process_group(t) != process_group(tsk)) &&
730 (t->signal->session == tsk->signal->session) &&
731 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
732 has_stopped_jobs(process_group(tsk))) {
733 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
734 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
737 /* Let father know we died
739 * Thread signals are configurable, but you aren't going to use
740 * that to send signals to arbitary processes.
741 * That stops right now.
743 * If the parent exec id doesn't match the exec id we saved
744 * when we started then we know the parent has changed security
747 * If our self_exec id doesn't match our parent_exec_id then
748 * we have changed execution domain as these two values started
749 * the same after a fork.
753 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
754 ( tsk->parent_exec_id != t->self_exec_id ||
755 tsk->self_exec_id != tsk->parent_exec_id)
756 && !capable(CAP_KILL))
757 tsk->exit_signal = SIGCHLD;
760 /* If something other than our normal parent is ptracing us, then
761 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
762 * only has special meaning to our real parent.
764 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
765 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
766 do_notify_parent(tsk, signal);
767 } else if (tsk->ptrace) {
768 do_notify_parent(tsk, SIGCHLD);
772 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
775 tsk->flags |= PF_DEAD;
778 * Clear these here so that update_process_times() won't try to deliver
779 * itimer, profile or rlimit signals to this task while it is in late exit.
781 tsk->it_virt_value = 0;
782 tsk->it_prof_value = 0;
783 tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
786 * In the preemption case it must be impossible for the task
787 * to get runnable again, so use "_raw_" unlock to keep
788 * preempt_count elevated until we schedule().
790 * To avoid deadlock on SMP, interrupts must be unmasked. If we
791 * don't, subsequently called functions (e.g, wait_task_inactive()
792 * via release_task()) will spin, with interrupt flags
793 * unwittingly blocked, until the other task sleeps. That task
794 * may itself be waiting for smp_call_function() to answer and
795 * complete, and with interrupts blocked that will never happen.
797 _raw_write_unlock(&tasklist_lock);
800 list_for_each_safe(_p, _n, &ptrace_dead) {
802 t = list_entry(_p,struct task_struct,ptrace_list);
806 /* If the process is dead, release it - nobody will wait for it */
807 if (state == TASK_DEAD)
812 asmlinkage NORET_TYPE void do_exit(long code)
814 struct task_struct *tsk = current;
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->pid == 1))
821 panic("Attempted to kill init!");
824 tsk->flags |= PF_EXITING;
825 del_timer_sync(&tsk->real_timer);
827 if (unlikely(in_atomic()))
828 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
829 current->comm, current->pid,
832 profile_exit_task(tsk);
834 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
835 current->ptrace_message = code;
836 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
848 if (tsk->signal->leader)
849 disassociate_ctty(1);
851 module_put(tsk->thread_info->exec_domain->module);
853 module_put(tsk->binfmt->module);
855 tsk->exit_code = code;
856 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
857 numtasks_put_ref(tsk->taskclass);
861 mpol_free(tsk->mempolicy);
862 tsk->mempolicy = NULL;
866 /* Avoid "noreturn function does return". */
870 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
878 EXPORT_SYMBOL(complete_and_exit);
880 asmlinkage long sys_exit(int error_code)
882 do_exit((error_code&0xff)<<8);
885 task_t fastcall *next_thread(const task_t *p)
887 const struct pid_link *link = p->pids + PIDTYPE_TGID;
888 const struct list_head *tmp, *head = &link->pidptr->task_list;
893 if (!spin_is_locked(&p->sighand->siglock) &&
894 !rwlock_is_locked(&tasklist_lock))
897 tmp = link->pid_chain.next;
901 return pid_task(tmp, PIDTYPE_TGID);
904 EXPORT_SYMBOL(next_thread);
907 * Take down every thread in the group. This is called by fatal signals
908 * as well as by sys_exit_group (below).
911 do_group_exit(int exit_code)
913 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
915 if (current->signal->group_exit)
916 exit_code = current->signal->group_exit_code;
917 else if (!thread_group_empty(current)) {
918 struct signal_struct *const sig = current->signal;
919 struct sighand_struct *const sighand = current->sighand;
920 read_lock(&tasklist_lock);
921 spin_lock_irq(&sighand->siglock);
923 /* Another thread got here before we took the lock. */
924 exit_code = sig->group_exit_code;
927 sig->group_exit_code = exit_code;
928 zap_other_threads(current);
930 spin_unlock_irq(&sighand->siglock);
931 read_unlock(&tasklist_lock);
939 * this kills every thread in the thread group. Note that any externally
940 * wait4()-ing process will get the correct exit code - even if this
941 * thread is not the thread group leader.
943 asmlinkage void sys_exit_group(int error_code)
945 do_group_exit((error_code & 0xff) << 8);
948 static int eligible_child(pid_t pid, int options, task_t *p)
954 if (process_group(p) != process_group(current))
956 } else if (pid != -1) {
957 if (process_group(p) != -pid)
962 * Do not consider detached threads that are
965 if (p->exit_signal == -1 && !p->ptrace)
968 /* Wait for all children (clone and not) if __WALL is set;
969 * otherwise, wait for clone children *only* if __WCLONE is
970 * set; otherwise, wait for non-clone children *only*. (Note:
971 * A "clone" child here is one that reports to its parent
972 * using a signal other than SIGCHLD.) */
973 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
974 && !(options & __WALL))
977 * Do not consider thread group leaders that are
978 * in a non-empty thread group:
980 if (current->tgid != p->tgid && delay_group_leader(p))
983 if (security_task_wait(p))
990 * Handle sys_wait4 work for one task in state TASK_ZOMBIE. We hold
991 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
992 * the lock and this task is uninteresting. If we return nonzero, we have
993 * released the lock and the system call should return.
995 static int wait_task_zombie(task_t *p, unsigned int __user *stat_addr, struct rusage __user *ru)
1001 * Try to move the task's state to DEAD
1002 * only one thread is allowed to do this:
1004 state = xchg(&p->state, TASK_DEAD);
1005 if (state != TASK_ZOMBIE) {
1006 BUG_ON(state != TASK_DEAD);
1009 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1011 * This can only happen in a race with a ptraced thread
1012 * dying on another processor.
1017 * Now we are sure this task is interesting, and no other
1018 * thread can reap it because we set its state to TASK_DEAD.
1020 read_unlock(&tasklist_lock);
1022 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1023 if (!retval && stat_addr) {
1024 if (p->signal->group_exit)
1025 retval = put_user(p->signal->group_exit_code, stat_addr);
1027 retval = put_user(p->exit_code, stat_addr);
1030 p->state = TASK_ZOMBIE;
1034 if (p->real_parent != p->parent) {
1035 write_lock_irq(&tasklist_lock);
1036 /* Double-check with lock held. */
1037 if (p->real_parent != p->parent) {
1039 p->state = TASK_ZOMBIE;
1040 /* If this is a detached thread, this is where it goes away. */
1041 if (p->exit_signal == -1) {
1042 /* release_task takes the lock itself. */
1043 write_unlock_irq(&tasklist_lock);
1047 do_notify_parent(p, p->exit_signal);
1048 write_unlock_irq(&tasklist_lock);
1053 write_unlock_irq(&tasklist_lock);
1062 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1063 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1064 * the lock and this task is uninteresting. If we return nonzero, we have
1065 * released the lock and the system call should return.
1067 static int wait_task_stopped(task_t *p, int delayed_group_leader,
1068 unsigned int __user *stat_addr,
1069 struct rusage __user *ru)
1071 int retval, exit_code;
1075 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1076 p->signal && p->signal->group_stop_count > 0)
1078 * A group stop is in progress and this is the group leader.
1079 * We won't report until all threads have stopped.
1084 * Now we are pretty sure this task is interesting.
1085 * Make sure it doesn't get reaped out from under us while we
1086 * give up the lock and then examine it below. We don't want to
1087 * keep holding onto the tasklist_lock while we call getrusage and
1088 * possibly take page faults for user memory.
1091 read_unlock(&tasklist_lock);
1092 write_lock_irq(&tasklist_lock);
1095 * This uses xchg to be atomic with the thread resuming and setting
1096 * it. It must also be done with the write lock held to prevent a
1097 * race with the TASK_ZOMBIE case.
1099 exit_code = xchg(&p->exit_code, 0);
1100 if (unlikely(p->state > TASK_STOPPED)) {
1102 * The task resumed and then died. Let the next iteration
1103 * catch it in TASK_ZOMBIE. Note that exit_code might
1104 * already be zero here if it resumed and did _exit(0).
1105 * The task itself is dead and won't touch exit_code again;
1106 * other processors in this function are locked out.
1108 p->exit_code = exit_code;
1111 if (unlikely(exit_code == 0)) {
1113 * Another thread in this function got to it first, or it
1114 * resumed, or it resumed and then died.
1116 write_unlock_irq(&tasklist_lock);
1118 read_lock(&tasklist_lock);
1122 /* move to end of parent's list to avoid starvation */
1124 add_parent(p, p->parent);
1126 write_unlock_irq(&tasklist_lock);
1128 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1129 if (!retval && stat_addr)
1130 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1139 asmlinkage long sys_wait4(pid_t pid,unsigned int __user *stat_addr, int options, struct rusage __user *ru)
1141 DECLARE_WAITQUEUE(wait, current);
1142 struct task_struct *tsk;
1145 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
1148 add_wait_queue(¤t->wait_chldexit,&wait);
1151 current->state = TASK_INTERRUPTIBLE;
1152 read_lock(&tasklist_lock);
1155 struct task_struct *p;
1156 struct list_head *_p;
1159 list_for_each(_p,&tsk->children) {
1160 p = list_entry(_p,struct task_struct,sibling);
1162 ret = eligible_child(pid, options, p);
1169 if (!(options & WUNTRACED) &&
1170 !(p->ptrace & PT_PTRACED))
1172 retval = wait_task_stopped(p, ret == 2,
1174 if (retval != 0) /* He released the lock. */
1179 * Eligible but we cannot release it yet:
1183 retval = wait_task_zombie(p, stat_addr, ru);
1184 if (retval != 0) /* He released the lock. */
1190 list_for_each (_p,&tsk->ptrace_children) {
1191 p = list_entry(_p,struct task_struct,ptrace_list);
1192 if (!eligible_child(pid, options, p))
1198 if (options & __WNOTHREAD)
1200 tsk = next_thread(tsk);
1201 if (tsk->signal != current->signal)
1203 } while (tsk != current);
1204 read_unlock(&tasklist_lock);
1207 if (options & WNOHANG)
1209 retval = -ERESTARTSYS;
1210 if (signal_pending(current))
1217 current->state = TASK_RUNNING;
1218 remove_wait_queue(¤t->wait_chldexit,&wait);
1222 #ifdef __ARCH_WANT_SYS_WAITPID
1225 * sys_waitpid() remains for compatibility. waitpid() should be
1226 * implemented by calling sys_wait4() from libc.a.
1228 asmlinkage long sys_waitpid(pid_t pid, unsigned __user *stat_addr, int options)
1230 return sys_wait4(pid, stat_addr, options, NULL);