code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / kernel / exit.c
blob0dd27ca1eb7d03b4e064c31e1e2f56d174fe3542
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/iocontext.h>
16 #include <linux/key.h>
17 #include <linux/security.h>
18 #include <linux/cpu.h>
19 #include <linux/acct.h>
20 #include <linux/tsacct_kern.h>
21 #include <linux/file.h>
22 #include <linux/fdtable.h>
23 #include <linux/binfmts.h>
24 #include <linux/nsproxy.h>
25 #include <linux/pid_namespace.h>
26 #include <linux/ptrace.h>
27 #include <linux/profile.h>
28 #include <linux/mount.h>
29 #include <linux/proc_fs.h>
30 #include <linux/kthread.h>
31 #include <linux/mempolicy.h>
32 #include <linux/taskstats_kern.h>
33 #include <linux/delayacct.h>
34 #include <linux/freezer.h>
35 #include <linux/cgroup.h>
36 #include <linux/syscalls.h>
37 #include <linux/signal.h>
38 #include <linux/posix-timers.h>
39 #include <linux/cn_proc.h>
40 #include <linux/mutex.h>
41 #include <linux/futex.h>
42 #include <linux/pipe_fs_i.h>
43 #include <linux/audit.h> /* for audit_free() */
44 #include <linux/resource.h>
45 #include <linux/blkdev.h>
46 #include <linux/task_io_accounting_ops.h>
47 #include <linux/tracehook.h>
48 #include <linux/fs_struct.h>
49 #include <linux/init_task.h>
50 #include <linux/perf_event.h>
51 #include <trace/events/sched.h>
52 #include <linux/hw_breakpoint.h>
54 #include <asm/uaccess.h>
55 #include <asm/unistd.h>
56 #include <asm/pgtable.h>
57 #include <asm/mmu_context.h>
59 static void exit_mm(struct task_struct * tsk);
61 static void __unhash_process(struct task_struct *p)
63 nr_threads--;
64 detach_pid(p, PIDTYPE_PID);
65 if (thread_group_leader(p)) {
66 detach_pid(p, PIDTYPE_PGID);
67 detach_pid(p, PIDTYPE_SID);
69 list_del_rcu(&p->tasks);
70 list_del_init(&p->sibling);
71 __get_cpu_var(process_counts)--;
73 list_del_rcu(&p->thread_group);
77 * This function expects the tasklist_lock write-locked.
79 static void __exit_signal(struct task_struct *tsk)
81 struct signal_struct *sig = tsk->signal;
82 struct sighand_struct *sighand;
84 BUG_ON(!sig);
85 BUG_ON(!atomic_read(&sig->count));
87 sighand = rcu_dereference_check(tsk->sighand,
88 rcu_read_lock_held() ||
89 lockdep_tasklist_lock_is_held());
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, tsk->utime);
124 sig->stime = cputime_add(sig->stime, tsk->stime);
125 sig->gtime = cputime_add(sig->gtime, tsk->gtime);
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);
155 * Make sure ->signal can't go away under rq->lock,
156 * see account_group_exec_runtime().
158 task_rq_unlock_wait(tsk);
159 __cleanup_signal(sig);
163 static void delayed_put_task_struct(struct rcu_head *rhp)
165 struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
167 #ifdef CONFIG_PERF_EVENTS
168 WARN_ON_ONCE(tsk->perf_event_ctxp);
169 #endif
170 trace_sched_process_free(tsk);
171 put_task_struct(tsk);
175 void release_task(struct task_struct * p)
177 struct task_struct *leader;
178 int zap_leader;
179 repeat:
180 tracehook_prepare_release_task(p);
181 /* don't need to get the RCU readlock here - the process is dead and
182 * can't be modifying its own credentials. But shut RCU-lockdep up */
183 rcu_read_lock();
184 atomic_dec(&__task_cred(p)->user->processes);
185 rcu_read_unlock();
187 proc_flush_task(p);
189 write_lock_irq(&tasklist_lock);
190 tracehook_finish_release_task(p);
191 __exit_signal(p);
194 * If we are the last non-leader member of the thread
195 * group, and the leader is zombie, then notify the
196 * group leader's parent process. (if it wants notification.)
198 zap_leader = 0;
199 leader = p->group_leader;
200 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
201 BUG_ON(task_detached(leader));
202 do_notify_parent(leader, leader->exit_signal);
204 * If we were the last child thread and the leader has
205 * exited already, and the leader's parent ignores SIGCHLD,
206 * then we are the one who should release the leader.
208 * do_notify_parent() will have marked it self-reaping in
209 * that case.
211 zap_leader = task_detached(leader);
214 * This maintains the invariant that release_task()
215 * only runs on a task in EXIT_DEAD, just for sanity.
217 if (zap_leader)
218 leader->exit_state = EXIT_DEAD;
221 write_unlock_irq(&tasklist_lock);
222 release_thread(p);
223 call_rcu(&p->rcu, delayed_put_task_struct);
225 p = leader;
226 if (unlikely(zap_leader))
227 goto repeat;
231 * This checks not only the pgrp, but falls back on the pid if no
232 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
233 * without this...
235 * The caller must hold rcu lock or the tasklist lock.
237 struct pid *session_of_pgrp(struct pid *pgrp)
239 struct task_struct *p;
240 struct pid *sid = NULL;
242 p = pid_task(pgrp, PIDTYPE_PGID);
243 if (p == NULL)
244 p = pid_task(pgrp, PIDTYPE_PID);
245 if (p != NULL)
246 sid = task_session(p);
248 return sid;
252 * Determine if a process group is "orphaned", according to the POSIX
253 * definition in 2.2.2.52. Orphaned process groups are not to be affected
254 * by terminal-generated stop signals. Newly orphaned process groups are
255 * to receive a SIGHUP and a SIGCONT.
257 * "I ask you, have you ever known what it is to be an orphan?"
259 static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
261 struct task_struct *p;
263 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
264 if ((p == ignored_task) ||
265 (p->exit_state && thread_group_empty(p)) ||
266 is_global_init(p->real_parent))
267 continue;
269 if (task_pgrp(p->real_parent) != pgrp &&
270 task_session(p->real_parent) == task_session(p))
271 return 0;
272 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
274 return 1;
277 int is_current_pgrp_orphaned(void)
279 int retval;
281 read_lock(&tasklist_lock);
282 retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
283 read_unlock(&tasklist_lock);
285 return retval;
288 static int has_stopped_jobs(struct pid *pgrp)
290 int retval = 0;
291 struct task_struct *p;
293 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
294 if (!task_is_stopped(p))
295 continue;
296 retval = 1;
297 break;
298 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
299 return retval;
303 * Check to see if any process groups have become orphaned as
304 * a result of our exiting, and if they have any stopped jobs,
305 * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
307 static void
308 kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
310 struct pid *pgrp = task_pgrp(tsk);
311 struct task_struct *ignored_task = tsk;
313 if (!parent)
314 /* exit: our father is in a different pgrp than
315 * we are and we were the only connection outside.
317 parent = tsk->real_parent;
318 else
319 /* reparent: our child is in a different pgrp than
320 * we are, and it was the only connection outside.
322 ignored_task = NULL;
324 if (task_pgrp(parent) != pgrp &&
325 task_session(parent) == task_session(tsk) &&
326 will_become_orphaned_pgrp(pgrp, ignored_task) &&
327 has_stopped_jobs(pgrp)) {
328 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
329 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
334 * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
336 * If a kernel thread is launched as a result of a system call, or if
337 * it ever exits, it should generally reparent itself to kthreadd so it
338 * isn't in the way of other processes and is correctly cleaned up on exit.
340 * The various task state such as scheduling policy and priority may have
341 * been inherited from a user process, so we reset them to sane values here.
343 * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
345 static void reparent_to_kthreadd(void)
347 write_lock_irq(&tasklist_lock);
349 ptrace_unlink(current);
350 /* Reparent to init */
351 current->real_parent = current->parent = kthreadd_task;
352 list_move_tail(&current->sibling, &current->real_parent->children);
354 /* Set the exit signal to SIGCHLD so we signal init on exit */
355 current->exit_signal = SIGCHLD;
357 if (task_nice(current) < 0)
358 set_user_nice(current, 0);
359 /* cpus_allowed? */
360 /* rt_priority? */
361 /* signals? */
362 memcpy(current->signal->rlim, init_task.signal->rlim,
363 sizeof(current->signal->rlim));
365 atomic_inc(&init_cred.usage);
366 commit_creds(&init_cred);
367 write_unlock_irq(&tasklist_lock);
370 void __set_special_pids(struct pid *pid)
372 struct task_struct *curr = current->group_leader;
374 if (task_session(curr) != pid)
375 change_pid(curr, PIDTYPE_SID, pid);
377 if (task_pgrp(curr) != pid)
378 change_pid(curr, PIDTYPE_PGID, pid);
381 static void set_special_pids(struct pid *pid)
383 write_lock_irq(&tasklist_lock);
384 __set_special_pids(pid);
385 write_unlock_irq(&tasklist_lock);
389 * Let kernel threads use this to say that they allow a certain signal.
390 * Must not be used if kthread was cloned with CLONE_SIGHAND.
392 int allow_signal(int sig)
394 if (!valid_signal(sig) || sig < 1)
395 return -EINVAL;
397 spin_lock_irq(&current->sighand->siglock);
398 /* This is only needed for daemonize()'ed kthreads */
399 sigdelset(&current->blocked, sig);
401 * Kernel threads handle their own signals. Let the signal code
402 * know it'll be handled, so that they don't get converted to
403 * SIGKILL or just silently dropped.
405 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
406 recalc_sigpending();
407 spin_unlock_irq(&current->sighand->siglock);
408 return 0;
411 EXPORT_SYMBOL(allow_signal);
413 int disallow_signal(int sig)
415 if (!valid_signal(sig) || sig < 1)
416 return -EINVAL;
418 spin_lock_irq(&current->sighand->siglock);
419 current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
420 recalc_sigpending();
421 spin_unlock_irq(&current->sighand->siglock);
422 return 0;
425 EXPORT_SYMBOL(disallow_signal);
428 * Put all the gunge required to become a kernel thread without
429 * attached user resources in one place where it belongs.
432 void daemonize(const char *name, ...)
434 va_list args;
435 sigset_t blocked;
437 va_start(args, name);
438 vsnprintf(current->comm, sizeof(current->comm), name, args);
439 va_end(args);
442 * If we were started as result of loading a module, close all of the
443 * user space pages. We don't need them, and if we didn't close them
444 * they would be locked into memory.
446 exit_mm(current);
448 * We don't want to have TIF_FREEZE set if the system-wide hibernation
449 * or suspend transition begins right now.
451 current->flags |= (PF_NOFREEZE | PF_KTHREAD);
453 if (current->nsproxy != &init_nsproxy) {
454 get_nsproxy(&init_nsproxy);
455 switch_task_namespaces(current, &init_nsproxy);
457 set_special_pids(&init_struct_pid);
458 proc_clear_tty(current);
460 /* Block and flush all signals */
461 sigfillset(&blocked);
462 sigprocmask(SIG_BLOCK, &blocked, NULL);
463 flush_signals(current);
465 /* Become as one with the init task */
467 daemonize_fs_struct();
468 exit_files(current);
469 current->files = init_task.files;
470 atomic_inc(&current->files->count);
472 reparent_to_kthreadd();
475 EXPORT_SYMBOL(daemonize);
477 static void close_files(struct files_struct * files)
479 int i, j;
480 struct fdtable *fdt;
482 j = 0;
485 * It is safe to dereference the fd table without RCU or
486 * ->file_lock because this is the last reference to the
487 * files structure. But use RCU to shut RCU-lockdep up.
489 rcu_read_lock();
490 fdt = files_fdtable(files);
491 rcu_read_unlock();
492 for (;;) {
493 unsigned long set;
494 i = j * __NFDBITS;
495 if (i >= fdt->max_fds)
496 break;
497 set = fdt->open_fds->fds_bits[j++];
498 while (set) {
499 if (set & 1) {
500 struct file * file = xchg(&fdt->fd[i], NULL);
501 if (file) {
502 filp_close(file, files);
503 cond_resched();
506 i++;
507 set >>= 1;
512 struct files_struct *get_files_struct(struct task_struct *task)
514 struct files_struct *files;
516 task_lock(task);
517 files = task->files;
518 if (files)
519 atomic_inc(&files->count);
520 task_unlock(task);
522 return files;
525 void put_files_struct(struct files_struct *files)
527 struct fdtable *fdt;
529 if (atomic_dec_and_test(&files->count)) {
530 close_files(files);
532 * Free the fd and fdset arrays if we expanded them.
533 * If the fdtable was embedded, pass files for freeing
534 * at the end of the RCU grace period. Otherwise,
535 * you can free files immediately.
537 rcu_read_lock();
538 fdt = files_fdtable(files);
539 if (fdt != &files->fdtab)
540 kmem_cache_free(files_cachep, files);
541 free_fdtable(fdt);
542 rcu_read_unlock();
546 void reset_files_struct(struct files_struct *files)
548 struct task_struct *tsk = current;
549 struct files_struct *old;
551 old = tsk->files;
552 task_lock(tsk);
553 tsk->files = files;
554 task_unlock(tsk);
555 put_files_struct(old);
558 void exit_files(struct task_struct *tsk)
560 struct files_struct * files = tsk->files;
562 if (files) {
563 task_lock(tsk);
564 tsk->files = NULL;
565 task_unlock(tsk);
566 put_files_struct(files);
570 #ifdef CONFIG_MM_OWNER
572 * Task p is exiting and it owned mm, lets find a new owner for it
574 static inline int
575 mm_need_new_owner(struct mm_struct *mm, struct task_struct *p)
578 * If there are other users of the mm and the owner (us) is exiting
579 * we need to find a new owner to take on the responsibility.
581 if (atomic_read(&mm->mm_users) <= 1)
582 return 0;
583 if (mm->owner != p)
584 return 0;
585 return 1;
588 void mm_update_next_owner(struct mm_struct *mm)
590 struct task_struct *c, *g, *p = current;
592 retry:
593 if (!mm_need_new_owner(mm, p))
594 return;
596 read_lock(&tasklist_lock);
598 * Search in the children
600 list_for_each_entry(c, &p->children, sibling) {
601 if (c->mm == mm)
602 goto assign_new_owner;
606 * Search in the siblings
608 list_for_each_entry(c, &p->real_parent->children, sibling) {
609 if (c->mm == mm)
610 goto assign_new_owner;
614 * Search through everything else. We should not get
615 * here often
617 do_each_thread(g, c) {
618 if (c->mm == mm)
619 goto assign_new_owner;
620 } while_each_thread(g, c);
622 read_unlock(&tasklist_lock);
624 * We found no owner yet mm_users > 1: this implies that we are
625 * most likely racing with swapoff (try_to_unuse()) or /proc or
626 * ptrace or page migration (get_task_mm()). Mark owner as NULL.
628 mm->owner = NULL;
629 return;
631 assign_new_owner:
632 BUG_ON(c == p);
633 get_task_struct(c);
635 * The task_lock protects c->mm from changing.
636 * We always want mm->owner->mm == mm
638 task_lock(c);
640 * Delay read_unlock() till we have the task_lock()
641 * to ensure that c does not slip away underneath us
643 read_unlock(&tasklist_lock);
644 if (c->mm != mm) {
645 task_unlock(c);
646 put_task_struct(c);
647 goto retry;
649 mm->owner = c;
650 task_unlock(c);
651 put_task_struct(c);
653 #endif /* CONFIG_MM_OWNER */
656 * Turn us into a lazy TLB process if we
657 * aren't already..
659 static void exit_mm(struct task_struct * tsk)
661 struct mm_struct *mm = tsk->mm;
662 struct core_state *core_state;
664 mm_release(tsk, mm);
665 if (!mm)
666 return;
668 * Serialize with any possible pending coredump.
669 * We must hold mmap_sem around checking core_state
670 * and clearing tsk->mm. The core-inducing thread
671 * will increment ->nr_threads for each thread in the
672 * group with ->mm != NULL.
674 down_read(&mm->mmap_sem);
675 core_state = mm->core_state;
676 if (core_state) {
677 struct core_thread self;
678 up_read(&mm->mmap_sem);
680 self.task = tsk;
681 self.next = xchg(&core_state->dumper.next, &self);
683 * Implies mb(), the result of xchg() must be visible
684 * to core_state->dumper.
686 if (atomic_dec_and_test(&core_state->nr_threads))
687 complete(&core_state->startup);
689 for (;;) {
690 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
691 if (!self.task) /* see coredump_finish() */
692 break;
693 schedule();
695 __set_task_state(tsk, TASK_RUNNING);
696 down_read(&mm->mmap_sem);
698 atomic_inc(&mm->mm_count);
699 BUG_ON(mm != tsk->active_mm);
700 /* more a memory barrier than a real lock */
701 task_lock(tsk);
702 tsk->mm = NULL;
703 up_read(&mm->mmap_sem);
704 enter_lazy_tlb(mm, current);
705 /* We don't want this task to be frozen prematurely */
706 clear_freeze_flag(tsk);
707 task_unlock(tsk);
708 mm_update_next_owner(mm);
709 mmput(mm);
713 * When we die, we re-parent all our children.
714 * Try to give them to another thread in our thread
715 * group, and if no such member exists, give it to
716 * the child reaper process (ie "init") in our pid
717 * space.
719 static struct task_struct *find_new_reaper(struct task_struct *father)
721 struct pid_namespace *pid_ns = task_active_pid_ns(father);
722 struct task_struct *thread;
724 thread = father;
725 while_each_thread(father, thread) {
726 if (thread->flags & PF_EXITING)
727 continue;
728 if (unlikely(pid_ns->child_reaper == father))
729 pid_ns->child_reaper = thread;
730 return thread;
733 if (unlikely(pid_ns->child_reaper == father)) {
734 write_unlock_irq(&tasklist_lock);
735 if (unlikely(pid_ns == &init_pid_ns))
736 panic("Attempted to kill init!");
738 zap_pid_ns_processes(pid_ns);
739 write_lock_irq(&tasklist_lock);
741 * We can not clear ->child_reaper or leave it alone.
742 * There may by stealth EXIT_DEAD tasks on ->children,
743 * forget_original_parent() must move them somewhere.
745 pid_ns->child_reaper = init_pid_ns.child_reaper;
748 return pid_ns->child_reaper;
752 * Any that need to be release_task'd are put on the @dead list.
754 static void reparent_leader(struct task_struct *father, struct task_struct *p,
755 struct list_head *dead)
757 list_move_tail(&p->sibling, &p->real_parent->children);
759 if (task_detached(p))
760 return;
762 * If this is a threaded reparent there is no need to
763 * notify anyone anything has happened.
765 if (same_thread_group(p->real_parent, father))
766 return;
768 /* We don't want people slaying init. */
769 p->exit_signal = SIGCHLD;
771 /* If it has exited notify the new parent about this child's death. */
772 if (!task_ptrace(p) &&
773 p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
774 do_notify_parent(p, p->exit_signal);
775 if (task_detached(p)) {
776 p->exit_state = EXIT_DEAD;
777 list_move_tail(&p->sibling, dead);
781 kill_orphaned_pgrp(p, father);
784 static void forget_original_parent(struct task_struct *father)
786 struct task_struct *p, *n, *reaper;
787 LIST_HEAD(dead_children);
789 exit_ptrace(father);
791 write_lock_irq(&tasklist_lock);
792 reaper = find_new_reaper(father);
794 list_for_each_entry_safe(p, n, &father->children, sibling) {
795 struct task_struct *t = p;
796 do {
797 t->real_parent = reaper;
798 if (t->parent == father) {
799 BUG_ON(task_ptrace(t));
800 t->parent = t->real_parent;
802 if (t->pdeath_signal)
803 group_send_sig_info(t->pdeath_signal,
804 SEND_SIG_NOINFO, t);
805 } while_each_thread(p, t);
806 reparent_leader(father, p, &dead_children);
808 write_unlock_irq(&tasklist_lock);
810 BUG_ON(!list_empty(&father->children));
812 list_for_each_entry_safe(p, n, &dead_children, sibling) {
813 list_del_init(&p->sibling);
814 release_task(p);
819 * Send signals to all our closest relatives so that they know
820 * to properly mourn us..
822 static void exit_notify(struct task_struct *tsk, int group_dead)
824 int signal;
825 void *cookie;
828 * This does two things:
830 * A. Make init inherit all the child processes
831 * B. Check to see if any process groups have become orphaned
832 * as a result of our exiting, and if they have any stopped
833 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
835 forget_original_parent(tsk);
836 exit_task_namespaces(tsk);
838 write_lock_irq(&tasklist_lock);
839 if (group_dead)
840 kill_orphaned_pgrp(tsk->group_leader, NULL);
842 /* Let father know we died
844 * Thread signals are configurable, but you aren't going to use
845 * that to send signals to arbitary processes.
846 * That stops right now.
848 * If the parent exec id doesn't match the exec id we saved
849 * when we started then we know the parent has changed security
850 * domain.
852 * If our self_exec id doesn't match our parent_exec_id then
853 * we have changed execution domain as these two values started
854 * the same after a fork.
856 if (tsk->exit_signal != SIGCHLD && !task_detached(tsk) &&
857 (tsk->parent_exec_id != tsk->real_parent->self_exec_id ||
858 tsk->self_exec_id != tsk->parent_exec_id))
859 tsk->exit_signal = SIGCHLD;
861 signal = tracehook_notify_death(tsk, &cookie, group_dead);
862 if (signal >= 0)
863 signal = do_notify_parent(tsk, signal);
865 tsk->exit_state = signal == DEATH_REAP ? EXIT_DEAD : EXIT_ZOMBIE;
867 /* mt-exec, de_thread() is waiting for us */
868 if (thread_group_leader(tsk) &&
869 tsk->signal->group_exit_task &&
870 tsk->signal->notify_count < 0)
871 wake_up_process(tsk->signal->group_exit_task);
873 write_unlock_irq(&tasklist_lock);
875 tracehook_report_death(tsk, signal, cookie, group_dead);
877 /* If the process is dead, release it - nobody will wait for it */
878 if (signal == DEATH_REAP)
879 release_task(tsk);
882 #ifdef CONFIG_DEBUG_STACK_USAGE
883 static void check_stack_usage(void)
885 static DEFINE_SPINLOCK(low_water_lock);
886 static int lowest_to_date = THREAD_SIZE;
887 unsigned long free;
889 free = stack_not_used(current);
891 if (free >= lowest_to_date)
892 return;
894 spin_lock(&low_water_lock);
895 if (free < lowest_to_date) {
896 printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
897 "left\n",
898 current->comm, free);
899 lowest_to_date = free;
901 spin_unlock(&low_water_lock);
903 #else
904 static inline void check_stack_usage(void) {}
905 #endif
907 NORET_TYPE void do_exit(long code)
909 struct task_struct *tsk = current;
910 int group_dead;
912 profile_task_exit(tsk);
914 WARN_ON(atomic_read(&tsk->fs_excl));
916 if (unlikely(in_interrupt()))
917 panic("Aiee, killing interrupt handler!");
918 if (unlikely(!tsk->pid))
919 panic("Attempted to kill the idle task!");
922 * If do_exit is called because this processes oopsed, it's possible
923 * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
924 * continuing. Amongst other possible reasons, this is to prevent
925 * mm_release()->clear_child_tid() from writing to a user-controlled
926 * kernel address.
928 set_fs(USER_DS);
930 tracehook_report_exit(&code);
932 validate_creds_for_do_exit(tsk);
935 * We're taking recursive faults here in do_exit. Safest is to just
936 * leave this task alone and wait for reboot.
938 if (unlikely(tsk->flags & PF_EXITING)) {
939 printk(KERN_ALERT
940 "Fixing recursive fault but reboot is needed!\n");
942 * We can do this unlocked here. The futex code uses
943 * this flag just to verify whether the pi state
944 * cleanup has been done or not. In the worst case it
945 * loops once more. We pretend that the cleanup was
946 * done as there is no way to return. Either the
947 * OWNER_DIED bit is set by now or we push the blocked
948 * task into the wait for ever nirwana as well.
950 tsk->flags |= PF_EXITPIDONE;
951 set_current_state(TASK_UNINTERRUPTIBLE);
952 schedule();
955 exit_irq_thread();
957 exit_signals(tsk); /* sets PF_EXITING */
959 * tsk->flags are checked in the futex code to protect against
960 * an exiting task cleaning up the robust pi futexes.
962 smp_mb();
963 raw_spin_unlock_wait(&tsk->pi_lock);
965 if (unlikely(in_atomic()))
966 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
967 current->comm, task_pid_nr(current),
968 preempt_count());
970 acct_update_integrals(tsk);
971 /* sync mm's RSS info before statistics gathering */
972 if (tsk->mm)
973 sync_mm_rss(tsk, tsk->mm);
974 group_dead = atomic_dec_and_test(&tsk->signal->live);
975 if (group_dead) {
976 hrtimer_cancel(&tsk->signal->real_timer);
977 exit_itimers(tsk->signal);
978 if (tsk->mm)
979 setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
981 acct_collect(code, group_dead);
982 if (group_dead)
983 tty_audit_exit();
984 if (unlikely(tsk->audit_context))
985 audit_free(tsk);
987 tsk->exit_code = code;
988 taskstats_exit(tsk, group_dead);
990 exit_mm(tsk);
992 if (group_dead)
993 acct_process();
994 trace_sched_process_exit(tsk);
996 exit_sem(tsk);
997 exit_files(tsk);
998 exit_fs(tsk);
999 check_stack_usage();
1000 exit_thread();
1001 cgroup_exit(tsk, 1);
1003 if (group_dead)
1004 disassociate_ctty(1);
1006 module_put(task_thread_info(tsk)->exec_domain->module);
1008 proc_exit_connector(tsk);
1011 * FIXME: do that only when needed, using sched_exit tracepoint
1013 flush_ptrace_hw_breakpoint(tsk);
1015 * Flush inherited counters to the parent - before the parent
1016 * gets woken up by child-exit notifications.
1018 perf_event_exit_task(tsk);
1020 exit_notify(tsk, group_dead);
1021 #ifdef CONFIG_NUMA
1022 mpol_put(tsk->mempolicy);
1023 tsk->mempolicy = NULL;
1024 #endif
1025 #ifdef CONFIG_FUTEX
1026 if (unlikely(current->pi_state_cache))
1027 kfree(current->pi_state_cache);
1028 #endif
1030 * Make sure we are holding no locks:
1032 debug_check_no_locks_held(tsk);
1034 * We can do this unlocked here. The futex code uses this flag
1035 * just to verify whether the pi state cleanup has been done
1036 * or not. In the worst case it loops once more.
1038 tsk->flags |= PF_EXITPIDONE;
1040 if (tsk->io_context)
1041 exit_io_context(tsk);
1043 if (tsk->splice_pipe)
1044 __free_pipe_info(tsk->splice_pipe);
1046 validate_creds_for_do_exit(tsk);
1048 preempt_disable();
1049 exit_rcu();
1050 /* causes final put_task_struct in finish_task_switch(). */
1051 tsk->state = TASK_DEAD;
1052 schedule();
1053 BUG();
1054 /* Avoid "noreturn function does return". */
1055 for (;;)
1056 cpu_relax(); /* For when BUG is null */
1059 EXPORT_SYMBOL_GPL(do_exit);
1061 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
1063 if (comp)
1064 complete(comp);
1066 do_exit(code);
1069 EXPORT_SYMBOL(complete_and_exit);
1071 SYSCALL_DEFINE1(exit, int, error_code)
1073 do_exit((error_code&0xff)<<8);
1077 * Take down every thread in the group. This is called by fatal signals
1078 * as well as by sys_exit_group (below).
1080 NORET_TYPE void
1081 do_group_exit(int exit_code)
1083 struct signal_struct *sig = current->signal;
1085 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
1087 if (signal_group_exit(sig))
1088 exit_code = sig->group_exit_code;
1089 else if (!thread_group_empty(current)) {
1090 struct sighand_struct *const sighand = current->sighand;
1091 spin_lock_irq(&sighand->siglock);
1092 if (signal_group_exit(sig))
1093 /* Another thread got here before we took the lock. */
1094 exit_code = sig->group_exit_code;
1095 else {
1096 sig->group_exit_code = exit_code;
1097 sig->flags = SIGNAL_GROUP_EXIT;
1098 zap_other_threads(current);
1100 spin_unlock_irq(&sighand->siglock);
1103 do_exit(exit_code);
1104 /* NOTREACHED */
1108 * this kills every thread in the thread group. Note that any externally
1109 * wait4()-ing process will get the correct exit code - even if this
1110 * thread is not the thread group leader.
1112 SYSCALL_DEFINE1(exit_group, int, error_code)
1114 do_group_exit((error_code & 0xff) << 8);
1115 /* NOTREACHED */
1116 return 0;
1119 struct wait_opts {
1120 enum pid_type wo_type;
1121 int wo_flags;
1122 struct pid *wo_pid;
1124 struct siginfo __user *wo_info;
1125 int __user *wo_stat;
1126 struct rusage __user *wo_rusage;
1128 wait_queue_t child_wait;
1129 int notask_error;
1132 static inline
1133 struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
1135 if (type != PIDTYPE_PID)
1136 task = task->group_leader;
1137 return task->pids[type].pid;
1140 static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
1142 return wo->wo_type == PIDTYPE_MAX ||
1143 task_pid_type(p, wo->wo_type) == wo->wo_pid;
1146 static int eligible_child(struct wait_opts *wo, struct task_struct *p)
1148 if (!eligible_pid(wo, p))
1149 return 0;
1150 /* Wait for all children (clone and not) if __WALL is set;
1151 * otherwise, wait for clone children *only* if __WCLONE is
1152 * set; otherwise, wait for non-clone children *only*. (Note:
1153 * A "clone" child here is one that reports to its parent
1154 * using a signal other than SIGCHLD.) */
1155 if (((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
1156 && !(wo->wo_flags & __WALL))
1157 return 0;
1159 return 1;
1162 static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
1163 pid_t pid, uid_t uid, int why, int status)
1165 struct siginfo __user *infop;
1166 int retval = wo->wo_rusage
1167 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1169 put_task_struct(p);
1170 infop = wo->wo_info;
1171 if (infop) {
1172 if (!retval)
1173 retval = put_user(SIGCHLD, &infop->si_signo);
1174 if (!retval)
1175 retval = put_user(0, &infop->si_errno);
1176 if (!retval)
1177 retval = put_user((short)why, &infop->si_code);
1178 if (!retval)
1179 retval = put_user(pid, &infop->si_pid);
1180 if (!retval)
1181 retval = put_user(uid, &infop->si_uid);
1182 if (!retval)
1183 retval = put_user(status, &infop->si_status);
1185 if (!retval)
1186 retval = pid;
1187 return retval;
1191 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1192 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1193 * the lock and this task is uninteresting. If we return nonzero, we have
1194 * released the lock and the system call should return.
1196 static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
1198 unsigned long state;
1199 int retval, status, traced;
1200 pid_t pid = task_pid_vnr(p);
1201 uid_t uid = __task_cred(p)->uid;
1202 struct siginfo __user *infop;
1204 if (!likely(wo->wo_flags & WEXITED))
1205 return 0;
1207 if (unlikely(wo->wo_flags & WNOWAIT)) {
1208 int exit_code = p->exit_code;
1209 int why;
1211 get_task_struct(p);
1212 read_unlock(&tasklist_lock);
1213 if ((exit_code & 0x7f) == 0) {
1214 why = CLD_EXITED;
1215 status = exit_code >> 8;
1216 } else {
1217 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1218 status = exit_code & 0x7f;
1220 return wait_noreap_copyout(wo, p, pid, uid, why, status);
1224 * Try to move the task's state to DEAD
1225 * only one thread is allowed to do this:
1227 state = xchg(&p->exit_state, EXIT_DEAD);
1228 if (state != EXIT_ZOMBIE) {
1229 BUG_ON(state != EXIT_DEAD);
1230 return 0;
1233 traced = ptrace_reparented(p);
1235 * It can be ptraced but not reparented, check
1236 * !task_detached() to filter out sub-threads.
1238 if (likely(!traced) && likely(!task_detached(p))) {
1239 struct signal_struct *psig;
1240 struct signal_struct *sig;
1241 unsigned long maxrss;
1242 cputime_t tgutime, tgstime;
1245 * The resource counters for the group leader are in its
1246 * own task_struct. Those for dead threads in the group
1247 * are in its signal_struct, as are those for the child
1248 * processes it has previously reaped. All these
1249 * accumulate in the parent's signal_struct c* fields.
1251 * We don't bother to take a lock here to protect these
1252 * p->signal fields, because they are only touched by
1253 * __exit_signal, which runs with tasklist_lock
1254 * write-locked anyway, and so is excluded here. We do
1255 * need to protect the access to parent->signal fields,
1256 * as other threads in the parent group can be right
1257 * here reaping other children at the same time.
1259 * We use thread_group_times() to get times for the thread
1260 * group, which consolidates times for all threads in the
1261 * group including the group leader.
1263 thread_group_times(p, &tgutime, &tgstime);
1264 spin_lock_irq(&p->real_parent->sighand->siglock);
1265 psig = p->real_parent->signal;
1266 sig = p->signal;
1267 psig->cutime =
1268 cputime_add(psig->cutime,
1269 cputime_add(tgutime,
1270 sig->cutime));
1271 psig->cstime =
1272 cputime_add(psig->cstime,
1273 cputime_add(tgstime,
1274 sig->cstime));
1275 psig->cgtime =
1276 cputime_add(psig->cgtime,
1277 cputime_add(p->gtime,
1278 cputime_add(sig->gtime,
1279 sig->cgtime)));
1280 psig->cmin_flt +=
1281 p->min_flt + sig->min_flt + sig->cmin_flt;
1282 psig->cmaj_flt +=
1283 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1284 psig->cnvcsw +=
1285 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1286 psig->cnivcsw +=
1287 p->nivcsw + sig->nivcsw + sig->cnivcsw;
1288 psig->cinblock +=
1289 task_io_get_inblock(p) +
1290 sig->inblock + sig->cinblock;
1291 psig->coublock +=
1292 task_io_get_oublock(p) +
1293 sig->oublock + sig->coublock;
1294 maxrss = max(sig->maxrss, sig->cmaxrss);
1295 if (psig->cmaxrss < maxrss)
1296 psig->cmaxrss = maxrss;
1297 task_io_accounting_add(&psig->ioac, &p->ioac);
1298 task_io_accounting_add(&psig->ioac, &sig->ioac);
1299 spin_unlock_irq(&p->real_parent->sighand->siglock);
1303 * Now we are sure this task is interesting, and no other
1304 * thread can reap it because we set its state to EXIT_DEAD.
1306 read_unlock(&tasklist_lock);
1308 retval = wo->wo_rusage
1309 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1310 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1311 ? p->signal->group_exit_code : p->exit_code;
1312 if (!retval && wo->wo_stat)
1313 retval = put_user(status, wo->wo_stat);
1315 infop = wo->wo_info;
1316 if (!retval && infop)
1317 retval = put_user(SIGCHLD, &infop->si_signo);
1318 if (!retval && infop)
1319 retval = put_user(0, &infop->si_errno);
1320 if (!retval && infop) {
1321 int why;
1323 if ((status & 0x7f) == 0) {
1324 why = CLD_EXITED;
1325 status >>= 8;
1326 } else {
1327 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1328 status &= 0x7f;
1330 retval = put_user((short)why, &infop->si_code);
1331 if (!retval)
1332 retval = put_user(status, &infop->si_status);
1334 if (!retval && infop)
1335 retval = put_user(pid, &infop->si_pid);
1336 if (!retval && infop)
1337 retval = put_user(uid, &infop->si_uid);
1338 if (!retval)
1339 retval = pid;
1341 if (traced) {
1342 write_lock_irq(&tasklist_lock);
1343 /* We dropped tasklist, ptracer could die and untrace */
1344 ptrace_unlink(p);
1346 * If this is not a detached task, notify the parent.
1347 * If it's still not detached after that, don't release
1348 * it now.
1350 if (!task_detached(p)) {
1351 do_notify_parent(p, p->exit_signal);
1352 if (!task_detached(p)) {
1353 p->exit_state = EXIT_ZOMBIE;
1354 p = NULL;
1357 write_unlock_irq(&tasklist_lock);
1359 if (p != NULL)
1360 release_task(p);
1362 return retval;
1365 static int *task_stopped_code(struct task_struct *p, bool ptrace)
1367 if (ptrace) {
1368 if (task_is_stopped_or_traced(p))
1369 return &p->exit_code;
1370 } else {
1371 if (p->signal->flags & SIGNAL_STOP_STOPPED)
1372 return &p->signal->group_exit_code;
1374 return NULL;
1378 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1379 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1380 * the lock and this task is uninteresting. If we return nonzero, we have
1381 * released the lock and the system call should return.
1383 static int wait_task_stopped(struct wait_opts *wo,
1384 int ptrace, struct task_struct *p)
1386 struct siginfo __user *infop;
1387 int retval, exit_code, *p_code, why;
1388 uid_t uid = 0; /* unneeded, required by compiler */
1389 pid_t pid;
1392 * Traditionally we see ptrace'd stopped tasks regardless of options.
1394 if (!ptrace && !(wo->wo_flags & WUNTRACED))
1395 return 0;
1397 exit_code = 0;
1398 spin_lock_irq(&p->sighand->siglock);
1400 p_code = task_stopped_code(p, ptrace);
1401 if (unlikely(!p_code))
1402 goto unlock_sig;
1404 exit_code = *p_code;
1405 if (!exit_code)
1406 goto unlock_sig;
1408 if (!unlikely(wo->wo_flags & WNOWAIT))
1409 *p_code = 0;
1411 uid = task_uid(p);
1412 unlock_sig:
1413 spin_unlock_irq(&p->sighand->siglock);
1414 if (!exit_code)
1415 return 0;
1418 * Now we are pretty sure this task is interesting.
1419 * Make sure it doesn't get reaped out from under us while we
1420 * give up the lock and then examine it below. We don't want to
1421 * keep holding onto the tasklist_lock while we call getrusage and
1422 * possibly take page faults for user memory.
1424 get_task_struct(p);
1425 pid = task_pid_vnr(p);
1426 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1427 read_unlock(&tasklist_lock);
1429 if (unlikely(wo->wo_flags & WNOWAIT))
1430 return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
1432 retval = wo->wo_rusage
1433 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1434 if (!retval && wo->wo_stat)
1435 retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
1437 infop = wo->wo_info;
1438 if (!retval && infop)
1439 retval = put_user(SIGCHLD, &infop->si_signo);
1440 if (!retval && infop)
1441 retval = put_user(0, &infop->si_errno);
1442 if (!retval && infop)
1443 retval = put_user((short)why, &infop->si_code);
1444 if (!retval && infop)
1445 retval = put_user(exit_code, &infop->si_status);
1446 if (!retval && infop)
1447 retval = put_user(pid, &infop->si_pid);
1448 if (!retval && infop)
1449 retval = put_user(uid, &infop->si_uid);
1450 if (!retval)
1451 retval = pid;
1452 put_task_struct(p);
1454 BUG_ON(!retval);
1455 return retval;
1459 * Handle do_wait work for one task in a live, non-stopped state.
1460 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1461 * the lock and this task is uninteresting. If we return nonzero, we have
1462 * released the lock and the system call should return.
1464 static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
1466 int retval;
1467 pid_t pid;
1468 uid_t uid;
1470 if (!unlikely(wo->wo_flags & WCONTINUED))
1471 return 0;
1473 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1474 return 0;
1476 spin_lock_irq(&p->sighand->siglock);
1477 /* Re-check with the lock held. */
1478 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1479 spin_unlock_irq(&p->sighand->siglock);
1480 return 0;
1482 if (!unlikely(wo->wo_flags & WNOWAIT))
1483 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1484 uid = task_uid(p);
1485 spin_unlock_irq(&p->sighand->siglock);
1487 pid = task_pid_vnr(p);
1488 get_task_struct(p);
1489 read_unlock(&tasklist_lock);
1491 if (!wo->wo_info) {
1492 retval = wo->wo_rusage
1493 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1494 put_task_struct(p);
1495 if (!retval && wo->wo_stat)
1496 retval = put_user(0xffff, wo->wo_stat);
1497 if (!retval)
1498 retval = pid;
1499 } else {
1500 retval = wait_noreap_copyout(wo, p, pid, uid,
1501 CLD_CONTINUED, SIGCONT);
1502 BUG_ON(retval == 0);
1505 return retval;
1509 * Consider @p for a wait by @parent.
1511 * -ECHILD should be in ->notask_error before the first call.
1512 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1513 * Returns zero if the search for a child should continue;
1514 * then ->notask_error is 0 if @p is an eligible child,
1515 * or another error from security_task_wait(), or still -ECHILD.
1517 static int wait_consider_task(struct wait_opts *wo, int ptrace,
1518 struct task_struct *p)
1520 int ret = eligible_child(wo, p);
1521 if (!ret)
1522 return ret;
1524 ret = security_task_wait(p);
1525 if (unlikely(ret < 0)) {
1527 * If we have not yet seen any eligible child,
1528 * then let this error code replace -ECHILD.
1529 * A permission error will give the user a clue
1530 * to look for security policy problems, rather
1531 * than for mysterious wait bugs.
1533 if (wo->notask_error)
1534 wo->notask_error = ret;
1535 return 0;
1538 if (likely(!ptrace) && unlikely(task_ptrace(p))) {
1540 * This child is hidden by ptrace.
1541 * We aren't allowed to see it now, but eventually we will.
1543 wo->notask_error = 0;
1544 return 0;
1547 if (p->exit_state == EXIT_DEAD)
1548 return 0;
1551 * We don't reap group leaders with subthreads.
1553 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
1554 return wait_task_zombie(wo, p);
1557 * It's stopped or running now, so it might
1558 * later continue, exit, or stop again.
1560 wo->notask_error = 0;
1562 if (task_stopped_code(p, ptrace))
1563 return wait_task_stopped(wo, ptrace, p);
1565 return wait_task_continued(wo, p);
1569 * Do the work of do_wait() for one thread in the group, @tsk.
1571 * -ECHILD should be in ->notask_error before the first call.
1572 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1573 * Returns zero if the search for a child should continue; then
1574 * ->notask_error is 0 if there were any eligible children,
1575 * or another error from security_task_wait(), or still -ECHILD.
1577 static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
1579 struct task_struct *p;
1581 list_for_each_entry(p, &tsk->children, sibling) {
1582 int ret = wait_consider_task(wo, 0, p);
1583 if (ret)
1584 return ret;
1587 return 0;
1590 static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
1592 struct task_struct *p;
1594 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1595 int ret = wait_consider_task(wo, 1, p);
1596 if (ret)
1597 return ret;
1600 return 0;
1603 static int child_wait_callback(wait_queue_t *wait, unsigned mode,
1604 int sync, void *key)
1606 struct wait_opts *wo = container_of(wait, struct wait_opts,
1607 child_wait);
1608 struct task_struct *p = key;
1610 if (!eligible_pid(wo, p))
1611 return 0;
1613 if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
1614 return 0;
1616 return default_wake_function(wait, mode, sync, key);
1619 void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
1621 __wake_up_sync_key(&parent->signal->wait_chldexit,
1622 TASK_INTERRUPTIBLE, 1, p);
1625 static long do_wait(struct wait_opts *wo)
1627 struct task_struct *tsk;
1628 int retval;
1630 trace_sched_process_wait(wo->wo_pid);
1632 init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
1633 wo->child_wait.private = current;
1634 add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
1635 repeat:
1637 * If there is nothing that can match our critiera just get out.
1638 * We will clear ->notask_error to zero if we see any child that
1639 * might later match our criteria, even if we are not able to reap
1640 * it yet.
1642 wo->notask_error = -ECHILD;
1643 if ((wo->wo_type < PIDTYPE_MAX) &&
1644 (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
1645 goto notask;
1647 set_current_state(TASK_INTERRUPTIBLE);
1648 read_lock(&tasklist_lock);
1649 tsk = current;
1650 do {
1651 retval = do_wait_thread(wo, tsk);
1652 if (retval)
1653 goto end;
1655 retval = ptrace_do_wait(wo, tsk);
1656 if (retval)
1657 goto end;
1659 if (wo->wo_flags & __WNOTHREAD)
1660 break;
1661 } while_each_thread(current, tsk);
1662 read_unlock(&tasklist_lock);
1664 notask:
1665 retval = wo->notask_error;
1666 if (!retval && !(wo->wo_flags & WNOHANG)) {
1667 retval = -ERESTARTSYS;
1668 if (!signal_pending(current)) {
1669 schedule();
1670 goto repeat;
1673 end:
1674 __set_current_state(TASK_RUNNING);
1675 remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
1676 return retval;
1679 SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1680 infop, int, options, struct rusage __user *, ru)
1682 struct wait_opts wo;
1683 struct pid *pid = NULL;
1684 enum pid_type type;
1685 long ret;
1687 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1688 return -EINVAL;
1689 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1690 return -EINVAL;
1692 switch (which) {
1693 case P_ALL:
1694 type = PIDTYPE_MAX;
1695 break;
1696 case P_PID:
1697 type = PIDTYPE_PID;
1698 if (upid <= 0)
1699 return -EINVAL;
1700 break;
1701 case P_PGID:
1702 type = PIDTYPE_PGID;
1703 if (upid <= 0)
1704 return -EINVAL;
1705 break;
1706 default:
1707 return -EINVAL;
1710 if (type < PIDTYPE_MAX)
1711 pid = find_get_pid(upid);
1713 wo.wo_type = type;
1714 wo.wo_pid = pid;
1715 wo.wo_flags = options;
1716 wo.wo_info = infop;
1717 wo.wo_stat = NULL;
1718 wo.wo_rusage = ru;
1719 ret = do_wait(&wo);
1721 if (ret > 0) {
1722 ret = 0;
1723 } else if (infop) {
1725 * For a WNOHANG return, clear out all the fields
1726 * we would set so the user can easily tell the
1727 * difference.
1729 if (!ret)
1730 ret = put_user(0, &infop->si_signo);
1731 if (!ret)
1732 ret = put_user(0, &infop->si_errno);
1733 if (!ret)
1734 ret = put_user(0, &infop->si_code);
1735 if (!ret)
1736 ret = put_user(0, &infop->si_pid);
1737 if (!ret)
1738 ret = put_user(0, &infop->si_uid);
1739 if (!ret)
1740 ret = put_user(0, &infop->si_status);
1743 put_pid(pid);
1745 /* avoid REGPARM breakage on x86: */
1746 asmlinkage_protect(5, ret, which, upid, infop, options, ru);
1747 return ret;
1750 SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1751 int, options, struct rusage __user *, ru)
1753 struct wait_opts wo;
1754 struct pid *pid = NULL;
1755 enum pid_type type;
1756 long ret;
1758 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1759 __WNOTHREAD|__WCLONE|__WALL))
1760 return -EINVAL;
1762 if (upid == -1)
1763 type = PIDTYPE_MAX;
1764 else if (upid < 0) {
1765 type = PIDTYPE_PGID;
1766 pid = find_get_pid(-upid);
1767 } else if (upid == 0) {
1768 type = PIDTYPE_PGID;
1769 pid = get_task_pid(current, PIDTYPE_PGID);
1770 } else /* upid > 0 */ {
1771 type = PIDTYPE_PID;
1772 pid = find_get_pid(upid);
1775 wo.wo_type = type;
1776 wo.wo_pid = pid;
1777 wo.wo_flags = options | WEXITED;
1778 wo.wo_info = NULL;
1779 wo.wo_stat = stat_addr;
1780 wo.wo_rusage = ru;
1781 ret = do_wait(&wo);
1782 put_pid(pid);
1784 /* avoid REGPARM breakage on x86: */
1785 asmlinkage_protect(4, ret, upid, stat_addr, options, ru);
1786 return ret;
1789 #ifdef __ARCH_WANT_SYS_WAITPID
1792 * sys_waitpid() remains for compatibility. waitpid() should be
1793 * implemented by calling sys_wait4() from libc.a.
1795 SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
1797 return sys_wait4(pid, stat_addr, options, NULL);
1800 #endif