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