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