ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / exit.c
blob7a5901a31c9674a63068e6e97e23bdc30e76a9ca
1 /*
2 * linux/kernel/exit.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/capability.h>
12 #include <linux/completion.h>
13 #include <linux/personality.h>
14 #include <linux/tty.h>
15 #include <linux/mnt_namespace.h>
16 #include <linux/iocontext.h>
17 #include <linux/key.h>
18 #include <linux/security.h>
19 #include <linux/cpu.h>
20 #include <linux/acct.h>
21 #include <linux/tsacct_kern.h>
22 #include <linux/file.h>
23 #include <linux/fdtable.h>
24 #include <linux/binfmts.h>
25 #include <linux/nsproxy.h>
26 #include <linux/pid_namespace.h>
27 #include <linux/ptrace.h>
28 #include <linux/profile.h>
29 #include <linux/mount.h>
30 #include <linux/proc_fs.h>
31 #include <linux/kthread.h>
32 #include <linux/mempolicy.h>
33 #include <linux/taskstats_kern.h>
34 #include <linux/delayacct.h>
35 #include <linux/freezer.h>
36 #include <linux/cgroup.h>
37 #include <linux/syscalls.h>
38 #include <linux/signal.h>
39 #include <linux/posix-timers.h>
40 #include <linux/cn_proc.h>
41 #include <linux/mutex.h>
42 #include <linux/futex.h>
43 #include <linux/compat.h>
44 #include <linux/pipe_fs_i.h>
45 #include <linux/audit.h> /* for audit_free() */
46 #include <linux/resource.h>
47 #include <linux/blkdev.h>
48 #include <linux/task_io_accounting_ops.h>
49 #include <linux/tracehook.h>
51 #include <asm/uaccess.h>
52 #include <asm/unistd.h>
53 #include <asm/pgtable.h>
54 #include <asm/mmu_context.h>
56 static void exit_mm(struct task_struct * tsk);
58 static inline int task_detached(struct task_struct *p)
60 return p->exit_signal == -1;
63 static void __unhash_process(struct task_struct *p)
65 nr_threads--;
66 detach_pid(p, PIDTYPE_PID);
67 if (thread_group_leader(p)) {
68 detach_pid(p, PIDTYPE_PGID);
69 detach_pid(p, PIDTYPE_SID);
71 list_del_rcu(&p->tasks);
72 __get_cpu_var(process_counts)--;
74 list_del_rcu(&p->thread_group);
75 list_del_init(&p->sibling);
79 * This function expects the tasklist_lock write-locked.
81 static void __exit_signal(struct task_struct *tsk)
83 struct signal_struct *sig = tsk->signal;
84 struct sighand_struct *sighand;
86 BUG_ON(!sig);
87 BUG_ON(!atomic_read(&sig->count));
89 sighand = rcu_dereference(tsk->sighand);
90 spin_lock(&sighand->siglock);
92 posix_cpu_timers_exit(tsk);
93 if (atomic_dec_and_test(&sig->count))
94 posix_cpu_timers_exit_group(tsk);
95 else {
97 * This can only happen if the caller is de_thread().
98 * FIXME: this is the temporary hack, we should teach
99 * posix-cpu-timers to handle this case correctly.
101 if (unlikely(has_group_leader_pid(tsk)))
102 posix_cpu_timers_exit_group(tsk);
105 * If there is any task waiting for the group exit
106 * then notify it:
108 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count)
109 wake_up_process(sig->group_exit_task);
111 if (tsk == sig->curr_target)
112 sig->curr_target = next_thread(tsk);
114 * Accumulate here the counters for all threads but the
115 * group leader as they die, so they can be added into
116 * the process-wide totals when those are taken.
117 * The group leader stays around as a zombie as long
118 * as there are other threads. When it gets reaped,
119 * the exit.c code will add its counts into these totals.
120 * We won't ever get here for the group leader, since it
121 * will have been the last reference on the signal_struct.
123 sig->utime = cputime_add(sig->utime, task_utime(tsk));
124 sig->stime = cputime_add(sig->stime, task_stime(tsk));
125 sig->gtime = cputime_add(sig->gtime, task_gtime(tsk));
126 sig->min_flt += tsk->min_flt;
127 sig->maj_flt += tsk->maj_flt;
128 sig->nvcsw += tsk->nvcsw;
129 sig->nivcsw += tsk->nivcsw;
130 sig->inblock += task_io_get_inblock(tsk);
131 sig->oublock += task_io_get_oublock(tsk);
132 task_io_accounting_add(&sig->ioac, &tsk->ioac);
133 sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
134 sig = NULL; /* Marker for below. */
137 __unhash_process(tsk);
140 * Do this under ->siglock, we can race with another thread
141 * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
143 flush_sigqueue(&tsk->pending);
145 tsk->signal = NULL;
146 tsk->sighand = NULL;
147 spin_unlock(&sighand->siglock);
149 __cleanup_sighand(sighand);
150 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
151 if (sig) {
152 flush_sigqueue(&sig->shared_pending);
153 taskstats_tgid_free(sig);
154 __cleanup_signal(sig);
158 static void delayed_put_task_struct(struct rcu_head *rhp)
160 put_task_struct(container_of(rhp, struct task_struct, rcu));
164 void release_task(struct task_struct * p)
166 struct task_struct *leader;
167 int zap_leader;
168 repeat:
169 tracehook_prepare_release_task(p);
170 atomic_dec(&p->user->processes);
171 proc_flush_task(p);
172 write_lock_irq(&tasklist_lock);
173 tracehook_finish_release_task(p);
174 __exit_signal(p);
177 * If we are the last non-leader member of the thread
178 * group, and the leader is zombie, then notify the
179 * group leader's parent process. (if it wants notification.)
181 zap_leader = 0;
182 leader = p->group_leader;
183 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
184 BUG_ON(task_detached(leader));
185 do_notify_parent(leader, leader->exit_signal);
187 * If we were the last child thread and the leader has
188 * exited already, and the leader's parent ignores SIGCHLD,
189 * then we are the one who should release the leader.
191 * do_notify_parent() will have marked it self-reaping in
192 * that case.
194 zap_leader = task_detached(leader);
197 * This maintains the invariant that release_task()
198 * only runs on a task in EXIT_DEAD, just for sanity.
200 if (zap_leader)
201 leader->exit_state = EXIT_DEAD;
204 write_unlock_irq(&tasklist_lock);
205 release_thread(p);
206 call_rcu(&p->rcu, delayed_put_task_struct);
208 p = leader;
209 if (unlikely(zap_leader))
210 goto repeat;
214 * This checks not only the pgrp, but falls back on the pid if no
215 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
216 * without this...
218 * The caller must hold rcu lock or the tasklist lock.
220 struct pid *session_of_pgrp(struct pid *pgrp)
222 struct task_struct *p;
223 struct pid *sid = NULL;
225 p = pid_task(pgrp, PIDTYPE_PGID);
226 if (p == NULL)
227 p = pid_task(pgrp, PIDTYPE_PID);
228 if (p != NULL)
229 sid = task_session(p);
231 return sid;
235 * Determine if a process group is "orphaned", according to the POSIX
236 * definition in 2.2.2.52. Orphaned process groups are not to be affected
237 * by terminal-generated stop signals. Newly orphaned process groups are
238 * to receive a SIGHUP and a SIGCONT.
240 * "I ask you, have you ever known what it is to be an orphan?"
242 static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
244 struct task_struct *p;
246 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
247 if ((p == ignored_task) ||
248 (p->exit_state && thread_group_empty(p)) ||
249 is_global_init(p->real_parent))
250 continue;
252 if (task_pgrp(p->real_parent) != pgrp &&
253 task_session(p->real_parent) == task_session(p))
254 return 0;
255 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
257 return 1;
260 int is_current_pgrp_orphaned(void)
262 int retval;
264 read_lock(&tasklist_lock);
265 retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
266 read_unlock(&tasklist_lock);
268 return retval;
271 static int has_stopped_jobs(struct pid *pgrp)
273 int retval = 0;
274 struct task_struct *p;
276 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
277 if (!task_is_stopped(p))
278 continue;
279 retval = 1;
280 break;
281 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
282 return retval;
286 * Check to see if any process groups have become orphaned as
287 * a result of our exiting, and if they have any stopped jobs,
288 * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
290 static void
291 kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
293 struct pid *pgrp = task_pgrp(tsk);
294 struct task_struct *ignored_task = tsk;
296 if (!parent)
297 /* exit: our father is in a different pgrp than
298 * we are and we were the only connection outside.
300 parent = tsk->real_parent;
301 else
302 /* reparent: our child is in a different pgrp than
303 * we are, and it was the only connection outside.
305 ignored_task = NULL;
307 if (task_pgrp(parent) != pgrp &&
308 task_session(parent) == task_session(tsk) &&
309 will_become_orphaned_pgrp(pgrp, ignored_task) &&
310 has_stopped_jobs(pgrp)) {
311 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
312 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
317 * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
319 * If a kernel thread is launched as a result of a system call, or if
320 * it ever exits, it should generally reparent itself to kthreadd so it
321 * isn't in the way of other processes and is correctly cleaned up on exit.
323 * The various task state such as scheduling policy and priority may have
324 * been inherited from a user process, so we reset them to sane values here.
326 * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
328 static void reparent_to_kthreadd(void)
330 write_lock_irq(&tasklist_lock);
332 ptrace_unlink(current);
333 /* Reparent to init */
334 current->real_parent = current->parent = kthreadd_task;
335 list_move_tail(&current->sibling, &current->real_parent->children);
337 /* Set the exit signal to SIGCHLD so we signal init on exit */
338 current->exit_signal = SIGCHLD;
340 if (task_nice(current) < 0)
341 set_user_nice(current, 0);
342 /* cpus_allowed? */
343 /* rt_priority? */
344 /* signals? */
345 security_task_reparent_to_init(current);
346 memcpy(current->signal->rlim, init_task.signal->rlim,
347 sizeof(current->signal->rlim));
348 atomic_inc(&(INIT_USER->__count));
349 write_unlock_irq(&tasklist_lock);
350 switch_uid(INIT_USER);
353 void __set_special_pids(struct pid *pid)
355 struct task_struct *curr = current->group_leader;
356 pid_t nr = pid_nr(pid);
358 if (task_session(curr) != pid) {
359 change_pid(curr, PIDTYPE_SID, pid);
360 set_task_session(curr, nr);
362 if (task_pgrp(curr) != pid) {
363 change_pid(curr, PIDTYPE_PGID, pid);
364 set_task_pgrp(curr, nr);
368 static void set_special_pids(struct pid *pid)
370 write_lock_irq(&tasklist_lock);
371 __set_special_pids(pid);
372 write_unlock_irq(&tasklist_lock);
376 * Let kernel threads use this to say that they
377 * allow a certain signal (since daemonize() will
378 * have disabled all of them by default).
380 int allow_signal(int sig)
382 if (!valid_signal(sig) || sig < 1)
383 return -EINVAL;
385 spin_lock_irq(&current->sighand->siglock);
386 sigdelset(&current->blocked, sig);
387 if (!current->mm) {
388 /* Kernel threads handle their own signals.
389 Let the signal code know it'll be handled, so
390 that they don't get converted to SIGKILL or
391 just silently dropped */
392 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
394 recalc_sigpending();
395 spin_unlock_irq(&current->sighand->siglock);
396 return 0;
399 EXPORT_SYMBOL(allow_signal);
401 int disallow_signal(int sig)
403 if (!valid_signal(sig) || sig < 1)
404 return -EINVAL;
406 spin_lock_irq(&current->sighand->siglock);
407 current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
408 recalc_sigpending();
409 spin_unlock_irq(&current->sighand->siglock);
410 return 0;
413 EXPORT_SYMBOL(disallow_signal);
416 * Put all the gunge required to become a kernel thread without
417 * attached user resources in one place where it belongs.
420 void daemonize(const char *name, ...)
422 va_list args;
423 struct fs_struct *fs;
424 sigset_t blocked;
426 va_start(args, name);
427 vsnprintf(current->comm, sizeof(current->comm), name, args);
428 va_end(args);
431 * If we were started as result of loading a module, close all of the
432 * user space pages. We don't need them, and if we didn't close them
433 * they would be locked into memory.
435 exit_mm(current);
437 * We don't want to have TIF_FREEZE set if the system-wide hibernation
438 * or suspend transition begins right now.
440 current->flags |= (PF_NOFREEZE | PF_KTHREAD);
442 if (current->nsproxy != &init_nsproxy) {
443 get_nsproxy(&init_nsproxy);
444 switch_task_namespaces(current, &init_nsproxy);
446 set_special_pids(&init_struct_pid);
447 proc_clear_tty(current);
449 /* Block and flush all signals */
450 sigfillset(&blocked);
451 sigprocmask(SIG_BLOCK, &blocked, NULL);
452 flush_signals(current);
454 /* Become as one with the init task */
456 exit_fs(current); /* current->fs->count--; */
457 fs = init_task.fs;
458 current->fs = fs;
459 atomic_inc(&fs->count);
461 exit_files(current);
462 current->files = init_task.files;
463 atomic_inc(&current->files->count);
465 reparent_to_kthreadd();
468 EXPORT_SYMBOL(daemonize);
470 static void close_files(struct files_struct * files)
472 int i, j;
473 struct fdtable *fdt;
475 j = 0;
478 * It is safe to dereference the fd table without RCU or
479 * ->file_lock because this is the last reference to the
480 * files structure.
482 fdt = files_fdtable(files);
483 for (;;) {
484 unsigned long set;
485 i = j * __NFDBITS;
486 if (i >= fdt->max_fds)
487 break;
488 set = fdt->open_fds->fds_bits[j++];
489 while (set) {
490 if (set & 1) {
491 struct file * file = xchg(&fdt->fd[i], NULL);
492 if (file) {
493 filp_close(file, files);
494 cond_resched();
497 i++;
498 set >>= 1;
503 struct files_struct *get_files_struct(struct task_struct *task)
505 struct files_struct *files;
507 task_lock(task);
508 files = task->files;
509 if (files)
510 atomic_inc(&files->count);
511 task_unlock(task);
513 return files;
516 void put_files_struct(struct files_struct *files)
518 struct fdtable *fdt;
520 if (atomic_dec_and_test(&files->count)) {
521 close_files(files);
523 * Free the fd and fdset arrays if we expanded them.
524 * If the fdtable was embedded, pass files for freeing
525 * at the end of the RCU grace period. Otherwise,
526 * you can free files immediately.
528 fdt = files_fdtable(files);
529 if (fdt != &files->fdtab)
530 kmem_cache_free(files_cachep, files);
531 free_fdtable(fdt);
535 void reset_files_struct(struct files_struct *files)
537 struct task_struct *tsk = current;
538 struct files_struct *old;
540 old = tsk->files;
541 task_lock(tsk);
542 tsk->files = files;
543 task_unlock(tsk);
544 put_files_struct(old);
547 void exit_files(struct task_struct *tsk)
549 struct files_struct * files = tsk->files;
551 if (files) {
552 task_lock(tsk);
553 tsk->files = NULL;
554 task_unlock(tsk);
555 put_files_struct(files);
559 void put_fs_struct(struct fs_struct *fs)
561 /* No need to hold fs->lock if we are killing it */
562 if (atomic_dec_and_test(&fs->count)) {
563 path_put(&fs->root);
564 path_put(&fs->pwd);
565 kmem_cache_free(fs_cachep, fs);
569 void exit_fs(struct task_struct *tsk)
571 struct fs_struct * fs = tsk->fs;
573 if (fs) {
574 task_lock(tsk);
575 tsk->fs = NULL;
576 task_unlock(tsk);
577 put_fs_struct(fs);
581 EXPORT_SYMBOL_GPL(exit_fs);
583 #ifdef CONFIG_MM_OWNER
585 * Task p is exiting and it owned mm, lets find a new owner for it
587 static inline int
588 mm_need_new_owner(struct mm_struct *mm, struct task_struct *p)
591 * If there are other users of the mm and the owner (us) is exiting
592 * we need to find a new owner to take on the responsibility.
594 if (atomic_read(&mm->mm_users) <= 1)
595 return 0;
596 if (mm->owner != p)
597 return 0;
598 return 1;
601 void mm_update_next_owner(struct mm_struct *mm)
603 struct task_struct *c, *g, *p = current;
605 retry:
606 if (!mm_need_new_owner(mm, p))
607 return;
609 read_lock(&tasklist_lock);
611 * Search in the children
613 list_for_each_entry(c, &p->children, sibling) {
614 if (c->mm == mm)
615 goto assign_new_owner;
619 * Search in the siblings
621 list_for_each_entry(c, &p->parent->children, sibling) {
622 if (c->mm == mm)
623 goto assign_new_owner;
627 * Search through everything else. We should not get
628 * here often
630 do_each_thread(g, c) {
631 if (c->mm == mm)
632 goto assign_new_owner;
633 } while_each_thread(g, c);
635 read_unlock(&tasklist_lock);
637 * We found no owner yet mm_users > 1: this implies that we are
638 * most likely racing with swapoff (try_to_unuse()) or /proc or
639 * ptrace or page migration (get_task_mm()). Mark owner as NULL,
640 * so that subsystems can understand the callback and take action.
642 down_write(&mm->mmap_sem);
643 cgroup_mm_owner_callbacks(mm->owner, NULL);
644 mm->owner = NULL;
645 up_write(&mm->mmap_sem);
646 return;
648 assign_new_owner:
649 BUG_ON(c == p);
650 get_task_struct(c);
652 * The task_lock protects c->mm from changing.
653 * We always want mm->owner->mm == mm
655 task_lock(c);
657 * Delay read_unlock() till we have the task_lock()
658 * to ensure that c does not slip away underneath us
660 read_unlock(&tasklist_lock);
661 if (c->mm != mm) {
662 task_unlock(c);
663 put_task_struct(c);
664 goto retry;
666 cgroup_mm_owner_callbacks(mm->owner, c);
667 mm->owner = c;
668 task_unlock(c);
669 put_task_struct(c);
671 #endif /* CONFIG_MM_OWNER */
674 * Turn us into a lazy TLB process if we
675 * aren't already..
677 static void exit_mm(struct task_struct * tsk)
679 struct mm_struct *mm = tsk->mm;
680 struct core_state *core_state;
682 mm_release(tsk, mm);
683 if (!mm)
684 return;
686 * Serialize with any possible pending coredump.
687 * We must hold mmap_sem around checking core_state
688 * and clearing tsk->mm. The core-inducing thread
689 * will increment ->nr_threads for each thread in the
690 * group with ->mm != NULL.
692 down_read(&mm->mmap_sem);
693 core_state = mm->core_state;
694 if (core_state) {
695 struct core_thread self;
696 up_read(&mm->mmap_sem);
698 self.task = tsk;
699 self.next = xchg(&core_state->dumper.next, &self);
701 * Implies mb(), the result of xchg() must be visible
702 * to core_state->dumper.
704 if (atomic_dec_and_test(&core_state->nr_threads))
705 complete(&core_state->startup);
707 for (;;) {
708 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
709 if (!self.task) /* see coredump_finish() */
710 break;
711 schedule();
713 __set_task_state(tsk, TASK_RUNNING);
714 down_read(&mm->mmap_sem);
716 atomic_inc(&mm->mm_count);
717 BUG_ON(mm != tsk->active_mm);
718 /* more a memory barrier than a real lock */
719 task_lock(tsk);
720 tsk->mm = NULL;
721 up_read(&mm->mmap_sem);
722 enter_lazy_tlb(mm, current);
723 /* We don't want this task to be frozen prematurely */
724 clear_freeze_flag(tsk);
725 task_unlock(tsk);
726 mm_update_next_owner(mm);
727 mmput(mm);
731 * Return nonzero if @parent's children should reap themselves.
733 * Called with write_lock_irq(&tasklist_lock) held.
735 static int ignoring_children(struct task_struct *parent)
737 int ret;
738 struct sighand_struct *psig = parent->sighand;
739 unsigned long flags;
740 spin_lock_irqsave(&psig->siglock, flags);
741 ret = (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
742 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT));
743 spin_unlock_irqrestore(&psig->siglock, flags);
744 return ret;
748 * Detach all tasks we were using ptrace on.
749 * Any that need to be release_task'd are put on the @dead list.
751 * Called with write_lock(&tasklist_lock) held.
753 static void ptrace_exit(struct task_struct *parent, struct list_head *dead)
755 struct task_struct *p, *n;
756 int ign = -1;
758 list_for_each_entry_safe(p, n, &parent->ptraced, ptrace_entry) {
759 __ptrace_unlink(p);
761 if (p->exit_state != EXIT_ZOMBIE)
762 continue;
765 * If it's a zombie, our attachedness prevented normal
766 * parent notification or self-reaping. Do notification
767 * now if it would have happened earlier. If it should
768 * reap itself, add it to the @dead list. We can't call
769 * release_task() here because we already hold tasklist_lock.
771 * If it's our own child, there is no notification to do.
772 * But if our normal children self-reap, then this child
773 * was prevented by ptrace and we must reap it now.
775 if (!task_detached(p) && thread_group_empty(p)) {
776 if (!same_thread_group(p->real_parent, parent))
777 do_notify_parent(p, p->exit_signal);
778 else {
779 if (ign < 0)
780 ign = ignoring_children(parent);
781 if (ign)
782 p->exit_signal = -1;
786 if (task_detached(p)) {
788 * Mark it as in the process of being reaped.
790 p->exit_state = EXIT_DEAD;
791 list_add(&p->ptrace_entry, dead);
797 * Finish up exit-time ptrace cleanup.
799 * Called without locks.
801 static void ptrace_exit_finish(struct task_struct *parent,
802 struct list_head *dead)
804 struct task_struct *p, *n;
806 BUG_ON(!list_empty(&parent->ptraced));
808 list_for_each_entry_safe(p, n, dead, ptrace_entry) {
809 list_del_init(&p->ptrace_entry);
810 release_task(p);
814 static void reparent_thread(struct task_struct *p, struct task_struct *father)
816 if (p->pdeath_signal)
817 /* We already hold the tasklist_lock here. */
818 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
820 list_move_tail(&p->sibling, &p->real_parent->children);
822 /* If this is a threaded reparent there is no need to
823 * notify anyone anything has happened.
825 if (same_thread_group(p->real_parent, father))
826 return;
828 /* We don't want people slaying init. */
829 if (!task_detached(p))
830 p->exit_signal = SIGCHLD;
832 /* If we'd notified the old parent about this child's death,
833 * also notify the new parent.
835 if (!ptrace_reparented(p) &&
836 p->exit_state == EXIT_ZOMBIE &&
837 !task_detached(p) && thread_group_empty(p))
838 do_notify_parent(p, p->exit_signal);
840 kill_orphaned_pgrp(p, father);
844 * When we die, we re-parent all our children.
845 * Try to give them to another thread in our thread
846 * group, and if no such member exists, give it to
847 * the child reaper process (ie "init") in our pid
848 * space.
850 static struct task_struct *find_new_reaper(struct task_struct *father)
852 struct pid_namespace *pid_ns = task_active_pid_ns(father);
853 struct task_struct *thread;
855 thread = father;
856 while_each_thread(father, thread) {
857 if (thread->flags & PF_EXITING)
858 continue;
859 if (unlikely(pid_ns->child_reaper == father))
860 pid_ns->child_reaper = thread;
861 return thread;
864 if (unlikely(pid_ns->child_reaper == father)) {
865 write_unlock_irq(&tasklist_lock);
866 if (unlikely(pid_ns == &init_pid_ns))
867 panic("Attempted to kill init!");
869 zap_pid_ns_processes(pid_ns);
870 write_lock_irq(&tasklist_lock);
872 * We can not clear ->child_reaper or leave it alone.
873 * There may by stealth EXIT_DEAD tasks on ->children,
874 * forget_original_parent() must move them somewhere.
876 pid_ns->child_reaper = init_pid_ns.child_reaper;
879 return pid_ns->child_reaper;
882 static void forget_original_parent(struct task_struct *father)
884 struct task_struct *p, *n, *reaper;
885 LIST_HEAD(ptrace_dead);
887 write_lock_irq(&tasklist_lock);
888 reaper = find_new_reaper(father);
890 * First clean up ptrace if we were using it.
892 ptrace_exit(father, &ptrace_dead);
894 list_for_each_entry_safe(p, n, &father->children, sibling) {
895 p->real_parent = reaper;
896 if (p->parent == father) {
897 BUG_ON(p->ptrace);
898 p->parent = p->real_parent;
900 reparent_thread(p, father);
903 write_unlock_irq(&tasklist_lock);
904 BUG_ON(!list_empty(&father->children));
906 ptrace_exit_finish(father, &ptrace_dead);
910 * Send signals to all our closest relatives so that they know
911 * to properly mourn us..
913 static void exit_notify(struct task_struct *tsk, int group_dead)
915 int signal;
916 void *cookie;
919 * This does two things:
921 * A. Make init inherit all the child processes
922 * B. Check to see if any process groups have become orphaned
923 * as a result of our exiting, and if they have any stopped
924 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
926 forget_original_parent(tsk);
927 exit_task_namespaces(tsk);
929 write_lock_irq(&tasklist_lock);
930 if (group_dead)
931 kill_orphaned_pgrp(tsk->group_leader, NULL);
933 /* Let father know we died
935 * Thread signals are configurable, but you aren't going to use
936 * that to send signals to arbitary processes.
937 * That stops right now.
939 * If the parent exec id doesn't match the exec id we saved
940 * when we started then we know the parent has changed security
941 * domain.
943 * If our self_exec id doesn't match our parent_exec_id then
944 * we have changed execution domain as these two values started
945 * the same after a fork.
947 if (tsk->exit_signal != SIGCHLD && !task_detached(tsk) &&
948 (tsk->parent_exec_id != tsk->real_parent->self_exec_id ||
949 tsk->self_exec_id != tsk->parent_exec_id))
950 tsk->exit_signal = SIGCHLD;
952 signal = tracehook_notify_death(tsk, &cookie, group_dead);
953 if (signal >= 0)
954 signal = do_notify_parent(tsk, signal);
956 tsk->exit_state = signal == DEATH_REAP ? EXIT_DEAD : EXIT_ZOMBIE;
958 /* mt-exec, de_thread() is waiting for us */
959 if (thread_group_leader(tsk) &&
960 tsk->signal->group_exit_task &&
961 tsk->signal->notify_count < 0)
962 wake_up_process(tsk->signal->group_exit_task);
964 write_unlock_irq(&tasklist_lock);
966 tracehook_report_death(tsk, signal, cookie, group_dead);
968 /* If the process is dead, release it - nobody will wait for it */
969 if (signal == DEATH_REAP)
970 release_task(tsk);
973 #ifdef CONFIG_DEBUG_STACK_USAGE
974 static void check_stack_usage(void)
976 static DEFINE_SPINLOCK(low_water_lock);
977 static int lowest_to_date = THREAD_SIZE;
978 unsigned long *n = end_of_stack(current);
979 unsigned long free;
981 while (*n == 0)
982 n++;
983 free = (unsigned long)n - (unsigned long)end_of_stack(current);
985 if (free >= lowest_to_date)
986 return;
988 spin_lock(&low_water_lock);
989 if (free < lowest_to_date) {
990 printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
991 "left\n",
992 current->comm, free);
993 lowest_to_date = free;
995 spin_unlock(&low_water_lock);
997 #else
998 static inline void check_stack_usage(void) {}
999 #endif
1001 NORET_TYPE void do_exit(long code)
1003 struct task_struct *tsk = current;
1004 int group_dead;
1006 profile_task_exit(tsk);
1008 WARN_ON(atomic_read(&tsk->fs_excl));
1010 if (unlikely(in_interrupt()))
1011 panic("Aiee, killing interrupt handler!");
1012 if (unlikely(!tsk->pid))
1013 panic("Attempted to kill the idle task!");
1016 * If do_exit is called because this processes oopsed, it's possible
1017 * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
1018 * continuing. Amongst other possible reasons, this is to prevent
1019 * mm_release()->clear_child_tid() from writing to a user-controlled
1020 * kernel address.
1022 set_fs(USER_DS);
1024 tracehook_report_exit(&code);
1027 * We're taking recursive faults here in do_exit. Safest is to just
1028 * leave this task alone and wait for reboot.
1030 if (unlikely(tsk->flags & PF_EXITING)) {
1031 printk(KERN_ALERT
1032 "Fixing recursive fault but reboot is needed!\n");
1034 * We can do this unlocked here. The futex code uses
1035 * this flag just to verify whether the pi state
1036 * cleanup has been done or not. In the worst case it
1037 * loops once more. We pretend that the cleanup was
1038 * done as there is no way to return. Either the
1039 * OWNER_DIED bit is set by now or we push the blocked
1040 * task into the wait for ever nirwana as well.
1042 tsk->flags |= PF_EXITPIDONE;
1043 if (tsk->io_context)
1044 exit_io_context();
1045 set_current_state(TASK_UNINTERRUPTIBLE);
1046 schedule();
1049 exit_signals(tsk); /* sets PF_EXITING */
1051 * tsk->flags are checked in the futex code to protect against
1052 * an exiting task cleaning up the robust pi futexes.
1054 smp_mb();
1055 spin_unlock_wait(&tsk->pi_lock);
1057 if (unlikely(in_atomic()))
1058 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
1059 current->comm, task_pid_nr(current),
1060 preempt_count());
1062 acct_update_integrals(tsk);
1063 if (tsk->mm) {
1064 update_hiwater_rss(tsk->mm);
1065 update_hiwater_vm(tsk->mm);
1067 group_dead = atomic_dec_and_test(&tsk->signal->live);
1068 if (group_dead) {
1069 hrtimer_cancel(&tsk->signal->real_timer);
1070 exit_itimers(tsk->signal);
1072 acct_collect(code, group_dead);
1073 #ifdef CONFIG_FUTEX
1074 if (unlikely(tsk->robust_list))
1075 exit_robust_list(tsk);
1076 #ifdef CONFIG_COMPAT
1077 if (unlikely(tsk->compat_robust_list))
1078 compat_exit_robust_list(tsk);
1079 #endif
1080 #endif
1081 if (group_dead)
1082 tty_audit_exit();
1083 if (unlikely(tsk->audit_context))
1084 audit_free(tsk);
1086 tsk->exit_code = code;
1087 taskstats_exit(tsk, group_dead);
1089 exit_mm(tsk);
1091 if (group_dead)
1092 acct_process();
1093 exit_sem(tsk);
1094 exit_files(tsk);
1095 exit_fs(tsk);
1096 check_stack_usage();
1097 exit_thread();
1098 cgroup_exit(tsk, 1);
1099 exit_keys(tsk);
1101 if (group_dead && tsk->signal->leader)
1102 disassociate_ctty(1);
1104 module_put(task_thread_info(tsk)->exec_domain->module);
1105 if (tsk->binfmt)
1106 module_put(tsk->binfmt->module);
1108 proc_exit_connector(tsk);
1109 exit_notify(tsk, group_dead);
1110 #ifdef CONFIG_NUMA
1111 mpol_put(tsk->mempolicy);
1112 tsk->mempolicy = NULL;
1113 #endif
1114 #ifdef CONFIG_FUTEX
1116 * This must happen late, after the PID is not
1117 * hashed anymore:
1119 if (unlikely(!list_empty(&tsk->pi_state_list)))
1120 exit_pi_state_list(tsk);
1121 if (unlikely(current->pi_state_cache))
1122 kfree(current->pi_state_cache);
1123 #endif
1125 * Make sure we are holding no locks:
1127 debug_check_no_locks_held(tsk);
1129 * We can do this unlocked here. The futex code uses this flag
1130 * just to verify whether the pi state cleanup has been done
1131 * or not. In the worst case it loops once more.
1133 tsk->flags |= PF_EXITPIDONE;
1135 if (tsk->io_context)
1136 exit_io_context();
1138 if (tsk->splice_pipe)
1139 __free_pipe_info(tsk->splice_pipe);
1141 preempt_disable();
1142 /* causes final put_task_struct in finish_task_switch(). */
1143 tsk->state = TASK_DEAD;
1145 schedule();
1146 BUG();
1147 /* Avoid "noreturn function does return". */
1148 for (;;)
1149 cpu_relax(); /* For when BUG is null */
1152 EXPORT_SYMBOL_GPL(do_exit);
1154 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
1156 if (comp)
1157 complete(comp);
1159 do_exit(code);
1162 EXPORT_SYMBOL(complete_and_exit);
1164 SYSCALL_DEFINE1(exit, int, error_code)
1166 do_exit((error_code&0xff)<<8);
1170 * Take down every thread in the group. This is called by fatal signals
1171 * as well as by sys_exit_group (below).
1173 NORET_TYPE void
1174 do_group_exit(int exit_code)
1176 struct signal_struct *sig = current->signal;
1178 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
1180 if (signal_group_exit(sig))
1181 exit_code = sig->group_exit_code;
1182 else if (!thread_group_empty(current)) {
1183 struct sighand_struct *const sighand = current->sighand;
1184 spin_lock_irq(&sighand->siglock);
1185 if (signal_group_exit(sig))
1186 /* Another thread got here before we took the lock. */
1187 exit_code = sig->group_exit_code;
1188 else {
1189 sig->group_exit_code = exit_code;
1190 sig->flags = SIGNAL_GROUP_EXIT;
1191 zap_other_threads(current);
1193 spin_unlock_irq(&sighand->siglock);
1196 do_exit(exit_code);
1197 /* NOTREACHED */
1201 * this kills every thread in the thread group. Note that any externally
1202 * wait4()-ing process will get the correct exit code - even if this
1203 * thread is not the thread group leader.
1205 SYSCALL_DEFINE1(exit_group, int, error_code)
1207 do_group_exit((error_code & 0xff) << 8);
1208 /* NOTREACHED */
1209 return 0;
1212 static struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
1214 struct pid *pid = NULL;
1215 if (type == PIDTYPE_PID)
1216 pid = task->pids[type].pid;
1217 else if (type < PIDTYPE_MAX)
1218 pid = task->group_leader->pids[type].pid;
1219 return pid;
1222 static int eligible_child(enum pid_type type, struct pid *pid, int options,
1223 struct task_struct *p)
1225 int err;
1227 if (type < PIDTYPE_MAX) {
1228 if (task_pid_type(p, type) != pid)
1229 return 0;
1232 /* Wait for all children (clone and not) if __WALL is set;
1233 * otherwise, wait for clone children *only* if __WCLONE is
1234 * set; otherwise, wait for non-clone children *only*. (Note:
1235 * A "clone" child here is one that reports to its parent
1236 * using a signal other than SIGCHLD.) */
1237 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
1238 && !(options & __WALL))
1239 return 0;
1241 err = security_task_wait(p);
1242 if (err)
1243 return err;
1245 return 1;
1248 static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1249 int why, int status,
1250 struct siginfo __user *infop,
1251 struct rusage __user *rusagep)
1253 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1255 put_task_struct(p);
1256 if (!retval)
1257 retval = put_user(SIGCHLD, &infop->si_signo);
1258 if (!retval)
1259 retval = put_user(0, &infop->si_errno);
1260 if (!retval)
1261 retval = put_user((short)why, &infop->si_code);
1262 if (!retval)
1263 retval = put_user(pid, &infop->si_pid);
1264 if (!retval)
1265 retval = put_user(uid, &infop->si_uid);
1266 if (!retval)
1267 retval = put_user(status, &infop->si_status);
1268 if (!retval)
1269 retval = pid;
1270 return retval;
1274 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1275 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1276 * the lock and this task is uninteresting. If we return nonzero, we have
1277 * released the lock and the system call should return.
1279 static int wait_task_zombie(struct task_struct *p, int options,
1280 struct siginfo __user *infop,
1281 int __user *stat_addr, struct rusage __user *ru)
1283 unsigned long state;
1284 int retval, status, traced;
1285 pid_t pid = task_pid_vnr(p);
1287 if (!likely(options & WEXITED))
1288 return 0;
1290 if (unlikely(options & WNOWAIT)) {
1291 uid_t uid = p->uid;
1292 int exit_code = p->exit_code;
1293 int why, status;
1295 get_task_struct(p);
1296 read_unlock(&tasklist_lock);
1297 if ((exit_code & 0x7f) == 0) {
1298 why = CLD_EXITED;
1299 status = exit_code >> 8;
1300 } else {
1301 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1302 status = exit_code & 0x7f;
1304 return wait_noreap_copyout(p, pid, uid, why,
1305 status, infop, ru);
1309 * Try to move the task's state to DEAD
1310 * only one thread is allowed to do this:
1312 state = xchg(&p->exit_state, EXIT_DEAD);
1313 if (state != EXIT_ZOMBIE) {
1314 BUG_ON(state != EXIT_DEAD);
1315 return 0;
1318 traced = ptrace_reparented(p);
1320 if (likely(!traced)) {
1321 struct signal_struct *psig;
1322 struct signal_struct *sig;
1325 * The resource counters for the group leader are in its
1326 * own task_struct. Those for dead threads in the group
1327 * are in its signal_struct, as are those for the child
1328 * processes it has previously reaped. All these
1329 * accumulate in the parent's signal_struct c* fields.
1331 * We don't bother to take a lock here to protect these
1332 * p->signal fields, because they are only touched by
1333 * __exit_signal, which runs with tasklist_lock
1334 * write-locked anyway, and so is excluded here. We do
1335 * need to protect the access to p->parent->signal fields,
1336 * as other threads in the parent group can be right
1337 * here reaping other children at the same time.
1339 spin_lock_irq(&p->parent->sighand->siglock);
1340 psig = p->parent->signal;
1341 sig = p->signal;
1342 psig->cutime =
1343 cputime_add(psig->cutime,
1344 cputime_add(p->utime,
1345 cputime_add(sig->utime,
1346 sig->cutime)));
1347 psig->cstime =
1348 cputime_add(psig->cstime,
1349 cputime_add(p->stime,
1350 cputime_add(sig->stime,
1351 sig->cstime)));
1352 psig->cgtime =
1353 cputime_add(psig->cgtime,
1354 cputime_add(p->gtime,
1355 cputime_add(sig->gtime,
1356 sig->cgtime)));
1357 psig->cmin_flt +=
1358 p->min_flt + sig->min_flt + sig->cmin_flt;
1359 psig->cmaj_flt +=
1360 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1361 psig->cnvcsw +=
1362 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1363 psig->cnivcsw +=
1364 p->nivcsw + sig->nivcsw + sig->cnivcsw;
1365 psig->cinblock +=
1366 task_io_get_inblock(p) +
1367 sig->inblock + sig->cinblock;
1368 psig->coublock +=
1369 task_io_get_oublock(p) +
1370 sig->oublock + sig->coublock;
1371 task_io_accounting_add(&psig->ioac, &p->ioac);
1372 task_io_accounting_add(&psig->ioac, &sig->ioac);
1373 spin_unlock_irq(&p->parent->sighand->siglock);
1377 * Now we are sure this task is interesting, and no other
1378 * thread can reap it because we set its state to EXIT_DEAD.
1380 read_unlock(&tasklist_lock);
1382 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1383 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1384 ? p->signal->group_exit_code : p->exit_code;
1385 if (!retval && stat_addr)
1386 retval = put_user(status, stat_addr);
1387 if (!retval && infop)
1388 retval = put_user(SIGCHLD, &infop->si_signo);
1389 if (!retval && infop)
1390 retval = put_user(0, &infop->si_errno);
1391 if (!retval && infop) {
1392 int why;
1394 if ((status & 0x7f) == 0) {
1395 why = CLD_EXITED;
1396 status >>= 8;
1397 } else {
1398 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1399 status &= 0x7f;
1401 retval = put_user((short)why, &infop->si_code);
1402 if (!retval)
1403 retval = put_user(status, &infop->si_status);
1405 if (!retval && infop)
1406 retval = put_user(pid, &infop->si_pid);
1407 if (!retval && infop)
1408 retval = put_user(p->uid, &infop->si_uid);
1409 if (!retval)
1410 retval = pid;
1412 if (traced) {
1413 write_lock_irq(&tasklist_lock);
1414 /* We dropped tasklist, ptracer could die and untrace */
1415 ptrace_unlink(p);
1417 * If this is not a detached task, notify the parent.
1418 * If it's still not detached after that, don't release
1419 * it now.
1421 if (!task_detached(p)) {
1422 do_notify_parent(p, p->exit_signal);
1423 if (!task_detached(p)) {
1424 p->exit_state = EXIT_ZOMBIE;
1425 p = NULL;
1428 write_unlock_irq(&tasklist_lock);
1430 if (p != NULL)
1431 release_task(p);
1433 return retval;
1437 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1438 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1439 * the lock and this task is uninteresting. If we return nonzero, we have
1440 * released the lock and the system call should return.
1442 static int wait_task_stopped(int ptrace, struct task_struct *p,
1443 int options, struct siginfo __user *infop,
1444 int __user *stat_addr, struct rusage __user *ru)
1446 int retval, exit_code, why;
1447 uid_t uid = 0; /* unneeded, required by compiler */
1448 pid_t pid;
1450 if (!(options & WUNTRACED))
1451 return 0;
1453 exit_code = 0;
1454 spin_lock_irq(&p->sighand->siglock);
1456 if (unlikely(!task_is_stopped_or_traced(p)))
1457 goto unlock_sig;
1459 if (!ptrace && p->signal->group_stop_count > 0)
1461 * A group stop is in progress and this is the group leader.
1462 * We won't report until all threads have stopped.
1464 goto unlock_sig;
1466 exit_code = p->exit_code;
1467 if (!exit_code)
1468 goto unlock_sig;
1470 if (!unlikely(options & WNOWAIT))
1471 p->exit_code = 0;
1473 uid = p->uid;
1474 unlock_sig:
1475 spin_unlock_irq(&p->sighand->siglock);
1476 if (!exit_code)
1477 return 0;
1480 * Now we are pretty sure this task is interesting.
1481 * Make sure it doesn't get reaped out from under us while we
1482 * give up the lock and then examine it below. We don't want to
1483 * keep holding onto the tasklist_lock while we call getrusage and
1484 * possibly take page faults for user memory.
1486 get_task_struct(p);
1487 pid = task_pid_vnr(p);
1488 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1489 read_unlock(&tasklist_lock);
1491 if (unlikely(options & WNOWAIT))
1492 return wait_noreap_copyout(p, pid, uid,
1493 why, exit_code,
1494 infop, ru);
1496 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1497 if (!retval && stat_addr)
1498 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1499 if (!retval && infop)
1500 retval = put_user(SIGCHLD, &infop->si_signo);
1501 if (!retval && infop)
1502 retval = put_user(0, &infop->si_errno);
1503 if (!retval && infop)
1504 retval = put_user((short)why, &infop->si_code);
1505 if (!retval && infop)
1506 retval = put_user(exit_code, &infop->si_status);
1507 if (!retval && infop)
1508 retval = put_user(pid, &infop->si_pid);
1509 if (!retval && infop)
1510 retval = put_user(uid, &infop->si_uid);
1511 if (!retval)
1512 retval = pid;
1513 put_task_struct(p);
1515 BUG_ON(!retval);
1516 return retval;
1520 * Handle do_wait work for one task in a live, non-stopped state.
1521 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1522 * the lock and this task is uninteresting. If we return nonzero, we have
1523 * released the lock and the system call should return.
1525 static int wait_task_continued(struct task_struct *p, int options,
1526 struct siginfo __user *infop,
1527 int __user *stat_addr, struct rusage __user *ru)
1529 int retval;
1530 pid_t pid;
1531 uid_t uid;
1533 if (!unlikely(options & WCONTINUED))
1534 return 0;
1536 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1537 return 0;
1539 spin_lock_irq(&p->sighand->siglock);
1540 /* Re-check with the lock held. */
1541 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1542 spin_unlock_irq(&p->sighand->siglock);
1543 return 0;
1545 if (!unlikely(options & WNOWAIT))
1546 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1547 spin_unlock_irq(&p->sighand->siglock);
1549 pid = task_pid_vnr(p);
1550 uid = p->uid;
1551 get_task_struct(p);
1552 read_unlock(&tasklist_lock);
1554 if (!infop) {
1555 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1556 put_task_struct(p);
1557 if (!retval && stat_addr)
1558 retval = put_user(0xffff, stat_addr);
1559 if (!retval)
1560 retval = pid;
1561 } else {
1562 retval = wait_noreap_copyout(p, pid, uid,
1563 CLD_CONTINUED, SIGCONT,
1564 infop, ru);
1565 BUG_ON(retval == 0);
1568 return retval;
1572 * Consider @p for a wait by @parent.
1574 * -ECHILD should be in *@notask_error before the first call.
1575 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1576 * Returns zero if the search for a child should continue;
1577 * then *@notask_error is 0 if @p is an eligible child,
1578 * or another error from security_task_wait(), or still -ECHILD.
1580 static int wait_consider_task(struct task_struct *parent, int ptrace,
1581 struct task_struct *p, int *notask_error,
1582 enum pid_type type, struct pid *pid, int options,
1583 struct siginfo __user *infop,
1584 int __user *stat_addr, struct rusage __user *ru)
1586 int ret = eligible_child(type, pid, options, p);
1587 if (!ret)
1588 return ret;
1590 if (unlikely(ret < 0)) {
1592 * If we have not yet seen any eligible child,
1593 * then let this error code replace -ECHILD.
1594 * A permission error will give the user a clue
1595 * to look for security policy problems, rather
1596 * than for mysterious wait bugs.
1598 if (*notask_error)
1599 *notask_error = ret;
1602 if (likely(!ptrace) && unlikely(p->ptrace)) {
1604 * This child is hidden by ptrace.
1605 * We aren't allowed to see it now, but eventually we will.
1607 *notask_error = 0;
1608 return 0;
1611 if (p->exit_state == EXIT_DEAD)
1612 return 0;
1615 * We don't reap group leaders with subthreads.
1617 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
1618 return wait_task_zombie(p, options, infop, stat_addr, ru);
1621 * It's stopped or running now, so it might
1622 * later continue, exit, or stop again.
1624 *notask_error = 0;
1626 if (task_is_stopped_or_traced(p))
1627 return wait_task_stopped(ptrace, p, options,
1628 infop, stat_addr, ru);
1630 return wait_task_continued(p, options, infop, stat_addr, ru);
1634 * Do the work of do_wait() for one thread in the group, @tsk.
1636 * -ECHILD should be in *@notask_error before the first call.
1637 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1638 * Returns zero if the search for a child should continue; then
1639 * *@notask_error is 0 if there were any eligible children,
1640 * or another error from security_task_wait(), or still -ECHILD.
1642 static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1643 enum pid_type type, struct pid *pid, int options,
1644 struct siginfo __user *infop, int __user *stat_addr,
1645 struct rusage __user *ru)
1647 struct task_struct *p;
1649 list_for_each_entry(p, &tsk->children, sibling) {
1651 * Do not consider detached threads.
1653 if (!task_detached(p)) {
1654 int ret = wait_consider_task(tsk, 0, p, notask_error,
1655 type, pid, options,
1656 infop, stat_addr, ru);
1657 if (ret)
1658 return ret;
1662 return 0;
1665 static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
1666 enum pid_type type, struct pid *pid, int options,
1667 struct siginfo __user *infop, int __user *stat_addr,
1668 struct rusage __user *ru)
1670 struct task_struct *p;
1673 * Traditionally we see ptrace'd stopped tasks regardless of options.
1675 options |= WUNTRACED;
1677 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1678 int ret = wait_consider_task(tsk, 1, p, notask_error,
1679 type, pid, options,
1680 infop, stat_addr, ru);
1681 if (ret)
1682 return ret;
1685 return 0;
1688 static long do_wait(enum pid_type type, struct pid *pid, int options,
1689 struct siginfo __user *infop, int __user *stat_addr,
1690 struct rusage __user *ru)
1692 DECLARE_WAITQUEUE(wait, current);
1693 struct task_struct *tsk;
1694 int retval;
1696 add_wait_queue(&current->signal->wait_chldexit,&wait);
1697 repeat:
1699 * If there is nothing that can match our critiera just get out.
1700 * We will clear @retval to zero if we see any child that might later
1701 * match our criteria, even if we are not able to reap it yet.
1703 retval = -ECHILD;
1704 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type])))
1705 goto end;
1707 current->state = TASK_INTERRUPTIBLE;
1708 read_lock(&tasklist_lock);
1709 tsk = current;
1710 do {
1711 int tsk_result = do_wait_thread(tsk, &retval,
1712 type, pid, options,
1713 infop, stat_addr, ru);
1714 if (!tsk_result)
1715 tsk_result = ptrace_do_wait(tsk, &retval,
1716 type, pid, options,
1717 infop, stat_addr, ru);
1718 if (tsk_result) {
1720 * tasklist_lock is unlocked and we have a final result.
1722 retval = tsk_result;
1723 goto end;
1726 if (options & __WNOTHREAD)
1727 break;
1728 tsk = next_thread(tsk);
1729 BUG_ON(tsk->signal != current->signal);
1730 } while (tsk != current);
1731 read_unlock(&tasklist_lock);
1733 if (!retval && !(options & WNOHANG)) {
1734 retval = -ERESTARTSYS;
1735 if (!signal_pending(current)) {
1736 schedule();
1737 goto repeat;
1741 end:
1742 current->state = TASK_RUNNING;
1743 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1744 if (infop) {
1745 if (retval > 0)
1746 retval = 0;
1747 else {
1749 * For a WNOHANG return, clear out all the fields
1750 * we would set so the user can easily tell the
1751 * difference.
1753 if (!retval)
1754 retval = put_user(0, &infop->si_signo);
1755 if (!retval)
1756 retval = put_user(0, &infop->si_errno);
1757 if (!retval)
1758 retval = put_user(0, &infop->si_code);
1759 if (!retval)
1760 retval = put_user(0, &infop->si_pid);
1761 if (!retval)
1762 retval = put_user(0, &infop->si_uid);
1763 if (!retval)
1764 retval = put_user(0, &infop->si_status);
1767 return retval;
1770 SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1771 infop, int, options, struct rusage __user *, ru)
1773 struct pid *pid = NULL;
1774 enum pid_type type;
1775 long ret;
1777 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1778 return -EINVAL;
1779 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1780 return -EINVAL;
1782 switch (which) {
1783 case P_ALL:
1784 type = PIDTYPE_MAX;
1785 break;
1786 case P_PID:
1787 type = PIDTYPE_PID;
1788 if (upid <= 0)
1789 return -EINVAL;
1790 break;
1791 case P_PGID:
1792 type = PIDTYPE_PGID;
1793 if (upid <= 0)
1794 return -EINVAL;
1795 break;
1796 default:
1797 return -EINVAL;
1800 if (type < PIDTYPE_MAX)
1801 pid = find_get_pid(upid);
1802 ret = do_wait(type, pid, options, infop, NULL, ru);
1803 put_pid(pid);
1805 /* avoid REGPARM breakage on x86: */
1806 asmlinkage_protect(5, ret, which, upid, infop, options, ru);
1807 return ret;
1810 SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1811 int, options, struct rusage __user *, ru)
1813 struct pid *pid = NULL;
1814 enum pid_type type;
1815 long ret;
1817 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1818 __WNOTHREAD|__WCLONE|__WALL))
1819 return -EINVAL;
1821 if (upid == -1)
1822 type = PIDTYPE_MAX;
1823 else if (upid < 0) {
1824 type = PIDTYPE_PGID;
1825 pid = find_get_pid(-upid);
1826 } else if (upid == 0) {
1827 type = PIDTYPE_PGID;
1828 pid = get_pid(task_pgrp(current));
1829 } else /* upid > 0 */ {
1830 type = PIDTYPE_PID;
1831 pid = find_get_pid(upid);
1834 ret = do_wait(type, pid, options | WEXITED, NULL, stat_addr, ru);
1835 put_pid(pid);
1837 /* avoid REGPARM breakage on x86: */
1838 asmlinkage_protect(4, ret, upid, stat_addr, options, ru);
1839 return ret;
1842 #ifdef __ARCH_WANT_SYS_WAITPID
1845 * sys_waitpid() remains for compatibility. waitpid() should be
1846 * implemented by calling sys_wait4() from libc.a.
1848 SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
1850 return sys_wait4(pid, stat_addr, options, NULL);
1853 #endif