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