blob: e8af8d0c2483232248ba163dc0e9eb03ad6ed504 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/exit.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/mm.h>
8#include <linux/slab.h>
9#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080011#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/completion.h>
13#include <linux/personality.h>
14#include <linux/tty.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080015#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/key.h>
17#include <linux/security.h>
18#include <linux/cpu.h>
19#include <linux/acct.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070020#include <linux/tsacct_kern.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/file.h>
22#include <linux/binfmts.h>
Serge E. Hallynab516012006-10-02 02:18:06 -070023#include <linux/nsproxy.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080024#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/ptrace.h>
26#include <linux/profile.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070027#include <linux/signalfd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mount.h>
29#include <linux/proc_fs.h>
Eric W. Biederman49d769d2007-05-09 02:34:33 -070030#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/mempolicy.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070032#include <linux/taskstats_kern.h>
Shailabh Nagarca74e922006-07-14 00:24:36 -070033#include <linux/delayacct.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070034#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/cpuset.h>
36#include <linux/syscalls.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070037#include <linux/signal.h>
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -080038#include <linux/posix-timers.h>
Matt Helsley9f460802005-11-07 00:59:16 -080039#include <linux/cn_proc.h>
Ingo Molnarde5097c2006-01-09 15:59:21 -080040#include <linux/mutex.h>
Ingo Molnar0771dfe2006-03-27 01:16:22 -080041#include <linux/futex.h>
Ingo Molnar34f192c2006-03-27 01:16:24 -080042#include <linux/compat.h>
Jens Axboeb92ce552006-04-11 13:52:07 +020043#include <linux/pipe_fs_i.h>
Al Virofa84cb92006-03-29 20:30:19 -050044#include <linux/audit.h> /* for audit_free() */
Adrian Bunk83cc5ed2006-06-25 05:47:41 -070045#include <linux/resource.h>
David Howells0d67a462006-08-29 19:05:56 +010046#include <linux/blkdev.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070047#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/uaccess.h>
50#include <asm/unistd.h>
51#include <asm/pgtable.h>
52#include <asm/mmu_context.h>
53
54extern void sem_exit (void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Adrian Bunk408b6642005-05-01 08:59:29 -070056static void exit_mm(struct task_struct * tsk);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void __unhash_process(struct task_struct *p)
59{
60 nr_threads--;
61 detach_pid(p, PIDTYPE_PID);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (thread_group_leader(p)) {
63 detach_pid(p, PIDTYPE_PGID);
64 detach_pid(p, PIDTYPE_SID);
Oleg Nesterovc97d9892006-03-28 16:11:06 -080065
Eric W. Biederman5e85d4a2006-04-18 22:20:16 -070066 list_del_rcu(&p->tasks);
Oleg Nesterov73b9ebf2006-03-28 16:11:07 -080067 __get_cpu_var(process_counts)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Oleg Nesterov47e65322006-03-28 16:11:25 -080069 list_del_rcu(&p->thread_group);
Oleg Nesterovc97d9892006-03-28 16:11:06 -080070 remove_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -080073/*
74 * This function expects the tasklist_lock write-locked.
75 */
76static void __exit_signal(struct task_struct *tsk)
77{
78 struct signal_struct *sig = tsk->signal;
79 struct sighand_struct *sighand;
80
81 BUG_ON(!sig);
82 BUG_ON(!atomic_read(&sig->count));
83
84 rcu_read_lock();
85 sighand = rcu_dereference(tsk->sighand);
86 spin_lock(&sighand->siglock);
87
Davide Libenzifba2afa2007-05-10 22:23:13 -070088 /*
89 * Notify that this sighand has been detached. This must
90 * be called with the tsk->sighand lock held. Also, this
91 * access tsk->sighand internally, so it must be called
92 * before tsk->sighand is reset.
93 */
94 signalfd_detach_locked(tsk);
95
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -080096 posix_cpu_timers_exit(tsk);
97 if (atomic_dec_and_test(&sig->count))
98 posix_cpu_timers_exit_group(tsk);
99 else {
100 /*
101 * If there is any task waiting for the group exit
102 * then notify it:
103 */
104 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
105 wake_up_process(sig->group_exit_task);
106 sig->group_exit_task = NULL;
107 }
108 if (tsk == sig->curr_target)
109 sig->curr_target = next_thread(tsk);
110 /*
111 * Accumulate here the counters for all threads but the
112 * group leader as they die, so they can be added into
113 * the process-wide totals when those are taken.
114 * The group leader stays around as a zombie as long
115 * as there are other threads. When it gets reaped,
116 * the exit.c code will add its counts into these totals.
117 * We won't ever get here for the group leader, since it
118 * will have been the last reference on the signal_struct.
119 */
120 sig->utime = cputime_add(sig->utime, tsk->utime);
121 sig->stime = cputime_add(sig->stime, tsk->stime);
122 sig->min_flt += tsk->min_flt;
123 sig->maj_flt += tsk->maj_flt;
124 sig->nvcsw += tsk->nvcsw;
125 sig->nivcsw += tsk->nivcsw;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -0700126 sig->inblock += task_io_get_inblock(tsk);
127 sig->oublock += task_io_get_oublock(tsk);
Balbir Singh172ba842007-07-09 18:52:00 +0200128 sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800129 sig = NULL; /* Marker for below. */
130 }
131
Oleg Nesterov58767002006-03-28 16:11:20 -0800132 __unhash_process(tsk);
133
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800134 tsk->signal = NULL;
Oleg Nesterova7e53282006-03-28 16:11:27 -0800135 tsk->sighand = NULL;
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800136 spin_unlock(&sighand->siglock);
137 rcu_read_unlock();
138
Oleg Nesterova7e53282006-03-28 16:11:27 -0800139 __cleanup_sighand(sighand);
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800140 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
141 flush_sigqueue(&tsk->pending);
142 if (sig) {
143 flush_sigqueue(&sig->shared_pending);
Oleg Nesterov093a8e82006-10-28 10:38:51 -0700144 taskstats_tgid_free(sig);
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800145 __cleanup_signal(sig);
146 }
147}
148
Eric W. Biederman8c7904a2006-03-31 02:31:37 -0800149static void delayed_put_task_struct(struct rcu_head *rhp)
150{
151 put_task_struct(container_of(rhp, struct task_struct, rcu));
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154void release_task(struct task_struct * p)
155{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700156 struct task_struct *leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 int zap_leader;
Oleg Nesterov1f09f972006-03-28 16:11:11 -0800158repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 atomic_dec(&p->user->processes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 write_lock_irq(&tasklist_lock);
Oleg Nesterov1f09f972006-03-28 16:11:11 -0800161 ptrace_unlink(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
163 __exit_signal(p);
Oleg Nesterov35f5cad2006-03-28 16:11:19 -0800164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * If we are the last non-leader member of the thread
167 * group, and the leader is zombie, then notify the
168 * group leader's parent process. (if it wants notification.)
169 */
170 zap_leader = 0;
171 leader = p->group_leader;
172 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
173 BUG_ON(leader->exit_signal == -1);
174 do_notify_parent(leader, leader->exit_signal);
175 /*
176 * If we were the last child thread and the leader has
177 * exited already, and the leader's parent ignores SIGCHLD,
178 * then we are the one who should release the leader.
179 *
180 * do_notify_parent() will have marked it self-reaping in
181 * that case.
182 */
183 zap_leader = (leader->exit_signal == -1);
184 }
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 write_unlock_irq(&tasklist_lock);
Eric W. Biederman48e64842006-06-26 00:25:48 -0700187 proc_flush_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 release_thread(p);
Eric W. Biederman8c7904a2006-03-31 02:31:37 -0800189 call_rcu(&p->rcu, delayed_put_task_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 p = leader;
192 if (unlikely(zap_leader))
193 goto repeat;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
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. Biederman04a2e6a2007-02-12 00:52:56 -0800200 *
201 * The caller must hold rcu lock or the tasklist lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -0800203struct pid *session_of_pgrp(struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct task_struct *p;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -0800206 struct pid *sid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -0800208 p = pid_task(pgrp, PIDTYPE_PGID);
Oleg Nesterov62dfb552006-12-08 02:38:03 -0800209 if (p == NULL)
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -0800210 p = pid_task(pgrp, PIDTYPE_PID);
Oleg Nesterov62dfb552006-12-08 02:38:03 -0800211 if (p != NULL)
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -0800212 sid = task_session(p);
Oleg Nesterov62dfb552006-12-08 02:38:03 -0800213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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. Biederman0475ac02007-02-12 00:52:57 -0800225static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct task_struct *p;
228 int ret = 1;
229
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800230 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (p == ignored_task
232 || p->exit_state
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700233 || is_init(p->real_parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 continue;
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800235 if (task_pgrp(p->real_parent) != pgrp &&
236 task_session(p->real_parent) == task_session(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 ret = 0;
238 break;
239 }
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800240 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return ret; /* (sighing) "Often!" */
242}
243
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -0800244int is_current_pgrp_orphaned(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int retval;
247
248 read_lock(&tasklist_lock);
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -0800249 retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 read_unlock(&tasklist_lock);
251
252 return retval;
253}
254
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800255static int has_stopped_jobs(struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 int retval = 0;
258 struct task_struct *p;
259
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800260 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (p->state != TASK_STOPPED)
262 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 retval = 1;
264 break;
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800265 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return retval;
267}
268
269/**
Eric W. Biederman49d769d2007-05-09 02:34:33 -0700270 * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
272 * If a kernel thread is launched as a result of a system call, or if
Eric W. Biederman49d769d2007-05-09 02:34:33 -0700273 * it ever exits, it should generally reparent itself to kthreadd so it
274 * isn't in the way of other processes and is correctly cleaned up on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
276 * The various task state such as scheduling policy and priority may have
277 * been inherited from a user process, so we reset them to sane values here.
278 *
Eric W. Biederman49d769d2007-05-09 02:34:33 -0700279 * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Eric W. Biederman49d769d2007-05-09 02:34:33 -0700281static void reparent_to_kthreadd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 write_lock_irq(&tasklist_lock);
284
285 ptrace_unlink(current);
286 /* Reparent to init */
Oleg Nesterov9b678ec2006-03-28 16:11:05 -0800287 remove_parent(current);
Eric W. Biederman49d769d2007-05-09 02:34:33 -0700288 current->real_parent = current->parent = kthreadd_task;
Oleg Nesterov9b678ec2006-03-28 16:11:05 -0800289 add_parent(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* Set the exit signal to SIGCHLD so we signal init on exit */
292 current->exit_signal = SIGCHLD;
293
Ingo Molnare05606d2007-07-09 18:51:59 +0200294 if (task_nice(current) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 set_user_nice(current, 0);
296 /* cpus_allowed? */
297 /* rt_priority? */
298 /* signals? */
299 security_task_reparent_to_init(current);
300 memcpy(current->signal->rlim, init_task.signal->rlim,
301 sizeof(current->signal->rlim));
302 atomic_inc(&(INIT_USER->__count));
303 write_unlock_irq(&tasklist_lock);
304 switch_uid(INIT_USER);
305}
306
307void __set_special_pids(pid_t session, pid_t pgrp)
308{
Oren Laadane19f2472006-01-08 01:03:58 -0800309 struct task_struct *curr = current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Cedric Le Goater937949d2006-12-08 02:37:54 -0800311 if (process_session(curr) != session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 detach_pid(curr, PIDTYPE_SID);
Cedric Le Goater1ec320a2006-12-08 02:37:55 -0800313 set_signal_session(curr->signal, session);
Sukadev Bhattiprolue713d0d2007-05-10 22:22:58 -0700314 attach_pid(curr, PIDTYPE_SID, find_pid(session));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 if (process_group(curr) != pgrp) {
317 detach_pid(curr, PIDTYPE_PGID);
318 curr->signal->pgrp = pgrp;
Sukadev Bhattiprolue713d0d2007-05-10 22:22:58 -0700319 attach_pid(curr, PIDTYPE_PGID, find_pid(pgrp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321}
322
Oleg Nesterovae424ae2006-12-08 02:36:08 -0800323static void set_special_pids(pid_t session, pid_t pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 write_lock_irq(&tasklist_lock);
326 __set_special_pids(session, pgrp);
327 write_unlock_irq(&tasklist_lock);
328}
329
330/*
331 * Let kernel threads use this to say that they
332 * allow a certain signal (since daemonize() will
333 * have disabled all of them by default).
334 */
335int allow_signal(int sig)
336{
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700337 if (!valid_signal(sig) || sig < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -EINVAL;
339
340 spin_lock_irq(&current->sighand->siglock);
341 sigdelset(&current->blocked, sig);
342 if (!current->mm) {
343 /* Kernel threads handle their own signals.
344 Let the signal code know it'll be handled, so
345 that they don't get converted to SIGKILL or
346 just silently dropped */
347 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
348 }
349 recalc_sigpending();
350 spin_unlock_irq(&current->sighand->siglock);
351 return 0;
352}
353
354EXPORT_SYMBOL(allow_signal);
355
356int disallow_signal(int sig)
357{
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700358 if (!valid_signal(sig) || sig < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EINVAL;
360
361 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700362 current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 recalc_sigpending();
364 spin_unlock_irq(&current->sighand->siglock);
365 return 0;
366}
367
368EXPORT_SYMBOL(disallow_signal);
369
370/*
371 * Put all the gunge required to become a kernel thread without
372 * attached user resources in one place where it belongs.
373 */
374
375void daemonize(const char *name, ...)
376{
377 va_list args;
378 struct fs_struct *fs;
379 sigset_t blocked;
380
381 va_start(args, name);
382 vsnprintf(current->comm, sizeof(current->comm), name, args);
383 va_end(args);
384
385 /*
386 * If we were started as result of loading a module, close all of the
387 * user space pages. We don't need them, and if we didn't close them
388 * they would be locked into memory.
389 */
390 exit_mm(current);
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700391 /*
392 * We don't want to have TIF_FREEZE set if the system-wide hibernation
393 * or suspend transition begins right now.
394 */
395 current->flags |= PF_NOFREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 set_special_pids(1, 1);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800398 proc_clear_tty(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* Block and flush all signals */
401 sigfillset(&blocked);
402 sigprocmask(SIG_BLOCK, &blocked, NULL);
403 flush_signals(current);
404
405 /* Become as one with the init task */
406
407 exit_fs(current); /* current->fs->count--; */
408 fs = init_task.fs;
409 current->fs = fs;
410 atomic_inc(&fs->count);
Serge E. Hallynab516012006-10-02 02:18:06 -0700411
Linus Torvalds444f3782007-01-30 13:35:18 -0800412 exit_task_namespaces(current);
Serge E. Hallynab516012006-10-02 02:18:06 -0700413 current->nsproxy = init_task.nsproxy;
Serge E. Hallynab516012006-10-02 02:18:06 -0700414 get_task_namespaces(current);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 exit_files(current);
417 current->files = init_task.files;
418 atomic_inc(&current->files->count);
419
Eric W. Biederman49d769d2007-05-09 02:34:33 -0700420 reparent_to_kthreadd();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423EXPORT_SYMBOL(daemonize);
424
Arjan van de Ven858119e2006-01-14 13:20:43 -0800425static void close_files(struct files_struct * files)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 int i, j;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700428 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 j = 0;
Dipankar Sarma4fb3a532005-09-16 19:28:13 -0700431
432 /*
433 * It is safe to dereference the fd table without RCU or
434 * ->file_lock because this is the last reference to the
435 * files structure.
436 */
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700437 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 for (;;) {
439 unsigned long set;
440 i = j * __NFDBITS;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800441 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700443 set = fdt->open_fds->fds_bits[j++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 while (set) {
445 if (set & 1) {
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700446 struct file * file = xchg(&fdt->fd[i], NULL);
Ingo Molnar944be0b2007-02-12 00:52:26 -0800447 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 filp_close(file, files);
Ingo Molnar944be0b2007-02-12 00:52:26 -0800449 cond_resched();
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 i++;
453 set >>= 1;
454 }
455 }
456}
457
458struct files_struct *get_files_struct(struct task_struct *task)
459{
460 struct files_struct *files;
461
462 task_lock(task);
463 files = task->files;
464 if (files)
465 atomic_inc(&files->count);
466 task_unlock(task);
467
468 return files;
469}
470
471void fastcall put_files_struct(struct files_struct *files)
472{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700473 struct fdtable *fdt;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (atomic_dec_and_test(&files->count)) {
476 close_files(files);
477 /*
478 * Free the fd and fdset arrays if we expanded them.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700479 * If the fdtable was embedded, pass files for freeing
480 * at the end of the RCU grace period. Otherwise,
481 * you can free files immediately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700483 fdt = files_fdtable(files);
Vadim Lobanov4fd45812006-12-10 02:21:17 -0800484 if (fdt != &files->fdtab)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700485 kmem_cache_free(files_cachep, files);
Vadim Lobanov01b2d932006-12-22 01:10:43 -0800486 free_fdtable(fdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488}
489
490EXPORT_SYMBOL(put_files_struct);
491
Kirill Korotaev3b9b8ab2006-09-29 02:00:05 -0700492void reset_files_struct(struct task_struct *tsk, struct files_struct *files)
493{
494 struct files_struct *old;
495
496 old = tsk->files;
497 task_lock(tsk);
498 tsk->files = files;
499 task_unlock(tsk);
500 put_files_struct(old);
501}
502EXPORT_SYMBOL(reset_files_struct);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static inline void __exit_files(struct task_struct *tsk)
505{
506 struct files_struct * files = tsk->files;
507
508 if (files) {
509 task_lock(tsk);
510 tsk->files = NULL;
511 task_unlock(tsk);
512 put_files_struct(files);
513 }
514}
515
516void exit_files(struct task_struct *tsk)
517{
518 __exit_files(tsk);
519}
520
521static inline void __put_fs_struct(struct fs_struct *fs)
522{
523 /* No need to hold fs->lock if we are killing it */
524 if (atomic_dec_and_test(&fs->count)) {
525 dput(fs->root);
526 mntput(fs->rootmnt);
527 dput(fs->pwd);
528 mntput(fs->pwdmnt);
529 if (fs->altroot) {
530 dput(fs->altroot);
531 mntput(fs->altrootmnt);
532 }
533 kmem_cache_free(fs_cachep, fs);
534 }
535}
536
537void put_fs_struct(struct fs_struct *fs)
538{
539 __put_fs_struct(fs);
540}
541
542static inline void __exit_fs(struct task_struct *tsk)
543{
544 struct fs_struct * fs = tsk->fs;
545
546 if (fs) {
547 task_lock(tsk);
548 tsk->fs = NULL;
549 task_unlock(tsk);
550 __put_fs_struct(fs);
551 }
552}
553
554void exit_fs(struct task_struct *tsk)
555{
556 __exit_fs(tsk);
557}
558
559EXPORT_SYMBOL_GPL(exit_fs);
560
561/*
562 * Turn us into a lazy TLB process if we
563 * aren't already..
564 */
Adrian Bunk408b6642005-05-01 08:59:29 -0700565static void exit_mm(struct task_struct * tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct mm_struct *mm = tsk->mm;
568
569 mm_release(tsk, mm);
570 if (!mm)
571 return;
572 /*
573 * Serialize with any possible pending coredump.
574 * We must hold mmap_sem around checking core_waiters
575 * and clearing tsk->mm. The core-inducing thread
576 * will increment core_waiters for each thread in the
577 * group with ->mm != NULL.
578 */
579 down_read(&mm->mmap_sem);
580 if (mm->core_waiters) {
581 up_read(&mm->mmap_sem);
582 down_write(&mm->mmap_sem);
583 if (!--mm->core_waiters)
584 complete(mm->core_startup_done);
585 up_write(&mm->mmap_sem);
586
587 wait_for_completion(&mm->core_done);
588 down_read(&mm->mmap_sem);
589 }
590 atomic_inc(&mm->mm_count);
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700591 BUG_ON(mm != tsk->active_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* more a memory barrier than a real lock */
593 task_lock(tsk);
594 tsk->mm = NULL;
595 up_read(&mm->mmap_sem);
596 enter_lazy_tlb(mm, current);
597 task_unlock(tsk);
598 mmput(mm);
599}
600
Ingo Molnar36c8b582006-07-03 00:25:41 -0700601static inline void
602choose_new_parent(struct task_struct *p, struct task_struct *reaper)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 /*
605 * Make sure we're not reparenting to ourselves and that
606 * the parent is not a zombie.
607 */
Oleg Nesterovd799f032006-03-28 16:11:04 -0800608 BUG_ON(p == reaper || reaper->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 p->real_parent = reaper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Ingo Molnar36c8b582006-07-03 00:25:41 -0700612static void
613reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Oleg Nesterov241ceee2006-12-24 23:30:44 +0300615 if (p->pdeath_signal)
616 /* We already hold the tasklist_lock here. */
617 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Move the child from its dying parent to the new one. */
620 if (unlikely(traced)) {
621 /* Preserve ptrace links if someone else is tracing this child. */
622 list_del_init(&p->ptrace_list);
623 if (p->parent != p->real_parent)
624 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
625 } else {
626 /* If this child is being traced, then we're the one tracing it
627 * anyway, so let go of it.
628 */
629 p->ptrace = 0;
Oleg Nesterov6ac781b2006-03-28 16:11:09 -0800630 remove_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 p->parent = p->real_parent;
Oleg Nesterov6ac781b2006-03-28 16:11:09 -0800632 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Eric W. Biedermanb2b2cbc2006-12-21 21:28:40 -0700634 if (p->state == TASK_TRACED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
636 * If it was at a trace stop, turn it into
637 * a normal stop since it's no longer being
638 * traced.
639 */
640 ptrace_untrace(p);
641 }
642 }
643
Eric W. Biedermanb2b2cbc2006-12-21 21:28:40 -0700644 /* If this is a threaded reparent there is no need to
645 * notify anyone anything has happened.
646 */
647 if (p->real_parent->group_leader == father->group_leader)
648 return;
649
650 /* We don't want people slaying init. */
651 if (p->exit_signal != -1)
652 p->exit_signal = SIGCHLD;
Eric W. Biedermanb2b2cbc2006-12-21 21:28:40 -0700653
654 /* If we'd notified the old parent about this child's death,
655 * also notify the new parent.
656 */
657 if (!traced && p->exit_state == EXIT_ZOMBIE &&
658 p->exit_signal != -1 && thread_group_empty(p))
659 do_notify_parent(p, p->exit_signal);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * process group orphan check
663 * Case ii: Our child is in a different pgrp
664 * than we are, and it was the only connection
665 * outside, so the child pgrp is now orphaned.
666 */
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800667 if ((task_pgrp(p) != task_pgrp(father)) &&
668 (task_session(p) == task_session(father))) {
669 struct pid *pgrp = task_pgrp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Cedric Le Goater937949d2006-12-08 02:37:54 -0800671 if (will_become_orphaned_pgrp(pgrp, NULL) &&
672 has_stopped_jobs(pgrp)) {
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800673 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
674 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 }
677}
678
679/*
680 * When we die, we re-parent all our children.
681 * Try to give them to another thread in our thread
682 * group, and if no such member exists, give it to
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -0800683 * the child reaper process (ie "init") in our pid
684 * space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700686static void
687forget_original_parent(struct task_struct *father, struct list_head *to_release)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct task_struct *p, *reaper = father;
690 struct list_head *_p, *_n;
691
692 do {
693 reaper = next_thread(reaper);
694 if (reaper == father) {
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -0800695 reaper = child_reaper(father);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 }
698 } while (reaper->exit_state);
699
700 /*
701 * There are only two places where our children can be:
702 *
703 * - in our child list
704 * - in our ptraced child list
705 *
706 * Search them and reparent children.
707 */
708 list_for_each_safe(_p, _n, &father->children) {
709 int ptrace;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700710 p = list_entry(_p, struct task_struct, sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 ptrace = p->ptrace;
713
714 /* if father isn't the real parent, then ptrace must be enabled */
715 BUG_ON(father != p->real_parent && !ptrace);
716
717 if (father == p->real_parent) {
718 /* reparent with a reaper, real father it's us */
Oleg Nesterovd799f032006-03-28 16:11:04 -0800719 choose_new_parent(p, reaper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 reparent_thread(p, father, 0);
721 } else {
722 /* reparent ptraced task to its real parent */
723 __ptrace_unlink (p);
724 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
725 thread_group_empty(p))
726 do_notify_parent(p, p->exit_signal);
727 }
728
729 /*
730 * if the ptraced child is a zombie with exit_signal == -1
731 * we must collect it before we exit, or it will remain
732 * zombie forever since we prevented it from self-reap itself
733 * while it was being traced by us, to be able to see it in wait4.
734 */
735 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
736 list_add(&p->ptrace_list, to_release);
737 }
738 list_for_each_safe(_p, _n, &father->ptrace_children) {
Ingo Molnar36c8b582006-07-03 00:25:41 -0700739 p = list_entry(_p, struct task_struct, ptrace_list);
Oleg Nesterovd799f032006-03-28 16:11:04 -0800740 choose_new_parent(p, reaper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 reparent_thread(p, father, 1);
742 }
743}
744
745/*
746 * Send signals to all our closest relatives so that they know
747 * to properly mourn us..
748 */
749static void exit_notify(struct task_struct *tsk)
750{
751 int state;
752 struct task_struct *t;
753 struct list_head ptrace_dead, *_p, *_n;
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800754 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
757 && !thread_group_empty(tsk)) {
758 /*
759 * This occurs when there was a race between our exit
760 * syscall and a group signal choosing us as the one to
761 * wake up. It could be that we are the only thread
762 * alerted to check for pending signals, but another thread
763 * should be woken now to take the signal since we will not.
764 * Now we'll wake all the threads in the group just to make
765 * sure someone gets all the pending signals.
766 */
767 read_lock(&tasklist_lock);
768 spin_lock_irq(&tsk->sighand->siglock);
769 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700770 if (!signal_pending(t) && !(t->flags & PF_EXITING))
771 recalc_sigpending_and_wake(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 spin_unlock_irq(&tsk->sighand->siglock);
773 read_unlock(&tasklist_lock);
774 }
775
776 write_lock_irq(&tasklist_lock);
777
778 /*
779 * This does two things:
780 *
781 * A. Make init inherit all the child processes
782 * B. Check to see if any process groups have become orphaned
783 * as a result of our exiting, and if they have any stopped
784 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
785 */
786
787 INIT_LIST_HEAD(&ptrace_dead);
788 forget_original_parent(tsk, &ptrace_dead);
789 BUG_ON(!list_empty(&tsk->children));
790 BUG_ON(!list_empty(&tsk->ptrace_children));
791
792 /*
793 * Check to see if any process groups have become orphaned
794 * as a result of our exiting, and if they have any stopped
795 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
796 *
797 * Case i: Our father is in a different pgrp than we are
798 * and we were the only connection outside, so our pgrp
799 * is about to become orphaned.
800 */
801
802 t = tsk->real_parent;
803
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800804 pgrp = task_pgrp(tsk);
805 if ((task_pgrp(t) != pgrp) &&
Eric W. Biederman14e9d572007-03-28 23:38:16 -0600806 (task_session(t) == task_session(tsk)) &&
Eric W. Biederman0475ac02007-02-12 00:52:57 -0800807 will_become_orphaned_pgrp(pgrp, tsk) &&
808 has_stopped_jobs(pgrp)) {
809 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
810 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 /* Let father know we died
814 *
815 * Thread signals are configurable, but you aren't going to use
816 * that to send signals to arbitary processes.
817 * That stops right now.
818 *
819 * If the parent exec id doesn't match the exec id we saved
820 * when we started then we know the parent has changed security
821 * domain.
822 *
823 * If our self_exec id doesn't match our parent_exec_id then
824 * we have changed execution domain as these two values started
825 * the same after a fork.
826 *
827 */
828
829 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
830 ( tsk->parent_exec_id != t->self_exec_id ||
831 tsk->self_exec_id != tsk->parent_exec_id)
832 && !capable(CAP_KILL))
833 tsk->exit_signal = SIGCHLD;
834
835
836 /* If something other than our normal parent is ptracing us, then
837 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
838 * only has special meaning to our real parent.
839 */
840 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
841 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
842 do_notify_parent(tsk, signal);
843 } else if (tsk->ptrace) {
844 do_notify_parent(tsk, SIGCHLD);
845 }
846
847 state = EXIT_ZOMBIE;
848 if (tsk->exit_signal == -1 &&
849 (likely(tsk->ptrace == 0) ||
850 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
851 state = EXIT_DEAD;
852 tsk->exit_state = state;
853
854 write_unlock_irq(&tasklist_lock);
855
856 list_for_each_safe(_p, _n, &ptrace_dead) {
857 list_del_init(_p);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700858 t = list_entry(_p, struct task_struct, ptrace_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 release_task(t);
860 }
861
862 /* If the process is dead, release it - nobody will wait for it */
863 if (state == EXIT_DEAD)
864 release_task(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Jeff Dikee18eecb2007-07-15 23:38:48 -0700867#ifdef CONFIG_DEBUG_STACK_USAGE
868static void check_stack_usage(void)
869{
870 static DEFINE_SPINLOCK(low_water_lock);
871 static int lowest_to_date = THREAD_SIZE;
872 unsigned long *n = end_of_stack(current);
873 unsigned long free;
874
875 while (*n == 0)
876 n++;
877 free = (unsigned long)n - (unsigned long)end_of_stack(current);
878
879 if (free >= lowest_to_date)
880 return;
881
882 spin_lock(&low_water_lock);
883 if (free < lowest_to_date) {
884 printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
885 "left\n",
886 current->comm, free);
887 lowest_to_date = free;
888 }
889 spin_unlock(&low_water_lock);
890}
891#else
892static inline void check_stack_usage(void) {}
893#endif
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895fastcall NORET_TYPE void do_exit(long code)
896{
897 struct task_struct *tsk = current;
898 int group_dead;
899
900 profile_task_exit(tsk);
901
Jens Axboe22e2c502005-06-27 10:55:12 +0200902 WARN_ON(atomic_read(&tsk->fs_excl));
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (unlikely(in_interrupt()))
905 panic("Aiee, killing interrupt handler!");
906 if (unlikely(!tsk->pid))
907 panic("Attempted to kill the idle task!");
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -0800908 if (unlikely(tsk == child_reaper(tsk))) {
909 if (tsk->nsproxy->pid_ns != &init_pid_ns)
910 tsk->nsproxy->pid_ns->child_reaper = init_pid_ns.child_reaper;
911 else
912 panic("Attempted to kill init!");
913 }
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
917 current->ptrace_message = code;
918 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
919 }
920
Alexander Nybergdf164db2005-06-23 00:09:13 -0700921 /*
922 * We're taking recursive faults here in do_exit. Safest is to just
923 * leave this task alone and wait for reboot.
924 */
925 if (unlikely(tsk->flags & PF_EXITING)) {
926 printk(KERN_ALERT
927 "Fixing recursive fault but reboot is needed!\n");
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700928 /*
929 * We can do this unlocked here. The futex code uses
930 * this flag just to verify whether the pi state
931 * cleanup has been done or not. In the worst case it
932 * loops once more. We pretend that the cleanup was
933 * done as there is no way to return. Either the
934 * OWNER_DIED bit is set by now or we push the blocked
935 * task into the wait for ever nirwana as well.
936 */
937 tsk->flags |= PF_EXITPIDONE;
Al Viroafc847b2006-02-28 12:51:55 -0500938 if (tsk->io_context)
939 exit_io_context();
Alexander Nybergdf164db2005-06-23 00:09:13 -0700940 set_current_state(TASK_UNINTERRUPTIBLE);
941 schedule();
942 }
943
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700944 /*
945 * tsk->flags are checked in the futex code to protect against
946 * an exiting task cleaning up the robust pi futexes.
947 */
948 spin_lock_irq(&tsk->pi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 tsk->flags |= PF_EXITING;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700950 spin_unlock_irq(&tsk->pi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (unlikely(in_atomic()))
953 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
954 current->comm, current->pid,
955 preempt_count());
956
957 acct_update_integrals(tsk);
Hugh Dickins365e9c872005-10-29 18:16:18 -0700958 if (tsk->mm) {
959 update_hiwater_rss(tsk->mm);
960 update_hiwater_vm(tsk->mm);
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 group_dead = atomic_dec_and_test(&tsk->signal->live);
Andrew Mortonc3068952005-08-04 16:49:32 -0700963 if (group_dead) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700964 hrtimer_cancel(&tsk->signal->real_timer);
Roland McGrath25f407f2005-10-21 15:03:29 -0700965 exit_itimers(tsk->signal);
Andrew Mortonc3068952005-08-04 16:49:32 -0700966 }
KaiGai Koheif6ec29a2006-06-25 05:49:25 -0700967 acct_collect(code, group_dead);
Ingo Molnar0771dfe2006-03-27 01:16:22 -0800968 if (unlikely(tsk->robust_list))
969 exit_robust_list(tsk);
Anton Blanchard2aa92582006-06-25 05:48:44 -0700970#if defined(CONFIG_FUTEX) && defined(CONFIG_COMPAT)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800971 if (unlikely(tsk->compat_robust_list))
972 compat_exit_robust_list(tsk);
973#endif
Miloslav Trmac522ed772007-07-15 23:40:56 -0700974 if (group_dead)
975 tty_audit_exit();
Al Virofa84cb92006-03-29 20:30:19 -0500976 if (unlikely(tsk->audit_context))
977 audit_free(tsk);
Oleg Nesterov115085e2006-12-06 20:36:51 -0800978
979 taskstats_exit(tsk, group_dead);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 exit_mm(tsk);
982
KaiGai Kohei0e464812006-06-25 05:49:24 -0700983 if (group_dead)
KaiGai Koheif6ec29a2006-06-25 05:49:25 -0700984 acct_process();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 exit_sem(tsk);
986 __exit_files(tsk);
987 __exit_fs(tsk);
Jeff Dikee18eecb2007-07-15 23:38:48 -0700988 check_stack_usage();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 exit_thread();
990 cpuset_exit(tsk);
991 exit_keys(tsk);
992
993 if (group_dead && tsk->signal->leader)
994 disassociate_ctty(1);
995
Al Viroa1261f52005-11-13 16:06:55 -0800996 module_put(task_thread_info(tsk)->exec_domain->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (tsk->binfmt)
998 module_put(tsk->binfmt->module);
999
1000 tsk->exit_code = code;
Matt Helsley9f460802005-11-07 00:59:16 -08001001 proc_exit_connector(tsk);
Linus Torvalds444f3782007-01-30 13:35:18 -08001002 exit_task_namespaces(tsk);
Serge E. Hallyn0f245282007-01-30 15:28:23 -06001003 exit_notify(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#ifdef CONFIG_NUMA
1005 mpol_free(tsk->mempolicy);
1006 tsk->mempolicy = NULL;
1007#endif
Ingo Molnarde5097c2006-01-09 15:59:21 -08001008 /*
Ingo Molnarc87e2832006-06-27 02:54:58 -07001009 * This must happen late, after the PID is not
1010 * hashed anymore:
1011 */
1012 if (unlikely(!list_empty(&tsk->pi_state_list)))
1013 exit_pi_state_list(tsk);
1014 if (unlikely(current->pi_state_cache))
1015 kfree(current->pi_state_cache);
1016 /*
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001017 * Make sure we are holding no locks:
Ingo Molnarde5097c2006-01-09 15:59:21 -08001018 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001019 debug_check_no_locks_held(tsk);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001020 /*
1021 * We can do this unlocked here. The futex code uses this flag
1022 * just to verify whether the pi state cleanup has been done
1023 * or not. In the worst case it loops once more.
1024 */
1025 tsk->flags |= PF_EXITPIDONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Al Viroafc847b2006-02-28 12:51:55 -05001027 if (tsk->io_context)
1028 exit_io_context();
1029
Jens Axboeb92ce552006-04-11 13:52:07 +02001030 if (tsk->splice_pipe)
1031 __free_pipe_info(tsk->splice_pipe);
1032
Coywolf Qi Hunt74072512005-10-30 15:02:47 -08001033 preempt_disable();
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001034 /* causes final put_task_struct in finish_task_switch(). */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001035 tsk->state = TASK_DEAD;
Coywolf Qi Hunt74072512005-10-30 15:02:47 -08001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 schedule();
1038 BUG();
1039 /* Avoid "noreturn function does return". */
Alan Cox54306cf2006-09-29 02:00:42 -07001040 for (;;)
1041 cpu_relax(); /* For when BUG is null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Russ Anderson012914d2005-04-23 00:08:00 -07001044EXPORT_SYMBOL_GPL(do_exit);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046NORET_TYPE void complete_and_exit(struct completion *comp, long code)
1047{
1048 if (comp)
1049 complete(comp);
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 do_exit(code);
1052}
1053
1054EXPORT_SYMBOL(complete_and_exit);
1055
1056asmlinkage long sys_exit(int error_code)
1057{
1058 do_exit((error_code&0xff)<<8);
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/*
1062 * Take down every thread in the group. This is called by fatal signals
1063 * as well as by sys_exit_group (below).
1064 */
1065NORET_TYPE void
1066do_group_exit(int exit_code)
1067{
1068 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
1069
1070 if (current->signal->flags & SIGNAL_GROUP_EXIT)
1071 exit_code = current->signal->group_exit_code;
1072 else if (!thread_group_empty(current)) {
1073 struct signal_struct *const sig = current->signal;
1074 struct sighand_struct *const sighand = current->sighand;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 spin_lock_irq(&sighand->siglock);
1076 if (sig->flags & SIGNAL_GROUP_EXIT)
1077 /* Another thread got here before we took the lock. */
1078 exit_code = sig->group_exit_code;
1079 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 sig->group_exit_code = exit_code;
1081 zap_other_threads(current);
1082 }
1083 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 do_exit(exit_code);
1087 /* NOTREACHED */
1088}
1089
1090/*
1091 * this kills every thread in the thread group. Note that any externally
1092 * wait4()-ing process will get the correct exit code - even if this
1093 * thread is not the thread group leader.
1094 */
1095asmlinkage void sys_exit_group(int error_code)
1096{
1097 do_group_exit((error_code & 0xff) << 8);
1098}
1099
Ingo Molnar36c8b582006-07-03 00:25:41 -07001100static int eligible_child(pid_t pid, int options, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Roland McGrath73243282007-05-06 14:50:20 -07001102 int err;
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (pid > 0) {
1105 if (p->pid != pid)
1106 return 0;
1107 } else if (!pid) {
1108 if (process_group(p) != process_group(current))
1109 return 0;
1110 } else if (pid != -1) {
1111 if (process_group(p) != -pid)
1112 return 0;
1113 }
1114
1115 /*
1116 * Do not consider detached threads that are
1117 * not ptraced:
1118 */
1119 if (p->exit_signal == -1 && !p->ptrace)
1120 return 0;
1121
1122 /* Wait for all children (clone and not) if __WALL is set;
1123 * otherwise, wait for clone children *only* if __WCLONE is
1124 * set; otherwise, wait for non-clone children *only*. (Note:
1125 * A "clone" child here is one that reports to its parent
1126 * using a signal other than SIGCHLD.) */
1127 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
1128 && !(options & __WALL))
1129 return 0;
1130 /*
1131 * Do not consider thread group leaders that are
1132 * in a non-empty thread group:
1133 */
Oleg Nesterov3b6362b2006-09-02 21:22:16 +04001134 if (delay_group_leader(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return 2;
1136
Roland McGrath73243282007-05-06 14:50:20 -07001137 err = security_task_wait(p);
1138 if (err)
1139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 return 1;
1142}
1143
Ingo Molnar36c8b582006-07-03 00:25:41 -07001144static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int why, int status,
1146 struct siginfo __user *infop,
1147 struct rusage __user *rusagep)
1148{
1149 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 put_task_struct(p);
1152 if (!retval)
1153 retval = put_user(SIGCHLD, &infop->si_signo);
1154 if (!retval)
1155 retval = put_user(0, &infop->si_errno);
1156 if (!retval)
1157 retval = put_user((short)why, &infop->si_code);
1158 if (!retval)
1159 retval = put_user(pid, &infop->si_pid);
1160 if (!retval)
1161 retval = put_user(uid, &infop->si_uid);
1162 if (!retval)
1163 retval = put_user(status, &infop->si_status);
1164 if (!retval)
1165 retval = pid;
1166 return retval;
1167}
1168
1169/*
1170 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1171 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1172 * the lock and this task is uninteresting. If we return nonzero, we have
1173 * released the lock and the system call should return.
1174 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001175static int wait_task_zombie(struct task_struct *p, int noreap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct siginfo __user *infop,
1177 int __user *stat_addr, struct rusage __user *ru)
1178{
1179 unsigned long state;
1180 int retval;
1181 int status;
1182
1183 if (unlikely(noreap)) {
1184 pid_t pid = p->pid;
1185 uid_t uid = p->uid;
1186 int exit_code = p->exit_code;
1187 int why, status;
1188
1189 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1190 return 0;
1191 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1192 return 0;
1193 get_task_struct(p);
1194 read_unlock(&tasklist_lock);
1195 if ((exit_code & 0x7f) == 0) {
1196 why = CLD_EXITED;
1197 status = exit_code >> 8;
1198 } else {
1199 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1200 status = exit_code & 0x7f;
1201 }
1202 return wait_noreap_copyout(p, pid, uid, why,
1203 status, infop, ru);
1204 }
1205
1206 /*
1207 * Try to move the task's state to DEAD
1208 * only one thread is allowed to do this:
1209 */
1210 state = xchg(&p->exit_state, EXIT_DEAD);
1211 if (state != EXIT_ZOMBIE) {
1212 BUG_ON(state != EXIT_DEAD);
1213 return 0;
1214 }
1215 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1216 /*
1217 * This can only happen in a race with a ptraced thread
1218 * dying on another processor.
1219 */
1220 return 0;
1221 }
1222
1223 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
Jesper Juhl3795e162006-01-09 20:54:39 -08001224 struct signal_struct *psig;
1225 struct signal_struct *sig;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * The resource counters for the group leader are in its
1229 * own task_struct. Those for dead threads in the group
1230 * are in its signal_struct, as are those for the child
1231 * processes it has previously reaped. All these
1232 * accumulate in the parent's signal_struct c* fields.
1233 *
1234 * We don't bother to take a lock here to protect these
1235 * p->signal fields, because they are only touched by
1236 * __exit_signal, which runs with tasklist_lock
1237 * write-locked anyway, and so is excluded here. We do
1238 * need to protect the access to p->parent->signal fields,
1239 * as other threads in the parent group can be right
1240 * here reaping other children at the same time.
1241 */
1242 spin_lock_irq(&p->parent->sighand->siglock);
Jesper Juhl3795e162006-01-09 20:54:39 -08001243 psig = p->parent->signal;
1244 sig = p->signal;
1245 psig->cutime =
1246 cputime_add(psig->cutime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 cputime_add(p->utime,
Jesper Juhl3795e162006-01-09 20:54:39 -08001248 cputime_add(sig->utime,
1249 sig->cutime)));
1250 psig->cstime =
1251 cputime_add(psig->cstime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 cputime_add(p->stime,
Jesper Juhl3795e162006-01-09 20:54:39 -08001253 cputime_add(sig->stime,
1254 sig->cstime)));
1255 psig->cmin_flt +=
1256 p->min_flt + sig->min_flt + sig->cmin_flt;
1257 psig->cmaj_flt +=
1258 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1259 psig->cnvcsw +=
1260 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1261 psig->cnivcsw +=
1262 p->nivcsw + sig->nivcsw + sig->cnivcsw;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001263 psig->cinblock +=
1264 task_io_get_inblock(p) +
1265 sig->inblock + sig->cinblock;
1266 psig->coublock +=
1267 task_io_get_oublock(p) +
1268 sig->oublock + sig->coublock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 spin_unlock_irq(&p->parent->sighand->siglock);
1270 }
1271
1272 /*
1273 * Now we are sure this task is interesting, and no other
1274 * thread can reap it because we set its state to EXIT_DEAD.
1275 */
1276 read_unlock(&tasklist_lock);
1277
1278 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1279 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1280 ? p->signal->group_exit_code : p->exit_code;
1281 if (!retval && stat_addr)
1282 retval = put_user(status, stat_addr);
1283 if (!retval && infop)
1284 retval = put_user(SIGCHLD, &infop->si_signo);
1285 if (!retval && infop)
1286 retval = put_user(0, &infop->si_errno);
1287 if (!retval && infop) {
1288 int why;
1289
1290 if ((status & 0x7f) == 0) {
1291 why = CLD_EXITED;
1292 status >>= 8;
1293 } else {
1294 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1295 status &= 0x7f;
1296 }
1297 retval = put_user((short)why, &infop->si_code);
1298 if (!retval)
1299 retval = put_user(status, &infop->si_status);
1300 }
1301 if (!retval && infop)
1302 retval = put_user(p->pid, &infop->si_pid);
1303 if (!retval && infop)
1304 retval = put_user(p->uid, &infop->si_uid);
1305 if (retval) {
1306 // TODO: is this safe?
1307 p->exit_state = EXIT_ZOMBIE;
1308 return retval;
1309 }
1310 retval = p->pid;
1311 if (p->real_parent != p->parent) {
1312 write_lock_irq(&tasklist_lock);
1313 /* Double-check with lock held. */
1314 if (p->real_parent != p->parent) {
1315 __ptrace_unlink(p);
1316 // TODO: is this safe?
1317 p->exit_state = EXIT_ZOMBIE;
1318 /*
1319 * If this is not a detached task, notify the parent.
1320 * If it's still not detached after that, don't release
1321 * it now.
1322 */
1323 if (p->exit_signal != -1) {
1324 do_notify_parent(p, p->exit_signal);
1325 if (p->exit_signal != -1)
1326 p = NULL;
1327 }
1328 }
1329 write_unlock_irq(&tasklist_lock);
1330 }
1331 if (p != NULL)
1332 release_task(p);
1333 BUG_ON(!retval);
1334 return retval;
1335}
1336
1337/*
1338 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1339 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1340 * the lock and this task is uninteresting. If we return nonzero, we have
1341 * released the lock and the system call should return.
1342 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001343static int wait_task_stopped(struct task_struct *p, int delayed_group_leader,
1344 int noreap, struct siginfo __user *infop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int __user *stat_addr, struct rusage __user *ru)
1346{
1347 int retval, exit_code;
1348
1349 if (!p->exit_code)
1350 return 0;
1351 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1352 p->signal && p->signal->group_stop_count > 0)
1353 /*
1354 * A group stop is in progress and this is the group leader.
1355 * We won't report until all threads have stopped.
1356 */
1357 return 0;
1358
1359 /*
1360 * Now we are pretty sure this task is interesting.
1361 * Make sure it doesn't get reaped out from under us while we
1362 * give up the lock and then examine it below. We don't want to
1363 * keep holding onto the tasklist_lock while we call getrusage and
1364 * possibly take page faults for user memory.
1365 */
1366 get_task_struct(p);
1367 read_unlock(&tasklist_lock);
1368
1369 if (unlikely(noreap)) {
1370 pid_t pid = p->pid;
1371 uid_t uid = p->uid;
1372 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1373
1374 exit_code = p->exit_code;
1375 if (unlikely(!exit_code) ||
Linus Torvalds14bf01b2005-10-01 11:04:18 -07001376 unlikely(p->state & TASK_TRACED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto bail_ref;
1378 return wait_noreap_copyout(p, pid, uid,
1379 why, (exit_code << 8) | 0x7f,
1380 infop, ru);
1381 }
1382
1383 write_lock_irq(&tasklist_lock);
1384
1385 /*
1386 * This uses xchg to be atomic with the thread resuming and setting
1387 * it. It must also be done with the write lock held to prevent a
1388 * race with the EXIT_ZOMBIE case.
1389 */
1390 exit_code = xchg(&p->exit_code, 0);
1391 if (unlikely(p->exit_state)) {
1392 /*
1393 * The task resumed and then died. Let the next iteration
1394 * catch it in EXIT_ZOMBIE. Note that exit_code might
1395 * already be zero here if it resumed and did _exit(0).
1396 * The task itself is dead and won't touch exit_code again;
1397 * other processors in this function are locked out.
1398 */
1399 p->exit_code = exit_code;
1400 exit_code = 0;
1401 }
1402 if (unlikely(exit_code == 0)) {
1403 /*
1404 * Another thread in this function got to it first, or it
1405 * resumed, or it resumed and then died.
1406 */
1407 write_unlock_irq(&tasklist_lock);
1408bail_ref:
1409 put_task_struct(p);
1410 /*
1411 * We are returning to the wait loop without having successfully
1412 * removed the process and having released the lock. We cannot
1413 * continue, since the "p" task pointer is potentially stale.
1414 *
1415 * Return -EAGAIN, and do_wait() will restart the loop from the
1416 * beginning. Do _not_ re-acquire the lock.
1417 */
1418 return -EAGAIN;
1419 }
1420
1421 /* move to end of parent's list to avoid starvation */
1422 remove_parent(p);
Oleg Nesterov8fafabd2006-03-28 16:11:05 -08001423 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 write_unlock_irq(&tasklist_lock);
1426
1427 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1428 if (!retval && stat_addr)
1429 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1430 if (!retval && infop)
1431 retval = put_user(SIGCHLD, &infop->si_signo);
1432 if (!retval && infop)
1433 retval = put_user(0, &infop->si_errno);
1434 if (!retval && infop)
1435 retval = put_user((short)((p->ptrace & PT_PTRACED)
1436 ? CLD_TRAPPED : CLD_STOPPED),
1437 &infop->si_code);
1438 if (!retval && infop)
1439 retval = put_user(exit_code, &infop->si_status);
1440 if (!retval && infop)
1441 retval = put_user(p->pid, &infop->si_pid);
1442 if (!retval && infop)
1443 retval = put_user(p->uid, &infop->si_uid);
1444 if (!retval)
1445 retval = p->pid;
1446 put_task_struct(p);
1447
1448 BUG_ON(!retval);
1449 return retval;
1450}
1451
1452/*
1453 * Handle do_wait work for one task in a live, non-stopped state.
1454 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1455 * the lock and this task is uninteresting. If we return nonzero, we have
1456 * released the lock and the system call should return.
1457 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001458static int wait_task_continued(struct task_struct *p, int noreap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct siginfo __user *infop,
1460 int __user *stat_addr, struct rusage __user *ru)
1461{
1462 int retval;
1463 pid_t pid;
1464 uid_t uid;
1465
1466 if (unlikely(!p->signal))
1467 return 0;
1468
1469 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1470 return 0;
1471
1472 spin_lock_irq(&p->sighand->siglock);
1473 /* Re-check with the lock held. */
1474 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1475 spin_unlock_irq(&p->sighand->siglock);
1476 return 0;
1477 }
1478 if (!noreap)
1479 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1480 spin_unlock_irq(&p->sighand->siglock);
1481
1482 pid = p->pid;
1483 uid = p->uid;
1484 get_task_struct(p);
1485 read_unlock(&tasklist_lock);
1486
1487 if (!infop) {
1488 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1489 put_task_struct(p);
1490 if (!retval && stat_addr)
1491 retval = put_user(0xffff, stat_addr);
1492 if (!retval)
1493 retval = p->pid;
1494 } else {
1495 retval = wait_noreap_copyout(p, pid, uid,
1496 CLD_CONTINUED, SIGCONT,
1497 infop, ru);
1498 BUG_ON(retval == 0);
1499 }
1500
1501 return retval;
1502}
1503
1504
1505static inline int my_ptrace_child(struct task_struct *p)
1506{
1507 if (!(p->ptrace & PT_PTRACED))
1508 return 0;
1509 if (!(p->ptrace & PT_ATTACHED))
1510 return 1;
1511 /*
1512 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1513 * we are the attacher. If we are the real parent, this is a race
1514 * inside ptrace_attach. It is waiting for the tasklist_lock,
1515 * which we have to switch the parent links, but has already set
1516 * the flags in p->ptrace.
1517 */
1518 return (p->parent != p->real_parent);
1519}
1520
1521static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1522 int __user *stat_addr, struct rusage __user *ru)
1523{
1524 DECLARE_WAITQUEUE(wait, current);
1525 struct task_struct *tsk;
1526 int flag, retval;
Roland McGrath73243282007-05-06 14:50:20 -07001527 int allowed, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 add_wait_queue(&current->signal->wait_chldexit,&wait);
1530repeat:
1531 /*
1532 * We will set this flag if we see any child that might later
1533 * match our criteria, even if we are not able to reap it yet.
1534 */
1535 flag = 0;
Roland McGrath73243282007-05-06 14:50:20 -07001536 allowed = denied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 current->state = TASK_INTERRUPTIBLE;
1538 read_lock(&tasklist_lock);
1539 tsk = current;
1540 do {
1541 struct task_struct *p;
1542 struct list_head *_p;
1543 int ret;
1544
1545 list_for_each(_p,&tsk->children) {
Ingo Molnar36c8b582006-07-03 00:25:41 -07001546 p = list_entry(_p, struct task_struct, sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 ret = eligible_child(pid, options, p);
1549 if (!ret)
1550 continue;
1551
Roland McGrath73243282007-05-06 14:50:20 -07001552 if (unlikely(ret < 0)) {
1553 denied = ret;
1554 continue;
1555 }
1556 allowed = 1;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 switch (p->state) {
1559 case TASK_TRACED:
Roland McGrath7f2a5252005-10-30 15:02:50 -08001560 /*
1561 * When we hit the race with PTRACE_ATTACH,
1562 * we will not report this child. But the
1563 * race means it has not yet been moved to
1564 * our ptrace_children list, so we need to
1565 * set the flag here to avoid a spurious ECHILD
1566 * when the race happens with the only child.
1567 */
1568 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (!my_ptrace_child(p))
1570 continue;
1571 /*FALLTHROUGH*/
1572 case TASK_STOPPED:
1573 /*
1574 * It's stopped now, so it might later
1575 * continue, exit, or stop again.
1576 */
1577 flag = 1;
1578 if (!(options & WUNTRACED) &&
1579 !my_ptrace_child(p))
1580 continue;
1581 retval = wait_task_stopped(p, ret == 2,
1582 (options & WNOWAIT),
1583 infop,
1584 stat_addr, ru);
1585 if (retval == -EAGAIN)
1586 goto repeat;
1587 if (retval != 0) /* He released the lock. */
1588 goto end;
1589 break;
1590 default:
1591 // case EXIT_DEAD:
1592 if (p->exit_state == EXIT_DEAD)
1593 continue;
1594 // case EXIT_ZOMBIE:
1595 if (p->exit_state == EXIT_ZOMBIE) {
1596 /*
1597 * Eligible but we cannot release
1598 * it yet:
1599 */
1600 if (ret == 2)
1601 goto check_continued;
1602 if (!likely(options & WEXITED))
1603 continue;
1604 retval = wait_task_zombie(
1605 p, (options & WNOWAIT),
1606 infop, stat_addr, ru);
1607 /* He released the lock. */
1608 if (retval != 0)
1609 goto end;
1610 break;
1611 }
1612check_continued:
1613 /*
1614 * It's running now, so it might later
1615 * exit, stop, or stop and then continue.
1616 */
1617 flag = 1;
1618 if (!unlikely(options & WCONTINUED))
1619 continue;
1620 retval = wait_task_continued(
1621 p, (options & WNOWAIT),
1622 infop, stat_addr, ru);
1623 if (retval != 0) /* He released the lock. */
1624 goto end;
1625 break;
1626 }
1627 }
1628 if (!flag) {
1629 list_for_each(_p, &tsk->ptrace_children) {
1630 p = list_entry(_p, struct task_struct,
1631 ptrace_list);
1632 if (!eligible_child(pid, options, p))
1633 continue;
1634 flag = 1;
1635 break;
1636 }
1637 }
1638 if (options & __WNOTHREAD)
1639 break;
1640 tsk = next_thread(tsk);
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001641 BUG_ON(tsk->signal != current->signal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 } while (tsk != current);
1643
1644 read_unlock(&tasklist_lock);
1645 if (flag) {
1646 retval = 0;
1647 if (options & WNOHANG)
1648 goto end;
1649 retval = -ERESTARTSYS;
1650 if (signal_pending(current))
1651 goto end;
1652 schedule();
1653 goto repeat;
1654 }
1655 retval = -ECHILD;
Roland McGrath73243282007-05-06 14:50:20 -07001656 if (unlikely(denied) && !allowed)
1657 retval = denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658end:
1659 current->state = TASK_RUNNING;
1660 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1661 if (infop) {
1662 if (retval > 0)
1663 retval = 0;
1664 else {
1665 /*
1666 * For a WNOHANG return, clear out all the fields
1667 * we would set so the user can easily tell the
1668 * difference.
1669 */
1670 if (!retval)
1671 retval = put_user(0, &infop->si_signo);
1672 if (!retval)
1673 retval = put_user(0, &infop->si_errno);
1674 if (!retval)
1675 retval = put_user(0, &infop->si_code);
1676 if (!retval)
1677 retval = put_user(0, &infop->si_pid);
1678 if (!retval)
1679 retval = put_user(0, &infop->si_uid);
1680 if (!retval)
1681 retval = put_user(0, &infop->si_status);
1682 }
1683 }
1684 return retval;
1685}
1686
1687asmlinkage long sys_waitid(int which, pid_t pid,
1688 struct siginfo __user *infop, int options,
1689 struct rusage __user *ru)
1690{
1691 long ret;
1692
1693 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1694 return -EINVAL;
1695 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1696 return -EINVAL;
1697
1698 switch (which) {
1699 case P_ALL:
1700 pid = -1;
1701 break;
1702 case P_PID:
1703 if (pid <= 0)
1704 return -EINVAL;
1705 break;
1706 case P_PGID:
1707 if (pid <= 0)
1708 return -EINVAL;
1709 pid = -pid;
1710 break;
1711 default:
1712 return -EINVAL;
1713 }
1714
1715 ret = do_wait(pid, options, infop, NULL, ru);
1716
1717 /* avoid REGPARM breakage on x86: */
1718 prevent_tail_call(ret);
1719 return ret;
1720}
1721
1722asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1723 int options, struct rusage __user *ru)
1724{
1725 long ret;
1726
1727 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1728 __WNOTHREAD|__WCLONE|__WALL))
1729 return -EINVAL;
1730 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1731
1732 /* avoid REGPARM breakage on x86: */
1733 prevent_tail_call(ret);
1734 return ret;
1735}
1736
1737#ifdef __ARCH_WANT_SYS_WAITPID
1738
1739/*
1740 * sys_waitpid() remains for compatibility. waitpid() should be
1741 * implemented by calling sys_wait4() from libc.a.
1742 */
1743asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1744{
1745 return sys_wait4(pid, stat_addr, options, NULL);
1746}
1747
1748#endif