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/vs_limit.h>
28 #include <linux/ckrm.h>
29 #include <linux/ckrm_tsk.h>
31 #include <asm/uaccess.h>
32 #include <asm/unistd.h>
33 #include <asm/pgtable.h>
34 #include <asm/mmu_context.h>
36 extern void sem_exit (void);
37 extern struct task_struct *child_reaper;
39 int getrusage(struct task_struct *, int, struct rusage __user *);
41 static void __unhash_process(struct task_struct *p)
44 /* tasklist_lock is held, is this sufficient? */
46 atomic_dec(&p->vx_info->cacct.nr_threads);
47 vx_nproc_dec(p->vx_info);
48 // atomic_dec(&p->vx_info->limit.res[RLIMIT_NPROC]);
50 detach_pid(p, PIDTYPE_PID);
51 detach_pid(p, PIDTYPE_TGID);
52 if (thread_group_leader(p)) {
53 detach_pid(p, PIDTYPE_PGID);
54 detach_pid(p, PIDTYPE_SID);
56 __get_cpu_var(process_counts)--;
62 void release_task(struct task_struct * p)
66 struct dentry *proc_dentry;
69 BUG_ON(p->state < TASK_ZOMBIE);
71 atomic_dec(&p->user->processes);
72 spin_lock(&p->proc_lock);
73 proc_dentry = proc_pid_unhash(p);
74 write_lock_irq(&tasklist_lock);
75 if (unlikely(p->ptrace))
77 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
83 * If we are the last non-leader member of the thread
84 * group, and the leader is zombie, then notify the
85 * group leader's parent process. (if it wants notification.)
88 leader = p->group_leader;
89 if (leader != p && thread_group_empty(leader) && leader->state == TASK_ZOMBIE) {
90 BUG_ON(leader->exit_signal == -1);
91 do_notify_parent(leader, leader->exit_signal);
93 * If we were the last child thread and the leader has
94 * exited already, and the leader's parent ignores SIGCHLD,
95 * then we are the one who should release the leader.
97 * do_notify_parent() will have marked it self-reaping in
100 zap_leader = (leader->exit_signal == -1);
103 p->parent->cutime += p->utime + p->cutime;
104 p->parent->cstime += p->stime + p->cstime;
105 p->parent->cmin_flt += p->min_flt + p->cmin_flt;
106 p->parent->cmaj_flt += p->maj_flt + p->cmaj_flt;
107 p->parent->cnvcsw += p->nvcsw + p->cnvcsw;
108 p->parent->cnivcsw += p->nivcsw + p->cnivcsw;
110 write_unlock_irq(&tasklist_lock);
111 spin_unlock(&p->proc_lock);
112 proc_pid_flush(proc_dentry);
117 if (unlikely(zap_leader))
121 /* we are using it only for SMP init */
123 void unhash_process(struct task_struct *p)
125 struct dentry *proc_dentry;
127 spin_lock(&p->proc_lock);
128 proc_dentry = proc_pid_unhash(p);
129 write_lock_irq(&tasklist_lock);
131 write_unlock_irq(&tasklist_lock);
132 spin_unlock(&p->proc_lock);
133 proc_pid_flush(proc_dentry);
137 * This checks not only the pgrp, but falls back on the pid if no
138 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
141 int session_of_pgrp(int pgrp)
143 struct task_struct *p;
148 read_lock(&tasklist_lock);
149 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid)
150 if (p->signal->session > 0) {
151 sid = p->signal->session;
154 p = find_task_by_pid(pgrp);
156 sid = p->signal->session;
158 read_unlock(&tasklist_lock);
164 * Determine if a process group is "orphaned", according to the POSIX
165 * definition in 2.2.2.52. Orphaned process groups are not to be affected
166 * by terminal-generated stop signals. Newly orphaned process groups are
167 * to receive a SIGHUP and a SIGCONT.
169 * "I ask you, have you ever known what it is to be an orphan?"
171 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
173 struct task_struct *p;
178 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
179 if (p == ignored_task
180 || p->state >= TASK_ZOMBIE
181 || p->real_parent->pid == 1)
183 if (process_group(p->real_parent) != pgrp
184 && p->real_parent->signal->session == p->signal->session) {
189 return ret; /* (sighing) "Often!" */
192 int is_orphaned_pgrp(int pgrp)
196 read_lock(&tasklist_lock);
197 retval = will_become_orphaned_pgrp(pgrp, NULL);
198 read_unlock(&tasklist_lock);
203 static inline int has_stopped_jobs(int pgrp)
206 struct task_struct *p;
210 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
211 if (p->state != TASK_STOPPED)
214 /* If p is stopped by a debugger on a signal that won't
215 stop it, then don't count p as stopped. This isn't
216 perfect but it's a good approximation. */
217 if (unlikely (p->ptrace)
218 && p->exit_code != SIGSTOP
219 && p->exit_code != SIGTSTP
220 && p->exit_code != SIGTTOU
221 && p->exit_code != SIGTTIN)
231 * reparent_to_init() - Reparent the calling kernel thread to the init task.
233 * If a kernel thread is launched as a result of a system call, or if
234 * it ever exits, it should generally reparent itself to init so that
235 * it is correctly cleaned up on exit.
237 * The various task state such as scheduling policy and priority may have
238 * been inherited from a user process, so we reset them to sane values here.
240 * NOTE that reparent_to_init() gives the caller full capabilities.
242 void reparent_to_init(void)
244 write_lock_irq(&tasklist_lock);
246 ptrace_unlink(current);
247 /* Reparent to init */
248 REMOVE_LINKS(current);
249 /* FIXME handle vchild_reaper/initpid */
250 current->parent = child_reaper;
251 current->real_parent = child_reaper;
254 /* Set the exit signal to SIGCHLD so we signal init on exit */
255 current->exit_signal = SIGCHLD;
257 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
258 set_user_nice(current, 0);
262 security_task_reparent_to_init(current);
263 memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
264 atomic_inc(&(INIT_USER->__count));
265 switch_uid(INIT_USER);
267 write_unlock_irq(&tasklist_lock);
270 void __set_special_pids(pid_t session, pid_t pgrp)
272 struct task_struct *curr = current;
274 if (curr->signal->session != session) {
275 detach_pid(curr, PIDTYPE_SID);
276 curr->signal->session = session;
277 attach_pid(curr, PIDTYPE_SID, session);
279 if (process_group(curr) != pgrp) {
280 detach_pid(curr, PIDTYPE_PGID);
281 curr->signal->pgrp = pgrp;
282 attach_pid(curr, PIDTYPE_PGID, pgrp);
286 void set_special_pids(pid_t session, pid_t pgrp)
288 write_lock_irq(&tasklist_lock);
289 __set_special_pids(session, pgrp);
290 write_unlock_irq(&tasklist_lock);
294 * Let kernel threads use this to say that they
295 * allow a certain signal (since daemonize() will
296 * have disabled all of them by default).
298 int allow_signal(int sig)
300 if (sig < 1 || sig > _NSIG)
303 spin_lock_irq(¤t->sighand->siglock);
304 sigdelset(¤t->blocked, sig);
306 /* Kernel threads handle their own signals.
307 Let the signal code know it'll be handled, so
308 that they don't get converted to SIGKILL or
309 just silently dropped */
310 current->sighand->action[(sig)-1].sa.sa_handler = (void *)2;
313 spin_unlock_irq(¤t->sighand->siglock);
317 EXPORT_SYMBOL(allow_signal);
319 int disallow_signal(int sig)
321 if (sig < 1 || sig > _NSIG)
324 spin_lock_irq(¤t->sighand->siglock);
325 sigaddset(¤t->blocked, sig);
327 spin_unlock_irq(¤t->sighand->siglock);
331 EXPORT_SYMBOL(disallow_signal);
334 * Put all the gunge required to become a kernel thread without
335 * attached user resources in one place where it belongs.
338 void daemonize(const char *name, ...)
341 struct fs_struct *fs;
344 va_start(args, name);
345 vsnprintf(current->comm, sizeof(current->comm), name, args);
349 * If we were started as result of loading a module, close all of the
350 * user space pages. We don't need them, and if we didn't close them
351 * they would be locked into memory.
355 set_special_pids(1, 1);
356 current->signal->tty = NULL;
358 /* Block and flush all signals */
359 sigfillset(&blocked);
360 sigprocmask(SIG_BLOCK, &blocked, NULL);
361 flush_signals(current);
363 /* Become as one with the init task */
365 exit_fs(current); /* current->fs->count--; */
368 atomic_inc(&fs->count);
370 current->files = init_task.files;
371 atomic_inc(¤t->files->count);
376 EXPORT_SYMBOL(daemonize);
378 static inline void close_files(struct files_struct * files)
386 if (i >= files->max_fdset || i >= files->max_fds)
388 set = files->open_fds->fds_bits[j++];
391 struct file * file = xchg(&files->fd[i], NULL);
393 filp_close(file, files);
402 struct files_struct *get_files_struct(struct task_struct *task)
404 struct files_struct *files;
409 atomic_inc(&files->count);
415 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.
422 if (files->fd != &files->fd_array[0])
423 free_fd_array(files->fd, files->max_fds);
424 if (files->max_fdset > __FD_SETSIZE) {
425 free_fdset(files->open_fds, files->max_fdset);
426 free_fdset(files->close_on_exec, files->max_fdset);
428 kmem_cache_free(files_cachep, files);
432 EXPORT_SYMBOL(put_files_struct);
434 static inline void __exit_files(struct task_struct *tsk)
436 struct files_struct * files = tsk->files;
442 put_files_struct(files);
446 void exit_files(struct task_struct *tsk)
451 static inline void __put_fs_struct(struct fs_struct *fs)
453 /* No need to hold fs->lock if we are killing it */
454 if (atomic_dec_and_test(&fs->count)) {
461 mntput(fs->altrootmnt);
463 kmem_cache_free(fs_cachep, fs);
467 void put_fs_struct(struct fs_struct *fs)
472 static inline void __exit_fs(struct task_struct *tsk)
474 struct fs_struct * fs = tsk->fs;
484 void exit_fs(struct task_struct *tsk)
489 EXPORT_SYMBOL_GPL(exit_fs);
492 * Turn us into a lazy TLB process if we
495 static inline void __exit_mm(struct task_struct * tsk)
497 struct mm_struct *mm = tsk->mm;
503 * Serialize with any possible pending coredump.
504 * We must hold mmap_sem around checking core_waiters
505 * and clearing tsk->mm. The core-inducing thread
506 * will increment core_waiters for each thread in the
507 * group with ->mm != NULL.
509 down_read(&mm->mmap_sem);
510 if (mm->core_waiters) {
511 up_read(&mm->mmap_sem);
512 down_write(&mm->mmap_sem);
513 if (!--mm->core_waiters)
514 complete(mm->core_startup_done);
515 up_write(&mm->mmap_sem);
517 wait_for_completion(&mm->core_done);
518 down_read(&mm->mmap_sem);
520 atomic_inc(&mm->mm_count);
521 if (mm != tsk->active_mm) BUG();
522 /* more a memory barrier than a real lock */
525 up_read(&mm->mmap_sem);
526 enter_lazy_tlb(mm, current);
531 void exit_mm(struct task_struct *tsk)
536 EXPORT_SYMBOL(exit_mm);
538 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
541 * Make sure we're not reparenting to ourselves and that
542 * the parent is not a zombie.
544 if (p == reaper || reaper->state >= TASK_ZOMBIE)
545 p->real_parent = child_reaper;
547 p->real_parent = reaper;
548 if (p->parent == p->real_parent)
552 static inline void reparent_thread(task_t *p, task_t *father, int traced)
554 /* We don't want people slaying init. */
555 if (p->exit_signal != -1)
556 p->exit_signal = SIGCHLD;
559 if (p->pdeath_signal)
560 /* We already hold the tasklist_lock here. */
561 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
563 /* Move the child from its dying parent to the new one. */
564 if (unlikely(traced)) {
565 /* Preserve ptrace links if someone else is tracing this child. */
566 list_del_init(&p->ptrace_list);
567 if (p->parent != p->real_parent)
568 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
570 /* If this child is being traced, then we're the one tracing it
571 * anyway, so let go of it.
574 list_del_init(&p->sibling);
575 p->parent = p->real_parent;
576 list_add_tail(&p->sibling, &p->parent->children);
578 /* If we'd notified the old parent about this child's death,
579 * also notify the new parent.
581 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
582 thread_group_empty(p))
583 do_notify_parent(p, p->exit_signal);
587 * process group orphan check
588 * Case ii: Our child is in a different pgrp
589 * than we are, and it was the only connection
590 * outside, so the child pgrp is now orphaned.
592 if ((process_group(p) != process_group(father)) &&
593 (p->signal->session == father->signal->session)) {
594 int pgrp = process_group(p);
596 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
597 __kill_pg_info(SIGHUP, (void *)1, pgrp);
598 __kill_pg_info(SIGCONT, (void *)1, pgrp);
604 * When we die, we re-parent all our children.
605 * Try to give them to another thread in our thread
606 * group, and if no such member exists, give it to
607 * the global child reaper process (ie "init")
609 static inline void forget_original_parent(struct task_struct * father)
611 struct task_struct *p, *reaper = father;
612 struct list_head *_p, *_n;
614 /* FIXME handle vchild_reaper/initpid */
615 reaper = father->group_leader;
616 if (reaper == father)
617 reaper = child_reaper;
620 * There are only two places where our children can be:
622 * - in our child list
623 * - in our ptraced child list
625 * Search them and reparent children.
627 list_for_each_safe(_p, _n, &father->children) {
628 p = list_entry(_p,struct task_struct,sibling);
629 if (father == p->real_parent) {
630 choose_new_parent(p, reaper, child_reaper);
631 reparent_thread(p, father, 0);
634 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
635 thread_group_empty(p))
636 do_notify_parent(p, p->exit_signal);
639 list_for_each_safe(_p, _n, &father->ptrace_children) {
640 p = list_entry(_p,struct task_struct,ptrace_list);
641 choose_new_parent(p, reaper, child_reaper);
642 reparent_thread(p, father, 1);
647 * Send signals to all our closest relatives so that they know
648 * to properly mourn us..
650 static void exit_notify(struct task_struct *tsk)
653 struct task_struct *t;
657 if (signal_pending(tsk) && !tsk->signal->group_exit
658 && !thread_group_empty(tsk)) {
660 * This occurs when there was a race between our exit
661 * syscall and a group signal choosing us as the one to
662 * wake up. It could be that we are the only thread
663 * alerted to check for pending signals, but another thread
664 * should be woken now to take the signal since we will not.
665 * Now we'll wake all the threads in the group just to make
666 * sure someone gets all the pending signals.
668 read_lock(&tasklist_lock);
669 spin_lock_irq(&tsk->sighand->siglock);
670 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
671 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
672 recalc_sigpending_tsk(t);
673 if (signal_pending(t))
674 signal_wake_up(t, 0);
676 spin_unlock_irq(&tsk->sighand->siglock);
677 read_unlock(&tasklist_lock);
680 write_lock_irq(&tasklist_lock);
683 * This does two things:
685 * A. Make init inherit all the child processes
686 * B. Check to see if any process groups have become orphaned
687 * as a result of our exiting, and if they have any stopped
688 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
691 forget_original_parent(tsk);
692 BUG_ON(!list_empty(&tsk->children));
695 * Check to see if any process groups have become orphaned
696 * as a result of our exiting, and if they have any stopped
697 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
699 * Case i: Our father is in a different pgrp than we are
700 * and we were the only connection outside, so our pgrp
701 * is about to become orphaned.
704 t = tsk->real_parent;
706 if ((process_group(t) != process_group(tsk)) &&
707 (t->signal->session == tsk->signal->session) &&
708 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
709 has_stopped_jobs(process_group(tsk))) {
710 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
711 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
714 /* Let father know we died
716 * Thread signals are configurable, but you aren't going to use
717 * that to send signals to arbitary processes.
718 * That stops right now.
720 * If the parent exec id doesn't match the exec id we saved
721 * when we started then we know the parent has changed security
724 * If our self_exec id doesn't match our parent_exec_id then
725 * we have changed execution domain as these two values started
726 * the same after a fork.
730 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
731 ( tsk->parent_exec_id != t->self_exec_id ||
732 tsk->self_exec_id != tsk->parent_exec_id)
733 && !capable(CAP_KILL))
734 tsk->exit_signal = SIGCHLD;
737 /* If something other than our normal parent is ptracing us, then
738 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
739 * only has special meaning to our real parent.
741 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
742 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
743 do_notify_parent(tsk, signal);
744 } else if (tsk->ptrace) {
745 do_notify_parent(tsk, SIGCHLD);
749 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
752 tsk->flags |= PF_DEAD;
755 * Clear these here so that update_process_times() won't try to deliver
756 * itimer, profile or rlimit signals to this task while it is in late exit.
758 tsk->it_virt_value = 0;
759 tsk->it_prof_value = 0;
760 tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
763 * In the preemption case it must be impossible for the task
764 * to get runnable again, so use "_raw_" unlock to keep
765 * preempt_count elevated until we schedule().
767 * To avoid deadlock on SMP, interrupts must be unmasked. If we
768 * don't, subsequently called functions (e.g, wait_task_inactive()
769 * via release_task()) will spin, with interrupt flags
770 * unwittingly blocked, until the other task sleeps. That task
771 * may itself be waiting for smp_call_function() to answer and
772 * complete, and with interrupts blocked that will never happen.
774 _raw_write_unlock(&tasklist_lock);
777 /* If the process is dead, release it - nobody will wait for it */
778 if (state == TASK_DEAD)
783 asmlinkage NORET_TYPE void do_exit(long code)
785 struct task_struct *tsk = current;
787 if (unlikely(in_interrupt()))
788 panic("Aiee, killing interrupt handler!");
789 if (unlikely(!tsk->pid))
790 panic("Attempted to kill the idle task!");
791 if (unlikely(tsk->pid == 1))
792 panic("Attempted to kill init!");
795 tsk->flags |= PF_EXITING;
796 del_timer_sync(&tsk->real_timer);
798 if (unlikely(in_atomic()))
799 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
800 current->comm, current->pid,
803 profile_exit_task(tsk);
805 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
806 current->ptrace_message = code;
807 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
811 if (current->tux_info) {
812 #ifdef CONFIG_TUX_DEBUG
813 printk("Possibly unexpected TUX-thread exit(%ld) at %p?\n",
814 code, __builtin_return_address(0));
826 mpol_free(tsk->mempolicy);
829 if (tsk->signal->leader)
830 disassociate_ctty(1);
832 module_put(tsk->thread_info->exec_domain->module);
834 module_put(tsk->binfmt->module);
836 tsk->exit_code = code;
837 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
838 numtasks_put_ref(tsk->taskclass);
843 /* Avoid "noreturn function does return". */
847 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
855 EXPORT_SYMBOL(complete_and_exit);
857 asmlinkage long sys_exit(int error_code)
859 do_exit((error_code&0xff)<<8);
862 task_t fastcall *next_thread(const task_t *p)
864 const struct pid_link *link = p->pids + PIDTYPE_TGID;
865 const struct list_head *tmp, *head = &link->pidptr->task_list;
870 if (!spin_is_locked(&p->sighand->siglock) &&
871 !rwlock_is_locked(&tasklist_lock))
874 tmp = link->pid_chain.next;
878 return pid_task(tmp, PIDTYPE_TGID);
881 EXPORT_SYMBOL(next_thread);
884 * Take down every thread in the group. This is called by fatal signals
885 * as well as by sys_exit_group (below).
888 do_group_exit(int exit_code)
890 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
892 if (current->signal->group_exit)
893 exit_code = current->signal->group_exit_code;
894 else if (!thread_group_empty(current)) {
895 struct signal_struct *const sig = current->signal;
896 struct sighand_struct *const sighand = current->sighand;
897 read_lock(&tasklist_lock);
898 spin_lock_irq(&sighand->siglock);
900 /* Another thread got here before we took the lock. */
901 exit_code = sig->group_exit_code;
904 sig->group_exit_code = exit_code;
905 zap_other_threads(current);
907 spin_unlock_irq(&sighand->siglock);
908 read_unlock(&tasklist_lock);
916 * this kills every thread in the thread group. Note that any externally
917 * wait4()-ing process will get the correct exit code - even if this
918 * thread is not the thread group leader.
920 asmlinkage void sys_exit_group(int error_code)
922 do_group_exit((error_code & 0xff) << 8);
925 static int eligible_child(pid_t pid, int options, task_t *p)
931 if (process_group(p) != process_group(current))
933 } else if (pid != -1) {
934 if (process_group(p) != -pid)
939 * Do not consider detached threads that are
942 if (p->exit_signal == -1 && !p->ptrace)
945 /* Wait for all children (clone and not) if __WALL is set;
946 * otherwise, wait for clone children *only* if __WCLONE is
947 * set; otherwise, wait for non-clone children *only*. (Note:
948 * A "clone" child here is one that reports to its parent
949 * using a signal other than SIGCHLD.) */
950 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
951 && !(options & __WALL))
954 * Do not consider thread group leaders that are
955 * in a non-empty thread group:
957 if (current->tgid != p->tgid && delay_group_leader(p))
960 if (security_task_wait(p))
967 * Handle sys_wait4 work for one task in state TASK_ZOMBIE. We hold
968 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
969 * the lock and this task is uninteresting. If we return nonzero, we have
970 * released the lock and the system call should return.
972 static int wait_task_zombie(task_t *p, unsigned int __user *stat_addr, struct rusage __user *ru)
978 * Try to move the task's state to DEAD
979 * only one thread is allowed to do this:
981 state = xchg(&p->state, TASK_DEAD);
982 if (state != TASK_ZOMBIE) {
983 BUG_ON(state != TASK_DEAD);
986 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
988 * This can only happen in a race with a ptraced thread
989 * dying on another processor.
994 * Now we are sure this task is interesting, and no other
995 * thread can reap it because we set its state to TASK_DEAD.
997 read_unlock(&tasklist_lock);
999 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1000 if (!retval && stat_addr) {
1001 if (p->signal->group_exit)
1002 retval = put_user(p->signal->group_exit_code, stat_addr);
1004 retval = put_user(p->exit_code, stat_addr);
1007 p->state = TASK_ZOMBIE;
1011 if (p->real_parent != p->parent) {
1012 write_lock_irq(&tasklist_lock);
1013 /* Double-check with lock held. */
1014 if (p->real_parent != p->parent) {
1016 p->state = TASK_ZOMBIE;
1017 /* If this is a detached thread, this is where it goes away. */
1018 if (p->exit_signal == -1) {
1019 /* release_task takes the lock itself. */
1020 write_unlock_irq(&tasklist_lock);
1024 do_notify_parent(p, p->exit_signal);
1025 write_unlock_irq(&tasklist_lock);
1030 write_unlock_irq(&tasklist_lock);
1039 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1040 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1041 * the lock and this task is uninteresting. If we return nonzero, we have
1042 * released the lock and the system call should return.
1044 static int wait_task_stopped(task_t *p, int delayed_group_leader,
1045 unsigned int __user *stat_addr,
1046 struct rusage __user *ru)
1048 int retval, exit_code;
1052 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1053 p->signal && p->signal->group_stop_count > 0)
1055 * A group stop is in progress and this is the group leader.
1056 * We won't report until all threads have stopped.
1061 * Now we are pretty sure this task is interesting.
1062 * Make sure it doesn't get reaped out from under us while we
1063 * give up the lock and then examine it below. We don't want to
1064 * keep holding onto the tasklist_lock while we call getrusage and
1065 * possibly take page faults for user memory.
1068 read_unlock(&tasklist_lock);
1069 write_lock_irq(&tasklist_lock);
1072 * This uses xchg to be atomic with the thread resuming and setting
1073 * it. It must also be done with the write lock held to prevent a
1074 * race with the TASK_ZOMBIE case.
1076 exit_code = xchg(&p->exit_code, 0);
1077 if (unlikely(p->state > TASK_STOPPED)) {
1079 * The task resumed and then died. Let the next iteration
1080 * catch it in TASK_ZOMBIE. Note that exit_code might
1081 * already be zero here if it resumed and did _exit(0).
1082 * The task itself is dead and won't touch exit_code again;
1083 * other processors in this function are locked out.
1085 p->exit_code = exit_code;
1088 if (unlikely(exit_code == 0)) {
1090 * Another thread in this function got to it first, or it
1091 * resumed, or it resumed and then died.
1093 write_unlock_irq(&tasklist_lock);
1095 read_lock(&tasklist_lock);
1099 /* move to end of parent's list to avoid starvation */
1101 add_parent(p, p->parent);
1103 write_unlock_irq(&tasklist_lock);
1105 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1106 if (!retval && stat_addr)
1107 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1116 asmlinkage long sys_wait4(pid_t pid,unsigned int __user *stat_addr, int options, struct rusage __user *ru)
1118 DECLARE_WAITQUEUE(wait, current);
1119 struct task_struct *tsk;
1122 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
1125 add_wait_queue(¤t->wait_chldexit,&wait);
1128 current->state = TASK_INTERRUPTIBLE;
1129 read_lock(&tasklist_lock);
1132 struct task_struct *p;
1133 struct list_head *_p;
1136 list_for_each(_p,&tsk->children) {
1137 p = list_entry(_p,struct task_struct,sibling);
1139 ret = eligible_child(pid, options, p);
1146 if (!(options & WUNTRACED) &&
1147 !(p->ptrace & PT_PTRACED))
1149 retval = wait_task_stopped(p, ret == 2,
1151 if (retval != 0) /* He released the lock. */
1156 * Eligible but we cannot release it yet:
1160 retval = wait_task_zombie(p, stat_addr, ru);
1161 if (retval != 0) /* He released the lock. */
1167 list_for_each (_p,&tsk->ptrace_children) {
1168 p = list_entry(_p,struct task_struct,ptrace_list);
1169 if (!eligible_child(pid, options, p))
1175 if (options & __WNOTHREAD)
1177 tsk = next_thread(tsk);
1178 if (tsk->signal != current->signal)
1180 } while (tsk != current);
1181 read_unlock(&tasklist_lock);
1184 if (options & WNOHANG)
1186 retval = -ERESTARTSYS;
1187 if (signal_pending(current))
1194 current->state = TASK_RUNNING;
1195 remove_wait_queue(¤t->wait_chldexit,&wait);
1199 #ifdef __ARCH_WANT_SYS_WAITPID
1202 * sys_waitpid() remains for compatibility. waitpid() should be
1203 * implemented by calling sys_wait4() from libc.a.
1205 asmlinkage long sys_waitpid(pid_t pid, unsigned __user *stat_addr, int options)
1207 return sys_wait4(pid, stat_addr, options, NULL);