Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/exit.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/mm.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 11 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/completion.h> |
| 13 | #include <linux/personality.h> |
| 14 | #include <linux/tty.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 15 | #include <linux/mnt_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/key.h> |
| 17 | #include <linux/security.h> |
| 18 | #include <linux/cpu.h> |
| 19 | #include <linux/acct.h> |
Jay Lan | 8f0ab51 | 2006-09-30 23:28:59 -0700 | [diff] [blame] | 20 | #include <linux/tsacct_kern.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 22 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/binfmts.h> |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 24 | #include <linux/nsproxy.h> |
Sukadev Bhattiprolu | 84d7378 | 2006-12-08 02:38:01 -0800 | [diff] [blame] | 25 | #include <linux/pid_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/ptrace.h> |
| 27 | #include <linux/profile.h> |
| 28 | #include <linux/mount.h> |
| 29 | #include <linux/proc_fs.h> |
Eric W. Biederman | 49d769d | 2007-05-09 02:34:33 -0700 | [diff] [blame] | 30 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/mempolicy.h> |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 32 | #include <linux/taskstats_kern.h> |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 33 | #include <linux/delayacct.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 34 | #include <linux/freezer.h> |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 35 | #include <linux/cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/syscalls.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 37 | #include <linux/signal.h> |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 38 | #include <linux/posix-timers.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 39 | #include <linux/cn_proc.h> |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 40 | #include <linux/mutex.h> |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 41 | #include <linux/futex.h> |
Ingo Molnar | 34f192c | 2006-03-27 01:16:24 -0800 | [diff] [blame] | 42 | #include <linux/compat.h> |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 43 | #include <linux/pipe_fs_i.h> |
Al Viro | fa84cb9 | 2006-03-29 20:30:19 -0500 | [diff] [blame] | 44 | #include <linux/audit.h> /* for audit_free() */ |
Adrian Bunk | 83cc5ed | 2006-06-25 05:47:41 -0700 | [diff] [blame] | 45 | #include <linux/resource.h> |
David Howells | 0d67a46 | 2006-08-29 19:05:56 +0100 | [diff] [blame] | 46 | #include <linux/blkdev.h> |
Eric Dumazet | 6eaeeab | 2007-05-10 22:22:37 -0700 | [diff] [blame] | 47 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #include <asm/uaccess.h> |
| 50 | #include <asm/unistd.h> |
| 51 | #include <asm/pgtable.h> |
| 52 | #include <asm/mmu_context.h> |
| 53 | |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 54 | static void exit_mm(struct task_struct * tsk); |
| 55 | |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 56 | static inline int task_detached(struct task_struct *p) |
| 57 | { |
| 58 | return p->exit_signal == -1; |
| 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static void __unhash_process(struct task_struct *p) |
| 62 | { |
| 63 | nr_threads--; |
| 64 | detach_pid(p, PIDTYPE_PID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (thread_group_leader(p)) { |
| 66 | detach_pid(p, PIDTYPE_PGID); |
| 67 | detach_pid(p, PIDTYPE_SID); |
Oleg Nesterov | c97d989 | 2006-03-28 16:11:06 -0800 | [diff] [blame] | 68 | |
Eric W. Biederman | 5e85d4a | 2006-04-18 22:20:16 -0700 | [diff] [blame] | 69 | list_del_rcu(&p->tasks); |
Oleg Nesterov | 73b9ebf | 2006-03-28 16:11:07 -0800 | [diff] [blame] | 70 | __get_cpu_var(process_counts)--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
Oleg Nesterov | 47e6532 | 2006-03-28 16:11:25 -0800 | [diff] [blame] | 72 | list_del_rcu(&p->thread_group); |
Oleg Nesterov | c97d989 | 2006-03-28 16:11:06 -0800 | [diff] [blame] | 73 | remove_parent(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 76 | /* |
| 77 | * This function expects the tasklist_lock write-locked. |
| 78 | */ |
| 79 | static void __exit_signal(struct task_struct *tsk) |
| 80 | { |
| 81 | struct signal_struct *sig = tsk->signal; |
| 82 | struct sighand_struct *sighand; |
| 83 | |
| 84 | BUG_ON(!sig); |
| 85 | BUG_ON(!atomic_read(&sig->count)); |
| 86 | |
| 87 | rcu_read_lock(); |
| 88 | sighand = rcu_dereference(tsk->sighand); |
| 89 | spin_lock(&sighand->siglock); |
| 90 | |
| 91 | posix_cpu_timers_exit(tsk); |
| 92 | if (atomic_dec_and_test(&sig->count)) |
| 93 | posix_cpu_timers_exit_group(tsk); |
| 94 | else { |
| 95 | /* |
| 96 | * If there is any task waiting for the group exit |
| 97 | * then notify it: |
| 98 | */ |
Oleg Nesterov | 6db840f | 2007-10-16 23:27:23 -0700 | [diff] [blame] | 99 | if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 100 | wake_up_process(sig->group_exit_task); |
Oleg Nesterov | 6db840f | 2007-10-16 23:27:23 -0700 | [diff] [blame] | 101 | |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 102 | if (tsk == sig->curr_target) |
| 103 | sig->curr_target = next_thread(tsk); |
| 104 | /* |
| 105 | * Accumulate here the counters for all threads but the |
| 106 | * group leader as they die, so they can be added into |
| 107 | * the process-wide totals when those are taken. |
| 108 | * The group leader stays around as a zombie as long |
| 109 | * as there are other threads. When it gets reaped, |
| 110 | * the exit.c code will add its counts into these totals. |
| 111 | * We won't ever get here for the group leader, since it |
| 112 | * will have been the last reference on the signal_struct. |
| 113 | */ |
| 114 | sig->utime = cputime_add(sig->utime, tsk->utime); |
| 115 | sig->stime = cputime_add(sig->stime, tsk->stime); |
Laurent Vivier | 9ac5231 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 116 | sig->gtime = cputime_add(sig->gtime, tsk->gtime); |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 117 | sig->min_flt += tsk->min_flt; |
| 118 | sig->maj_flt += tsk->maj_flt; |
| 119 | sig->nvcsw += tsk->nvcsw; |
| 120 | sig->nivcsw += tsk->nivcsw; |
Eric Dumazet | 6eaeeab | 2007-05-10 22:22:37 -0700 | [diff] [blame] | 121 | sig->inblock += task_io_get_inblock(tsk); |
| 122 | sig->oublock += task_io_get_oublock(tsk); |
Balbir Singh | 172ba84 | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 123 | sig->sum_sched_runtime += tsk->se.sum_exec_runtime; |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 124 | sig = NULL; /* Marker for below. */ |
| 125 | } |
| 126 | |
Oleg Nesterov | 5876700 | 2006-03-28 16:11:20 -0800 | [diff] [blame] | 127 | __unhash_process(tsk); |
| 128 | |
Oleg Nesterov | da7978b | 2008-05-23 13:04:41 -0700 | [diff] [blame] | 129 | /* |
| 130 | * Do this under ->siglock, we can race with another thread |
| 131 | * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals. |
| 132 | */ |
| 133 | flush_sigqueue(&tsk->pending); |
| 134 | |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 135 | tsk->signal = NULL; |
Oleg Nesterov | a7e5328 | 2006-03-28 16:11:27 -0800 | [diff] [blame] | 136 | tsk->sighand = NULL; |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 137 | spin_unlock(&sighand->siglock); |
| 138 | rcu_read_unlock(); |
| 139 | |
Oleg Nesterov | a7e5328 | 2006-03-28 16:11:27 -0800 | [diff] [blame] | 140 | __cleanup_sighand(sighand); |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 141 | clear_tsk_thread_flag(tsk,TIF_SIGPENDING); |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 142 | if (sig) { |
| 143 | flush_sigqueue(&sig->shared_pending); |
Oleg Nesterov | 093a8e8 | 2006-10-28 10:38:51 -0700 | [diff] [blame] | 144 | taskstats_tgid_free(sig); |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 145 | __cleanup_signal(sig); |
| 146 | } |
| 147 | } |
| 148 | |
Eric W. Biederman | 8c7904a | 2006-03-31 02:31:37 -0800 | [diff] [blame] | 149 | static void delayed_put_task_struct(struct rcu_head *rhp) |
| 150 | { |
| 151 | put_task_struct(container_of(rhp, struct task_struct, rcu)); |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | void release_task(struct task_struct * p) |
| 155 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 156 | struct task_struct *leader; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | int zap_leader; |
Oleg Nesterov | 1f09f97 | 2006-03-28 16:11:11 -0800 | [diff] [blame] | 158 | repeat: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | atomic_dec(&p->user->processes); |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 160 | proc_flush_task(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | write_lock_irq(&tasklist_lock); |
Oleg Nesterov | 1f09f97 | 2006-03-28 16:11:11 -0800 | [diff] [blame] | 162 | ptrace_unlink(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children)); |
| 164 | __exit_signal(p); |
Oleg Nesterov | 35f5cad | 2006-03-28 16:11:19 -0800 | [diff] [blame] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* |
| 167 | * If we are the last non-leader member of the thread |
| 168 | * group, and the leader is zombie, then notify the |
| 169 | * group leader's parent process. (if it wants notification.) |
| 170 | */ |
| 171 | zap_leader = 0; |
| 172 | leader = p->group_leader; |
| 173 | if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) { |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 174 | BUG_ON(task_detached(leader)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | do_notify_parent(leader, leader->exit_signal); |
| 176 | /* |
| 177 | * If we were the last child thread and the leader has |
| 178 | * exited already, and the leader's parent ignores SIGCHLD, |
| 179 | * then we are the one who should release the leader. |
| 180 | * |
| 181 | * do_notify_parent() will have marked it self-reaping in |
| 182 | * that case. |
| 183 | */ |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 184 | zap_leader = task_detached(leader); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | write_unlock_irq(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | release_thread(p); |
Eric W. Biederman | 8c7904a | 2006-03-31 02:31:37 -0800 | [diff] [blame] | 189 | call_rcu(&p->rcu, delayed_put_task_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | p = leader; |
| 192 | if (unlikely(zap_leader)) |
| 193 | goto repeat; |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /* |
| 197 | * This checks not only the pgrp, but falls back on the pid if no |
| 198 | * satisfactory pgrp is found. I dunno - gdb doesn't work correctly |
| 199 | * without this... |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 200 | * |
| 201 | * The caller must hold rcu lock or the tasklist lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | */ |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 203 | struct pid *session_of_pgrp(struct pid *pgrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
| 205 | struct task_struct *p; |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 206 | struct pid *sid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 208 | p = pid_task(pgrp, PIDTYPE_PGID); |
Oleg Nesterov | 62dfb55 | 2006-12-08 02:38:03 -0800 | [diff] [blame] | 209 | if (p == NULL) |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 210 | p = pid_task(pgrp, PIDTYPE_PID); |
Oleg Nesterov | 62dfb55 | 2006-12-08 02:38:03 -0800 | [diff] [blame] | 211 | if (p != NULL) |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 212 | sid = task_session(p); |
Oleg Nesterov | 62dfb55 | 2006-12-08 02:38:03 -0800 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return sid; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Determine if a process group is "orphaned", according to the POSIX |
| 219 | * definition in 2.2.2.52. Orphaned process groups are not to be affected |
| 220 | * by terminal-generated stop signals. Newly orphaned process groups are |
| 221 | * to receive a SIGHUP and a SIGCONT. |
| 222 | * |
| 223 | * "I ask you, have you ever known what it is to be an orphan?" |
| 224 | */ |
Eric W. Biederman | 0475ac0 | 2007-02-12 00:52:57 -0800 | [diff] [blame] | 225 | static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Eric W. Biederman | 0475ac0 | 2007-02-12 00:52:57 -0800 | [diff] [blame] | 229 | do_each_pid_task(pgrp, PIDTYPE_PGID, p) { |
Oleg Nesterov | 05e83df | 2008-03-02 21:44:42 +0300 | [diff] [blame] | 230 | if ((p == ignored_task) || |
| 231 | (p->exit_state && thread_group_empty(p)) || |
| 232 | is_global_init(p->real_parent)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | continue; |
Oleg Nesterov | 05e83df | 2008-03-02 21:44:42 +0300 | [diff] [blame] | 234 | |
Eric W. Biederman | 0475ac0 | 2007-02-12 00:52:57 -0800 | [diff] [blame] | 235 | if (task_pgrp(p->real_parent) != pgrp && |
Oleg Nesterov | 05e83df | 2008-03-02 21:44:42 +0300 | [diff] [blame] | 236 | task_session(p->real_parent) == task_session(p)) |
| 237 | return 0; |
Eric W. Biederman | 0475ac0 | 2007-02-12 00:52:57 -0800 | [diff] [blame] | 238 | } while_each_pid_task(pgrp, PIDTYPE_PGID, p); |
Oleg Nesterov | 05e83df | 2008-03-02 21:44:42 +0300 | [diff] [blame] | 239 | |
| 240 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Eric W. Biederman | 3e7cd6c | 2007-02-12 00:52:58 -0800 | [diff] [blame] | 243 | int is_current_pgrp_orphaned(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
| 245 | int retval; |
| 246 | |
| 247 | read_lock(&tasklist_lock); |
Eric W. Biederman | 3e7cd6c | 2007-02-12 00:52:58 -0800 | [diff] [blame] | 248 | retval = will_become_orphaned_pgrp(task_pgrp(current), NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | read_unlock(&tasklist_lock); |
| 250 | |
| 251 | return retval; |
| 252 | } |
| 253 | |
Eric W. Biederman | 0475ac0 | 2007-02-12 00:52:57 -0800 | [diff] [blame] | 254 | static int has_stopped_jobs(struct pid *pgrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | int retval = 0; |
| 257 | struct task_struct *p; |
| 258 | |
Eric W. Biederman | 0475ac0 | 2007-02-12 00:52:57 -0800 | [diff] [blame] | 259 | do_each_pid_task(pgrp, PIDTYPE_PGID, p) { |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 260 | if (!task_is_stopped(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | retval = 1; |
| 263 | break; |
Eric W. Biederman | 0475ac0 | 2007-02-12 00:52:57 -0800 | [diff] [blame] | 264 | } while_each_pid_task(pgrp, PIDTYPE_PGID, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return retval; |
| 266 | } |
| 267 | |
Oleg Nesterov | f49ee50 | 2008-03-02 21:44:40 +0300 | [diff] [blame] | 268 | /* |
| 269 | * Check to see if any process groups have become orphaned as |
| 270 | * a result of our exiting, and if they have any stopped jobs, |
| 271 | * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) |
| 272 | */ |
| 273 | static void |
| 274 | kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent) |
| 275 | { |
| 276 | struct pid *pgrp = task_pgrp(tsk); |
| 277 | struct task_struct *ignored_task = tsk; |
| 278 | |
| 279 | if (!parent) |
| 280 | /* exit: our father is in a different pgrp than |
| 281 | * we are and we were the only connection outside. |
| 282 | */ |
| 283 | parent = tsk->real_parent; |
| 284 | else |
| 285 | /* reparent: our child is in a different pgrp than |
| 286 | * we are, and it was the only connection outside. |
| 287 | */ |
| 288 | ignored_task = NULL; |
| 289 | |
| 290 | if (task_pgrp(parent) != pgrp && |
| 291 | task_session(parent) == task_session(tsk) && |
| 292 | will_become_orphaned_pgrp(pgrp, ignored_task) && |
| 293 | has_stopped_jobs(pgrp)) { |
| 294 | __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp); |
| 295 | __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp); |
| 296 | } |
| 297 | } |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | /** |
Eric W. Biederman | 49d769d | 2007-05-09 02:34:33 -0700 | [diff] [blame] | 300 | * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | * |
| 302 | * If a kernel thread is launched as a result of a system call, or if |
Eric W. Biederman | 49d769d | 2007-05-09 02:34:33 -0700 | [diff] [blame] | 303 | * it ever exits, it should generally reparent itself to kthreadd so it |
| 304 | * isn't in the way of other processes and is correctly cleaned up on exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | * |
| 306 | * The various task state such as scheduling policy and priority may have |
| 307 | * been inherited from a user process, so we reset them to sane values here. |
| 308 | * |
Eric W. Biederman | 49d769d | 2007-05-09 02:34:33 -0700 | [diff] [blame] | 309 | * NOTE that reparent_to_kthreadd() gives the caller full capabilities. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | */ |
Eric W. Biederman | 49d769d | 2007-05-09 02:34:33 -0700 | [diff] [blame] | 311 | static void reparent_to_kthreadd(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | write_lock_irq(&tasklist_lock); |
| 314 | |
| 315 | ptrace_unlink(current); |
| 316 | /* Reparent to init */ |
Oleg Nesterov | 9b678ec | 2006-03-28 16:11:05 -0800 | [diff] [blame] | 317 | remove_parent(current); |
Eric W. Biederman | 49d769d | 2007-05-09 02:34:33 -0700 | [diff] [blame] | 318 | current->real_parent = current->parent = kthreadd_task; |
Oleg Nesterov | 9b678ec | 2006-03-28 16:11:05 -0800 | [diff] [blame] | 319 | add_parent(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | /* Set the exit signal to SIGCHLD so we signal init on exit */ |
| 322 | current->exit_signal = SIGCHLD; |
| 323 | |
Ingo Molnar | e05606d | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 324 | if (task_nice(current) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | set_user_nice(current, 0); |
| 326 | /* cpus_allowed? */ |
| 327 | /* rt_priority? */ |
| 328 | /* signals? */ |
| 329 | security_task_reparent_to_init(current); |
| 330 | memcpy(current->signal->rlim, init_task.signal->rlim, |
| 331 | sizeof(current->signal->rlim)); |
| 332 | atomic_inc(&(INIT_USER->__count)); |
| 333 | write_unlock_irq(&tasklist_lock); |
| 334 | switch_uid(INIT_USER); |
| 335 | } |
| 336 | |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 337 | void __set_special_pids(struct pid *pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Oren Laadan | e19f247 | 2006-01-08 01:03:58 -0800 | [diff] [blame] | 339 | struct task_struct *curr = current->group_leader; |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 340 | pid_t nr = pid_nr(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 342 | if (task_session(curr) != pid) { |
Oleg Nesterov | 7d8da09 | 2008-04-30 00:54:27 -0700 | [diff] [blame] | 343 | change_pid(curr, PIDTYPE_SID, pid); |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 344 | set_task_session(curr, nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 346 | if (task_pgrp(curr) != pid) { |
Oleg Nesterov | 7d8da09 | 2008-04-30 00:54:27 -0700 | [diff] [blame] | 347 | change_pid(curr, PIDTYPE_PGID, pid); |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 348 | set_task_pgrp(curr, nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 352 | static void set_special_pids(struct pid *pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | write_lock_irq(&tasklist_lock); |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 355 | __set_special_pids(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | write_unlock_irq(&tasklist_lock); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Let kernel threads use this to say that they |
| 361 | * allow a certain signal (since daemonize() will |
| 362 | * have disabled all of them by default). |
| 363 | */ |
| 364 | int allow_signal(int sig) |
| 365 | { |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 366 | if (!valid_signal(sig) || sig < 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return -EINVAL; |
| 368 | |
| 369 | spin_lock_irq(¤t->sighand->siglock); |
| 370 | sigdelset(¤t->blocked, sig); |
| 371 | if (!current->mm) { |
| 372 | /* Kernel threads handle their own signals. |
| 373 | Let the signal code know it'll be handled, so |
| 374 | that they don't get converted to SIGKILL or |
| 375 | just silently dropped */ |
| 376 | current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2; |
| 377 | } |
| 378 | recalc_sigpending(); |
| 379 | spin_unlock_irq(¤t->sighand->siglock); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | EXPORT_SYMBOL(allow_signal); |
| 384 | |
| 385 | int disallow_signal(int sig) |
| 386 | { |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 387 | if (!valid_signal(sig) || sig < 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | |
| 390 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 10ab825 | 2007-05-09 02:34:37 -0700 | [diff] [blame] | 391 | current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | recalc_sigpending(); |
| 393 | spin_unlock_irq(¤t->sighand->siglock); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | EXPORT_SYMBOL(disallow_signal); |
| 398 | |
| 399 | /* |
| 400 | * Put all the gunge required to become a kernel thread without |
| 401 | * attached user resources in one place where it belongs. |
| 402 | */ |
| 403 | |
| 404 | void daemonize(const char *name, ...) |
| 405 | { |
| 406 | va_list args; |
| 407 | struct fs_struct *fs; |
| 408 | sigset_t blocked; |
| 409 | |
| 410 | va_start(args, name); |
| 411 | vsnprintf(current->comm, sizeof(current->comm), name, args); |
| 412 | va_end(args); |
| 413 | |
| 414 | /* |
| 415 | * If we were started as result of loading a module, close all of the |
| 416 | * user space pages. We don't need them, and if we didn't close them |
| 417 | * they would be locked into memory. |
| 418 | */ |
| 419 | exit_mm(current); |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 420 | /* |
| 421 | * We don't want to have TIF_FREEZE set if the system-wide hibernation |
| 422 | * or suspend transition begins right now. |
| 423 | */ |
| 424 | current->flags |= PF_NOFREEZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 426 | if (current->nsproxy != &init_nsproxy) { |
| 427 | get_nsproxy(&init_nsproxy); |
| 428 | switch_task_namespaces(current, &init_nsproxy); |
| 429 | } |
Oleg Nesterov | 297bd42 | 2008-02-08 04:19:10 -0800 | [diff] [blame] | 430 | set_special_pids(&init_struct_pid); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 431 | proc_clear_tty(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | /* Block and flush all signals */ |
| 434 | sigfillset(&blocked); |
| 435 | sigprocmask(SIG_BLOCK, &blocked, NULL); |
| 436 | flush_signals(current); |
| 437 | |
| 438 | /* Become as one with the init task */ |
| 439 | |
| 440 | exit_fs(current); /* current->fs->count--; */ |
| 441 | fs = init_task.fs; |
| 442 | current->fs = fs; |
| 443 | atomic_inc(&fs->count); |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 444 | |
Daniel Walker | d4c5e41 | 2007-10-18 23:39:59 -0700 | [diff] [blame] | 445 | exit_files(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | current->files = init_task.files; |
| 447 | atomic_inc(¤t->files->count); |
| 448 | |
Eric W. Biederman | 49d769d | 2007-05-09 02:34:33 -0700 | [diff] [blame] | 449 | reparent_to_kthreadd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | EXPORT_SYMBOL(daemonize); |
| 453 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 454 | static void close_files(struct files_struct * files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | int i, j; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 457 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | j = 0; |
Dipankar Sarma | 4fb3a53 | 2005-09-16 19:28:13 -0700 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * It is safe to dereference the fd table without RCU or |
| 463 | * ->file_lock because this is the last reference to the |
| 464 | * files structure. |
| 465 | */ |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 466 | fdt = files_fdtable(files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | for (;;) { |
| 468 | unsigned long set; |
| 469 | i = j * __NFDBITS; |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 470 | if (i >= fdt->max_fds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | break; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 472 | set = fdt->open_fds->fds_bits[j++]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | while (set) { |
| 474 | if (set & 1) { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 475 | struct file * file = xchg(&fdt->fd[i], NULL); |
Ingo Molnar | 944be0b | 2007-02-12 00:52:26 -0800 | [diff] [blame] | 476 | if (file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | filp_close(file, files); |
Ingo Molnar | 944be0b | 2007-02-12 00:52:26 -0800 | [diff] [blame] | 478 | cond_resched(); |
| 479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | i++; |
| 482 | set >>= 1; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | struct files_struct *get_files_struct(struct task_struct *task) |
| 488 | { |
| 489 | struct files_struct *files; |
| 490 | |
| 491 | task_lock(task); |
| 492 | files = task->files; |
| 493 | if (files) |
| 494 | atomic_inc(&files->count); |
| 495 | task_unlock(task); |
| 496 | |
| 497 | return files; |
| 498 | } |
| 499 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 500 | void put_files_struct(struct files_struct *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 502 | struct fdtable *fdt; |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (atomic_dec_and_test(&files->count)) { |
| 505 | close_files(files); |
| 506 | /* |
| 507 | * Free the fd and fdset arrays if we expanded them. |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 508 | * If the fdtable was embedded, pass files for freeing |
| 509 | * at the end of the RCU grace period. Otherwise, |
| 510 | * you can free files immediately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | */ |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 512 | fdt = files_fdtable(files); |
Vadim Lobanov | 4fd4581 | 2006-12-10 02:21:17 -0800 | [diff] [blame] | 513 | if (fdt != &files->fdtab) |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 514 | kmem_cache_free(files_cachep, files); |
Vadim Lobanov | 01b2d93 | 2006-12-22 01:10:43 -0800 | [diff] [blame] | 515 | free_fdtable(fdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Al Viro | 3b12538 | 2008-04-22 05:31:30 -0400 | [diff] [blame] | 519 | void reset_files_struct(struct files_struct *files) |
Kirill Korotaev | 3b9b8ab | 2006-09-29 02:00:05 -0700 | [diff] [blame] | 520 | { |
Al Viro | 3b12538 | 2008-04-22 05:31:30 -0400 | [diff] [blame] | 521 | struct task_struct *tsk = current; |
Kirill Korotaev | 3b9b8ab | 2006-09-29 02:00:05 -0700 | [diff] [blame] | 522 | struct files_struct *old; |
| 523 | |
| 524 | old = tsk->files; |
| 525 | task_lock(tsk); |
| 526 | tsk->files = files; |
| 527 | task_unlock(tsk); |
| 528 | put_files_struct(old); |
| 529 | } |
Kirill Korotaev | 3b9b8ab | 2006-09-29 02:00:05 -0700 | [diff] [blame] | 530 | |
Al Viro | 1ec7f1d | 2008-04-22 05:35:42 -0400 | [diff] [blame] | 531 | void exit_files(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
| 533 | struct files_struct * files = tsk->files; |
| 534 | |
| 535 | if (files) { |
| 536 | task_lock(tsk); |
| 537 | tsk->files = NULL; |
| 538 | task_unlock(tsk); |
| 539 | put_files_struct(files); |
| 540 | } |
| 541 | } |
| 542 | |
Al Viro | 1ec7f1d | 2008-04-22 05:35:42 -0400 | [diff] [blame] | 543 | void put_fs_struct(struct fs_struct *fs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
| 545 | /* No need to hold fs->lock if we are killing it */ |
| 546 | if (atomic_dec_and_test(&fs->count)) { |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 547 | path_put(&fs->root); |
| 548 | path_put(&fs->pwd); |
| 549 | if (fs->altroot.dentry) |
| 550 | path_put(&fs->altroot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | kmem_cache_free(fs_cachep, fs); |
| 552 | } |
| 553 | } |
| 554 | |
Al Viro | 1ec7f1d | 2008-04-22 05:35:42 -0400 | [diff] [blame] | 555 | void exit_fs(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
| 557 | struct fs_struct * fs = tsk->fs; |
| 558 | |
| 559 | if (fs) { |
| 560 | task_lock(tsk); |
| 561 | tsk->fs = NULL; |
| 562 | task_unlock(tsk); |
Al Viro | 1ec7f1d | 2008-04-22 05:35:42 -0400 | [diff] [blame] | 563 | put_fs_struct(fs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | EXPORT_SYMBOL_GPL(exit_fs); |
| 568 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 569 | #ifdef CONFIG_MM_OWNER |
| 570 | /* |
| 571 | * Task p is exiting and it owned mm, lets find a new owner for it |
| 572 | */ |
| 573 | static inline int |
| 574 | mm_need_new_owner(struct mm_struct *mm, struct task_struct *p) |
| 575 | { |
| 576 | /* |
| 577 | * If there are other users of the mm and the owner (us) is exiting |
| 578 | * we need to find a new owner to take on the responsibility. |
| 579 | */ |
| 580 | if (!mm) |
| 581 | return 0; |
| 582 | if (atomic_read(&mm->mm_users) <= 1) |
| 583 | return 0; |
| 584 | if (mm->owner != p) |
| 585 | return 0; |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | void mm_update_next_owner(struct mm_struct *mm) |
| 590 | { |
| 591 | struct task_struct *c, *g, *p = current; |
| 592 | |
| 593 | retry: |
| 594 | if (!mm_need_new_owner(mm, p)) |
| 595 | return; |
| 596 | |
| 597 | read_lock(&tasklist_lock); |
| 598 | /* |
| 599 | * Search in the children |
| 600 | */ |
| 601 | list_for_each_entry(c, &p->children, sibling) { |
| 602 | if (c->mm == mm) |
| 603 | goto assign_new_owner; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Search in the siblings |
| 608 | */ |
| 609 | list_for_each_entry(c, &p->parent->children, sibling) { |
| 610 | if (c->mm == mm) |
| 611 | goto assign_new_owner; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Search through everything else. We should not get |
| 616 | * here often |
| 617 | */ |
| 618 | do_each_thread(g, c) { |
| 619 | if (c->mm == mm) |
| 620 | goto assign_new_owner; |
| 621 | } while_each_thread(g, c); |
| 622 | |
| 623 | read_unlock(&tasklist_lock); |
| 624 | return; |
| 625 | |
| 626 | assign_new_owner: |
| 627 | BUG_ON(c == p); |
| 628 | get_task_struct(c); |
| 629 | /* |
| 630 | * The task_lock protects c->mm from changing. |
| 631 | * We always want mm->owner->mm == mm |
| 632 | */ |
| 633 | task_lock(c); |
| 634 | /* |
| 635 | * Delay read_unlock() till we have the task_lock() |
| 636 | * to ensure that c does not slip away underneath us |
| 637 | */ |
| 638 | read_unlock(&tasklist_lock); |
| 639 | if (c->mm != mm) { |
| 640 | task_unlock(c); |
| 641 | put_task_struct(c); |
| 642 | goto retry; |
| 643 | } |
| 644 | cgroup_mm_owner_callbacks(mm->owner, c); |
| 645 | mm->owner = c; |
| 646 | task_unlock(c); |
| 647 | put_task_struct(c); |
| 648 | } |
| 649 | #endif /* CONFIG_MM_OWNER */ |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* |
| 652 | * Turn us into a lazy TLB process if we |
| 653 | * aren't already.. |
| 654 | */ |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 655 | static void exit_mm(struct task_struct * tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | struct mm_struct *mm = tsk->mm; |
| 658 | |
| 659 | mm_release(tsk, mm); |
| 660 | if (!mm) |
| 661 | return; |
| 662 | /* |
| 663 | * Serialize with any possible pending coredump. |
| 664 | * We must hold mmap_sem around checking core_waiters |
| 665 | * and clearing tsk->mm. The core-inducing thread |
| 666 | * will increment core_waiters for each thread in the |
| 667 | * group with ->mm != NULL. |
| 668 | */ |
| 669 | down_read(&mm->mmap_sem); |
| 670 | if (mm->core_waiters) { |
| 671 | up_read(&mm->mmap_sem); |
| 672 | down_write(&mm->mmap_sem); |
| 673 | if (!--mm->core_waiters) |
| 674 | complete(mm->core_startup_done); |
| 675 | up_write(&mm->mmap_sem); |
| 676 | |
| 677 | wait_for_completion(&mm->core_done); |
| 678 | down_read(&mm->mmap_sem); |
| 679 | } |
| 680 | atomic_inc(&mm->mm_count); |
Eric Sesterhenn | 125e187 | 2006-06-23 02:06:06 -0700 | [diff] [blame] | 681 | BUG_ON(mm != tsk->active_mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | /* more a memory barrier than a real lock */ |
| 683 | task_lock(tsk); |
| 684 | tsk->mm = NULL; |
| 685 | up_read(&mm->mmap_sem); |
| 686 | enter_lazy_tlb(mm, current); |
Rafael J. Wysocki | 0c1eecf | 2007-07-19 01:47:33 -0700 | [diff] [blame] | 687 | /* We don't want this task to be frozen prematurely */ |
| 688 | clear_freeze_flag(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | task_unlock(tsk); |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 690 | mm_update_next_owner(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | mmput(mm); |
| 692 | } |
| 693 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 694 | static void |
| 695 | reparent_thread(struct task_struct *p, struct task_struct *father, int traced) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | { |
Oleg Nesterov | 241ceee | 2006-12-24 23:30:44 +0300 | [diff] [blame] | 697 | if (p->pdeath_signal) |
| 698 | /* We already hold the tasklist_lock here. */ |
| 699 | group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p); |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | /* Move the child from its dying parent to the new one. */ |
| 702 | if (unlikely(traced)) { |
| 703 | /* Preserve ptrace links if someone else is tracing this child. */ |
| 704 | list_del_init(&p->ptrace_list); |
Oleg Nesterov | 53b6f9f | 2008-04-30 00:53:13 -0700 | [diff] [blame] | 705 | if (ptrace_reparented(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | list_add(&p->ptrace_list, &p->real_parent->ptrace_children); |
| 707 | } else { |
| 708 | /* If this child is being traced, then we're the one tracing it |
| 709 | * anyway, so let go of it. |
| 710 | */ |
| 711 | p->ptrace = 0; |
Oleg Nesterov | 6ac781b | 2006-03-28 16:11:09 -0800 | [diff] [blame] | 712 | remove_parent(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | p->parent = p->real_parent; |
Oleg Nesterov | 6ac781b | 2006-03-28 16:11:09 -0800 | [diff] [blame] | 714 | add_parent(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 716 | if (task_is_traced(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | /* |
| 718 | * If it was at a trace stop, turn it into |
| 719 | * a normal stop since it's no longer being |
| 720 | * traced. |
| 721 | */ |
| 722 | ptrace_untrace(p); |
| 723 | } |
| 724 | } |
| 725 | |
Eric W. Biederman | b2b2cbc | 2006-12-21 21:28:40 -0700 | [diff] [blame] | 726 | /* If this is a threaded reparent there is no need to |
| 727 | * notify anyone anything has happened. |
| 728 | */ |
Oleg Nesterov | 376e1d2 | 2008-04-30 00:53:12 -0700 | [diff] [blame] | 729 | if (same_thread_group(p->real_parent, father)) |
Eric W. Biederman | b2b2cbc | 2006-12-21 21:28:40 -0700 | [diff] [blame] | 730 | return; |
| 731 | |
| 732 | /* We don't want people slaying init. */ |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 733 | if (!task_detached(p)) |
Eric W. Biederman | b2b2cbc | 2006-12-21 21:28:40 -0700 | [diff] [blame] | 734 | p->exit_signal = SIGCHLD; |
Eric W. Biederman | b2b2cbc | 2006-12-21 21:28:40 -0700 | [diff] [blame] | 735 | |
| 736 | /* If we'd notified the old parent about this child's death, |
| 737 | * also notify the new parent. |
| 738 | */ |
| 739 | if (!traced && p->exit_state == EXIT_ZOMBIE && |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 740 | !task_detached(p) && thread_group_empty(p)) |
Eric W. Biederman | b2b2cbc | 2006-12-21 21:28:40 -0700 | [diff] [blame] | 741 | do_notify_parent(p, p->exit_signal); |
| 742 | |
Oleg Nesterov | f49ee50 | 2008-03-02 21:44:40 +0300 | [diff] [blame] | 743 | kill_orphaned_pgrp(p, father); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* |
| 747 | * When we die, we re-parent all our children. |
| 748 | * Try to give them to another thread in our thread |
| 749 | * group, and if no such member exists, give it to |
Sukadev Bhattiprolu | 84d7378 | 2006-12-08 02:38:01 -0800 | [diff] [blame] | 750 | * the child reaper process (ie "init") in our pid |
| 751 | * space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | */ |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 753 | static void forget_original_parent(struct task_struct *father) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | { |
Matthias Kaehlcke | 03ff179 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 755 | struct task_struct *p, *n, *reaper = father; |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 756 | struct list_head ptrace_dead; |
| 757 | |
| 758 | INIT_LIST_HEAD(&ptrace_dead); |
| 759 | |
| 760 | write_lock_irq(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
| 762 | do { |
| 763 | reaper = next_thread(reaper); |
| 764 | if (reaper == father) { |
Sukadev Bhattiprolu | 88f21d8 | 2007-10-18 23:39:50 -0700 | [diff] [blame] | 765 | reaper = task_child_reaper(father); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | break; |
| 767 | } |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 768 | } while (reaper->flags & PF_EXITING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | /* |
| 771 | * There are only two places where our children can be: |
| 772 | * |
| 773 | * - in our child list |
| 774 | * - in our ptraced child list |
| 775 | * |
| 776 | * Search them and reparent children. |
| 777 | */ |
Matthias Kaehlcke | 03ff179 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 778 | list_for_each_entry_safe(p, n, &father->children, sibling) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | int ptrace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | ptrace = p->ptrace; |
| 782 | |
| 783 | /* if father isn't the real parent, then ptrace must be enabled */ |
| 784 | BUG_ON(father != p->real_parent && !ptrace); |
| 785 | |
| 786 | if (father == p->real_parent) { |
| 787 | /* reparent with a reaper, real father it's us */ |
Oleg Nesterov | 84eb646 | 2007-10-16 23:26:49 -0700 | [diff] [blame] | 788 | p->real_parent = reaper; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | reparent_thread(p, father, 0); |
| 790 | } else { |
| 791 | /* reparent ptraced task to its real parent */ |
| 792 | __ptrace_unlink (p); |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 793 | if (p->exit_state == EXIT_ZOMBIE && !task_detached(p) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | thread_group_empty(p)) |
| 795 | do_notify_parent(p, p->exit_signal); |
| 796 | } |
| 797 | |
| 798 | /* |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 799 | * if the ptraced child is a detached zombie we must collect |
| 800 | * it before we exit, or it will remain zombie forever since |
| 801 | * we prevented it from self-reap itself while it was being |
| 802 | * traced by us, to be able to see it in wait4. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | */ |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 804 | if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && task_detached(p))) |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 805 | list_add(&p->ptrace_list, &ptrace_dead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | } |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 807 | |
Matthias Kaehlcke | 03ff179 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 808 | list_for_each_entry_safe(p, n, &father->ptrace_children, ptrace_list) { |
Oleg Nesterov | 84eb646 | 2007-10-16 23:26:49 -0700 | [diff] [blame] | 809 | p->real_parent = reaper; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | reparent_thread(p, father, 1); |
| 811 | } |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 812 | |
| 813 | write_unlock_irq(&tasklist_lock); |
| 814 | BUG_ON(!list_empty(&father->children)); |
| 815 | BUG_ON(!list_empty(&father->ptrace_children)); |
| 816 | |
| 817 | list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_list) { |
| 818 | list_del_init(&p->ptrace_list); |
| 819 | release_task(p); |
| 820 | } |
| 821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* |
| 825 | * Send signals to all our closest relatives so that they know |
| 826 | * to properly mourn us.. |
| 827 | */ |
Oleg Nesterov | 821c7de | 2008-03-02 21:44:44 +0300 | [diff] [blame] | 828 | static void exit_notify(struct task_struct *tsk, int group_dead) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | { |
| 830 | int state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | /* |
| 833 | * This does two things: |
| 834 | * |
| 835 | * A. Make init inherit all the child processes |
| 836 | * B. Check to see if any process groups have become orphaned |
| 837 | * as a result of our exiting, and if they have any stopped |
| 838 | * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) |
| 839 | */ |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 840 | forget_original_parent(tsk); |
Pavel Emelyanov | 2e4a707 | 2007-10-18 23:40:01 -0700 | [diff] [blame] | 841 | exit_task_namespaces(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
Oleg Nesterov | 762a24b | 2007-10-18 23:40:00 -0700 | [diff] [blame] | 843 | write_lock_irq(&tasklist_lock); |
Oleg Nesterov | 821c7de | 2008-03-02 21:44:44 +0300 | [diff] [blame] | 844 | if (group_dead) |
| 845 | kill_orphaned_pgrp(tsk->group_leader, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
Oleg Nesterov | 2472844 | 2007-08-04 01:04:41 +0400 | [diff] [blame] | 847 | /* Let father know we died |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | * |
| 849 | * Thread signals are configurable, but you aren't going to use |
Daniel Walker | d4c5e41 | 2007-10-18 23:39:59 -0700 | [diff] [blame] | 850 | * that to send signals to arbitary processes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | * That stops right now. |
| 852 | * |
| 853 | * If the parent exec id doesn't match the exec id we saved |
| 854 | * when we started then we know the parent has changed security |
| 855 | * domain. |
| 856 | * |
| 857 | * If our self_exec id doesn't match our parent_exec_id then |
| 858 | * we have changed execution domain as these two values started |
| 859 | * the same after a fork. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | */ |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 861 | if (tsk->exit_signal != SIGCHLD && !task_detached(tsk) && |
Oleg Nesterov | f49ee50 | 2008-03-02 21:44:40 +0300 | [diff] [blame] | 862 | (tsk->parent_exec_id != tsk->real_parent->self_exec_id || |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 863 | tsk->self_exec_id != tsk->parent_exec_id) && |
| 864 | !capable(CAP_KILL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | tsk->exit_signal = SIGCHLD; |
| 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | /* If something other than our normal parent is ptracing us, then |
| 868 | * send it a SIGCHLD instead of honoring exit_signal. exit_signal |
| 869 | * only has special meaning to our real parent. |
| 870 | */ |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 871 | if (!task_detached(tsk) && thread_group_empty(tsk)) { |
Oleg Nesterov | 53b6f9f | 2008-04-30 00:53:13 -0700 | [diff] [blame] | 872 | int signal = ptrace_reparented(tsk) ? |
| 873 | SIGCHLD : tsk->exit_signal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | do_notify_parent(tsk, signal); |
| 875 | } else if (tsk->ptrace) { |
| 876 | do_notify_parent(tsk, SIGCHLD); |
| 877 | } |
| 878 | |
| 879 | state = EXIT_ZOMBIE; |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 880 | if (task_detached(tsk) && likely(!tsk->ptrace)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | state = EXIT_DEAD; |
| 882 | tsk->exit_state = state; |
| 883 | |
Oleg Nesterov | 2800d8d | 2008-04-30 00:53:12 -0700 | [diff] [blame] | 884 | /* mt-exec, de_thread() is waiting for us */ |
Oleg Nesterov | 6db840f | 2007-10-16 23:27:23 -0700 | [diff] [blame] | 885 | if (thread_group_leader(tsk) && |
| 886 | tsk->signal->notify_count < 0 && |
| 887 | tsk->signal->group_exit_task) |
| 888 | wake_up_process(tsk->signal->group_exit_task); |
| 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | write_unlock_irq(&tasklist_lock); |
| 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* If the process is dead, release it - nobody will wait for it */ |
| 893 | if (state == EXIT_DEAD) |
| 894 | release_task(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Jeff Dike | e18eecb | 2007-07-15 23:38:48 -0700 | [diff] [blame] | 897 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 898 | static void check_stack_usage(void) |
| 899 | { |
| 900 | static DEFINE_SPINLOCK(low_water_lock); |
| 901 | static int lowest_to_date = THREAD_SIZE; |
Jeff Dike | e18eecb | 2007-07-15 23:38:48 -0700 | [diff] [blame] | 902 | unsigned long free; |
| 903 | |
Eric Sandeen | 7c9f886 | 2008-04-22 16:38:23 -0500 | [diff] [blame^] | 904 | free = stack_not_used(current); |
Jeff Dike | e18eecb | 2007-07-15 23:38:48 -0700 | [diff] [blame] | 905 | |
| 906 | if (free >= lowest_to_date) |
| 907 | return; |
| 908 | |
| 909 | spin_lock(&low_water_lock); |
| 910 | if (free < lowest_to_date) { |
| 911 | printk(KERN_WARNING "%s used greatest stack depth: %lu bytes " |
| 912 | "left\n", |
| 913 | current->comm, free); |
| 914 | lowest_to_date = free; |
| 915 | } |
| 916 | spin_unlock(&low_water_lock); |
| 917 | } |
| 918 | #else |
| 919 | static inline void check_stack_usage(void) {} |
| 920 | #endif |
| 921 | |
Oleg Nesterov | 84eb646 | 2007-10-16 23:26:49 -0700 | [diff] [blame] | 922 | static inline void exit_child_reaper(struct task_struct *tsk) |
| 923 | { |
Sukadev Bhattiprolu | 88f21d8 | 2007-10-18 23:39:50 -0700 | [diff] [blame] | 924 | if (likely(tsk->group_leader != task_child_reaper(tsk))) |
Oleg Nesterov | 84eb646 | 2007-10-16 23:26:49 -0700 | [diff] [blame] | 925 | return; |
| 926 | |
Sukadev Bhattiprolu | 3eb07c8 | 2007-10-18 23:40:13 -0700 | [diff] [blame] | 927 | if (tsk->nsproxy->pid_ns == &init_pid_ns) |
| 928 | panic("Attempted to kill init!"); |
| 929 | |
| 930 | /* |
| 931 | * @tsk is the last thread in the 'cgroup-init' and is exiting. |
| 932 | * Terminate all remaining processes in the namespace and reap them |
| 933 | * before exiting @tsk. |
| 934 | * |
| 935 | * Note that @tsk (last thread of cgroup-init) may not necessarily |
| 936 | * be the child-reaper (i.e main thread of cgroup-init) of the |
| 937 | * namespace i.e the child_reaper may have already exited. |
| 938 | * |
| 939 | * Even after a child_reaper exits, we let it inherit orphaned children, |
| 940 | * because, pid_ns->child_reaper remains valid as long as there is |
| 941 | * at least one living sub-thread in the cgroup init. |
| 942 | |
| 943 | * This living sub-thread of the cgroup-init will be notified when |
| 944 | * a child inherited by the 'child-reaper' exits (do_notify_parent() |
| 945 | * uses __group_send_sig_info()). Further, when reaping child processes, |
| 946 | * do_wait() iterates over children of all living sub threads. |
| 947 | |
| 948 | * i.e even though 'child_reaper' thread is listed as the parent of the |
| 949 | * orphaned children, any living sub-thread in the cgroup-init can |
| 950 | * perform the role of the child_reaper. |
| 951 | */ |
| 952 | zap_pid_ns_processes(tsk->nsproxy->pid_ns); |
Oleg Nesterov | 84eb646 | 2007-10-16 23:26:49 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 955 | NORET_TYPE void do_exit(long code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
| 957 | struct task_struct *tsk = current; |
| 958 | int group_dead; |
| 959 | |
| 960 | profile_task_exit(tsk); |
| 961 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 962 | WARN_ON(atomic_read(&tsk->fs_excl)); |
| 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | if (unlikely(in_interrupt())) |
| 965 | panic("Aiee, killing interrupt handler!"); |
| 966 | if (unlikely(!tsk->pid)) |
| 967 | panic("Attempted to kill the idle task!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
| 969 | if (unlikely(current->ptrace & PT_TRACE_EXIT)) { |
| 970 | current->ptrace_message = code; |
| 971 | ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP); |
| 972 | } |
| 973 | |
Alexander Nyberg | df164db | 2005-06-23 00:09:13 -0700 | [diff] [blame] | 974 | /* |
| 975 | * We're taking recursive faults here in do_exit. Safest is to just |
| 976 | * leave this task alone and wait for reboot. |
| 977 | */ |
| 978 | if (unlikely(tsk->flags & PF_EXITING)) { |
| 979 | printk(KERN_ALERT |
| 980 | "Fixing recursive fault but reboot is needed!\n"); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 981 | /* |
| 982 | * We can do this unlocked here. The futex code uses |
| 983 | * this flag just to verify whether the pi state |
| 984 | * cleanup has been done or not. In the worst case it |
| 985 | * loops once more. We pretend that the cleanup was |
| 986 | * done as there is no way to return. Either the |
| 987 | * OWNER_DIED bit is set by now or we push the blocked |
| 988 | * task into the wait for ever nirwana as well. |
| 989 | */ |
| 990 | tsk->flags |= PF_EXITPIDONE; |
Al Viro | afc847b | 2006-02-28 12:51:55 -0500 | [diff] [blame] | 991 | if (tsk->io_context) |
| 992 | exit_io_context(); |
Alexander Nyberg | df164db | 2005-06-23 00:09:13 -0700 | [diff] [blame] | 993 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 994 | schedule(); |
| 995 | } |
| 996 | |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 997 | exit_signals(tsk); /* sets PF_EXITING */ |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 998 | /* |
| 999 | * tsk->flags are checked in the futex code to protect against |
| 1000 | * an exiting task cleaning up the robust pi futexes. |
| 1001 | */ |
Oleg Nesterov | d2ee719 | 2007-10-16 23:26:47 -0700 | [diff] [blame] | 1002 | smp_mb(); |
| 1003 | spin_unlock_wait(&tsk->pi_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | if (unlikely(in_atomic())) |
| 1006 | printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1007 | current->comm, task_pid_nr(current), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | preempt_count()); |
| 1009 | |
| 1010 | acct_update_integrals(tsk); |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 1011 | if (tsk->mm) { |
| 1012 | update_hiwater_rss(tsk->mm); |
| 1013 | update_hiwater_vm(tsk->mm); |
| 1014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | group_dead = atomic_dec_and_test(&tsk->signal->live); |
Andrew Morton | c306895 | 2005-08-04 16:49:32 -0700 | [diff] [blame] | 1016 | if (group_dead) { |
Oleg Nesterov | 84eb646 | 2007-10-16 23:26:49 -0700 | [diff] [blame] | 1017 | exit_child_reaper(tsk); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1018 | hrtimer_cancel(&tsk->signal->real_timer); |
Roland McGrath | 25f407f | 2005-10-21 15:03:29 -0700 | [diff] [blame] | 1019 | exit_itimers(tsk->signal); |
Andrew Morton | c306895 | 2005-08-04 16:49:32 -0700 | [diff] [blame] | 1020 | } |
KaiGai Kohei | f6ec29a | 2006-06-25 05:49:25 -0700 | [diff] [blame] | 1021 | acct_collect(code, group_dead); |
Alexey Dobriyan | 42b2dd0 | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1022 | #ifdef CONFIG_FUTEX |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 1023 | if (unlikely(tsk->robust_list)) |
| 1024 | exit_robust_list(tsk); |
Alexey Dobriyan | 42b2dd0 | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1025 | #ifdef CONFIG_COMPAT |
Ingo Molnar | 34f192c | 2006-03-27 01:16:24 -0800 | [diff] [blame] | 1026 | if (unlikely(tsk->compat_robust_list)) |
| 1027 | compat_exit_robust_list(tsk); |
| 1028 | #endif |
Alexey Dobriyan | 42b2dd0 | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1029 | #endif |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1030 | if (group_dead) |
| 1031 | tty_audit_exit(); |
Al Viro | fa84cb9 | 2006-03-29 20:30:19 -0500 | [diff] [blame] | 1032 | if (unlikely(tsk->audit_context)) |
| 1033 | audit_free(tsk); |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 1034 | |
Jonathan Lim | f2ab6d8 | 2007-08-30 23:56:23 -0700 | [diff] [blame] | 1035 | tsk->exit_code = code; |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 1036 | taskstats_exit(tsk, group_dead); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | exit_mm(tsk); |
| 1039 | |
KaiGai Kohei | 0e46481 | 2006-06-25 05:49:24 -0700 | [diff] [blame] | 1040 | if (group_dead) |
KaiGai Kohei | f6ec29a | 2006-06-25 05:49:25 -0700 | [diff] [blame] | 1041 | acct_process(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | exit_sem(tsk); |
Al Viro | 1ec7f1d | 2008-04-22 05:35:42 -0400 | [diff] [blame] | 1043 | exit_files(tsk); |
| 1044 | exit_fs(tsk); |
Jeff Dike | e18eecb | 2007-07-15 23:38:48 -0700 | [diff] [blame] | 1045 | check_stack_usage(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | exit_thread(); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1047 | cgroup_exit(tsk, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | exit_keys(tsk); |
| 1049 | |
| 1050 | if (group_dead && tsk->signal->leader) |
| 1051 | disassociate_ctty(1); |
| 1052 | |
Al Viro | a1261f54 | 2005-11-13 16:06:55 -0800 | [diff] [blame] | 1053 | module_put(task_thread_info(tsk)->exec_domain->module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | if (tsk->binfmt) |
| 1055 | module_put(tsk->binfmt->module); |
| 1056 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 1057 | proc_exit_connector(tsk); |
Oleg Nesterov | 821c7de | 2008-03-02 21:44:44 +0300 | [diff] [blame] | 1058 | exit_notify(tsk, group_dead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | #ifdef CONFIG_NUMA |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1060 | mpol_put(tsk->mempolicy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | tsk->mempolicy = NULL; |
| 1062 | #endif |
Alexey Dobriyan | 42b2dd0 | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1063 | #ifdef CONFIG_FUTEX |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 1064 | /* |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1065 | * This must happen late, after the PID is not |
| 1066 | * hashed anymore: |
| 1067 | */ |
| 1068 | if (unlikely(!list_empty(&tsk->pi_state_list))) |
| 1069 | exit_pi_state_list(tsk); |
| 1070 | if (unlikely(current->pi_state_cache)) |
| 1071 | kfree(current->pi_state_cache); |
Alexey Dobriyan | 42b2dd0 | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1072 | #endif |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1073 | /* |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 1074 | * Make sure we are holding no locks: |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 1075 | */ |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 1076 | debug_check_no_locks_held(tsk); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1077 | /* |
| 1078 | * We can do this unlocked here. The futex code uses this flag |
| 1079 | * just to verify whether the pi state cleanup has been done |
| 1080 | * or not. In the worst case it loops once more. |
| 1081 | */ |
| 1082 | tsk->flags |= PF_EXITPIDONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
Al Viro | afc847b | 2006-02-28 12:51:55 -0500 | [diff] [blame] | 1084 | if (tsk->io_context) |
| 1085 | exit_io_context(); |
| 1086 | |
Jens Axboe | b92ce55 | 2006-04-11 13:52:07 +0200 | [diff] [blame] | 1087 | if (tsk->splice_pipe) |
| 1088 | __free_pipe_info(tsk->splice_pipe); |
| 1089 | |
Coywolf Qi Hunt | 7407251 | 2005-10-30 15:02:47 -0800 | [diff] [blame] | 1090 | preempt_disable(); |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 1091 | /* causes final put_task_struct in finish_task_switch(). */ |
Oleg Nesterov | c394cc9 | 2006-09-29 02:01:11 -0700 | [diff] [blame] | 1092 | tsk->state = TASK_DEAD; |
Coywolf Qi Hunt | 7407251 | 2005-10-30 15:02:47 -0800 | [diff] [blame] | 1093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | schedule(); |
| 1095 | BUG(); |
| 1096 | /* Avoid "noreturn function does return". */ |
Alan Cox | 54306cf | 2006-09-29 02:00:42 -0700 | [diff] [blame] | 1097 | for (;;) |
| 1098 | cpu_relax(); /* For when BUG is null */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Russ Anderson | 012914d | 2005-04-23 00:08:00 -0700 | [diff] [blame] | 1101 | EXPORT_SYMBOL_GPL(do_exit); |
| 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | NORET_TYPE void complete_and_exit(struct completion *comp, long code) |
| 1104 | { |
| 1105 | if (comp) |
| 1106 | complete(comp); |
Oleg Nesterov | 55a101f | 2006-09-29 02:01:10 -0700 | [diff] [blame] | 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | do_exit(code); |
| 1109 | } |
| 1110 | |
| 1111 | EXPORT_SYMBOL(complete_and_exit); |
| 1112 | |
| 1113 | asmlinkage long sys_exit(int error_code) |
| 1114 | { |
| 1115 | do_exit((error_code&0xff)<<8); |
| 1116 | } |
| 1117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | /* |
| 1119 | * Take down every thread in the group. This is called by fatal signals |
| 1120 | * as well as by sys_exit_group (below). |
| 1121 | */ |
| 1122 | NORET_TYPE void |
| 1123 | do_group_exit(int exit_code) |
| 1124 | { |
Oleg Nesterov | bfc4b08 | 2008-04-30 00:52:36 -0700 | [diff] [blame] | 1125 | struct signal_struct *sig = current->signal; |
| 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | BUG_ON(exit_code & 0x80); /* core dumps don't get here */ |
| 1128 | |
Oleg Nesterov | bfc4b08 | 2008-04-30 00:52:36 -0700 | [diff] [blame] | 1129 | if (signal_group_exit(sig)) |
| 1130 | exit_code = sig->group_exit_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | else if (!thread_group_empty(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | struct sighand_struct *const sighand = current->sighand; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | spin_lock_irq(&sighand->siglock); |
Oleg Nesterov | ed5d2ca | 2008-02-04 22:27:24 -0800 | [diff] [blame] | 1134 | if (signal_group_exit(sig)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | /* Another thread got here before we took the lock. */ |
| 1136 | exit_code = sig->group_exit_code; |
| 1137 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | sig->group_exit_code = exit_code; |
Oleg Nesterov | ed5d2ca | 2008-02-04 22:27:24 -0800 | [diff] [blame] | 1139 | sig->flags = SIGNAL_GROUP_EXIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | zap_other_threads(current); |
| 1141 | } |
| 1142 | spin_unlock_irq(&sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | do_exit(exit_code); |
| 1146 | /* NOTREACHED */ |
| 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * this kills every thread in the thread group. Note that any externally |
| 1151 | * wait4()-ing process will get the correct exit code - even if this |
| 1152 | * thread is not the thread group leader. |
| 1153 | */ |
| 1154 | asmlinkage void sys_exit_group(int error_code) |
| 1155 | { |
| 1156 | do_group_exit((error_code & 0xff) << 8); |
| 1157 | } |
| 1158 | |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1159 | static struct pid *task_pid_type(struct task_struct *task, enum pid_type type) |
| 1160 | { |
| 1161 | struct pid *pid = NULL; |
| 1162 | if (type == PIDTYPE_PID) |
| 1163 | pid = task->pids[type].pid; |
| 1164 | else if (type < PIDTYPE_MAX) |
| 1165 | pid = task->group_leader->pids[type].pid; |
| 1166 | return pid; |
| 1167 | } |
| 1168 | |
| 1169 | static int eligible_child(enum pid_type type, struct pid *pid, int options, |
| 1170 | struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | { |
Roland McGrath | 7324328 | 2007-05-06 14:50:20 -0700 | [diff] [blame] | 1172 | int err; |
| 1173 | |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1174 | if (type < PIDTYPE_MAX) { |
| 1175 | if (task_pid_type(p, type) != pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Do not consider detached threads that are |
| 1181 | * not ptraced: |
| 1182 | */ |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 1183 | if (task_detached(p) && !p->ptrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | return 0; |
| 1185 | |
| 1186 | /* Wait for all children (clone and not) if __WALL is set; |
| 1187 | * otherwise, wait for clone children *only* if __WCLONE is |
| 1188 | * set; otherwise, wait for non-clone children *only*. (Note: |
| 1189 | * A "clone" child here is one that reports to its parent |
| 1190 | * using a signal other than SIGCHLD.) */ |
| 1191 | if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0)) |
| 1192 | && !(options & __WALL)) |
| 1193 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
Roland McGrath | 7324328 | 2007-05-06 14:50:20 -0700 | [diff] [blame] | 1195 | err = security_task_wait(p); |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1196 | if (likely(!err)) |
| 1197 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1199 | if (type != PIDTYPE_PID) |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1200 | return 0; |
| 1201 | /* This child was explicitly requested, abort */ |
| 1202 | read_unlock(&tasklist_lock); |
| 1203 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1206 | static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | int why, int status, |
| 1208 | struct siginfo __user *infop, |
| 1209 | struct rusage __user *rusagep) |
| 1210 | { |
| 1211 | int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | put_task_struct(p); |
| 1214 | if (!retval) |
| 1215 | retval = put_user(SIGCHLD, &infop->si_signo); |
| 1216 | if (!retval) |
| 1217 | retval = put_user(0, &infop->si_errno); |
| 1218 | if (!retval) |
| 1219 | retval = put_user((short)why, &infop->si_code); |
| 1220 | if (!retval) |
| 1221 | retval = put_user(pid, &infop->si_pid); |
| 1222 | if (!retval) |
| 1223 | retval = put_user(uid, &infop->si_uid); |
| 1224 | if (!retval) |
| 1225 | retval = put_user(status, &infop->si_status); |
| 1226 | if (!retval) |
| 1227 | retval = pid; |
| 1228 | return retval; |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold |
| 1233 | * read_lock(&tasklist_lock) on entry. If we return zero, we still hold |
| 1234 | * the lock and this task is uninteresting. If we return nonzero, we have |
| 1235 | * released the lock and the system call should return. |
| 1236 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1237 | static int wait_task_zombie(struct task_struct *p, int noreap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | struct siginfo __user *infop, |
| 1239 | int __user *stat_addr, struct rusage __user *ru) |
| 1240 | { |
| 1241 | unsigned long state; |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1242 | int retval, status, traced; |
Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 1243 | pid_t pid = task_pid_vnr(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
| 1245 | if (unlikely(noreap)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | uid_t uid = p->uid; |
| 1247 | int exit_code = p->exit_code; |
| 1248 | int why, status; |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | get_task_struct(p); |
| 1251 | read_unlock(&tasklist_lock); |
| 1252 | if ((exit_code & 0x7f) == 0) { |
| 1253 | why = CLD_EXITED; |
| 1254 | status = exit_code >> 8; |
| 1255 | } else { |
| 1256 | why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED; |
| 1257 | status = exit_code & 0x7f; |
| 1258 | } |
| 1259 | return wait_noreap_copyout(p, pid, uid, why, |
| 1260 | status, infop, ru); |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | * Try to move the task's state to DEAD |
| 1265 | * only one thread is allowed to do this: |
| 1266 | */ |
| 1267 | state = xchg(&p->exit_state, EXIT_DEAD); |
| 1268 | if (state != EXIT_ZOMBIE) { |
| 1269 | BUG_ON(state != EXIT_DEAD); |
| 1270 | return 0; |
| 1271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | |
Oleg Nesterov | 53b6f9f | 2008-04-30 00:53:13 -0700 | [diff] [blame] | 1273 | traced = ptrace_reparented(p); |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1274 | |
| 1275 | if (likely(!traced)) { |
Jesper Juhl | 3795e16 | 2006-01-09 20:54:39 -0800 | [diff] [blame] | 1276 | struct signal_struct *psig; |
| 1277 | struct signal_struct *sig; |
| 1278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | /* |
| 1280 | * The resource counters for the group leader are in its |
| 1281 | * own task_struct. Those for dead threads in the group |
| 1282 | * are in its signal_struct, as are those for the child |
| 1283 | * processes it has previously reaped. All these |
| 1284 | * accumulate in the parent's signal_struct c* fields. |
| 1285 | * |
| 1286 | * We don't bother to take a lock here to protect these |
| 1287 | * p->signal fields, because they are only touched by |
| 1288 | * __exit_signal, which runs with tasklist_lock |
| 1289 | * write-locked anyway, and so is excluded here. We do |
| 1290 | * need to protect the access to p->parent->signal fields, |
| 1291 | * as other threads in the parent group can be right |
| 1292 | * here reaping other children at the same time. |
| 1293 | */ |
| 1294 | spin_lock_irq(&p->parent->sighand->siglock); |
Jesper Juhl | 3795e16 | 2006-01-09 20:54:39 -0800 | [diff] [blame] | 1295 | psig = p->parent->signal; |
| 1296 | sig = p->signal; |
| 1297 | psig->cutime = |
| 1298 | cputime_add(psig->cutime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | cputime_add(p->utime, |
Jesper Juhl | 3795e16 | 2006-01-09 20:54:39 -0800 | [diff] [blame] | 1300 | cputime_add(sig->utime, |
| 1301 | sig->cutime))); |
| 1302 | psig->cstime = |
| 1303 | cputime_add(psig->cstime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | cputime_add(p->stime, |
Jesper Juhl | 3795e16 | 2006-01-09 20:54:39 -0800 | [diff] [blame] | 1305 | cputime_add(sig->stime, |
| 1306 | sig->cstime))); |
Laurent Vivier | 9ac5231 | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 1307 | psig->cgtime = |
| 1308 | cputime_add(psig->cgtime, |
| 1309 | cputime_add(p->gtime, |
| 1310 | cputime_add(sig->gtime, |
| 1311 | sig->cgtime))); |
Jesper Juhl | 3795e16 | 2006-01-09 20:54:39 -0800 | [diff] [blame] | 1312 | psig->cmin_flt += |
| 1313 | p->min_flt + sig->min_flt + sig->cmin_flt; |
| 1314 | psig->cmaj_flt += |
| 1315 | p->maj_flt + sig->maj_flt + sig->cmaj_flt; |
| 1316 | psig->cnvcsw += |
| 1317 | p->nvcsw + sig->nvcsw + sig->cnvcsw; |
| 1318 | psig->cnivcsw += |
| 1319 | p->nivcsw + sig->nivcsw + sig->cnivcsw; |
Eric Dumazet | 6eaeeab | 2007-05-10 22:22:37 -0700 | [diff] [blame] | 1320 | psig->cinblock += |
| 1321 | task_io_get_inblock(p) + |
| 1322 | sig->inblock + sig->cinblock; |
| 1323 | psig->coublock += |
| 1324 | task_io_get_oublock(p) + |
| 1325 | sig->oublock + sig->coublock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | spin_unlock_irq(&p->parent->sighand->siglock); |
| 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | * Now we are sure this task is interesting, and no other |
| 1331 | * thread can reap it because we set its state to EXIT_DEAD. |
| 1332 | */ |
| 1333 | read_unlock(&tasklist_lock); |
| 1334 | |
| 1335 | retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; |
| 1336 | status = (p->signal->flags & SIGNAL_GROUP_EXIT) |
| 1337 | ? p->signal->group_exit_code : p->exit_code; |
| 1338 | if (!retval && stat_addr) |
| 1339 | retval = put_user(status, stat_addr); |
| 1340 | if (!retval && infop) |
| 1341 | retval = put_user(SIGCHLD, &infop->si_signo); |
| 1342 | if (!retval && infop) |
| 1343 | retval = put_user(0, &infop->si_errno); |
| 1344 | if (!retval && infop) { |
| 1345 | int why; |
| 1346 | |
| 1347 | if ((status & 0x7f) == 0) { |
| 1348 | why = CLD_EXITED; |
| 1349 | status >>= 8; |
| 1350 | } else { |
| 1351 | why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED; |
| 1352 | status &= 0x7f; |
| 1353 | } |
| 1354 | retval = put_user((short)why, &infop->si_code); |
| 1355 | if (!retval) |
| 1356 | retval = put_user(status, &infop->si_status); |
| 1357 | } |
| 1358 | if (!retval && infop) |
Oleg Nesterov | 3a515e4 | 2008-02-08 04:19:07 -0800 | [diff] [blame] | 1359 | retval = put_user(pid, &infop->si_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | if (!retval && infop) |
| 1361 | retval = put_user(p->uid, &infop->si_uid); |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1362 | if (!retval) |
Oleg Nesterov | 3a515e4 | 2008-02-08 04:19:07 -0800 | [diff] [blame] | 1363 | retval = pid; |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1364 | |
| 1365 | if (traced) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | write_lock_irq(&tasklist_lock); |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1367 | /* We dropped tasklist, ptracer could die and untrace */ |
| 1368 | ptrace_unlink(p); |
| 1369 | /* |
| 1370 | * If this is not a detached task, notify the parent. |
| 1371 | * If it's still not detached after that, don't release |
| 1372 | * it now. |
| 1373 | */ |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 1374 | if (!task_detached(p)) { |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1375 | do_notify_parent(p, p->exit_signal); |
Oleg Nesterov | d839fd4 | 2008-04-30 00:53:11 -0700 | [diff] [blame] | 1376 | if (!task_detached(p)) { |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1377 | p->exit_state = EXIT_ZOMBIE; |
| 1378 | p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | write_unlock_irq(&tasklist_lock); |
| 1382 | } |
| 1383 | if (p != NULL) |
| 1384 | release_task(p); |
Oleg Nesterov | 2f4e6e2 | 2007-10-16 23:26:58 -0700 | [diff] [blame] | 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | return retval; |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold |
| 1391 | * read_lock(&tasklist_lock) on entry. If we return zero, we still hold |
| 1392 | * the lock and this task is uninteresting. If we return nonzero, we have |
| 1393 | * released the lock and the system call should return. |
| 1394 | */ |
Oleg Nesterov | 1bad95c | 2008-02-08 04:19:03 -0800 | [diff] [blame] | 1395 | static int wait_task_stopped(struct task_struct *p, |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1396 | int noreap, struct siginfo __user *infop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | int __user *stat_addr, struct rusage __user *ru) |
| 1398 | { |
Oleg Nesterov | ee7c82d | 2008-02-08 04:19:01 -0800 | [diff] [blame] | 1399 | int retval, exit_code, why; |
| 1400 | uid_t uid = 0; /* unneeded, required by compiler */ |
Oleg Nesterov | c895078 | 2007-11-28 16:21:24 -0800 | [diff] [blame] | 1401 | pid_t pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
Oleg Nesterov | ee7c82d | 2008-02-08 04:19:01 -0800 | [diff] [blame] | 1403 | exit_code = 0; |
| 1404 | spin_lock_irq(&p->sighand->siglock); |
| 1405 | |
| 1406 | if (unlikely(!task_is_stopped_or_traced(p))) |
| 1407 | goto unlock_sig; |
| 1408 | |
Oleg Nesterov | 1bad95c | 2008-02-08 04:19:03 -0800 | [diff] [blame] | 1409 | if (!(p->ptrace & PT_PTRACED) && p->signal->group_stop_count > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | /* |
| 1411 | * A group stop is in progress and this is the group leader. |
| 1412 | * We won't report until all threads have stopped. |
| 1413 | */ |
Oleg Nesterov | ee7c82d | 2008-02-08 04:19:01 -0800 | [diff] [blame] | 1414 | goto unlock_sig; |
| 1415 | |
| 1416 | exit_code = p->exit_code; |
| 1417 | if (!exit_code) |
| 1418 | goto unlock_sig; |
| 1419 | |
| 1420 | if (!noreap) |
| 1421 | p->exit_code = 0; |
| 1422 | |
| 1423 | uid = p->uid; |
| 1424 | unlock_sig: |
| 1425 | spin_unlock_irq(&p->sighand->siglock); |
| 1426 | if (!exit_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return 0; |
| 1428 | |
| 1429 | /* |
| 1430 | * Now we are pretty sure this task is interesting. |
| 1431 | * Make sure it doesn't get reaped out from under us while we |
| 1432 | * give up the lock and then examine it below. We don't want to |
| 1433 | * keep holding onto the tasklist_lock while we call getrusage and |
| 1434 | * possibly take page faults for user memory. |
| 1435 | */ |
| 1436 | get_task_struct(p); |
Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 1437 | pid = task_pid_vnr(p); |
Oleg Nesterov | ee7c82d | 2008-02-08 04:19:01 -0800 | [diff] [blame] | 1438 | why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | read_unlock(&tasklist_lock); |
| 1440 | |
Oleg Nesterov | ee7c82d | 2008-02-08 04:19:01 -0800 | [diff] [blame] | 1441 | if (unlikely(noreap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | return wait_noreap_copyout(p, pid, uid, |
Scott James Remnant | e6ceb32 | 2007-11-28 16:22:07 -0800 | [diff] [blame] | 1443 | why, exit_code, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | infop, ru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; |
| 1447 | if (!retval && stat_addr) |
| 1448 | retval = put_user((exit_code << 8) | 0x7f, stat_addr); |
| 1449 | if (!retval && infop) |
| 1450 | retval = put_user(SIGCHLD, &infop->si_signo); |
| 1451 | if (!retval && infop) |
| 1452 | retval = put_user(0, &infop->si_errno); |
| 1453 | if (!retval && infop) |
Roland McGrath | 6efcae46 | 2008-03-08 11:41:22 -0800 | [diff] [blame] | 1454 | retval = put_user((short)why, &infop->si_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | if (!retval && infop) |
| 1456 | retval = put_user(exit_code, &infop->si_status); |
| 1457 | if (!retval && infop) |
Oleg Nesterov | c895078 | 2007-11-28 16:21:24 -0800 | [diff] [blame] | 1458 | retval = put_user(pid, &infop->si_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | if (!retval && infop) |
Oleg Nesterov | ee7c82d | 2008-02-08 04:19:01 -0800 | [diff] [blame] | 1460 | retval = put_user(uid, &infop->si_uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | if (!retval) |
Oleg Nesterov | c895078 | 2007-11-28 16:21:24 -0800 | [diff] [blame] | 1462 | retval = pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | put_task_struct(p); |
| 1464 | |
| 1465 | BUG_ON(!retval); |
| 1466 | return retval; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * Handle do_wait work for one task in a live, non-stopped state. |
| 1471 | * read_lock(&tasklist_lock) on entry. If we return zero, we still hold |
| 1472 | * the lock and this task is uninteresting. If we return nonzero, we have |
| 1473 | * released the lock and the system call should return. |
| 1474 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1475 | static int wait_task_continued(struct task_struct *p, int noreap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | struct siginfo __user *infop, |
| 1477 | int __user *stat_addr, struct rusage __user *ru) |
| 1478 | { |
| 1479 | int retval; |
| 1480 | pid_t pid; |
| 1481 | uid_t uid; |
| 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) |
| 1484 | return 0; |
| 1485 | |
| 1486 | spin_lock_irq(&p->sighand->siglock); |
| 1487 | /* Re-check with the lock held. */ |
| 1488 | if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) { |
| 1489 | spin_unlock_irq(&p->sighand->siglock); |
| 1490 | return 0; |
| 1491 | } |
| 1492 | if (!noreap) |
| 1493 | p->signal->flags &= ~SIGNAL_STOP_CONTINUED; |
| 1494 | spin_unlock_irq(&p->sighand->siglock); |
| 1495 | |
Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 1496 | pid = task_pid_vnr(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | uid = p->uid; |
| 1498 | get_task_struct(p); |
| 1499 | read_unlock(&tasklist_lock); |
| 1500 | |
| 1501 | if (!infop) { |
| 1502 | retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; |
| 1503 | put_task_struct(p); |
| 1504 | if (!retval && stat_addr) |
| 1505 | retval = put_user(0xffff, stat_addr); |
| 1506 | if (!retval) |
Oleg Nesterov | 3a515e4 | 2008-02-08 04:19:07 -0800 | [diff] [blame] | 1507 | retval = pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } else { |
| 1509 | retval = wait_noreap_copyout(p, pid, uid, |
| 1510 | CLD_CONTINUED, SIGCONT, |
| 1511 | infop, ru); |
| 1512 | BUG_ON(retval == 0); |
| 1513 | } |
| 1514 | |
| 1515 | return retval; |
| 1516 | } |
| 1517 | |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1518 | static long do_wait(enum pid_type type, struct pid *pid, int options, |
| 1519 | struct siginfo __user *infop, int __user *stat_addr, |
| 1520 | struct rusage __user *ru) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | { |
| 1522 | DECLARE_WAITQUEUE(wait, current); |
| 1523 | struct task_struct *tsk; |
| 1524 | int flag, retval; |
| 1525 | |
| 1526 | add_wait_queue(¤t->signal->wait_chldexit,&wait); |
| 1527 | repeat: |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1528 | /* If there is nothing that can match our critier just get out */ |
| 1529 | retval = -ECHILD; |
| 1530 | if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type]))) |
| 1531 | goto end; |
| 1532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | /* |
| 1534 | * We will set this flag if we see any child that might later |
| 1535 | * match our criteria, even if we are not able to reap it yet. |
| 1536 | */ |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1537 | flag = retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | current->state = TASK_INTERRUPTIBLE; |
| 1539 | read_lock(&tasklist_lock); |
| 1540 | tsk = current; |
| 1541 | do { |
| 1542 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
Matthias Kaehlcke | 03ff179 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 1544 | list_for_each_entry(p, &tsk->children, sibling) { |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1545 | int ret = eligible_child(type, pid, options, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | if (!ret) |
| 1547 | continue; |
| 1548 | |
Roland McGrath | 7324328 | 2007-05-06 14:50:20 -0700 | [diff] [blame] | 1549 | if (unlikely(ret < 0)) { |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1550 | retval = ret; |
| 1551 | } else if (task_is_stopped_or_traced(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | /* |
| 1553 | * It's stopped now, so it might later |
| 1554 | * continue, exit, or stop again. |
| 1555 | */ |
| 1556 | flag = 1; |
Oleg Nesterov | 34a1738 | 2008-02-08 04:18:59 -0800 | [diff] [blame] | 1557 | if (!(p->ptrace & PT_PTRACED) && |
| 1558 | !(options & WUNTRACED)) |
| 1559 | continue; |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 1560 | |
Oleg Nesterov | 1bad95c | 2008-02-08 04:19:03 -0800 | [diff] [blame] | 1561 | retval = wait_task_stopped(p, |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 1562 | (options & WNOWAIT), infop, |
| 1563 | stat_addr, ru); |
Oleg Nesterov | 96fabbf | 2008-02-08 04:19:04 -0800 | [diff] [blame] | 1564 | } else if (p->exit_state == EXIT_ZOMBIE && |
| 1565 | !delay_group_leader(p)) { |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 1566 | /* |
Oleg Nesterov | 96fabbf | 2008-02-08 04:19:04 -0800 | [diff] [blame] | 1567 | * We don't reap group leaders with subthreads. |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 1568 | */ |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 1569 | if (!likely(options & WEXITED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | continue; |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 1571 | retval = wait_task_zombie(p, |
| 1572 | (options & WNOWAIT), infop, |
| 1573 | stat_addr, ru); |
Oleg Nesterov | 0a76fe8 | 2008-02-06 01:37:06 -0800 | [diff] [blame] | 1574 | } else if (p->exit_state != EXIT_DEAD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | /* |
| 1576 | * It's running now, so it might later |
| 1577 | * exit, stop, or stop and then continue. |
| 1578 | */ |
| 1579 | flag = 1; |
| 1580 | if (!unlikely(options & WCONTINUED)) |
| 1581 | continue; |
Matthew Wilcox | 338077e | 2007-12-06 11:09:35 -0500 | [diff] [blame] | 1582 | retval = wait_task_continued(p, |
| 1583 | (options & WNOWAIT), infop, |
| 1584 | stat_addr, ru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | } |
Oleg Nesterov | 9cbab81 | 2008-02-08 04:19:02 -0800 | [diff] [blame] | 1586 | if (retval != 0) /* tasklist_lock released */ |
| 1587 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | } |
| 1589 | if (!flag) { |
Matthias Kaehlcke | 03ff179 | 2007-10-18 23:39:57 -0700 | [diff] [blame] | 1590 | list_for_each_entry(p, &tsk->ptrace_children, |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1591 | ptrace_list) { |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1592 | flag = eligible_child(type, pid, options, p); |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1593 | if (!flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | continue; |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1595 | if (likely(flag > 0)) |
| 1596 | break; |
| 1597 | retval = flag; |
| 1598 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | if (options & __WNOTHREAD) |
| 1602 | break; |
| 1603 | tsk = next_thread(tsk); |
Eric Sesterhenn | 125e187 | 2006-06-23 02:06:06 -0700 | [diff] [blame] | 1604 | BUG_ON(tsk->signal != current->signal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | } while (tsk != current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | read_unlock(&tasklist_lock); |
Oleg Nesterov | f2cc3eb | 2008-02-08 04:19:06 -0800 | [diff] [blame] | 1607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | if (flag) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | if (options & WNOHANG) |
| 1610 | goto end; |
| 1611 | retval = -ERESTARTSYS; |
| 1612 | if (signal_pending(current)) |
| 1613 | goto end; |
| 1614 | schedule(); |
| 1615 | goto repeat; |
| 1616 | } |
| 1617 | retval = -ECHILD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | end: |
| 1619 | current->state = TASK_RUNNING; |
| 1620 | remove_wait_queue(¤t->signal->wait_chldexit,&wait); |
| 1621 | if (infop) { |
| 1622 | if (retval > 0) |
Oleg Nesterov | 9cbab81 | 2008-02-08 04:19:02 -0800 | [diff] [blame] | 1623 | retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | else { |
| 1625 | /* |
| 1626 | * For a WNOHANG return, clear out all the fields |
| 1627 | * we would set so the user can easily tell the |
| 1628 | * difference. |
| 1629 | */ |
| 1630 | if (!retval) |
| 1631 | retval = put_user(0, &infop->si_signo); |
| 1632 | if (!retval) |
| 1633 | retval = put_user(0, &infop->si_errno); |
| 1634 | if (!retval) |
| 1635 | retval = put_user(0, &infop->si_code); |
| 1636 | if (!retval) |
| 1637 | retval = put_user(0, &infop->si_pid); |
| 1638 | if (!retval) |
| 1639 | retval = put_user(0, &infop->si_uid); |
| 1640 | if (!retval) |
| 1641 | retval = put_user(0, &infop->si_status); |
| 1642 | } |
| 1643 | } |
| 1644 | return retval; |
| 1645 | } |
| 1646 | |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1647 | asmlinkage long sys_waitid(int which, pid_t upid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | struct siginfo __user *infop, int options, |
| 1649 | struct rusage __user *ru) |
| 1650 | { |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1651 | struct pid *pid = NULL; |
| 1652 | enum pid_type type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | long ret; |
| 1654 | |
| 1655 | if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED)) |
| 1656 | return -EINVAL; |
| 1657 | if (!(options & (WEXITED|WSTOPPED|WCONTINUED))) |
| 1658 | return -EINVAL; |
| 1659 | |
| 1660 | switch (which) { |
| 1661 | case P_ALL: |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1662 | type = PIDTYPE_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | break; |
| 1664 | case P_PID: |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1665 | type = PIDTYPE_PID; |
| 1666 | if (upid <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | return -EINVAL; |
| 1668 | break; |
| 1669 | case P_PGID: |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1670 | type = PIDTYPE_PGID; |
| 1671 | if (upid <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | break; |
| 1674 | default: |
| 1675 | return -EINVAL; |
| 1676 | } |
| 1677 | |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1678 | if (type < PIDTYPE_MAX) |
| 1679 | pid = find_get_pid(upid); |
| 1680 | ret = do_wait(type, pid, options, infop, NULL, ru); |
| 1681 | put_pid(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
| 1683 | /* avoid REGPARM breakage on x86: */ |
Roland McGrath | 54a0151 | 2008-04-10 15:37:38 -0700 | [diff] [blame] | 1684 | asmlinkage_protect(5, ret, which, upid, infop, options, ru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | return ret; |
| 1686 | } |
| 1687 | |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1688 | asmlinkage long sys_wait4(pid_t upid, int __user *stat_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | int options, struct rusage __user *ru) |
| 1690 | { |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1691 | struct pid *pid = NULL; |
| 1692 | enum pid_type type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | long ret; |
| 1694 | |
| 1695 | if (options & ~(WNOHANG|WUNTRACED|WCONTINUED| |
| 1696 | __WNOTHREAD|__WCLONE|__WALL)) |
| 1697 | return -EINVAL; |
Eric W. Biederman | 161550d | 2008-02-08 04:19:14 -0800 | [diff] [blame] | 1698 | |
| 1699 | if (upid == -1) |
| 1700 | type = PIDTYPE_MAX; |
| 1701 | else if (upid < 0) { |
| 1702 | type = PIDTYPE_PGID; |
| 1703 | pid = find_get_pid(-upid); |
| 1704 | } else if (upid == 0) { |
| 1705 | type = PIDTYPE_PGID; |
| 1706 | pid = get_pid(task_pgrp(current)); |
| 1707 | } else /* upid > 0 */ { |
| 1708 | type = PIDTYPE_PID; |
| 1709 | pid = find_get_pid(upid); |
| 1710 | } |
| 1711 | |
| 1712 | ret = do_wait(type, pid, options | WEXITED, NULL, stat_addr, ru); |
| 1713 | put_pid(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | |
| 1715 | /* avoid REGPARM breakage on x86: */ |
Roland McGrath | 54a0151 | 2008-04-10 15:37:38 -0700 | [diff] [blame] | 1716 | asmlinkage_protect(4, ret, upid, stat_addr, options, ru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | return ret; |
| 1718 | } |
| 1719 | |
| 1720 | #ifdef __ARCH_WANT_SYS_WAITPID |
| 1721 | |
| 1722 | /* |
| 1723 | * sys_waitpid() remains for compatibility. waitpid() should be |
| 1724 | * implemented by calling sys_wait4() from libc.a. |
| 1725 | */ |
| 1726 | asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options) |
| 1727 | { |
| 1728 | return sys_wait4(pid, stat_addr, options, NULL); |
| 1729 | } |
| 1730 | |
| 1731 | #endif |