More unused junk.
[linux-2.6/linux-mips.git] / kernel / exit.c
blobc5b8ec241a83c3fc908ad3b2de1c3c021cf96412
1 /*
2 * linux/kernel/exit.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/mm.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>
26 #include <asm/uaccess.h>
27 #include <asm/pgtable.h>
28 #include <asm/mmu_context.h>
30 extern void sem_exit (void);
31 extern struct task_struct *child_reaper;
33 int getrusage(struct task_struct *, int, struct rusage *);
35 static void __unhash_process(struct task_struct *p)
37 nr_threads--;
38 detach_pid(p, PIDTYPE_PID);
39 detach_pid(p, PIDTYPE_TGID);
40 if (thread_group_leader(p)) {
41 detach_pid(p, PIDTYPE_PGID);
42 detach_pid(p, PIDTYPE_SID);
43 if (p->pid)
44 per_cpu(process_counts, smp_processor_id())--;
47 REMOVE_LINKS(p);
50 void release_task(struct task_struct * p)
52 task_t *leader;
53 struct dentry *proc_dentry;
55 BUG_ON(p->state < TASK_ZOMBIE);
57 atomic_dec(&p->user->processes);
58 spin_lock(&p->proc_lock);
59 proc_dentry = proc_pid_unhash(p);
60 write_lock_irq(&tasklist_lock);
61 if (unlikely(p->ptrace))
62 __ptrace_unlink(p);
63 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
64 __exit_signal(p);
65 __exit_sighand(p);
66 __unhash_process(p);
69 * If we are the last non-leader member of the thread
70 * group, and the leader is zombie, then notify the
71 * group leader's parent process. (if it wants notification.)
73 leader = p->group_leader;
74 if (leader != p && thread_group_empty(leader) &&
75 leader->state == TASK_ZOMBIE && leader->exit_signal != -1)
76 do_notify_parent(leader, leader->exit_signal);
78 p->parent->cutime += p->utime + p->cutime;
79 p->parent->cstime += p->stime + p->cstime;
80 p->parent->cmin_flt += p->min_flt + p->cmin_flt;
81 p->parent->cmaj_flt += p->maj_flt + p->cmaj_flt;
82 p->parent->cnswap += p->nswap + p->cnswap;
83 sched_exit(p);
84 write_unlock_irq(&tasklist_lock);
85 spin_unlock(&p->proc_lock);
86 proc_pid_flush(proc_dentry);
87 release_thread(p);
88 put_task_struct(p);
91 /* we are using it only for SMP init */
93 void unhash_process(struct task_struct *p)
95 struct dentry *proc_dentry;
97 spin_lock(&p->proc_lock);
98 proc_dentry = proc_pid_unhash(p);
99 write_lock_irq(&tasklist_lock);
100 __unhash_process(p);
101 write_unlock_irq(&tasklist_lock);
102 spin_unlock(&p->proc_lock);
103 proc_pid_flush(proc_dentry);
107 * This checks not only the pgrp, but falls back on the pid if no
108 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
109 * without this...
111 int session_of_pgrp(int pgrp)
113 struct task_struct *p;
114 struct list_head *l;
115 struct pid *pid;
116 int sid = -1;
118 read_lock(&tasklist_lock);
119 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid)
120 if (p->session > 0) {
121 sid = p->session;
122 goto out;
124 p = find_task_by_pid(pgrp);
125 if (p)
126 sid = p->session;
127 out:
128 read_unlock(&tasklist_lock);
130 return sid;
134 * Determine if a process group is "orphaned", according to the POSIX
135 * definition in 2.2.2.52. Orphaned process groups are not to be affected
136 * by terminal-generated stop signals. Newly orphaned process groups are
137 * to receive a SIGHUP and a SIGCONT.
139 * "I ask you, have you ever known what it is to be an orphan?"
141 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
143 struct task_struct *p;
144 struct list_head *l;
145 struct pid *pid;
146 int ret = 1;
148 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
149 if (p == ignored_task
150 || p->state >= TASK_ZOMBIE
151 || p->real_parent->pid == 1)
152 continue;
153 if (p->real_parent->pgrp != pgrp
154 && p->real_parent->session == p->session) {
155 ret = 0;
156 break;
159 return ret; /* (sighing) "Often!" */
162 int is_orphaned_pgrp(int pgrp)
164 int retval;
166 read_lock(&tasklist_lock);
167 retval = will_become_orphaned_pgrp(pgrp, NULL);
168 read_unlock(&tasklist_lock);
170 return retval;
173 static inline int has_stopped_jobs(int pgrp)
175 int retval = 0;
176 struct task_struct *p;
177 struct list_head *l;
178 struct pid *pid;
180 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
181 if (p->state != TASK_STOPPED)
182 continue;
184 /* If p is stopped by a debugger on a signal that won't
185 stop it, then don't count p as stopped. This isn't
186 perfect but it's a good approximation. */
187 if (unlikely (p->ptrace)
188 && p->exit_code != SIGSTOP
189 && p->exit_code != SIGTSTP
190 && p->exit_code != SIGTTOU
191 && p->exit_code != SIGTTIN)
192 continue;
194 retval = 1;
195 break;
197 return retval;
201 * reparent_to_init() - Reparent the calling kernel thread to the init task.
203 * If a kernel thread is launched as a result of a system call, or if
204 * it ever exits, it should generally reparent itself to init so that
205 * it is correctly cleaned up on exit.
207 * The various task state such as scheduling policy and priority may have
208 * been inherited from a user process, so we reset them to sane values here.
210 * NOTE that reparent_to_init() gives the caller full capabilities.
212 void reparent_to_init(void)
214 write_lock_irq(&tasklist_lock);
216 ptrace_unlink(current);
217 /* Reparent to init */
218 REMOVE_LINKS(current);
219 current->parent = child_reaper;
220 current->real_parent = child_reaper;
221 SET_LINKS(current);
223 /* Set the exit signal to SIGCHLD so we signal init on exit */
224 current->exit_signal = SIGCHLD;
226 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
227 set_user_nice(current, 0);
228 /* cpus_allowed? */
229 /* rt_priority? */
230 /* signals? */
231 security_task_reparent_to_init(current);
232 memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
233 switch_uid(INIT_USER);
235 write_unlock_irq(&tasklist_lock);
238 void __set_special_pids(pid_t session, pid_t pgrp)
240 struct task_struct *curr = current;
242 if (curr->session != session) {
243 detach_pid(curr, PIDTYPE_SID);
244 curr->session = session;
245 attach_pid(curr, PIDTYPE_SID, session);
247 if (curr->pgrp != pgrp) {
248 detach_pid(curr, PIDTYPE_PGID);
249 curr->pgrp = pgrp;
250 attach_pid(curr, PIDTYPE_PGID, pgrp);
254 void set_special_pids(pid_t session, pid_t pgrp)
256 write_lock_irq(&tasklist_lock);
257 __set_special_pids(session, pgrp);
258 write_unlock_irq(&tasklist_lock);
262 * Let kernel threads use this to say that they
263 * allow a certain signal (since daemonize() will
264 * have disabled all of them by default).
266 int allow_signal(int sig)
268 if (sig < 1 || sig > _NSIG)
269 return -EINVAL;
271 spin_lock_irq(&current->sighand->siglock);
272 sigdelset(&current->blocked, sig);
273 recalc_sigpending();
274 spin_unlock_irq(&current->sighand->siglock);
275 return 0;
278 EXPORT_SYMBOL(allow_signal);
281 * Put all the gunge required to become a kernel thread without
282 * attached user resources in one place where it belongs.
285 void daemonize(const char *name, ...)
287 va_list args;
288 struct fs_struct *fs;
289 sigset_t blocked;
291 va_start(args, name);
292 vsnprintf(current->comm, sizeof(current->comm), name, args);
293 va_end(args);
296 * If we were started as result of loading a module, close all of the
297 * user space pages. We don't need them, and if we didn't close them
298 * they would be locked into memory.
300 exit_mm(current);
302 set_special_pids(1, 1);
303 current->tty = NULL;
305 /* Block and flush all signals */
306 sigfillset(&blocked);
307 sigprocmask(SIG_BLOCK, &blocked, NULL);
308 flush_signals(current);
310 /* Become as one with the init task */
312 exit_fs(current); /* current->fs->count--; */
313 fs = init_task.fs;
314 current->fs = fs;
315 atomic_inc(&fs->count);
316 exit_files(current);
317 current->files = init_task.files;
318 atomic_inc(&current->files->count);
320 reparent_to_init();
323 static inline void close_files(struct files_struct * files)
325 int i, j;
327 j = 0;
328 for (;;) {
329 unsigned long set;
330 i = j * __NFDBITS;
331 if (i >= files->max_fdset || i >= files->max_fds)
332 break;
333 set = files->open_fds->fds_bits[j++];
334 while (set) {
335 if (set & 1) {
336 struct file * file = xchg(&files->fd[i], NULL);
337 if (file)
338 filp_close(file, files);
340 i++;
341 set >>= 1;
346 void put_files_struct(struct files_struct *files)
348 if (atomic_dec_and_test(&files->count)) {
349 close_files(files);
351 * Free the fd and fdset arrays if we expanded them.
353 if (files->fd != &files->fd_array[0])
354 free_fd_array(files->fd, files->max_fds);
355 if (files->max_fdset > __FD_SETSIZE) {
356 free_fdset(files->open_fds, files->max_fdset);
357 free_fdset(files->close_on_exec, files->max_fdset);
359 kmem_cache_free(files_cachep, files);
363 static inline void __exit_files(struct task_struct *tsk)
365 struct files_struct * files = tsk->files;
367 if (files) {
368 task_lock(tsk);
369 tsk->files = NULL;
370 task_unlock(tsk);
371 put_files_struct(files);
375 void exit_files(struct task_struct *tsk)
377 __exit_files(tsk);
380 static inline void __put_fs_struct(struct fs_struct *fs)
382 /* No need to hold fs->lock if we are killing it */
383 if (atomic_dec_and_test(&fs->count)) {
384 dput(fs->root);
385 mntput(fs->rootmnt);
386 dput(fs->pwd);
387 mntput(fs->pwdmnt);
388 if (fs->altroot) {
389 dput(fs->altroot);
390 mntput(fs->altrootmnt);
392 kmem_cache_free(fs_cachep, fs);
396 void put_fs_struct(struct fs_struct *fs)
398 __put_fs_struct(fs);
401 static inline void __exit_fs(struct task_struct *tsk)
403 struct fs_struct * fs = tsk->fs;
405 if (fs) {
406 task_lock(tsk);
407 tsk->fs = NULL;
408 task_unlock(tsk);
409 __put_fs_struct(fs);
413 void exit_fs(struct task_struct *tsk)
415 __exit_fs(tsk);
419 * Turn us into a lazy TLB process if we
420 * aren't already..
422 static inline void __exit_mm(struct task_struct * tsk)
424 struct mm_struct *mm = tsk->mm;
426 mm_release(tsk, mm);
427 if (!mm)
428 return;
430 * Serialize with any possible pending coredump:
432 if (mm->core_waiters) {
433 down_write(&mm->mmap_sem);
434 if (!--mm->core_waiters)
435 complete(mm->core_startup_done);
436 up_write(&mm->mmap_sem);
438 wait_for_completion(&mm->core_done);
440 atomic_inc(&mm->mm_count);
441 if (mm != tsk->active_mm) BUG();
442 /* more a memory barrier than a real lock */
443 task_lock(tsk);
444 tsk->mm = NULL;
445 enter_lazy_tlb(mm, current, smp_processor_id());
446 task_unlock(tsk);
447 mmput(mm);
450 void exit_mm(struct task_struct *tsk)
452 __exit_mm(tsk);
455 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
458 * Make sure we're not reparenting to ourselves and that
459 * the parent is not a zombie.
461 if (p == reaper || reaper->state >= TASK_ZOMBIE)
462 p->real_parent = child_reaper;
463 else
464 p->real_parent = reaper;
465 if (p->parent == p->real_parent)
466 BUG();
469 static inline void reparent_thread(task_t *p, task_t *father, int traced)
471 /* We don't want people slaying init. */
472 if (p->exit_signal != -1)
473 p->exit_signal = SIGCHLD;
474 p->self_exec_id++;
476 if (p->pdeath_signal)
477 send_group_sig_info(p->pdeath_signal, 0, p);
479 /* Move the child from its dying parent to the new one. */
480 if (unlikely(traced)) {
481 /* Preserve ptrace links if someone else is tracing this child. */
482 list_del_init(&p->ptrace_list);
483 if (p->parent != p->real_parent)
484 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
485 } else {
486 /* If this child is being traced, then we're the one tracing it
487 * anyway, so let go of it.
489 p->ptrace = 0;
490 list_del_init(&p->sibling);
491 p->parent = p->real_parent;
492 list_add_tail(&p->sibling, &p->parent->children);
494 /* If we'd notified the old parent about this child's death,
495 * also notify the new parent.
497 if (p->state == TASK_ZOMBIE && p->exit_signal != -1)
498 do_notify_parent(p, p->exit_signal);
502 * process group orphan check
503 * Case ii: Our child is in a different pgrp
504 * than we are, and it was the only connection
505 * outside, so the child pgrp is now orphaned.
507 if ((p->pgrp != father->pgrp) &&
508 (p->session == father->session)) {
509 int pgrp = p->pgrp;
511 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
512 __kill_pg_info(SIGHUP, (void *)1, pgrp);
513 __kill_pg_info(SIGCONT, (void *)1, pgrp);
519 * When we die, we re-parent all our children.
520 * Try to give them to another thread in our thread
521 * group, and if no such member exists, give it to
522 * the global child reaper process (ie "init")
524 static inline void forget_original_parent(struct task_struct * father)
526 struct task_struct *p, *reaper = father;
527 struct list_head *_p, *_n;
529 reaper = father->group_leader;
530 if (reaper == father)
531 reaper = child_reaper;
534 * There are only two places where our children can be:
536 * - in our child list
537 * - in our ptraced child list
539 * Search them and reparent children.
541 list_for_each_safe(_p, _n, &father->children) {
542 p = list_entry(_p,struct task_struct,sibling);
543 if (father == p->real_parent) {
544 choose_new_parent(p, reaper, child_reaper);
545 reparent_thread(p, father, 0);
546 } else {
547 ptrace_unlink (p);
548 if (p->state == TASK_ZOMBIE && p->exit_signal != -1)
549 do_notify_parent(p, p->exit_signal);
552 list_for_each_safe(_p, _n, &father->ptrace_children) {
553 p = list_entry(_p,struct task_struct,ptrace_list);
554 choose_new_parent(p, reaper, child_reaper);
555 reparent_thread(p, father, 1);
560 * Send signals to all our closest relatives so that they know
561 * to properly mourn us..
563 static void exit_notify(struct task_struct *tsk)
565 struct task_struct *t;
567 if (signal_pending(tsk) && !tsk->signal->group_exit
568 && !thread_group_empty(tsk)) {
570 * This occurs when there was a race between our exit
571 * syscall and a group signal choosing us as the one to
572 * wake up. It could be that we are the only thread
573 * alerted to check for pending signals, but another thread
574 * should be woken now to take the signal since we will not.
575 * Now we'll wake all the threads in the group just to make
576 * sure someone gets all the pending signals.
578 read_lock(&tasklist_lock);
579 spin_lock_irq(&tsk->sighand->siglock);
580 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
581 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
582 recalc_sigpending_tsk(t);
583 if (signal_pending(t))
584 signal_wake_up(t, 0);
586 spin_unlock_irq(&tsk->sighand->siglock);
587 read_unlock(&tasklist_lock);
590 write_lock_irq(&tasklist_lock);
593 * This does two things:
595 * A. Make init inherit all the child processes
596 * B. Check to see if any process groups have become orphaned
597 * as a result of our exiting, and if they have any stopped
598 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
601 forget_original_parent(tsk);
602 BUG_ON(!list_empty(&tsk->children));
605 * Check to see if any process groups have become orphaned
606 * as a result of our exiting, and if they have any stopped
607 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
609 * Case i: Our father is in a different pgrp than we are
610 * and we were the only connection outside, so our pgrp
611 * is about to become orphaned.
614 t = tsk->real_parent;
616 if ((t->pgrp != tsk->pgrp) &&
617 (t->session == tsk->session) &&
618 will_become_orphaned_pgrp(tsk->pgrp, tsk) &&
619 has_stopped_jobs(tsk->pgrp)) {
620 __kill_pg_info(SIGHUP, (void *)1, tsk->pgrp);
621 __kill_pg_info(SIGCONT, (void *)1, tsk->pgrp);
624 /* Let father know we died
626 * Thread signals are configurable, but you aren't going to use
627 * that to send signals to arbitary processes.
628 * That stops right now.
630 * If the parent exec id doesn't match the exec id we saved
631 * when we started then we know the parent has changed security
632 * domain.
634 * If our self_exec id doesn't match our parent_exec_id then
635 * we have changed execution domain as these two values started
636 * the same after a fork.
640 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
641 ( tsk->parent_exec_id != t->self_exec_id ||
642 tsk->self_exec_id != tsk->parent_exec_id)
643 && !capable(CAP_KILL))
644 tsk->exit_signal = SIGCHLD;
647 /* If something other than our normal parent is ptracing us, then
648 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
649 * only has special meaning to our real parent.
651 if (tsk->exit_signal != -1) {
652 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
653 do_notify_parent(tsk, signal);
656 tsk->state = TASK_ZOMBIE;
658 * In the preemption case it must be impossible for the task
659 * to get runnable again, so use "_raw_" unlock to keep
660 * preempt_count elevated until we schedule().
662 * To avoid deadlock on SMP, interrupts must be unmasked. If we
663 * don't, subsequently called functions (e.g, wait_task_inactive()
664 * via release_task()) will spin, with interrupt flags
665 * unwittingly blocked, until the other task sleeps. That task
666 * may itself be waiting for smp_call_function() to answer and
667 * complete, and with interrupts blocked that will never happen.
669 _raw_write_unlock(&tasklist_lock);
670 local_irq_enable();
673 NORET_TYPE void do_exit(long code)
675 struct task_struct *tsk = current;
677 if (unlikely(in_interrupt()))
678 panic("Aiee, killing interrupt handler!");
679 if (unlikely(!tsk->pid))
680 panic("Attempted to kill the idle task!");
681 if (unlikely(tsk->pid == 1))
682 panic("Attempted to kill init!");
683 tsk->flags |= PF_EXITING;
684 del_timer_sync(&tsk->real_timer);
686 if (unlikely(in_atomic()))
687 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
688 current->comm, current->pid,
689 preempt_count());
691 profile_exit_task(tsk);
693 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
694 current->ptrace_message = code;
695 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
698 acct_process(code);
699 __exit_mm(tsk);
701 sem_exit();
702 __exit_files(tsk);
703 __exit_fs(tsk);
704 exit_namespace(tsk);
705 exit_itimers(tsk);
706 exit_thread();
708 if (tsk->leader)
709 disassociate_ctty(1);
711 module_put(tsk->thread_info->exec_domain->module);
712 if (tsk->binfmt)
713 module_put(tsk->binfmt->module);
715 tsk->exit_code = code;
716 exit_notify(tsk);
718 if (tsk->exit_signal == -1)
719 release_task(tsk);
721 schedule();
722 BUG();
723 /* Avoid "noreturn function does return". */
724 for (;;) ;
727 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
729 if (comp)
730 complete(comp);
732 do_exit(code);
735 asmlinkage long sys_exit(int error_code)
737 do_exit((error_code&0xff)<<8);
740 task_t *next_thread(task_t *p)
742 struct pid_link *link = p->pids + PIDTYPE_TGID;
743 struct list_head *tmp, *head = &link->pidptr->task_list;
745 #ifdef CONFIG_SMP
746 if (!p->sighand)
747 BUG();
748 if (!spin_is_locked(&p->sighand->siglock) &&
749 !rwlock_is_locked(&tasklist_lock))
750 BUG();
751 #endif
752 tmp = link->pid_chain.next;
753 if (tmp == head)
754 tmp = head->next;
756 return pid_task(tmp, PIDTYPE_TGID);
760 * Take down every thread in the group. This is called by fatal signals
761 * as well as by sys_exit_group (below).
763 NORET_TYPE void
764 do_group_exit(int exit_code)
766 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
768 if (current->signal->group_exit)
769 exit_code = current->signal->group_exit_code;
770 else if (!thread_group_empty(current)) {
771 struct signal_struct *const sig = current->signal;
772 struct sighand_struct *const sighand = current->sighand;
773 read_lock(&tasklist_lock);
774 spin_lock_irq(&sighand->siglock);
775 if (sig->group_exit)
776 /* Another thread got here before we took the lock. */
777 exit_code = sig->group_exit_code;
778 else {
779 sig->group_exit = 1;
780 sig->group_exit_code = exit_code;
781 zap_other_threads(current);
783 spin_unlock_irq(&sighand->siglock);
784 read_unlock(&tasklist_lock);
787 do_exit(exit_code);
788 /* NOTREACHED */
792 * this kills every thread in the thread group. Note that any externally
793 * wait4()-ing process will get the correct exit code - even if this
794 * thread is not the thread group leader.
796 asmlinkage void sys_exit_group(int error_code)
798 do_group_exit((error_code & 0xff) << 8);
801 static int eligible_child(pid_t pid, int options, task_t *p)
803 if (pid > 0) {
804 if (p->pid != pid)
805 return 0;
806 } else if (!pid) {
807 if (p->pgrp != current->pgrp)
808 return 0;
809 } else if (pid != -1) {
810 if (p->pgrp != -pid)
811 return 0;
815 * Do not consider detached threads that are
816 * not ptraced:
818 if (p->exit_signal == -1 && !p->ptrace)
819 return 0;
821 /* Wait for all children (clone and not) if __WALL is set;
822 * otherwise, wait for clone children *only* if __WCLONE is
823 * set; otherwise, wait for non-clone children *only*. (Note:
824 * A "clone" child here is one that reports to its parent
825 * using a signal other than SIGCHLD.) */
826 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
827 && !(options & __WALL))
828 return 0;
830 * Do not consider thread group leaders that are
831 * in a non-empty thread group:
833 if (current->tgid != p->tgid && delay_group_leader(p))
834 return 2;
836 if (security_task_wait(p))
837 return 0;
839 return 1;
843 * Handle sys_wait4 work for one task in state TASK_ZOMBIE. We hold
844 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
845 * the lock and this task is uninteresting. If we return nonzero, we have
846 * released the lock and the system call should return.
848 static int wait_task_zombie(task_t *p, unsigned int *stat_addr, struct rusage *ru)
850 unsigned long state;
851 int retval;
854 * Try to move the task's state to DEAD
855 * only one thread is allowed to do this:
857 state = xchg(&p->state, TASK_DEAD);
858 if (state != TASK_ZOMBIE) {
859 BUG_ON(state != TASK_DEAD);
860 return 0;
862 if (unlikely(p->exit_signal == -1))
864 * This can only happen in a race with a ptraced thread
865 * dying on another processor.
867 return 0;
870 * Now we are sure this task is interesting, and no other
871 * thread can reap it because we set its state to TASK_DEAD.
873 read_unlock(&tasklist_lock);
875 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
876 if (!retval && stat_addr) {
877 if (p->signal->group_exit)
878 retval = put_user(p->signal->group_exit_code, stat_addr);
879 else
880 retval = put_user(p->exit_code, stat_addr);
882 if (retval) {
883 p->state = TASK_ZOMBIE;
884 return retval;
886 retval = p->pid;
887 if (p->real_parent != p->parent) {
888 write_lock_irq(&tasklist_lock);
889 /* Double-check with lock held. */
890 if (p->real_parent != p->parent) {
891 __ptrace_unlink(p);
892 do_notify_parent(p, p->exit_signal);
893 p->state = TASK_ZOMBIE;
894 p = NULL;
896 write_unlock_irq(&tasklist_lock);
898 if (p != NULL)
899 release_task(p);
900 BUG_ON(!retval);
901 return retval;
905 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
906 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
907 * the lock and this task is uninteresting. If we return nonzero, we have
908 * released the lock and the system call should return.
910 static int wait_task_stopped(task_t *p, int delayed_group_leader,
911 unsigned int *stat_addr, struct rusage *ru)
913 int retval, exit_code;
915 if (!p->exit_code)
916 return 0;
917 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
918 p->signal && p->signal->group_stop_count > 0)
920 * A group stop is in progress and this is the group leader.
921 * We won't report until all threads have stopped.
923 return 0;
926 * Now we are pretty sure this task is interesting.
927 * Make sure it doesn't get reaped out from under us while we
928 * give up the lock and then examine it below. We don't want to
929 * keep holding onto the tasklist_lock while we call getrusage and
930 * possibly take page faults for user memory.
932 get_task_struct(p);
933 read_unlock(&tasklist_lock);
934 write_lock_irq(&tasklist_lock);
937 * This uses xchg to be atomic with the thread resuming and setting
938 * it. It must also be done with the write lock held to prevent a
939 * race with the TASK_ZOMBIE case.
941 exit_code = xchg(&p->exit_code, 0);
942 if (unlikely(p->state > TASK_STOPPED)) {
944 * The task resumed and then died. Let the next iteration
945 * catch it in TASK_ZOMBIE. Note that exit_code might
946 * already be zero here if it resumed and did _exit(0).
947 * The task itself is dead and won't touch exit_code again;
948 * other processors in this function are locked out.
950 p->exit_code = exit_code;
951 exit_code = 0;
953 if (unlikely(exit_code == 0)) {
955 * Another thread in this function got to it first, or it
956 * resumed, or it resumed and then died.
958 write_unlock_irq(&tasklist_lock);
959 put_task_struct(p);
960 read_lock(&tasklist_lock);
961 return 0;
964 /* move to end of parent's list to avoid starvation */
965 remove_parent(p);
966 add_parent(p, p->parent);
968 write_unlock_irq(&tasklist_lock);
970 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
971 if (!retval && stat_addr)
972 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
973 if (!retval)
974 retval = p->pid;
975 put_task_struct(p);
977 BUG_ON(!retval);
978 return retval;
981 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
983 DECLARE_WAITQUEUE(wait, current);
984 struct task_struct *tsk;
985 int flag, retval;
987 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
988 return -EINVAL;
990 add_wait_queue(&current->wait_chldexit,&wait);
991 repeat:
992 flag = 0;
993 current->state = TASK_INTERRUPTIBLE;
994 read_lock(&tasklist_lock);
995 tsk = current;
996 do {
997 struct task_struct *p;
998 struct list_head *_p;
999 int ret;
1001 list_for_each(_p,&tsk->children) {
1002 p = list_entry(_p,struct task_struct,sibling);
1004 ret = eligible_child(pid, options, p);
1005 if (!ret)
1006 continue;
1007 flag = 1;
1009 switch (p->state) {
1010 case TASK_STOPPED:
1011 if (!(options & WUNTRACED) &&
1012 !(p->ptrace & PT_PTRACED))
1013 continue;
1014 retval = wait_task_stopped(p, ret == 2,
1015 stat_addr, ru);
1016 if (retval != 0) /* He released the lock. */
1017 goto end_wait4;
1018 break;
1019 case TASK_ZOMBIE:
1021 * Eligible but we cannot release it yet:
1023 if (ret == 2)
1024 continue;
1025 retval = wait_task_zombie(p, stat_addr, ru);
1026 if (retval != 0) /* He released the lock. */
1027 goto end_wait4;
1028 break;
1031 if (!flag) {
1032 list_for_each (_p,&tsk->ptrace_children) {
1033 p = list_entry(_p,struct task_struct,ptrace_list);
1034 if (!eligible_child(pid, options, p))
1035 continue;
1036 flag = 1;
1037 break;
1040 if (options & __WNOTHREAD)
1041 break;
1042 tsk = next_thread(tsk);
1043 if (tsk->signal != current->signal)
1044 BUG();
1045 } while (tsk != current);
1046 read_unlock(&tasklist_lock);
1047 if (flag) {
1048 retval = 0;
1049 if (options & WNOHANG)
1050 goto end_wait4;
1051 retval = -ERESTARTSYS;
1052 if (signal_pending(current))
1053 goto end_wait4;
1054 schedule();
1055 goto repeat;
1057 retval = -ECHILD;
1058 end_wait4:
1059 current->state = TASK_RUNNING;
1060 remove_wait_queue(&current->wait_chldexit,&wait);
1061 return retval;
1064 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__arm__)
1067 * sys_waitpid() remains for compatibility. waitpid() should be
1068 * implemented by calling sys_wait4() from libc.a.
1070 asmlinkage long sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
1072 return sys_wait4(pid, stat_addr, options, NULL);
1075 #endif