[PATCH] wait_for_helper: trivial style cleanup
[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>
9f46080c 32#include <linux/cn_proc.h>
de5097c2 33#include <linux/mutex.h>
0771dfef 34#include <linux/futex.h>
34f192c6 35#include <linux/compat.h>
1da177e4
LT
36
37#include <asm/uaccess.h>
38#include <asm/unistd.h>
39#include <asm/pgtable.h>
40#include <asm/mmu_context.h>
41
42extern void sem_exit (void);
43extern struct task_struct *child_reaper;
44
45int getrusage(struct task_struct *, int, struct rusage __user *);
46
408b664a
AB
47static void exit_mm(struct task_struct * tsk);
48
1da177e4
LT
49static void __unhash_process(struct task_struct *p)
50{
51 nr_threads--;
52 detach_pid(p, PIDTYPE_PID);
53 detach_pid(p, PIDTYPE_TGID);
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
LT
60 }
61
c97d9893 62 remove_parent(p);
1da177e4
LT
63}
64
65void release_task(struct task_struct * p)
66{
67 int zap_leader;
68 task_t *leader;
69 struct dentry *proc_dentry;
70
71repeat:
72 atomic_dec(&p->user->processes);
73 spin_lock(&p->proc_lock);
74 proc_dentry = proc_pid_unhash(p);
75 write_lock_irq(&tasklist_lock);
76 if (unlikely(p->ptrace))
77 __ptrace_unlink(p);
78 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
79 __exit_signal(p);
71a2224d
CL
80 /*
81 * Note that the fastpath in sys_times depends on __exit_signal having
82 * updated the counters before a task is removed from the tasklist of
83 * the process by __unhash_process.
84 */
1da177e4
LT
85 __unhash_process(p);
86
87 /*
88 * If we are the last non-leader member of the thread
89 * group, and the leader is zombie, then notify the
90 * group leader's parent process. (if it wants notification.)
91 */
92 zap_leader = 0;
93 leader = p->group_leader;
94 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
95 BUG_ON(leader->exit_signal == -1);
96 do_notify_parent(leader, leader->exit_signal);
97 /*
98 * If we were the last child thread and the leader has
99 * exited already, and the leader's parent ignores SIGCHLD,
100 * then we are the one who should release the leader.
101 *
102 * do_notify_parent() will have marked it self-reaping in
103 * that case.
104 */
105 zap_leader = (leader->exit_signal == -1);
106 }
107
108 sched_exit(p);
109 write_unlock_irq(&tasklist_lock);
110 spin_unlock(&p->proc_lock);
111 proc_pid_flush(proc_dentry);
112 release_thread(p);
113 put_task_struct(p);
114
115 p = leader;
116 if (unlikely(zap_leader))
117 goto repeat;
118}
119
1da177e4
LT
120/*
121 * This checks not only the pgrp, but falls back on the pid if no
122 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
123 * without this...
124 */
125int session_of_pgrp(int pgrp)
126{
127 struct task_struct *p;
128 int sid = -1;
129
130 read_lock(&tasklist_lock);
131 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
132 if (p->signal->session > 0) {
133 sid = p->signal->session;
134 goto out;
135 }
136 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
137 p = find_task_by_pid(pgrp);
138 if (p)
139 sid = p->signal->session;
140out:
141 read_unlock(&tasklist_lock);
142
143 return sid;
144}
145
146/*
147 * Determine if a process group is "orphaned", according to the POSIX
148 * definition in 2.2.2.52. Orphaned process groups are not to be affected
149 * by terminal-generated stop signals. Newly orphaned process groups are
150 * to receive a SIGHUP and a SIGCONT.
151 *
152 * "I ask you, have you ever known what it is to be an orphan?"
153 */
154static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
155{
156 struct task_struct *p;
157 int ret = 1;
158
159 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
160 if (p == ignored_task
161 || p->exit_state
162 || p->real_parent->pid == 1)
163 continue;
164 if (process_group(p->real_parent) != pgrp
165 && p->real_parent->signal->session == p->signal->session) {
166 ret = 0;
167 break;
168 }
169 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
170 return ret; /* (sighing) "Often!" */
171}
172
173int is_orphaned_pgrp(int pgrp)
174{
175 int retval;
176
177 read_lock(&tasklist_lock);
178 retval = will_become_orphaned_pgrp(pgrp, NULL);
179 read_unlock(&tasklist_lock);
180
181 return retval;
182}
183
858119e1 184static int has_stopped_jobs(int pgrp)
1da177e4
LT
185{
186 int retval = 0;
187 struct task_struct *p;
188
189 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
190 if (p->state != TASK_STOPPED)
191 continue;
192
193 /* If p is stopped by a debugger on a signal that won't
194 stop it, then don't count p as stopped. This isn't
195 perfect but it's a good approximation. */
196 if (unlikely (p->ptrace)
197 && p->exit_code != SIGSTOP
198 && p->exit_code != SIGTSTP
199 && p->exit_code != SIGTTOU
200 && p->exit_code != SIGTTIN)
201 continue;
202
203 retval = 1;
204 break;
205 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
206 return retval;
207}
208
209/**
4dc3b16b 210 * reparent_to_init - Reparent the calling kernel thread to the init task.
1da177e4
LT
211 *
212 * If a kernel thread is launched as a result of a system call, or if
213 * it ever exits, it should generally reparent itself to init so that
214 * it is correctly cleaned up on exit.
215 *
216 * The various task state such as scheduling policy and priority may have
217 * been inherited from a user process, so we reset them to sane values here.
218 *
219 * NOTE that reparent_to_init() gives the caller full capabilities.
220 */
858119e1 221static void reparent_to_init(void)
1da177e4
LT
222{
223 write_lock_irq(&tasklist_lock);
224
225 ptrace_unlink(current);
226 /* Reparent to init */
9b678ece 227 remove_parent(current);
1da177e4
LT
228 current->parent = child_reaper;
229 current->real_parent = child_reaper;
9b678ece 230 add_parent(current);
1da177e4
LT
231
232 /* Set the exit signal to SIGCHLD so we signal init on exit */
233 current->exit_signal = SIGCHLD;
234
b0a9499c
IM
235 if ((current->policy == SCHED_NORMAL ||
236 current->policy == SCHED_BATCH)
237 && (task_nice(current) < 0))
1da177e4
LT
238 set_user_nice(current, 0);
239 /* cpus_allowed? */
240 /* rt_priority? */
241 /* signals? */
242 security_task_reparent_to_init(current);
243 memcpy(current->signal->rlim, init_task.signal->rlim,
244 sizeof(current->signal->rlim));
245 atomic_inc(&(INIT_USER->__count));
246 write_unlock_irq(&tasklist_lock);
247 switch_uid(INIT_USER);
248}
249
250void __set_special_pids(pid_t session, pid_t pgrp)
251{
e19f247a 252 struct task_struct *curr = current->group_leader;
1da177e4
LT
253
254 if (curr->signal->session != session) {
255 detach_pid(curr, PIDTYPE_SID);
256 curr->signal->session = session;
257 attach_pid(curr, PIDTYPE_SID, session);
258 }
259 if (process_group(curr) != pgrp) {
260 detach_pid(curr, PIDTYPE_PGID);
261 curr->signal->pgrp = pgrp;
262 attach_pid(curr, PIDTYPE_PGID, pgrp);
263 }
264}
265
266void set_special_pids(pid_t session, pid_t pgrp)
267{
268 write_lock_irq(&tasklist_lock);
269 __set_special_pids(session, pgrp);
270 write_unlock_irq(&tasklist_lock);
271}
272
273/*
274 * Let kernel threads use this to say that they
275 * allow a certain signal (since daemonize() will
276 * have disabled all of them by default).
277 */
278int allow_signal(int sig)
279{
7ed20e1a 280 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
281 return -EINVAL;
282
283 spin_lock_irq(&current->sighand->siglock);
284 sigdelset(&current->blocked, sig);
285 if (!current->mm) {
286 /* Kernel threads handle their own signals.
287 Let the signal code know it'll be handled, so
288 that they don't get converted to SIGKILL or
289 just silently dropped */
290 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
291 }
292 recalc_sigpending();
293 spin_unlock_irq(&current->sighand->siglock);
294 return 0;
295}
296
297EXPORT_SYMBOL(allow_signal);
298
299int disallow_signal(int sig)
300{
7ed20e1a 301 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
302 return -EINVAL;
303
304 spin_lock_irq(&current->sighand->siglock);
305 sigaddset(&current->blocked, sig);
306 recalc_sigpending();
307 spin_unlock_irq(&current->sighand->siglock);
308 return 0;
309}
310
311EXPORT_SYMBOL(disallow_signal);
312
313/*
314 * Put all the gunge required to become a kernel thread without
315 * attached user resources in one place where it belongs.
316 */
317
318void daemonize(const char *name, ...)
319{
320 va_list args;
321 struct fs_struct *fs;
322 sigset_t blocked;
323
324 va_start(args, name);
325 vsnprintf(current->comm, sizeof(current->comm), name, args);
326 va_end(args);
327
328 /*
329 * If we were started as result of loading a module, close all of the
330 * user space pages. We don't need them, and if we didn't close them
331 * they would be locked into memory.
332 */
333 exit_mm(current);
334
335 set_special_pids(1, 1);
70522e12 336 mutex_lock(&tty_mutex);
1da177e4 337 current->signal->tty = NULL;
70522e12 338 mutex_unlock(&tty_mutex);
1da177e4
LT
339
340 /* Block and flush all signals */
341 sigfillset(&blocked);
342 sigprocmask(SIG_BLOCK, &blocked, NULL);
343 flush_signals(current);
344
345 /* Become as one with the init task */
346
347 exit_fs(current); /* current->fs->count--; */
348 fs = init_task.fs;
349 current->fs = fs;
350 atomic_inc(&fs->count);