[PATCH] cleanup in proc_check_chroot()
[linux-2.6-block.git] / kernel / exit.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/exit.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/config.h>
8#include <linux/mm.h>
9#include <linux/slab.h>
10#include <linux/interrupt.h>
11#include <linux/smp_lock.h>
12#include <linux/module.h>
c59ede7b 13#include <linux/capability.h>
1da177e4
LT
14#include <linux/completion.h>
15#include <linux/personality.h>
16#include <linux/tty.h>
17#include <linux/namespace.h>
18#include <linux/key.h>
19#include <linux/security.h>
20#include <linux/cpu.h>
21#include <linux/acct.h>
22#include <linux/file.h>
23#include <linux/binfmts.h>
24#include <linux/ptrace.h>
25#include <linux/profile.h>
26#include <linux/mount.h>
27#include <linux/proc_fs.h>
28#include <linux/mempolicy.h>
29#include <linux/cpuset.h>
30#include <linux/syscalls.h>
7ed20e1a 31#include <linux/signal.h>
6a14c5c9 32#include <linux/posix-timers.h>
9f46080c 33#include <linux/cn_proc.h>
de5097c2 34#include <linux/mutex.h>
0771dfef 35#include <linux/futex.h>
34f192c6 36#include <linux/compat.h>
1da177e4
LT
37
38#include <asm/uaccess.h>
39#include <asm/unistd.h>
40#include <asm/pgtable.h>
41#include <asm/mmu_context.h>
42
43extern void sem_exit (void);
44extern struct task_struct *child_reaper;
45
46int getrusage(struct task_struct *, int, struct rusage __user *);
47
408b664a
AB
48static void exit_mm(struct task_struct * tsk);
49
1da177e4
LT
50static void __unhash_process(struct task_struct *p)
51{
52 nr_threads--;
53 detach_pid(p, PIDTYPE_PID);
1da177e4
LT
54 if (thread_group_leader(p)) {
55 detach_pid(p, PIDTYPE_PGID);
56 detach_pid(p, PIDTYPE_SID);
c97d9893
ON
57
58 list_del_init(&p->tasks);
73b9ebfe 59 __get_cpu_var(process_counts)--;
1da177e4 60 }
47e65328 61 list_del_rcu(&p->thread_group);
c97d9893 62 remove_parent(p);
1da177e4
LT
63}
64
6a14c5c9
ON
65/*
66 * This function expects the tasklist_lock write-locked.
67 */
68static void __exit_signal(struct task_struct *tsk)
69{
70 struct signal_struct *sig = tsk->signal;
71 struct sighand_struct *sighand;
72
73 BUG_ON(!sig);
74 BUG_ON(!atomic_read(&sig->count));
75
76 rcu_read_lock();
77 sighand = rcu_dereference(tsk->sighand);
78 spin_lock(&sighand->siglock);
79
80 posix_cpu_timers_exit(tsk);
81 if (atomic_dec_and_test(&sig->count))
82 posix_cpu_timers_exit_group(tsk);
83 else {
84 /*
85 * If there is any task waiting for the group exit
86 * then notify it:
87 */
88 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
89 wake_up_process(sig->group_exit_task);
90 sig->group_exit_task = NULL;
91 }
92 if (tsk == sig->curr_target)
93 sig->curr_target = next_thread(tsk);
94 /*
95 * Accumulate here the counters for all threads but the
96 * group leader as they die, so they can be added into
97 * the process-wide totals when those are taken.
98 * The group leader stays around as a zombie as long
99 * as there are other threads. When it gets reaped,
100 * the exit.c code will add its counts into these totals.
101 * We won't ever get here for the group leader, since it
102 * will have been the last reference on the signal_struct.
103 */
104 sig->utime = cputime_add(sig->utime, tsk->utime);
105 sig->stime = cputime_add(sig->stime, tsk->stime);
106 sig->min_flt += tsk->min_flt;
107 sig->maj_flt += tsk->maj_flt;
108 sig->nvcsw += tsk->nvcsw;
109 sig->nivcsw += tsk->nivcsw;
110 sig->sched_time += tsk->sched_time;
111 sig = NULL; /* Marker for below. */
112 }
113
5876700c
ON
114 __unhash_process(tsk);
115
6a14c5c9 116 tsk->signal = NULL;
a7e5328a 117 tsk->sighand = NULL;
6a14c5c9
ON
118 spin_unlock(&sighand->siglock);
119 rcu_read_unlock();
120
a7e5328a 121 __cleanup_sighand(sighand);
6a14c5c9
ON
122 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
123 flush_sigqueue(&tsk->pending);
124 if (sig) {
125 flush_sigqueue(&sig->shared_pending);
126 __cleanup_signal(sig);
127 }
128}
129
1da177e4
LT
130void release_task(struct task_struct * p)
131{
132 int zap_leader;
133 task_t *leader;
134 struct dentry *proc_dentry;
135
1f09f974 136repeat:
1da177e4
LT
137 atomic_dec(&p->user->processes);
138 spin_lock(&p->proc_lock);
139 proc_dentry = proc_pid_unhash(p);
140 write_lock_irq(&tasklist_lock);
1f09f974 141 ptrace_unlink(p);
1da177e4
LT
142 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
143 __exit_signal(p);
35f5cad8 144
1da177e4
LT
145 /*
146 * If we are the last non-leader member of the thread
147 * group, and the leader is zombie, then notify the
148 * group leader's parent process. (if it wants notification.)
149 */
150 zap_leader = 0;
151 leader = p->group_leader;
152 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
153 BUG_ON(leader->exit_signal == -1);
154 do_notify_parent(leader, leader->exit_signal);
155 /*
156 * If we were the last child thread and the leader has
157 * exited already, and the leader's parent ignores SIGCHLD,
158 * then we are the one who should release the leader.
159 *
160 * do_notify_parent() will have marked it self-reaping in
161 * that case.
162 */
163 zap_leader = (leader->exit_signal == -1);
164 }
165
166 sched_exit(p);
167 write_unlock_irq(&tasklist_lock);
168 spin_unlock(&p->proc_lock);
169 proc_pid_flush(proc_dentry);
170 release_thread(p);
171 put_task_struct(p);
172
173 p = leader;
174 if (unlikely(zap_leader))
175 goto repeat;
176}
177
1da177e4
LT
178/*
179 * This checks not only the pgrp, but falls back on the pid if no
180 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
181 * without this...
182 */
183int session_of_pgrp(int pgrp)
184{
185 struct task_struct *p;
186 int sid = -1;
187
188 read_lock(&tasklist_lock);
189 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
190 if (p->signal->session > 0) {
191 sid = p->signal->session;
192 goto out;
193 }
194 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
195 p = find_task_by_pid(pgrp);
196 if (p)
197 sid = p->signal->session;
198out:
199 read_unlock(&tasklist_lock);
200
201 return sid;
202}
203
204/*
205 * Determine if a process group is "orphaned", according to the POSIX
206 * definition in 2.2.2.52. Orphaned process groups are not to be affected
207 * by terminal-generated stop signals. Newly orphaned process groups are
208 * to receive a SIGHUP and a SIGCONT.
209 *
210 * "I ask you, have you ever known what it is to be an orphan?"
211 */
212static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
213{
214 struct task_struct *p;
215 int ret = 1;
216
217 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
218 if (p == ignored_task
219 || p->exit_state
220 || p->real_parent->pid == 1)
221 continue;
222 if (process_group(p->real_parent) != pgrp
223 && p->real_parent->signal->session == p->signal->session) {
224 ret = 0;
225 break;
226 }
227 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
228 return ret; /* (sighing) "Often!" */
229}
230
231int is_orphaned_pgrp(int pgrp)
232{
233 int retval;
234
235 read_lock(&tasklist_lock);
236 retval = will_become_orphaned_pgrp(pgrp, NULL);
237 read_unlock(&tasklist_lock);
238
239 return retval;
240}
241
858119e1 242static int has_stopped_jobs(int pgrp)
1da177e4
LT
243{
244 int retval = 0;
245 struct task_struct *p;
246
247 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
248 if (p->state != TASK_STOPPED)
249 continue;
250
251 /* If p is stopped by a debugger on a signal that won't
252 stop it, then don't count p as stopped. This isn't
253 perfect but it's a good approximation. */
254 if (unlikely (p->ptrace)
255 && p->exit_code != SIGSTOP
256 && p->exit_code != SIGTSTP
257 && p->exit_code != SIGTTOU
258 && p->exit_code != SIGTTIN)
259 continue;
260
261 retval = 1;
262 break;
263 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
264 return retval;
265}
266
267/**
4dc3b16b 268 * reparent_to_init - Reparent the calling kernel thread to the init task.
1da177e4
LT
269 *
270 * If a kernel thread is launched as a result of a system call, or if
271 * it ever exits, it should generally reparent itself to init so that
272 * it is correctly cleaned up on exit.
273 *
274 * The various task state such as scheduling policy and priority may have
275 * been inherited from a user process, so we reset them to sane values here.
276 *
277 * NOTE that reparent_to_init() gives the caller full capabilities.
278 */
858119e1 279static void reparent_to_init(void)
1da177e4
LT
280{
281 write_lock_irq(&tasklist_lock);
282
283 ptrace_unlink(current);
284 /* Reparent to init */
9b678ece 285 remove_parent(current);
1da177e4
LT
286 current->parent = child_reaper;
287 current->real_parent = child_reaper;
9b678ece 288 add_parent(current);
1da177e4
LT
289
290 /* Set the exit signal to SIGCHLD so we signal init on exit */
291 current->exit_signal = SIGCHLD;
292
b0a9499c
IM
293 if ((current->policy == SCHED_NORMAL ||
294 current->policy == SCHED_BATCH)
295 && (task_nice(current) < 0))
1da177e4
LT
296 set_user_nice(current, 0);
297 /* cpus_allowed? */
298 /* rt_priority? */
299 /* signals? */
300 security_task_reparent_to_init(current);
301 memcpy(current->signal->rlim, init_task.signal->rlim,
302 sizeof(current->signal->rlim));
303 atomic_inc(&(INIT_USER->__count));
304 write_unlock_irq(&tasklist_lock);
305 switch_uid(INIT_USER);
306}
307
308void __set_special_pids(pid_t session, pid_t pgrp)
309{
e19f247a 310 struct task_struct *curr = current->group_leader;
1da177e4
LT
311
312 if (curr->signal->session != session) {
313 detach_pid(curr, PIDTYPE_SID);
314 curr->signal->session = session;
315 attach_pid(curr, PIDTYPE_SID, session);
316 }
317 if (process_group(curr) != pgrp) {
318 detach_pid(curr, PIDTYPE_PGID);
319 curr->signal->pgrp = pgrp;
320 attach_pid(curr, PIDTYPE_PGID, pgrp);
321 }
322}
323
324void set_special_pids(pid_t session, pid_t pgrp)
325{
326 write_lock_irq(&tasklist_lock);
327 __set_special_pids(session, pgrp);
328 write_unlock_irq(&tasklist_lock);
329}
330
331/*
332 * Let kernel threads use this to say that they
333 * allow a certain signal (since daemonize() will
334 * have disabled all of them by default).
335 */
336int allow_signal(int sig)
337{
7ed20e1a 338 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
339 return -EINVAL;
340
341 spin_lock_irq(&current->sighand->siglock);
342 sigdelset(&current->blocked, sig);
343 if (!current->mm) {
344 /* Kernel threads handle their own signals.
345 Let the signal code know it'll be handled, so
346 that they don't get converted to SIGKILL or
347 just silently dropped */
348 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
349 }
350 recalc_sigpending();
351 spin_unlock_irq(&current->sighand->siglock);
352 return 0;
353}
354
355EXPORT_SYMBOL(allow_signal);
356
357int disallow_signal(int sig)
358{
7ed20e1a 359 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
360 return -EINVAL;
361
362 spin_lock_irq(&current->sighand->siglock);
363 sigaddset(&current->blocked, sig);
364 recalc_sigpending();
365 spin_unlock_irq(&current->sighand->siglock);
366 return 0;
367}
368
369EXPORT_SYMBOL(disallow_signal);
370
371/*
372 * Put all the gunge required to become a kernel thread without
373 * attached user resources in one place where it belongs.
374 */
375
376void daemonize(const char *name, ...)
377{
378 va_list args;
379 struct fs_struct *fs;
380 sigset_t blocked;
381
382 va_start(args, name);
383 vsnprintf(current->comm, sizeof(current->comm), name, args);
384 va_end(args);
385
386 /*
387 * If we were started as result of loading a module, close all of the
388 * user space pages. We don't need them, and if we didn't close them
389 * they would be locked into memory.
390 */
391 exit_mm(current);
392
393 set_special_pids(1, 1);
70522e12 394 mutex_lock(&tty_mutex);
1da177e4 395 current->signal->tty = NULL;
70522e12 396 mutex_unlock(&tty_mutex);
1da177e4
LT
397
398 /* Block and flush all signals */
399 sigfillset(&blocked);
400 sigprocmask(SIG_BLOCK, &blocked, NULL);
401 flush_signals(current);
402
403 /* Become as one with the init task */
404
405 exit_fs(current); /* current->fs->count--; */
406 fs = init_task.fs;
407 current->fs = fs;
408 atomic_inc(&fs->count);