[ARM] Fix gcc3 multi-line string literal build error.
[linux-2.6/history.git] / kernel / exit.c
blobb6174f82adf971208a61503b34b72818c39fbc88
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 __get_cpu_var(process_counts)--;
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 p->parent->cnvcsw += p->nvcsw + p->cnvcsw;
84 p->parent->cnivcsw += p->nivcsw + p->cnivcsw;
85 sched_exit(p);
86 write_unlock_irq(&tasklist_lock);
87 spin_unlock(&p->proc_lock);
88 proc_pid_flush(proc_dentry);
89 release_thread(p);
90 put_task_struct(p);
93 /* we are using it only for SMP init */
95 void unhash_process(struct task_struct *p)
97 struct dentry *proc_dentry;
99 spin_lock(&p->proc_lock);
100 proc_dentry = proc_pid_unhash(p);
101 write_lock_irq(&tasklist_lock);
102 __unhash_process(p);
103 write_unlock_irq(&tasklist_lock);
104 spin_unlock(&p->proc_lock);
105 proc_pid_flush(proc_dentry);
109 * This checks not only the pgrp, but falls back on the pid if no
110 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
111 * without this...
113 int session_of_pgrp(int pgrp)
115 struct task_struct *p;
116 struct list_head *l;
117 struct pid *pid;
118 int sid = -1;
120 read_lock(&tasklist_lock);
121 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid)
122 if (p->session > 0) {
123 sid = p->session;
124 goto out;
126 p = find_task_by_pid(pgrp);
127 if (p)
128 sid = p->session;
129 out:
130 read_unlock(&tasklist_lock);
132 return sid;
136 * Determine if a process group is "orphaned", according to the POSIX
137 * definition in 2.2.2.52. Orphaned process groups are not to be affected
138 * by terminal-generated stop signals. Newly orphaned process groups are
139 * to receive a SIGHUP and a SIGCONT.
141 * "I ask you, have you ever known what it is to be an orphan?"
143 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
145 struct task_struct *p;
146 struct list_head *l;
147 struct pid *pid;
148 int ret = 1;
150 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
151 if (p == ignored_task
152 || p->state >= TASK_ZOMBIE
153 || p->real_parent->pid == 1)
154 continue;
155 if (p->real_parent->pgrp != pgrp
156 && p->real_parent->session == p->session) {
157 ret = 0;
158 break;
161 return ret; /* (sighing) "Often!" */
164 int is_orphaned_pgrp(int pgrp)
166 int retval;
168 read_lock(&tasklist_lock);
169 retval = will_become_orphaned_pgrp(pgrp, NULL);
170 read_unlock(&tasklist_lock);
172 return retval;
175 static inline int has_stopped_jobs(int pgrp)
177 int retval = 0;
178 struct task_struct *p;
179 struct list_head *l;
180 struct pid *pid;
182 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
183 if (p->state != TASK_STOPPED)
184 continue;
186 /* If p is stopped by a debugger on a signal that won't
187 stop it, then don't count p as stopped. This isn't
188 perfect but it's a good approximation. */
189 if (unlikely (p->ptrace)
190 && p->exit_code != SIGSTOP
191 && p->exit_code != SIGTSTP
192 && p->exit_code != SIGTTOU
193 && p->exit_code != SIGTTIN)
194 continue;
196 retval = 1;
197 break;
199 return retval;
203 * reparent_to_init() - Reparent the calling kernel thread to the init task.
205 * If a kernel thread is launched as a result of a system call, or if
206 * it ever exits, it should generally reparent itself to init so that
207 * it is correctly cleaned up on exit.
209 * The various task state such as scheduling policy and priority may have
210 * been inherited from a user process, so we reset them to sane values here.
212 * NOTE that reparent_to_init() gives the caller full capabilities.
214 void reparent_to_init(void)
216 write_lock_irq(&tasklist_lock);
218 ptrace_unlink(current);
219 /* Reparent to init */
220 REMOVE_LINKS(current);
221 current->parent = child_reaper;
222 current->real_parent = child_reaper;
223 SET_LINKS(current);
225 /* Set the exit signal to SIGCHLD so we signal init on exit */
226 current->exit_signal = SIGCHLD;
228 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
229 set_user_nice(current, 0);
230 /* cpus_allowed? */
231 /* rt_priority? */
232 /* signals? */
233 security_task_reparent_to_init(current);
234 memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
235 atomic_inc(&(INIT_USER->__count));
236 switch_uid(INIT_USER);
238 write_unlock_irq(&tasklist_lock);
241 void __set_special_pids(pid_t session, pid_t pgrp)
243 struct task_struct *curr = current;
245 if (curr->session != session) {
246 detach_pid(curr, PIDTYPE_SID);
247 curr->session = session;
248 attach_pid(curr, PIDTYPE_SID, session);
250 if (curr->pgrp != pgrp) {
251 detach_pid(curr, PIDTYPE_PGID);
252 curr->pgrp = pgrp;
253 attach_pid(curr, PIDTYPE_PGID, pgrp);
257 void set_special_pids(pid_t session, pid_t pgrp)
259 write_lock_irq(&tasklist_lock);
260 __set_special_pids(session, pgrp);
261 write_unlock_irq(&tasklist_lock);
265 * Let kernel threads use this to say that they
266 * allow a certain signal (since daemonize() will
267 * have disabled all of them by default).
269 int allow_signal(int sig)
271 if (sig < 1 || sig > _NSIG)
272 return -EINVAL;
274 spin_lock_irq(&current->sighand->siglock);
275 sigdelset(&current->blocked, sig);
276 recalc_sigpending();
277 spin_unlock_irq(&current->sighand->siglock);
278 return 0;
281 EXPORT_SYMBOL(allow_signal);
284 * Put all the gunge required to become a kernel thread without
285 * attached user resources in one place where it belongs.
288 void daemonize(const char *name, ...)
290 va_list args;
291 struct fs_struct *fs;
292 sigset_t blocked;
294 va_start(args, name);
295 vsnprintf(current->comm, sizeof(current->comm), name, args);
296 va_end(args);
299 * If we were started as result of loading a module, close all of the
300 * user space pages. We don't need them, and if we didn't close them
301 * they would be locked into memory.
303 exit_mm(current);
305 set_special_pids(1, 1);
306 current->tty = NULL;
308 /* Block and flush all signals */
309 sigfillset(&blocked);
310 sigprocmask(SIG_BLOCK, &blocked, NULL);
311 flush_signals(current);
313 /* Become as one with the init task */
315 exit_fs(current); /* current->fs->count--; */
316 fs = init_task.fs;
317 current->fs = fs;
318 atomic_inc(&fs->count);
319 exit_files(current);
320 current->files = init_task.files;
321 atomic_inc(&current->files->count);
323 reparent_to_init();
326 static inline void close_files(struct files_struct * files)
328 int i, j;
330 j = 0;
331 for (;;) {
332 unsigned long set;
333 i = j * __NFDBITS;
334 if (i >= files->max_fdset || i >= files->max_fds)
335 break;
336 set = files->open_fds->fds_bits[j++];
337 while (set) {
338 if (set & 1) {
339 struct file * file = xchg(&files->fd[i], NULL);
340 if (file)
341 filp_close(file, files);
343 i++;
344 set >>= 1;
349 void put_files_struct(struct files_struct *files)
351 if (atomic_dec_and_test(&files->count)) {
352 close_files(files);
354 * Free the fd and fdset arrays if we expanded them.
356 if (files->fd != &files->fd_array[0])
357 free_fd_array(files->fd, files->max_fds);
358 if (files->max_fdset > __FD_SETSIZE) {
359 free_fdset(files->open_fds, files->max_fdset);
360 free_fdset(files->close_on_exec, files->max_fdset);
362 kmem_cache_free(files_cachep, files);
366 static inline void __exit_files(struct task_struct *tsk)
368 struct files_struct * files = tsk->files;
370 if (files) {
371 task_lock(tsk);
372 tsk->files = NULL;
373 task_unlock(tsk);
374 put_files_struct(files);
378 void exit_files(struct task_struct *tsk)
380 __exit_files(tsk);
383 static inline void __put_fs_struct(struct fs_struct *fs)
385 /* No need to hold fs->lock if we are killing it */
386 if (atomic_dec_and_test(&fs->count)) {
387 dput(fs->root);
388 mntput(fs->rootmnt);
389 dput(fs->pwd);
390 mntput(fs->pwdmnt);
391 if (fs->altroot) {
392 dput(fs->altroot);
393 mntput(fs->altrootmnt);
395 kmem_cache_free(fs_cachep, fs);
399 void put_fs_struct(struct fs_struct *fs)
401 __put_fs_struct(fs);
404 static inline void __exit_fs(struct task_struct *tsk)
406 struct fs_struct * fs = tsk->fs;
408 if (fs) {
409 task_lock(tsk);
410 tsk->fs = NULL;
411 task_unlock(tsk);
412 __put_fs_struct(fs);
416 void exit_fs(struct task_struct *tsk)
418 __exit_fs(tsk);
422 * Turn us into a lazy TLB process if we
423 * aren't already..
425 static inline void __exit_mm(struct task_struct * tsk)
427 struct mm_struct *mm = tsk->mm;
429 mm_release(tsk, mm);
430 if (!mm)
431 return;
433 * Serialize with any possible pending coredump:
435 if (mm->core_waiters) {
436 down_write(&mm->mmap_sem);
437 if (!--mm->core_waiters)
438 complete(mm->core_startup_done);
439 up_write(&mm->mmap_sem);
441 wait_for_completion(&mm->core_done);
443 atomic_inc(&mm->mm_count);
444 if (mm != tsk->active_mm) BUG();
445 /* more a memory barrier than a real lock */
446 task_lock(tsk);
447 tsk->mm = NULL;
448 enter_lazy_tlb(mm, current);
449 task_unlock(tsk);
450 mmput(mm);
453 void exit_mm(struct task_struct *tsk)
455 __exit_mm(tsk);
458 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
461 * Make sure we're not reparenting to ourselves and that
462 * the parent is not a zombie.
464 if (p == reaper || reaper->state >= TASK_ZOMBIE)
465 p->real_parent = child_reaper;
466 else
467 p->real_parent = reaper;
468 if (p->parent == p->real_parent)
469 BUG();
472 static inline void reparent_thread(task_t *p, task_t *father, int traced)
474 /* We don't want people slaying init. */
475 if (p->exit_signal != -1)
476 p->exit_signal = SIGCHLD;
477 p->self_exec_id++;
479 if (p->pdeath_signal)
480 send_group_sig_info(p->pdeath_signal, 0, p);
482 /* Move the child from its dying parent to the new one. */
483 if (unlikely(traced)) {
484 /* Preserve ptrace links if someone else is tracing this child. */
485 list_del_init(&p->ptrace_list);
486 if (p->parent != p->real_parent)
487 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
488 } else {
489 /* If this child is being traced, then we're the one tracing it
490 * anyway, so let go of it.
492 p->ptrace = 0;
493 list_del_init(&p->sibling);
494 p->parent = p->real_parent;
495 list_add_tail(&p->sibling, &p->parent->children);
497 /* If we'd notified the old parent about this child's death,
498 * also notify the new parent.
500 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
501 thread_group_empty(p))
502 do_notify_parent(p, p->exit_signal);
506 * process group orphan check
507 * Case ii: Our child is in a different pgrp
508 * than we are, and it was the only connection
509 * outside, so the child pgrp is now orphaned.
511 if ((p->pgrp != father->pgrp) &&
512 (p->session == father->session)) {
513 int pgrp = p->pgrp;
515 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
516 __kill_pg_info(SIGHUP, (void *)1, pgrp);
517 __kill_pg_info(SIGCONT, (void *)1, pgrp);
523 * When we die, we re-parent all our children.
524 * Try to give them to another thread in our thread
525 * group, and if no such member exists, give it to
526 * the global child reaper process (ie "init")
528 static inline void forget_original_parent(struct task_struct * father)
530 struct task_struct *p, *reaper = father;
531 struct list_head *_p, *_n;
533 reaper = father->group_leader;
534 if (reaper == father)
535 reaper = child_reaper;
538 * There are only two places where our children can be:
540 * - in our child list
541 * - in our ptraced child list
543 * Search them and reparent children.
545 list_for_each_safe(_p, _n, &father->children) {
546 p = list_entry(_p,struct task_struct,sibling);
547 if (father == p->real_parent) {
548 choose_new_parent(p, reaper, child_reaper);
549 reparent_thread(p, father, 0);
550 } else {
551 ptrace_unlink (p);
552 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
553 thread_group_empty(p))
554 do_notify_parent(p, p->exit_signal);
557 list_for_each_safe(_p, _n, &father->ptrace_children) {
558 p = list_entry(_p,struct task_struct,ptrace_list);
559 choose_new_parent(p, reaper, child_reaper);
560 reparent_thread(p, father, 1);
565 * Send signals to all our closest relatives so that they know
566 * to properly mourn us..
568 static void exit_notify(struct task_struct *tsk)
570 struct task_struct *t;
572 if (signal_pending(tsk) && !tsk->signal->group_exit
573 && !thread_group_empty(tsk)) {
575 * This occurs when there was a race between our exit
576 * syscall and a group signal choosing us as the one to
577 * wake up. It could be that we are the only thread
578 * alerted to check for pending signals, but another thread
579 * should be woken now to take the signal since we will not.
580 * Now we'll wake all the threads in the group just to make
581 * sure someone gets all the pending signals.
583 read_lock(&tasklist_lock);
584 spin_lock_irq(&tsk->sighand->siglock);
585 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
586 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
587 recalc_sigpending_tsk(t);
588 if (signal_pending(t))
589 signal_wake_up(t, 0);
591 spin_unlock_irq(&tsk->sighand->siglock);
592 read_unlock(&tasklist_lock);
595 write_lock_irq(&tasklist_lock);
598 * This does two things:
600 * A. Make init inherit all the child processes
601 * B. Check to see if any process groups have become orphaned
602 * as a result of our exiting, and if they have any stopped
603 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
606 forget_original_parent(tsk);
607 BUG_ON(!list_empty(&tsk->children));
610 * Check to see if any process groups have become orphaned
611 * as a result of our exiting, and if they have any stopped
612 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
614 * Case i: Our father is in a different pgrp than we are
615 * and we were the only connection outside, so our pgrp
616 * is about to become orphaned.
619 t = tsk->real_parent;
621 if ((t->pgrp != tsk->pgrp) &&
622 (t->session == tsk->session) &&
623 will_become_orphaned_pgrp(tsk->pgrp, tsk) &&
624 has_stopped_jobs(tsk->pgrp)) {
625 __kill_pg_info(SIGHUP, (void *)1, tsk->pgrp);
626 __kill_pg_info(SIGCONT, (void *)1, tsk->pgrp);
629 /* Let father know we died
631 * Thread signals are configurable, but you aren't going to use
632 * that to send signals to arbitary processes.
633 * That stops right now.
635 * If the parent exec id doesn't match the exec id we saved
636 * when we started then we know the parent has changed security
637 * domain.
639 * If our self_exec id doesn't match our parent_exec_id then
640 * we have changed execution domain as these two values started
641 * the same after a fork.
645 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
646 ( tsk->parent_exec_id != t->self_exec_id ||
647 tsk->self_exec_id != tsk->parent_exec_id)
648 && !capable(CAP_KILL))
649 tsk->exit_signal = SIGCHLD;
652 /* If something other than our normal parent is ptracing us, then
653 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
654 * only has special meaning to our real parent.
656 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
657 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
658 do_notify_parent(tsk, signal);
659 } else if (tsk->ptrace) {
660 do_notify_parent(tsk, SIGCHLD);
663 tsk->state = TASK_ZOMBIE;
665 * In the preemption case it must be impossible for the task
666 * to get runnable again, so use "_raw_" unlock to keep
667 * preempt_count elevated until we schedule().
669 * To avoid deadlock on SMP, interrupts must be unmasked. If we
670 * don't, subsequently called functions (e.g, wait_task_inactive()
671 * via release_task()) will spin, with interrupt flags
672 * unwittingly blocked, until the other task sleeps. That task
673 * may itself be waiting for smp_call_function() to answer and
674 * complete, and with interrupts blocked that will never happen.
676 _raw_write_unlock(&tasklist_lock);
677 local_irq_enable();
680 NORET_TYPE void do_exit(long code)
682 struct task_struct *tsk = current;
684 if (unlikely(in_interrupt()))
685 panic("Aiee, killing interrupt handler!");
686 if (unlikely(!tsk->pid))
687 panic("Attempted to kill the idle task!");
688 if (unlikely(tsk->pid == 1))
689 panic("Attempted to kill init!");
690 if (tsk->io_context)
691 exit_io_context();
692 tsk->flags |= PF_EXITING;
693 del_timer_sync(&tsk->real_timer);
695 if (unlikely(in_atomic()))
696 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
697 current->comm, current->pid,
698 preempt_count());
700 profile_exit_task(tsk);
702 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
703 current->ptrace_message = code;
704 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
707 acct_process(code);
708 __exit_mm(tsk);
710 exit_sem(tsk);
711 __exit_files(tsk);
712 __exit_fs(tsk);
713 exit_namespace(tsk);
714 exit_itimers(tsk);
715 exit_thread();
717 if (tsk->leader)
718 disassociate_ctty(1);
720 module_put(tsk->thread_info->exec_domain->module);
721 if (tsk->binfmt)
722 module_put(tsk->binfmt->module);
724 tsk->exit_code = code;
725 exit_notify(tsk);
727 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
728 release_task(tsk);
730 schedule();
731 BUG();
732 /* Avoid "noreturn function does return". */
733 for (;;) ;
736 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
738 if (comp)
739 complete(comp);
741 do_exit(code);
744 asmlinkage long sys_exit(int error_code)
746 do_exit((error_code&0xff)<<8);
749 task_t *next_thread(task_t *p)
751 struct pid_link *link = p->pids + PIDTYPE_TGID;
752 struct list_head *tmp, *head = &link->pidptr->task_list;
754 #ifdef CONFIG_SMP
755 if (!p->sighand)
756 BUG();
757 if (!spin_is_locked(&p->sighand->siglock) &&
758 !rwlock_is_locked(&tasklist_lock))
759 BUG();
760 #endif
761 tmp = link->pid_chain.next;
762 if (tmp == head)
763 tmp = head->next;
765 return pid_task(tmp, PIDTYPE_TGID);
769 * Take down every thread in the group. This is called by fatal signals
770 * as well as by sys_exit_group (below).
772 NORET_TYPE void
773 do_group_exit(int exit_code)
775 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
777 if (current->signal->group_exit)
778 exit_code = current->signal->group_exit_code;
779 else if (!thread_group_empty(current)) {
780 struct signal_struct *const sig = current->signal;
781 struct sighand_struct *const sighand = current->sighand;
782 read_lock(&tasklist_lock);
783 spin_lock_irq(&sighand->siglock);
784 if (sig->group_exit)
785 /* Another thread got here before we took the lock. */
786 exit_code = sig->group_exit_code;
787 else {
788 sig->group_exit = 1;
789 sig->group_exit_code = exit_code;
790 zap_other_threads(current);
792 spin_unlock_irq(&sighand->siglock);
793 read_unlock(&tasklist_lock);
796 do_exit(exit_code);
797 /* NOTREACHED */
801 * this kills every thread in the thread group. Note that any externally
802 * wait4()-ing process will get the correct exit code - even if this
803 * thread is not the thread group leader.
805 asmlinkage void sys_exit_group(int error_code)
807 do_group_exit((error_code & 0xff) << 8);
810 static int eligible_child(pid_t pid, int options, task_t *p)
812 if (pid > 0) {
813 if (p->pid != pid)
814 return 0;
815 } else if (!pid) {
816 if (p->pgrp != current->pgrp)
817 return 0;
818 } else if (pid != -1) {
819 if (p->pgrp != -pid)
820 return 0;
824 * Do not consider detached threads that are
825 * not ptraced:
827 if (p->exit_signal == -1 && !p->ptrace)
828 return 0;
830 /* Wait for all children (clone and not) if __WALL is set;
831 * otherwise, wait for clone children *only* if __WCLONE is
832 * set; otherwise, wait for non-clone children *only*. (Note:
833 * A "clone" child here is one that reports to its parent
834 * using a signal other than SIGCHLD.) */
835 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
836 && !(options & __WALL))
837 return 0;
839 * Do not consider thread group leaders that are
840 * in a non-empty thread group:
842 if (current->tgid != p->tgid && delay_group_leader(p))
843 return 2;
845 if (security_task_wait(p))
846 return 0;
848 return 1;
852 * Handle sys_wait4 work for one task in state TASK_ZOMBIE. We hold
853 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
854 * the lock and this task is uninteresting. If we return nonzero, we have
855 * released the lock and the system call should return.
857 static int wait_task_zombie(task_t *p, unsigned int *stat_addr, struct rusage *ru)
859 unsigned long state;
860 int retval;
863 * Try to move the task's state to DEAD
864 * only one thread is allowed to do this:
866 state = xchg(&p->state, TASK_DEAD);
867 if (state != TASK_ZOMBIE) {
868 BUG_ON(state != TASK_DEAD);
869 return 0;
871 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
873 * This can only happen in a race with a ptraced thread
874 * dying on another processor.
876 return 0;
879 * Now we are sure this task is interesting, and no other
880 * thread can reap it because we set its state to TASK_DEAD.
882 read_unlock(&tasklist_lock);
884 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
885 if (!retval && stat_addr) {
886 if (p->signal->group_exit)
887 retval = put_user(p->signal->group_exit_code, stat_addr);
888 else
889 retval = put_user(p->exit_code, stat_addr);
891 if (retval) {
892 p->state = TASK_ZOMBIE;
893 return retval;
895 retval = p->pid;
896 if (p->real_parent != p->parent) {
897 write_lock_irq(&tasklist_lock);
898 /* Double-check with lock held. */
899 if (p->real_parent != p->parent) {
900 __ptrace_unlink(p);
901 p->state = TASK_ZOMBIE;
902 /* If this is a detached thread, this is where it goes away. */
903 if (p->exit_signal == -1) {
904 /* release_task takes the lock itself. */
905 write_unlock_irq(&tasklist_lock);
906 release_task (p);
908 else {
909 do_notify_parent(p, p->exit_signal);
910 write_unlock_irq(&tasklist_lock);
912 p = NULL;
914 else
915 write_unlock_irq(&tasklist_lock);
917 if (p != NULL)
918 release_task(p);
919 BUG_ON(!retval);
920 return retval;
924 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
925 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
926 * the lock and this task is uninteresting. If we return nonzero, we have
927 * released the lock and the system call should return.
929 static int wait_task_stopped(task_t *p, int delayed_group_leader,
930 unsigned int *stat_addr, struct rusage *ru)
932 int retval, exit_code;
934 if (!p->exit_code)
935 return 0;
936 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
937 p->signal && p->signal->group_stop_count > 0)
939 * A group stop is in progress and this is the group leader.
940 * We won't report until all threads have stopped.
942 return 0;
945 * Now we are pretty sure this task is interesting.
946 * Make sure it doesn't get reaped out from under us while we
947 * give up the lock and then examine it below. We don't want to
948 * keep holding onto the tasklist_lock while we call getrusage and
949 * possibly take page faults for user memory.
951 get_task_struct(p);
952 read_unlock(&tasklist_lock);
953 write_lock_irq(&tasklist_lock);
956 * This uses xchg to be atomic with the thread resuming and setting
957 * it. It must also be done with the write lock held to prevent a
958 * race with the TASK_ZOMBIE case.
960 exit_code = xchg(&p->exit_code, 0);
961 if (unlikely(p->state > TASK_STOPPED)) {
963 * The task resumed and then died. Let the next iteration
964 * catch it in TASK_ZOMBIE. Note that exit_code might
965 * already be zero here if it resumed and did _exit(0).
966 * The task itself is dead and won't touch exit_code again;
967 * other processors in this function are locked out.
969 p->exit_code = exit_code;
970 exit_code = 0;
972 if (unlikely(exit_code == 0)) {
974 * Another thread in this function got to it first, or it
975 * resumed, or it resumed and then died.
977 write_unlock_irq(&tasklist_lock);
978 put_task_struct(p);
979 read_lock(&tasklist_lock);
980 return 0;
983 /* move to end of parent's list to avoid starvation */
984 remove_parent(p);
985 add_parent(p, p->parent);
987 write_unlock_irq(&tasklist_lock);
989 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
990 if (!retval && stat_addr)
991 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
992 if (!retval)
993 retval = p->pid;
994 put_task_struct(p);
996 BUG_ON(!retval);
997 return retval;
1000 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
1002 DECLARE_WAITQUEUE(wait, current);
1003 struct task_struct *tsk;
1004 int flag, retval;
1006 if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
1007 return -EINVAL;
1009 add_wait_queue(&current->wait_chldexit,&wait);
1010 repeat:
1011 flag = 0;
1012 current->state = TASK_INTERRUPTIBLE;
1013 read_lock(&tasklist_lock);
1014 tsk = current;
1015 do {
1016 struct task_struct *p;
1017 struct list_head *_p;
1018 int ret;
1020 list_for_each(_p,&tsk->children) {
1021 p = list_entry(_p,struct task_struct,sibling);
1023 ret = eligible_child(pid, options, p);
1024 if (!ret)
1025 continue;
1026 flag = 1;
1028 switch (p->state) {
1029 case TASK_STOPPED:
1030 if (!(options & WUNTRACED) &&
1031 !(p->ptrace & PT_PTRACED))
1032 continue;
1033 retval = wait_task_stopped(p, ret == 2,
1034 stat_addr, ru);
1035 if (retval != 0) /* He released the lock. */
1036 goto end_wait4;
1037 break;
1038 case TASK_ZOMBIE:
1040 * Eligible but we cannot release it yet:
1042 if (ret == 2)
1043 continue;
1044 retval = wait_task_zombie(p, stat_addr, ru);
1045 if (retval != 0) /* He released the lock. */
1046 goto end_wait4;
1047 break;
1050 if (!flag) {
1051 list_for_each (_p,&tsk->ptrace_children) {
1052 p = list_entry(_p,struct task_struct,ptrace_list);
1053 if (!eligible_child(pid, options, p))
1054 continue;
1055 flag = 1;
1056 break;
1059 if (options & __WNOTHREAD)
1060 break;
1061 tsk = next_thread(tsk);
1062 if (tsk->signal != current->signal)
1063 BUG();
1064 } while (tsk != current);
1065 read_unlock(&tasklist_lock);
1066 if (flag) {
1067 retval = 0;
1068 if (options & WNOHANG)
1069 goto end_wait4;
1070 retval = -ERESTARTSYS;
1071 if (signal_pending(current))
1072 goto end_wait4;
1073 schedule();
1074 goto repeat;
1076 retval = -ECHILD;
1077 end_wait4:
1078 current->state = TASK_RUNNING;
1079 remove_wait_queue(&current->wait_chldexit,&wait);
1080 return retval;
1083 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__arm__)
1086 * sys_waitpid() remains for compatibility. waitpid() should be
1087 * implemented by calling sys_wait4() from libc.a.
1089 asmlinkage long sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
1091 return sys_wait4(pid, stat_addr, options, NULL);
1094 #endif