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