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