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>
27 #include <asm/uaccess.h>
28 #include <asm/unistd.h>
29 #include <asm/pgtable.h>
30 #include <asm/mmu_context.h>
32 extern void sem_exit (void);
33 extern struct task_struct *child_reaper;
35 int getrusage(struct task_struct *, int, struct rusage __user *);
37 static void __unhash_process(struct task_struct *p)
40 detach_pid(p, PIDTYPE_PID);
41 detach_pid(p, PIDTYPE_TGID);
42 if (thread_group_leader(p)) {
43 detach_pid(p, PIDTYPE_PGID);
44 detach_pid(p, PIDTYPE_SID);
46 __get_cpu_var(process_counts)--;
52 void release_task(struct task_struct * p)
56 struct dentry *proc_dentry;
59 BUG_ON(p->state < TASK_ZOMBIE);
61 atomic_dec(&p->user->processes);
62 spin_lock(&p->proc_lock);
63 proc_dentry = proc_pid_unhash(p);
64 write_lock_irq(&tasklist_lock);
65 if (unlikely(p->ptrace))
67 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
73 * If we are the last non-leader member of the thread
74 * group, and the leader is zombie, then notify the
75 * group leader's parent process. (if it wants notification.)
78 leader = p->group_leader;
79 if (leader != p && thread_group_empty(leader) && leader->state == TASK_ZOMBIE) {
80 BUG_ON(leader->exit_signal == -1);
81 do_notify_parent(leader, leader->exit_signal);
83 * If we were the last child thread and the leader has
84 * exited already, and the leader's parent ignores SIGCHLD,
85 * then we are the one who should release the leader.
87 * do_notify_parent() will have marked it self-reaping in
90 zap_leader = (leader->exit_signal == -1);
93 p->parent->cutime += p->utime + p->cutime;
94 p->parent->cstime += p->stime + p->cstime;
95 p->parent->cmin_flt += p->min_flt + p->cmin_flt;
96 p->parent->cmaj_flt += p->maj_flt + p->cmaj_flt;
97 p->parent->cnvcsw += p->nvcsw + p->cnvcsw;
98 p->parent->cnivcsw += p->nivcsw + p->cnivcsw;
100 write_unlock_irq(&tasklist_lock);
101 spin_unlock(&p->proc_lock);
107 if (unlikely(zap_leader))
111 /* we are using it only for SMP init */
113 void unhash_process(struct task_struct *p)
115 struct dentry *proc_dentry;
117 spin_lock(&p->proc_lock);
118 proc_dentry = proc_pid_unhash(p);
119 write_lock_irq(&tasklist_lock);
121 write_unlock_irq(&tasklist_lock);
122 spin_unlock(&p->proc_lock);
123 proc_pid_flush(proc_dentry);
127 * This checks not only the pgrp, but falls back on the pid if no
128 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
131 int session_of_pgrp(int pgrp)
133 struct task_struct *p;
138 read_lock(&tasklist_lock);
139 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid)
140 if (p->signal->session > 0) {
141 sid = p->signal->session;
144 p = find_task_by_pid(pgrp);
146 sid = p->signal->session;
148 read_unlock(&tasklist_lock);
154 * Determine if a process group is "orphaned", according to the POSIX
155 * definition in 2.2.2.52. Orphaned process groups are not to be affected
156 * by terminal-generated stop signals. Newly orphaned process groups are
157 * to receive a SIGHUP and a SIGCONT.
159 * "I ask you, have you ever known what it is to be an orphan?"
161 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
163 struct task_struct *p;
168 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
169 if (p == ignored_task
170 || p->state >= TASK_ZOMBIE
171 || p->real_parent->pid == 1)
173 if (process_group(p->real_parent) != pgrp
174 && p->real_parent->signal->session == p->signal->session) {
179 return ret; /* (sighing) "Often!" */
182 int is_orphaned_pgrp(int pgrp)
186 read_lock(&tasklist_lock);
187 retval = will_become_orphaned_pgrp(pgrp, NULL);
188 read_unlock(&tasklist_lock);
193 static inline int has_stopped_jobs(int pgrp)
196 struct task_struct *p;
200 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
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)
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 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->rlim, init_task.rlim, sizeof(*(current->rlim)));
253 atomic_inc(&(INIT_USER->__count));
254 switch_uid(INIT_USER);
256 write_unlock_irq(&tasklist_lock);
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 (sig < 1 || sig > _NSIG)
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 (sig < 1 || sig > _NSIG)
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);
345 current->signal->tty = NULL;
347 /* Block and flush all signals */
348 sigfillset(&blocked);
349 sigprocmask(SIG_BLOCK, &blocked, NULL);
350 flush_signals(current);
352 /* Become as one with the init task */
354 exit_fs(current); /* current->fs->count--; */
357 atomic_inc(&fs->count);
359 current->files = init_task.files;
360 atomic_inc(¤t->files->count);
365 EXPORT_SYMBOL(daemonize);
367 static inline void close_files(struct files_struct * files)
375 if (i >= files->max_fdset || i >= files->max_fds)
377 set = files->open_fds->fds_bits[j++];
380 struct file * file = xchg(&files->fd[i], NULL);
382 filp_close(file, files);
392 struct files_struct *get_files_struct(struct task_struct *task)
394 struct files_struct *files;
399 atomic_inc(&files->count);
405 void fastcall put_files_struct(struct files_struct *files)
407 if (atomic_dec_and_test(&files->count)) {
410 * Free the fd and fdset arrays if we expanded them.
412 if (files->fd != &files->fd_array[0])
413 free_fd_array(files->fd, files->max_fds);
414 if (files->max_fdset > __FD_SETSIZE) {
415 free_fdset(files->open_fds, files->max_fdset);
416 free_fdset(files->close_on_exec, files->max_fdset);
418 kmem_cache_free(files_cachep, files);
422 EXPORT_SYMBOL(put_files_struct);
424 static inline void __exit_files(struct task_struct *tsk)
426 struct files_struct * files = tsk->files;
432 put_files_struct(files);
436 void exit_files(struct task_struct *tsk)
441 static inline void __put_fs_struct(struct fs_struct *fs)
443 /* No need to hold fs->lock if we are killing it */
444 if (atomic_dec_and_test(&fs->count)) {
451 mntput(fs->altrootmnt);
453 kmem_cache_free(fs_cachep, fs);
457 void put_fs_struct(struct fs_struct *fs)
462 static inline void __exit_fs(struct task_struct *tsk)
464 struct fs_struct * fs = tsk->fs;
474 void exit_fs(struct task_struct *tsk)
479 EXPORT_SYMBOL_GPL(exit_fs);
482 * Turn us into a lazy TLB process if we
485 static inline void __exit_mm(struct task_struct * tsk)
487 struct mm_struct *mm = tsk->mm;
493 * Serialize with any possible pending coredump.
494 * We must hold mmap_sem around checking core_waiters
495 * and clearing tsk->mm. The core-inducing thread
496 * will increment core_waiters for each thread in the
497 * group with ->mm != NULL.
499 down_read(&mm->mmap_sem);
500 if (mm->core_waiters) {
501 up_read(&mm->mmap_sem);
502 down_write(&mm->mmap_sem);
503 if (!--mm->core_waiters)
504 complete(mm->core_startup_done);
505 up_write(&mm->mmap_sem);
507 wait_for_completion(&mm->core_done);
508 down_read(&mm->mmap_sem);
510 atomic_inc(&mm->mm_count);
511 if (mm != tsk->active_mm) BUG();
512 /* more a memory barrier than a real lock */
515 up_read(&mm->mmap_sem);
516 enter_lazy_tlb(mm, current);
521 void exit_mm(struct task_struct *tsk)
526 EXPORT_SYMBOL(exit_mm);
528 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
531 * Make sure we're not reparenting to ourselves and that
532 * the parent is not a zombie.
534 if (p == reaper || reaper->state >= TASK_ZOMBIE)
535 p->real_parent = child_reaper;
537 p->real_parent = reaper;
538 if (p->parent == p->real_parent)
542 static inline void reparent_thread(task_t *p, task_t *father, int traced)
544 /* We don't want people slaying init. */
545 if (p->exit_signal != -1)
546 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, (void *) 0, 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->state == TASK_ZOMBIE && p->exit_signal != -1 &&
572 thread_group_empty(p))
573 do_notify_parent(p, p->exit_signal);
577 * process group orphan check
578 * Case ii: Our child is in a different pgrp
579 * than we are, and it was the only connection
580 * outside, so the child pgrp is now orphaned.
582 if ((process_group(p) != process_group(father)) &&
583 (p->signal->session == father->signal->session)) {
584 int pgrp = process_group(p);
586 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
587 __kill_pg_info(SIGHUP, (void *)1, pgrp);
588 __kill_pg_info(SIGCONT, (void *)1, pgrp);
594 * When we die, we re-parent all our children.
595 * Try to give them to another thread in our thread
596 * group, and if no such member exists, give it to
597 * the global child reaper process (ie "init")
599 static inline void forget_original_parent(struct task_struct * father,
600 struct list_head *to_release)
602 struct task_struct *p, *reaper = father;
603 struct list_head *_p, *_n;
605 reaper = father->group_leader;
606 if (reaper == father)
607 reaper = child_reaper;
610 * There are only two places where our children can be:
612 * - in our child list
613 * - in our ptraced child list
615 * Search them and reparent children.
617 list_for_each_safe(_p, _n, &father->children) {
619 p = list_entry(_p,struct task_struct,sibling);
623 /* if father isn't the real parent, then ptrace must be enabled */
624 BUG_ON(father != p->real_parent && !ptrace);
626 if (father == p->real_parent) {
627 /* reparent with a reaper, real father it's us */
628 choose_new_parent(p, reaper, child_reaper);
629 reparent_thread(p, father, 0);
631 /* reparent ptraced task to its real parent */
633 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
634 thread_group_empty(p))
635 do_notify_parent(p, p->exit_signal);
639 * if the ptraced child is a zombie with exit_signal == -1
640 * we must collect it before we exit, or it will remain
641 * zombie forever since we prevented it from self-reap itself
642 * while it was being traced by us, to be able to see it in wait4.
644 if (unlikely(ptrace && p->state == TASK_ZOMBIE && p->exit_signal == -1))
645 list_add(&p->ptrace_list, to_release);
647 list_for_each_safe(_p, _n, &father->ptrace_children) {
648 p = list_entry(_p,struct task_struct,ptrace_list);
649 choose_new_parent(p, reaper, child_reaper);
650 reparent_thread(p, father, 1);
655 * Send signals to all our closest relatives so that they know
656 * to properly mourn us..
658 static void exit_notify(struct task_struct *tsk)
661 struct task_struct *t;
662 struct list_head ptrace_dead, *_p, *_n;
664 if (signal_pending(tsk) && !tsk->signal->group_exit
665 && !thread_group_empty(tsk)) {
667 * This occurs when there was a race between our exit
668 * syscall and a group signal choosing us as the one to
669 * wake up. It could be that we are the only thread
670 * alerted to check for pending signals, but another thread
671 * should be woken now to take the signal since we will not.
672 * Now we'll wake all the threads in the group just to make
673 * sure someone gets all the pending signals.
675 read_lock(&tasklist_lock);
676 spin_lock_irq(&tsk->sighand->siglock);
677 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
678 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
679 recalc_sigpending_tsk(t);
680 if (signal_pending(t))
681 signal_wake_up(t, 0);
683 spin_unlock_irq(&tsk->sighand->siglock);
684 read_unlock(&tasklist_lock);
687 write_lock_irq(&tasklist_lock);
690 * This does two things:
692 * A. Make init inherit all the child processes
693 * B. Check to see if any process groups have become orphaned
694 * as a result of our exiting, and if they have any stopped
695 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
698 INIT_LIST_HEAD(&ptrace_dead);
699 forget_original_parent(tsk, &ptrace_dead);
700 BUG_ON(!list_empty(&tsk->children));
701 BUG_ON(!list_empty(&tsk->ptrace_children));
704 * Check to see if any process groups have become orphaned
705 * as a result of our exiting, and if they have any stopped
706 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
708 * Case i: Our father is in a different pgrp than we are
709 * and we were the only connection outside, so our pgrp
710 * is about to become orphaned.
713 t = tsk->real_parent;
715 if ((process_group(t) != process_group(tsk)) &&
716 (t->signal->session == tsk->signal->session) &&
717 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
718 has_stopped_jobs(process_group(tsk))) {
719 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
720 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
723 /* Let father know we died
725 * Thread signals are configurable, but you aren't going to use
726 * that to send signals to arbitary processes.
727 * That stops right now.
729 * If the parent exec id doesn't match the exec id we saved
730 * when we started then we know the parent has changed security
733 * If our self_exec id doesn't match our parent_exec_id then
734 * we have changed execution domain as these two values started
735 * the same after a fork.
739 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
740 ( tsk->parent_exec_id != t->self_exec_id ||
741 tsk->self_exec_id != tsk->parent_exec_id)
742 && !capable(CAP_KILL))
743 tsk->exit_signal = SIGCHLD;
746 /* If something other than our normal parent is ptracing us, then
747 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
748 * only has special meaning to our real parent.
750 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
751 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
752 do_notify_parent(tsk, signal);
753 } else if (tsk->ptrace) {
754 do_notify_parent(tsk, SIGCHLD);
758 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
761 tsk->flags |= PF_DEAD;
764 * Clear these here so that update_process_times() won't try to deliver
765 * itimer, profile or rlimit signals to this task while it is in late exit.
767 tsk->it_virt_value = 0;
768 tsk->it_prof_value = 0;
769 tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
772 * In the preemption case it must be impossible for the task
773 * to get runnable again, so use "_raw_" unlock to keep
774 * preempt_count elevated until we schedule().
776 * To avoid deadlock on SMP, interrupts must be unmasked. If we
777 * don't, subsequently called functions (e.g, wait_task_inactive()
778 * via release_task()) will spin, with interrupt flags
779 * unwittingly blocked, until the other task sleeps. That task
780 * may itself be waiting for smp_call_function() to answer and
781 * complete, and with interrupts blocked that will never happen.
783 _raw_write_unlock(&tasklist_lock);
786 list_for_each_safe(_p, _n, &ptrace_dead) {
788 t = list_entry(_p,struct task_struct,ptrace_list);
792 /* If the process is dead, release it - nobody will wait for it */
793 if (state == TASK_DEAD)
798 asmlinkage NORET_TYPE void do_exit(long code)
800 struct task_struct *tsk = current;
802 if (unlikely(in_interrupt()))
803 panic("Aiee, killing interrupt handler!");
804 if (unlikely(!tsk->pid))
805 panic("Attempted to kill the idle task!");
806 if (unlikely(tsk->pid == 1))
807 panic("Attempted to kill init!");
810 tsk->flags |= PF_EXITING;
811 del_timer_sync(&tsk->real_timer);
813 if (unlikely(in_atomic()))
814 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
815 current->comm, current->pid,
818 profile_exit_task(tsk);
820 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
821 current->ptrace_message = code;
822 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
834 if (tsk->signal->leader)
835 disassociate_ctty(1);
837 module_put(tsk->thread_info->exec_domain->module);
839 module_put(tsk->binfmt->module);
841 tsk->exit_code = code;
844 mpol_free(tsk->mempolicy);
845 tsk->mempolicy = NULL;
849 /* Avoid "noreturn function does return". */
853 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
861 EXPORT_SYMBOL(complete_and_exit);
863 asmlinkage long sys_exit(int error_code)
865 do_exit((error_code&0xff)<<8);
868 task_t fastcall *next_thread(const task_t *p)
870 const struct pid_link *link = p->pids + PIDTYPE_TGID;
871 const struct list_head *tmp, *head = &link->pidptr->task_list;
876 if (!spin_is_locked(&p->sighand->siglock) &&
877 !rwlock_is_locked(&tasklist_lock))
880 tmp = link->pid_chain.next;
884 return pid_task(tmp, PIDTYPE_TGID);
887 EXPORT_SYMBOL(next_thread);
890 * Take down every thread in the group. This is called by fatal signals
891 * as well as by sys_exit_group (below).
894 do_group_exit(int exit_code)
896 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
898 if (current->signal->group_exit)
899 exit_code = current->signal->group_exit_code;
900 else if (!thread_group_empty(current)) {
901 struct signal_struct *const sig = current->signal;
902 struct sighand_struct *const sighand = current->sighand;
903 read_lock(&tasklist_lock);
904 spin_lock_irq(&sighand->siglock);
906 /* Another thread got here before we took the lock. */
907 exit_code = sig->group_exit_code;
910 sig->group_exit_code = exit_code;
911 zap_other_threads(current);
913 spin_unlock_irq(&sighand->siglock);
914 read_unlock(&tasklist_lock);
922 * this kills every thread in the thread group. Note that any externally
923 * wait4()-ing process will get the correct exit code - even if this
924 * thread is not the thread group leader.
926 asmlinkage void sys_exit_group(int error_code)
928 do_group_exit((error_code & 0xff) << 8);
931 static int eligible_child(pid_t pid, int options, task_t *p)
937 if (process_group(p) != process_group(current))
939 } else if (pid != -1) {
940 if (process_group(p) != -pid)
945 * Do not consider detached threads that are
948 if (p->exit_signal == -1 && !p->ptrace)
951 /* Wait for all children (clone and not) if __WALL is set;
952 * otherwise, wait for clone children *only* if __WCLONE is
953 * set; otherwise, wait for non-clone children *only*. (Note:
954 * A "clone" child here is one that reports to its parent
955 * using a signal other than SIGCHLD.) */
956 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
957 && !(options & __WALL))
960 * Do not consider thread group leaders that are
961 * in a non-empty thread group:
963 if (current->tgid != p->tgid && delay_group_leader(p))
966 if (security_task_wait(p))
973 * Handle sys_wait4 work for one task in state TASK_ZOMBIE. We hold
974 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
975 * the lock and this task is uninteresting. If we return nonzero, we have
976 * released the lock and the system call should return.
978 static int wait_task_zombie(task_t *p, unsigned int __user *stat_addr, struct rusage __user *ru)
984 * Try to move the task's state to DEAD
985 * only one thread is allowed to do this:
987 state = xchg(&p->state, TASK_DEAD);
988 if (state != TASK_ZOMBIE) {
989 BUG_ON(state != TASK_DEAD);
992 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
994 * This can only happen in a race with a ptraced thread
995 * dying on another processor.
1000 * Now we are sure this task is interesting, and no other
1001 * thread can reap it because we set its state to TASK_DEAD.
1003 read_unlock(&tasklist_lock);
1005 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1006 if (!retval && stat_addr) {
1007 if (p->signal->group_exit)
1008 retval = put_user(p->signal->group_exit_code, stat_addr);
1010 retval = put_user(p->exit_code, stat_addr);
1013 p->state = TASK_ZOMBIE;
1017 if (p->real_parent != p->parent) {
1018 write_lock_irq(&tasklist_lock);
1019 /* Double-check with lock held. */
1020 if (p->real_parent != p->parent) {
1022 p->state = TASK_ZOMBIE;
1023 /* If this is a detached thread, this is where it goes away. */
1024 if (p->exit_signal == -1) {
1025 /* release_task takes the lock itself. */
1026 write_unlock_irq(&tasklist_lock);
1030 do_notify_parent(p, p->exit_signal);
1031 write_unlock_irq(&tasklist_lock);
1036 write_unlock_irq(&tasklist_lock);
1045 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1046 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1047 * the lock and this task is uninteresting. If we return nonzero, we have
1048 * released the lock and the system call should return.
1050 static int wait_task_stopped(task_t *p, int delayed_group_leader,
1051 unsigned int __user *stat_addr,
1052 struct rusage __user *ru)
1054 int retval, exit_code;
1058 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1059 p->signal && p->signal->group_stop_count > 0)
1061 * A group stop is in progress and this is the group leader.
1062 * We won't report until all threads have stopped.
1067 * Now we are pretty sure this task is interesting.
1068 * Make sure it doesn't get reaped out from under us while we
1069 * give up the lock and then examine it below. We don't want to
1070 * keep holding onto the tasklist_lock while we call getrusage and
1071 * possibly take page faults for user memory.
1074 read_unlock(&tasklist_lock);
1075 write_lock_irq(&tasklist_lock);
1078 * This uses xchg to be atomic with the thread resuming and setting
1079 * it. It must also be done with the write lock held to prevent a
1080 * race with the TASK_ZOMBIE case.
1082 exit_code = xchg(&p->exit_code, 0);
1083 if (unlikely(p->state > TASK_STOPPED)) {
1085 * The task resumed and then died. Let the next iteration
1086 * catch it in TASK_ZOMBIE. Note that exit_code might
1087 * already be zero here if it resumed and did _exit(0).
1088 * The task itself is dead and won't touch exit_code again;
1089 * other processors in this function are locked out.
1091 p->exit_code = exit_code;
1094 if (unlikely(exit_code == 0)) {
1096 * Another thread in this function got to it first, or it
1097 * resumed, or it resumed and then died.
1099 write_unlock_irq(&tasklist_lock);
1101 read_lock(&tasklist_lock);
1105 /* move to end of parent's list to avoid starvation */
1107 add_parent(p, p->parent);
1109 write_unlock_irq(&tasklist_lock);
1111 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1112 if (!retval && stat_addr)
1113 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1122 asmlinkage long sys_wait4(pid_t pid,unsigned int __user *stat_addr, int options, struct rusage __user *ru)
1124 DECLARE_WAITQUEUE(wait, current);
1125 struct task_struct *tsk;
1128 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
1131 add_wait_queue(¤t->wait_chldexit,&wait);
1134 current->state = TASK_INTERRUPTIBLE;
1135 read_lock(&tasklist_lock);
1138 struct task_struct *p;
1139 struct list_head *_p;
1142 list_for_each(_p,&tsk->children) {
1143 p = list_entry(_p,struct task_struct,sibling);
1145 ret = eligible_child(pid, options, p);
1152 if (!(options & WUNTRACED) &&
1153 !(p->ptrace & PT_PTRACED))
1155 retval = wait_task_stopped(p, ret == 2,
1157 if (retval != 0) /* He released the lock. */
1162 * Eligible but we cannot release it yet:
1166 retval = wait_task_zombie(p, stat_addr, ru);
1167 if (retval != 0) /* He released the lock. */
1173 list_for_each (_p,&tsk->ptrace_children) {
1174 p = list_entry(_p,struct task_struct,ptrace_list);
1175 if (!eligible_child(pid, options, p))
1181 if (options & __WNOTHREAD)
1183 tsk = next_thread(tsk);
1184 if (tsk->signal != current->signal)
1186 } while (tsk != current);
1187 read_unlock(&tasklist_lock);
1190 if (options & WNOHANG)
1192 retval = -ERESTARTSYS;
1193 if (signal_pending(current))
1200 current->state = TASK_RUNNING;
1201 remove_wait_queue(¤t->wait_chldexit,&wait);
1205 #ifdef __ARCH_WANT_SYS_WAITPID
1208 * sys_waitpid() remains for compatibility. waitpid() should be
1209 * implemented by calling sys_wait4() from libc.a.
1211 asmlinkage long sys_waitpid(pid_t pid, unsigned __user *stat_addr, int options)
1213 return sys_wait4(pid, stat_addr, options, NULL);