[PATCH] splice: add optional input and output offsets
[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
8c7904a0
EB
130static void delayed_put_task_struct(struct rcu_head *rhp)
131{
132 put_task_struct(container_of(rhp, struct task_struct, rcu));
133}
134
1da177e4
LT
135void release_task(struct task_struct * p)
136{
137 int zap_leader;
138 task_t *leader;
139 struct dentry *proc_dentry;
140
1f09f974 141repeat:
1da177e4
LT
142 atomic_dec(&p->user->processes);
143 spin_lock(&p->proc_lock);
144 proc_dentry = proc_pid_unhash(p);
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);
173 spin_unlock(&p->proc_lock);
174 proc_pid_flush(proc_dentry);
175 release_thread(p);
8c7904a0 176 call_rcu(&p->rcu, delayed_put_task_struct);
1da177e4
LT
177
178 p = leader;
179 if (unlikely(zap_leader))
180 goto repeat;
181}
182
1da177e4
LT
183/*
184 * This checks not only the pgrp, but falls back on the pid if no
185 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
186 * without this...
187 */
188int session_of_pgrp(int pgrp)
189{
190 struct task_struct *p;
191 int sid = -1;
192
193 read_lock(&tasklist_lock);
194 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
195 if (p->signal->session > 0) {
196 sid = p->signal->session;
197 goto out;
198 }
199 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
200 p = find_task_by_pid(pgrp);
201 if (p)
202 sid = p->signal->session;
203out:
204 read_unlock(&tasklist_lock);
205
206 return sid;
207}
208
209/*
210 * Determine if a process group is "orphaned", according to the POSIX
211 * definition in 2.2.2.52. Orphaned process groups are not to be affected
212 * by terminal-generated stop signals. Newly orphaned process groups are
213 * to receive a SIGHUP and a SIGCONT.
214 *
215 * "I ask you, have you ever known what it is to be an orphan?"
216 */
217static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
218{
219 struct task_struct *p;
220 int ret = 1;
221
222 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
223 if (p == ignored_task
224 || p->exit_state
225 || p->real_parent->pid == 1)
226 continue;
227 if (process_group(p->real_parent) != pgrp
228 && p->real_parent->signal->session == p->signal->session) {
229 ret = 0;
230 break;
231 }
232 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
233 return ret; /* (sighing) "Often!" */
234}
235
236int is_orphaned_pgrp(int pgrp)
237{
238 int retval;
239
240 read_lock(&tasklist_lock);
241 retval = will_become_orphaned_pgrp(pgrp, NULL);
242 read_unlock(&tasklist_lock);
243
244 return retval;
245}
246
858119e1 247static int has_stopped_jobs(int pgrp)
1da177e4
LT
248{
249 int retval = 0;
250 struct task_struct *p;
251
252 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
253 if (p->state != TASK_STOPPED)
254 continue;
255
256 /* If p is stopped by a debugger on a signal that won't
257 stop it, then don't count p as stopped. This isn't
258 perfect but it's a good approximation. */
259 if (unlikely (p->ptrace)
260 && p->exit_code != SIGSTOP
261 && p->exit_code != SIGTSTP
262 && p->exit_code != SIGTTOU
263 && p->exit_code != SIGTTIN)
264 continue;
265
266 retval = 1;
267 break;
268 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
269 return retval;
270}
271
272/**
4dc3b16b 273 * reparent_to_init - Reparent the calling kernel thread to the init task.
1da177e4
LT
274 *
275 * If a kernel thread is launched as a result of a system call, or if
276 * it ever exits, it should generally reparent itself to init so that
277 * it is correctly cleaned up on exit.
278 *
279 * The various task state such as scheduling policy and priority may have
280 * been inherited from a user process, so we reset them to sane values here.
281 *
282 * NOTE that reparent_to_init() gives the caller full capabilities.
283 */
858119e1 284static void reparent_to_init(void)
1da177e4
LT
285{
286 write_lock_irq(&tasklist_lock);
287
288 ptrace_unlink(current);
289 /* Reparent to init */
9b678ece 290 remove_parent(current);
1da177e4
LT
291 current->parent = child_reaper;
292 current->real_parent = child_reaper;
9b678ece 293 add_parent(current);
1da177e4
LT
294
295 /* Set the exit signal to SIGCHLD so we signal init on exit */
296 current->exit_signal = SIGCHLD;
297
b0a9499c
IM
298 if ((current->policy == SCHED_NORMAL ||
299 current->policy == SCHED_BATCH)
300 && (task_nice(current) < 0))
1da177e4
LT
301 set_user_nice(current, 0);
302 /* cpus_allowed? */
303 /* rt_priority? */
304 /* signals? */
305 security_task_reparent_to_init(current);
306 memcpy(current->signal->rlim, init_task.signal->rlim,
307 sizeof(current->signal->rlim));
308 atomic_inc(&(INIT_USER->__count));
309 write_unlock_irq(&tasklist_lock);
310 switch_uid(INIT_USER);
311}
312
313void __set_special_pids(pid_t session, pid_t pgrp)
314{
e19f247a 315 struct task_struct *curr = current->group_leader;
1da177e4
LT
316
317 if (curr->signal->session != session) {
318 detach_pid(curr, PIDTYPE_SID);
319 curr->signal->session = session;
320 attach_pid(curr, PIDTYPE_SID, session);
321 }
322 if (process_group(curr) != pgrp) {
323 detach_pid(curr, PIDTYPE_PGID);
324 curr->signal->pgrp = pgrp;
325 attach_pid(curr, PIDTYPE_PGID, pgrp);
326 }
327}
328
329void set_special_pids(pid_t session, pid_t pgrp)
330{
331 write_lock_irq(&tasklist_lock);
332 __set_special_pids(session, pgrp);
333 write_unlock_irq(&tasklist_lock);
334}
335
336/*
337 * Let kernel threads use this to say that they
338 * allow a certain signal (since daemonize() will
339 * have disabled all of them by default).
340 */
341int allow_signal(int sig)
342{
7ed20e1a 343 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
344 return -EINVAL;
345
346 spin_lock_irq(&current->sighand->siglock);
347 sigdelset(&current->blocked, sig);
348 if (!current->mm) {
349 /* Kernel threads handle their own signals.
350 Let the signal code know it'll be handled, so
351 that they don't get converted to SIGKILL or
352 just silently dropped */
353 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
354 }
355 recalc_sigpending();
356 spin_unlock_irq(&current->sighand->siglock);
357 return 0;
358}
359
360EXPORT_SYMBOL(allow_signal);
361
362int disallow_signal(int sig)
363{
7ed20e1a 364 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
365 return -EINVAL;
366
367 spin_lock_irq(&current->sighand->siglock);
368 sigaddset(&current->blocked, sig);
369 recalc_sigpending();
370 spin_unlock_irq(&current->sighand->siglock);
371 return 0;
372}
373
374EXPORT_SYMBOL(disallow_signal);
375
376/*
377 * Put all the gunge required to become a kernel thread without
378 * attached user resources in one place where it belongs.
379 */
380
381void daemonize(const char *name, ...)
382{
383 va_list args;
384 struct fs_struct *fs;
385 sigset_t blocked;
386
387 va_start(args, name);
388 vsnprintf(current->comm, sizeof(current->comm), name, args);
389 va_end(args);
390
391 /*
392 * If we were started as result of loading a module, close all of the
393 * user space pages. We don't need them, and if we didn't close them
394 * they would be locked into memory.
395 */
396 exit_mm(current);
397
398 set_special_pids(1, 1);
70522e12 399 mutex_lock(&tty_mutex);
1da177e4 400 current->signal->tty = NULL;
70522e12 401 mutex_unlock(&tty_mutex);
1da177e4
LT
402
403 /* Block and flush all signals */
404 sigfillset(&blocked);
405 sigprocmask(SIG_BLOCK, &blocked, NULL);
406 flush_signals(current);
407
408 /* Become as one with the init task */
409
410 exit_fs(current); /* current->fs->count--; */
411 fs = init_task.fs;
412 current->fs = fs;
413 atomic_inc(&fs->count);