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