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