]>
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> | |
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> |
1da177e4 LT |
31 | |
32 | #include <asm/uaccess.h> | |
33 | #include <asm/unistd.h> | |
34 | #include <asm/pgtable.h> | |
35 | #include <asm/mmu_context.h> | |
36 | ||
37 | extern void sem_exit (void); | |
38 | extern struct task_struct *child_reaper; | |
39 | ||
40 | int getrusage(struct task_struct *, int, struct rusage __user *); | |
41 | ||
408b664a AB |
42 | static void exit_mm(struct task_struct * tsk); |
43 | ||
1da177e4 LT |
44 | static void __unhash_process(struct task_struct *p) |
45 | { | |
46 | nr_threads--; | |
47 | detach_pid(p, PIDTYPE_PID); | |
48 | detach_pid(p, PIDTYPE_TGID); | |
49 | if (thread_group_leader(p)) { | |
50 | detach_pid(p, PIDTYPE_PGID); | |
51 | detach_pid(p, PIDTYPE_SID); | |
52 | if (p->pid) | |
53 | __get_cpu_var(process_counts)--; | |
54 | } | |
55 | ||
56 | REMOVE_LINKS(p); | |
57 | } | |
58 | ||
59 | void release_task(struct task_struct * p) | |
60 | { | |
61 | int zap_leader; | |
62 | task_t *leader; | |
63 | struct dentry *proc_dentry; | |
64 | ||
65 | repeat: | |
66 | atomic_dec(&p->user->processes); | |
67 | spin_lock(&p->proc_lock); | |
68 | proc_dentry = proc_pid_unhash(p); | |
69 | write_lock_irq(&tasklist_lock); | |
70 | if (unlikely(p->ptrace)) | |
71 | __ptrace_unlink(p); | |
72 | BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children)); | |
73 | __exit_signal(p); | |
74 | __exit_sighand(p); | |
71a2224d CL |
75 | /* |
76 | * Note that the fastpath in sys_times depends on __exit_signal having | |
77 | * updated the counters before a task is removed from the tasklist of | |
78 | * the process by __unhash_process. | |
79 | */ | |
1da177e4 LT |
80 | __unhash_process(p); |
81 | ||
82 | /* | |
83 | * If we are the last non-leader member of the thread | |
84 | * group, and the leader is zombie, then notify the | |
85 | * group leader's parent process. (if it wants notification.) | |
86 | */ | |
87 | zap_leader = 0; | |
88 | leader = p->group_leader; | |
89 | if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) { | |
90 | BUG_ON(leader->exit_signal == -1); | |
91 | do_notify_parent(leader, leader->exit_signal); | |
92 | /* | |
93 | * If we were the last child thread and the leader has | |
94 | * exited already, and the leader's parent ignores SIGCHLD, | |
95 | * then we are the one who should release the leader. | |
96 | * | |
97 | * do_notify_parent() will have marked it self-reaping in | |
98 | * that case. | |
99 | */ | |
100 | zap_leader = (leader->exit_signal == -1); | |
101 | } | |
102 | ||
103 | sched_exit(p); | |
104 | write_unlock_irq(&tasklist_lock); | |
105 | spin_unlock(&p->proc_lock); | |
106 | proc_pid_flush(proc_dentry); | |
107 | release_thread(p); | |
108 | put_task_struct(p); | |
109 | ||
110 | p = leader; | |
111 | if (unlikely(zap_leader)) | |
112 | goto repeat; | |
113 | } | |
114 | ||
115 | /* we are using it only for SMP init */ | |
116 | ||
117 | void unhash_process(struct task_struct *p) | |
118 | { | |
119 | struct dentry *proc_dentry; | |
120 | ||
121 | spin_lock(&p->proc_lock); | |
122 | proc_dentry = proc_pid_unhash(p); | |
123 | write_lock_irq(&tasklist_lock); | |
124 | __unhash_process(p); | |
125 | write_unlock_irq(&tasklist_lock); | |
126 | spin_unlock(&p->proc_lock); | |
127 | proc_pid_flush(proc_dentry); | |
128 | } | |
129 | ||
130 | /* | |
131 | * This checks not only the pgrp, but falls back on the pid if no | |
132 | * satisfactory pgrp is found. I dunno - gdb doesn't work correctly | |
133 | * without this... | |
134 | */ | |
135 | int session_of_pgrp(int pgrp) | |
136 | { | |
137 | struct task_struct *p; | |
138 | int sid = -1; | |
139 | ||
140 | read_lock(&tasklist_lock); | |
141 | do_each_task_pid(pgrp, PIDTYPE_PGID, p) { | |
142 | if (p->signal->session > 0) { | |
143 | sid = p->signal->session; | |
144 | goto out; | |
145 | } | |
146 | } while_each_task_pid(pgrp, PIDTYPE_PGID, p); | |
147 | p = find_task_by_pid(pgrp); | |
148 | if (p) | |
149 | sid = p->signal->session; | |
150 | out: | |
151 | read_unlock(&tasklist_lock); | |
152 | ||
153 | return sid; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Determine if a process group is "orphaned", according to the POSIX | |
158 | * definition in 2.2.2.52. Orphaned process groups are not to be affected | |
159 | * by terminal-generated stop signals. Newly orphaned process groups are | |
160 | * to receive a SIGHUP and a SIGCONT. | |
161 | * | |
162 | * "I ask you, have you ever known what it is to be an orphan?" | |
163 | */ | |
164 | static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task) | |
165 | { | |
166 | struct task_struct *p; | |
167 | int ret = 1; | |
168 | ||
169 | do_each_task_pid(pgrp, PIDTYPE_PGID, p) { | |
170 | if (p == ignored_task | |
171 | || p->exit_state | |
172 | || p->real_parent->pid == 1) | |
173 | continue; | |
174 | if (process_group(p->real_parent) != pgrp | |
175 | && p->real_parent->signal->session == p->signal->session) { | |
176 | ret = 0; | |
177 | break; | |
178 | } | |
179 | } while_each_task_pid(pgrp, PIDTYPE_PGID, p); | |
180 | return ret; /* (sighing) "Often!" */ | |
181 | } | |
182 | ||
183 | int is_orphaned_pgrp(int pgrp) | |
184 | { | |
185 | int retval; | |
186 | ||
187 | read_lock(&tasklist_lock); | |
188 | retval = will_become_orphaned_pgrp(pgrp, NULL); | |
189 | read_unlock(&tasklist_lock); | |
190 | ||
191 | return retval; | |
192 | } | |
193 | ||
194 | static inline int has_stopped_jobs(int pgrp) | |
195 | { | |
196 | int retval = 0; | |
197 | struct task_struct *p; | |
198 | ||
199 | do_each_task_pid(pgrp, PIDTYPE_PGID, p) { | |
200 | if (p->state != TASK_STOPPED) | |
201 | continue; | |
202 | ||
203 | /* If p is stopped by a debugger on a signal that won't | |
204 | stop it, then don't count p as stopped. This isn't | |
205 | perfect but it's a good approximation. */ | |
206 | if (unlikely (p->ptrace) | |
207 | && p->exit_code != SIGSTOP | |
208 | && p->exit_code != SIGTSTP | |
209 | && p->exit_code != SIGTTOU | |
210 | && p->exit_code != SIGTTIN) | |
211 | continue; | |
212 | ||
213 | retval = 1; | |
214 | break; | |
215 | } while_each_task_pid(pgrp, PIDTYPE_PGID, p); | |
216 | return retval; | |
217 | } | |
218 | ||
219 | /** | |
4dc3b16b | 220 | * reparent_to_init - Reparent the calling kernel thread to the init task. |
1da177e4 LT |
221 | * |
222 | * If a kernel thread is launched as a result of a system call, or if | |
223 | * it ever exits, it should generally reparent itself to init so that | |
224 | * it is correctly cleaned up on exit. | |
225 | * | |
226 | * The various task state such as scheduling policy and priority may have | |
227 | * been inherited from a user process, so we reset them to sane values here. | |
228 | * | |
229 | * NOTE that reparent_to_init() gives the caller full capabilities. | |
230 | */ | |
6c46ada7 | 231 | static inline void reparent_to_init(void) |
1da177e4 LT |
232 | { |
233 | write_lock_irq(&tasklist_lock); | |
234 | ||
235 | ptrace_unlink(current); | |
236 | /* Reparent to init */ | |
237 | REMOVE_LINKS(current); | |
238 | current->parent = child_reaper; | |
239 | current->real_parent = child_reaper; | |
240 | SET_LINKS(current); | |
241 | ||
242 | /* Set the exit signal to SIGCHLD so we signal init on exit */ | |
243 | current->exit_signal = SIGCHLD; | |
244 | ||
245 | if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0)) | |
246 | set_user_nice(current, 0); | |
247 | /* cpus_allowed? */ | |
248 | /* rt_priority? */ | |
249 | /* signals? */ | |
250 | security_task_reparent_to_init(current); | |
251 | memcpy(current->signal->rlim, init_task.signal->rlim, | |
252 | sizeof(current->signal->rlim)); | |
253 | atomic_inc(&(INIT_USER->__count)); | |
254 | write_unlock_irq(&tasklist_lock); | |
255 | switch_uid(INIT_USER); | |
256 | } | |
257 | ||
258 | void __set_special_pids(pid_t session, pid_t pgrp) | |
259 | { | |
260 | struct task_struct *curr = current; | |
261 | ||
262 | if (curr->signal->session != session) { | |
263 | detach_pid(curr, PIDTYPE_SID); | |
264 | curr->signal->session = session; | |
265 | attach_pid(curr, PIDTYPE_SID, session); | |
266 | } | |
267 | if (process_group(curr) != pgrp) { | |
268 | detach_pid(curr, PIDTYPE_PGID); | |
269 | curr->signal->pgrp = pgrp; | |
270 | attach_pid(curr, PIDTYPE_PGID, pgrp); | |
271 | } | |
272 | } | |
273 | ||
274 | void set_special_pids(pid_t session, pid_t pgrp) | |
275 | { | |
276 | write_lock_irq(&tasklist_lock); | |
277 | __set_special_pids(session, pgrp); | |
278 | write_unlock_irq(&tasklist_lock); | |
279 | } | |
280 | ||
281 | /* | |
282 | * Let kernel threads use this to say that they | |
283 | * allow a certain signal (since daemonize() will | |
284 | * have disabled all of them by default). | |
285 | */ | |
286 | int allow_signal(int sig) | |
287 | { | |
7ed20e1a | 288 | if (!valid_signal(sig) || sig < 1) |
1da177e4 LT |
289 | return -EINVAL; |
290 | ||
291 | spin_lock_irq(¤t->sighand->siglock); | |
292 | sigdelset(¤t->blocked, sig); | |
293 | if (!current->mm) { | |
294 | /* Kernel threads handle their own signals. | |
295 | Let the signal code know it'll be handled, so | |
296 | that they don't get converted to SIGKILL or | |
297 | just silently dropped */ | |
298 | current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2; | |
299 | } | |
300 | recalc_sigpending(); | |
301 | spin_unlock_irq(¤t->sighand->siglock); | |
302 | return 0; | |
303 | } | |
304 | ||
305 | EXPORT_SYMBOL(allow_signal); | |
306 | ||
307 | int disallow_signal(int sig) | |
308 | { | |
7ed20e1a | 309 | if (!valid_signal(sig) || sig < 1) |
1da177e4 LT |
310 | return -EINVAL; |
311 | ||
312 | spin_lock_irq(¤t->sighand->siglock); | |
313 | sigaddset(¤t->blocked, sig); | |
314 | recalc_sigpending(); | |
315 | spin_unlock_irq(¤t->sighand->siglock); | |
316 | return 0; | |
317 | } | |
318 | ||
319 | EXPORT_SYMBOL(disallow_signal); | |
320 | ||
321 | /* | |
322 | * Put all the gunge required to become a kernel thread without | |
323 | * attached user resources in one place where it belongs. | |
324 | */ | |
325 | ||
326 | void daemonize(const char *name, ...) | |
327 | { | |
328 | va_list args; | |
329 | struct fs_struct *fs; | |
330 | sigset_t blocked; | |
331 | ||
332 | va_start(args, name); | |
333 | vsnprintf(current->comm, sizeof(current->comm), name, args); | |
334 | va_end(args); | |
335 | ||
336 | /* | |
337 | * If we were started as result of loading a module, close all of the | |
338 | * user space pages. We don't need them, and if we didn't close them | |
339 | * they would be locked into memory. | |
340 | */ | |
341 | exit_mm(current); | |
342 | ||
343 | set_special_pids(1, 1); | |
344 | down(&tty_sem); | |
345 | current->signal->tty = NULL; | |
346 | up(&tty_sem); | |
347 | ||
348 | /* Block and flush all signals */ | |
349 | sigfillset(&blocked); | |
350 | sigprocmask(SIG_BLOCK, &blocked, NULL); | |
351 | flush_signals(current); | |
352 | ||
353 | /* Become as one with the init task */ | |
354 | ||
355 | exit_fs(current); /* current->fs->count--; */ | |
356 | fs = init_task.fs; | |
357 | current->fs = fs; | |
358 | atomic_inc(&fs->count); | |
359 | exit_files(current); | |
360 | current->files = init_task.files; | |
361 | atomic_inc(¤t->files->count); | |
362 | ||
363 | reparent_to_init(); | |
364 | } | |
365 | ||
366 | EXPORT_SYMBOL(daemonize); | |
367 | ||
368 | static inline void close_files(struct files_struct * files) | |
369 | { | |
370 | int i, j; | |
badf1662 | 371 | struct fdtable *fdt; |
1da177e4 LT |
372 | |
373 | j = 0; | |
4fb3a538 DS |
374 | |
375 | /* | |
376 | * It is safe to dereference the fd table without RCU or | |
377 | * ->file_lock because this is the last reference to the | |
378 | * files structure. | |
379 | */ | |
badf1662 | 380 | fdt = files_fdtable(files); |
1da177e4 LT |
381 | for (;;) { |
382 | unsigned long set; | |
383 | i = j * __NFDBITS; | |
badf1662 | 384 | if (i >= fdt->max_fdset || i >= fdt->max_fds) |
1da177e4 | 385 | break; |
badf1662 | 386 | set = fdt->open_fds->fds_bits[j++]; |
1da177e4 LT |
387 | while (set) { |
388 | if (set & 1) { | |
badf1662 | 389 | struct file * file = xchg(&fdt->fd[i], NULL); |
1da177e4 LT |
390 | if (file) |
391 | filp_close(file, files); | |
392 | } | |
393 | i++; | |
394 | set >>= 1; | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | struct files_struct *get_files_struct(struct task_struct *task) | |
400 | { | |
401 | struct files_struct *files; | |
402 | ||
403 | task_lock(task); | |
404 | files = task->files; | |
405 | if (files) | |
406 | atomic_inc(&files->count); | |
407 | task_unlock(task); | |
408 | ||
409 | return files; | |
410 | } | |
411 | ||
412 | void fastcall put_files_struct(struct files_struct *files) | |
413 | { | |
badf1662 DS |
414 | struct fdtable *fdt; |
415 | ||
1da177e4 LT |
416 | if (atomic_dec_and_test(&files->count)) { |
417 | close_files(files); | |
418 | /* | |
419 | * Free the fd and fdset arrays if we expanded them. | |
ab2af1f5 DS |
420 | * If the fdtable was embedded, pass files for freeing |
421 | * at the end of the RCU grace period. Otherwise, | |
422 | * you can free files immediately. | |
1da177e4 | 423 | */ |
badf1662 | 424 | fdt = files_fdtable(files); |
ab2af1f5 DS |
425 | if (fdt == &files->fdtab) |
426 | fdt->free_files = files; | |
427 | else | |
428 | kmem_cache_free(files_cachep, files); | |
429 | free_fdtable(fdt); | |
1da177e4 LT |
430 | } |
431 | } | |
432 | ||
433 | EXPORT_SYMBOL(put_files_struct); | |
434 | ||
435 | static inline void __exit_files(struct task_struct *tsk) | |
436 | { | |
437 | struct files_struct * files = tsk->files; | |
438 | ||
439 | if (files) { | |
440 | task_lock(tsk); | |
441 | tsk->files = NULL; | |
442 | task_unlock(tsk); | |
443 | put_files_struct(files); | |
444 | } | |
445 | } | |
446 | ||
447 | void exit_files(struct task_struct *tsk) | |
448 | { | |
449 | __exit_files(tsk); | |
450 | } | |
451 | ||
452 | static inline void __put_fs_struct(struct fs_struct *fs) | |
453 | { | |
454 | /* No need to hold fs->lock if we are killing it */ | |
455 | if (atomic_dec_and_test(&fs->count)) { | |
456 | dput(fs->root); | |
457 | mntput(fs->rootmnt); | |
458 | dput(fs->pwd); | |
459 | mntput(fs->pwdmnt); | |
460 | if (fs->altroot) { | |
461 | dput(fs->altroot); | |
462 | mntput(fs->altrootmnt); | |
463 | } | |
464 | kmem_cache_free(fs_cachep, fs); | |
465 | } | |
466 | } | |
467 | ||
468 | void put_fs_struct(struct fs_struct *fs) | |
469 | { | |
470 | __put_fs_struct(fs); | |
471 | } | |
472 | ||
473 | static inline void __exit_fs(struct task_struct *tsk) | |
474 | { | |
475 | struct fs_struct * fs = tsk->fs; | |
476 | ||
477 | if (fs) { | |
478 | task_lock(tsk); | |
479 | tsk->fs = NULL; | |
480 | task_unlock(tsk); | |
481 | __put_fs_struct(fs); | |
482 | } | |
483 | } | |
484 | ||
485 | void exit_fs(struct task_struct *tsk) | |
486 | { | |
487 | __exit_fs(tsk); | |
488 | } | |
489 | ||
490 | EXPORT_SYMBOL_GPL(exit_fs); | |
491 | ||
492 | /* | |
493 | * Turn us into a lazy TLB process if we | |
494 | * aren't already.. | |
495 | */ | |
408b664a | 496 | static void exit_mm(struct task_struct * tsk) |
1da177e4 LT |
497 | { |
498 | struct mm_struct *mm = tsk->mm; | |
499 | ||
500 | mm_release(tsk, mm); | |
501 | if (!mm) | |
502 | return; | |
503 | /* | |
504 | * Serialize with any possible pending coredump. | |
505 | * We must hold mmap_sem around checking core_waiters | |
506 | * and clearing tsk->mm. The core-inducing thread | |
507 | * will increment core_waiters for each thread in the | |
508 | * group with ->mm != NULL. | |
509 | */ | |
510 | down_read(&mm->mmap_sem); | |
511 | if (mm->core_waiters) { | |
512 | up_read(&mm->mmap_sem); | |
513 | down_write(&mm->mmap_sem); | |
514 | if (!--mm->core_waiters) | |
515 | complete(mm->core_startup_done); | |
516 | up_write(&mm->mmap_sem); | |
517 | ||
518 | wait_for_completion(&mm->core_done); | |
519 | down_read(&mm->mmap_sem); | |
520 | } | |
521 | atomic_inc(&mm->mm_count); | |
522 | if (mm != tsk->active_mm) BUG(); | |
523 | /* more a memory barrier than a real lock */ | |
524 | task_lock(tsk); | |
525 | tsk->mm = NULL; | |
526 | up_read(&mm->mmap_sem); | |
527 | enter_lazy_tlb(mm, current); | |
528 | task_unlock(tsk); | |
529 | mmput(mm); | |
530 | } | |
531 | ||
532 | static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper) | |
533 | { | |
534 | /* | |
535 | * Make sure we're not reparenting to ourselves and that | |
536 | * the parent is not a zombie. | |
537 | */ | |
538 | BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE); | |
539 | p->real_parent = reaper; | |
1da177e4 LT |
540 | } |
541 | ||
542 | static inline void reparent_thread(task_t *p, task_t *father, int traced) | |
543 | { | |
544 | /* We don't want people slaying init. */ | |
545 | if (p->exit_signal != -1) | |
546 | p->exit_signal = SIGCHLD; | |
547 | ||
548 | if (p->pdeath_signal) | |
549 | /* We already hold the tasklist_lock here. */ | |
550 | group_send_sig_info(p->pdeath_signal, (void *) 0, p); | |
551 | ||
552 | /* Move the child from its dying parent to the new one. */ | |
553 | if (unlikely(traced)) { | |
554 | /* Preserve ptrace links if someone else is tracing this child. */ | |
555 | list_del_init(&p->ptrace_list); | |
556 | if (p->parent != p->real_parent) | |
557 | list_add(&p->ptrace_list, &p->real_parent->ptrace_children); | |
558 | } else { | |
559 | /* If this child is being traced, then we're the one tracing it | |
560 | * anyway, so let go of it. | |
561 | */ | |
562 | p->ptrace = 0; | |
563 | list_del_init(&p->sibling); | |
564 | p->parent = p->real_parent; | |
565 | list_add_tail(&p->sibling, &p->parent->children); | |
566 | ||
567 | /* If we'd notified the old parent about this child's death, | |
568 | * also notify the new parent. | |
569 | */ | |
570 | if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 && | |
571 | thread_group_empty(p)) | |
572 | do_notify_parent(p, p->exit_signal); | |
573 | else if (p->state == TASK_TRACED) { | |
574 | /* | |
575 | * If it was at a trace stop, turn it into | |
576 | * a normal stop since it's no longer being | |
577 | * traced. | |
578 | */ | |
579 | ptrace_untrace(p); | |
580 | } | |
581 | } | |
582 | ||
583 | /* | |
584 | * process group orphan check | |
585 | * Case ii: Our child is in a different pgrp | |
586 | * than we are, and it was the only connection | |
587 | * outside, so the child pgrp is now orphaned. | |
588 | */ | |
589 | if ((process_group(p) != process_group(father)) && | |
590 | (p->signal->session == father->signal->session)) { | |
591 | int pgrp = process_group(p); | |
592 | ||
593 | if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) { | |
594 | __kill_pg_info(SIGHUP, (void *)1, pgrp); | |
595 | __kill_pg_info(SIGCONT, (void *)1, pgrp); | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | /* | |
601 | * When we die, we re-parent all our children. | |
602 | * Try to give them to another thread in our thread | |
603 | * group, and if no such member exists, give it to | |
604 | * the global child reaper process (ie "init") | |
605 | */ | |
606 | static inline void forget_original_parent(struct task_struct * father, | |
607 | struct list_head *to_release) | |
608 | { | |
609 | struct task_struct *p, *reaper = father; | |
610 | struct list_head *_p, *_n; | |
611 | ||
612 | do { | |
613 | reaper = next_thread(reaper); | |
614 | if (reaper == father) { | |
615 | reaper = child_reaper; | |
616 | break; | |
617 | } | |
618 | } while (reaper->exit_state); | |
619 | ||
620 | /* | |
621 | * There are only two places where our children can be: | |
622 | * | |
623 | * - in our child list | |
624 | * - in our ptraced child list | |
625 | * | |
626 | * Search them and reparent children. | |
627 | */ | |
628 | list_for_each_safe(_p, _n, &father->children) { | |
629 | int ptrace; | |
630 | p = list_entry(_p,struct task_struct,sibling); | |
631 | ||
632 | ptrace = p->ptrace; | |
633 | ||
634 | /* if father isn't the real parent, then ptrace must be enabled */ | |
635 | BUG_ON(father != p->real_parent && !ptrace); | |
636 | ||
637 | if (father == p->real_parent) { | |
638 | /* reparent with a reaper, real father it's us */ | |
639 | choose_new_parent(p, reaper, child_reaper); | |
640 | reparent_thread(p, father, 0); | |
641 | } else { | |
642 | /* reparent ptraced task to its real parent */ | |
643 | __ptrace_unlink (p); | |
644 | if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 && | |
645 | thread_group_empty(p)) | |
646 | do_notify_parent(p, p->exit_signal); | |
647 | } | |
648 | ||
649 | /* | |
650 | * if the ptraced child is a zombie with exit_signal == -1 | |
651 | * we must collect it before we exit, or it will remain | |
652 | * zombie forever since we prevented it from self-reap itself | |
653 | * while it was being traced by us, to be able to see it in wait4. | |
654 | */ | |
655 | if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1)) | |
656 | list_add(&p->ptrace_list, to_release); | |
657 | } | |
658 | list_for_each_safe(_p, _n, &father->ptrace_children) { | |
659 | p = list_entry(_p,struct task_struct,ptrace_list); | |
660 | choose_new_parent(p, reaper, child_reaper); | |
661 | reparent_thread(p, father, 1); | |
662 | } | |
663 | } | |
664 | ||
665 | /* | |
666 | * Send signals to all our closest relatives so that they know | |
667 | * to properly mourn us.. | |
668 | */ | |
669 | static void exit_notify(struct task_struct *tsk) | |
670 | { | |
671 | int state; | |
672 | struct task_struct *t; | |
673 | struct list_head ptrace_dead, *_p, *_n; | |
674 | ||
675 | if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT) | |
676 | && !thread_group_empty(tsk)) { | |
677 | /* | |
678 | * This occurs when there was a race between our exit | |
679 | * syscall and a group signal choosing us as the one to | |
680 | * wake up. It could be that we are the only thread | |
681 | * alerted to check for pending signals, but another thread | |
682 | * should be woken now to take the signal since we will not. | |
683 | * Now we'll wake all the threads in the group just to make | |
684 | * sure someone gets all the pending signals. | |
685 | */ | |
686 | read_lock(&tasklist_lock); | |
687 | spin_lock_irq(&tsk->sighand->siglock); | |
688 | for (t = next_thread(tsk); t != tsk; t = next_thread(t)) | |
689 | if (!signal_pending(t) && !(t->flags & PF_EXITING)) { | |
690 | recalc_sigpending_tsk(t); | |
691 | if (signal_pending(t)) | |
692 | signal_wake_up(t, 0); | |
693 | } | |
694 | spin_unlock_irq(&tsk->sighand->siglock); | |
695 | read_unlock(&tasklist_lock); | |
696 | } | |
697 | ||
698 | write_lock_irq(&tasklist_lock); | |
699 | ||
700 | /* | |
701 | * This does two things: | |
702 | * | |
703 | * A. Make init inherit all the child processes | |
704 | * B. Check to see if any process groups have become orphaned | |
705 | * as a result of our exiting, and if they have any stopped | |
706 | * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) | |
707 | */ | |
708 | ||
709 | INIT_LIST_HEAD(&ptrace_dead); | |
710 | forget_original_parent(tsk, &ptrace_dead); | |
711 | BUG_ON(!list_empty(&tsk->children)); | |
712 | BUG_ON(!list_empty(&tsk->ptrace_children)); | |
713 | ||
714 | /* | |
715 | * Check to see if any process groups have become orphaned | |
716 | * as a result of our exiting, and if they have any stopped | |
717 | * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) | |
718 | * | |
719 | * Case i: Our father is in a different pgrp than we are | |
720 | * and we were the only connection outside, so our pgrp | |
721 | * is about to become orphaned. | |
722 | */ | |
723 | ||
724 | t = tsk->real_parent; | |
725 | ||
726 | if ((process_group(t) != process_group(tsk)) && | |
727 | (t->signal->session == tsk->signal->session) && | |
728 | will_become_orphaned_pgrp(process_group(tsk), tsk) && | |
729 | has_stopped_jobs(process_group(tsk))) { | |
730 | __kill_pg_info(SIGHUP, (void *)1, process_group(tsk)); | |
731 | __kill_pg_info(SIGCONT, (void *)1, process_group(tsk)); | |
732 | } | |
733 | ||
734 | /* Let father know we died | |
735 | * | |
736 | * Thread signals are configurable, but you aren't going to use | |
737 | * that to send signals to arbitary processes. | |
738 | * That stops right now. | |
739 | * | |
740 | * If the parent exec id doesn't match the exec id we saved | |
741 | * when we started then we know the parent has changed security | |
742 | * domain. | |
743 | * | |
744 | * If our self_exec id doesn't match our parent_exec_id then | |
745 | * we have changed execution domain as these two values started | |
746 | * the same after a fork. | |
747 | * | |
748 | */ | |
749 | ||
750 | if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 && | |
751 | ( tsk->parent_exec_id != t->self_exec_id || | |
752 | tsk->self_exec_id != tsk->parent_exec_id) | |
753 | && !capable(CAP_KILL)) | |
754 | tsk->exit_signal = SIGCHLD; | |
755 | ||
756 | ||
757 | /* If something other than our normal parent is ptracing us, then | |
758 | * send it a SIGCHLD instead of honoring exit_signal. exit_signal | |
759 | * only has special meaning to our real parent. | |
760 | */ | |
761 | if (tsk->exit_signal != -1 && thread_group_empty(tsk)) { | |
762 | int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD; | |
763 | do_notify_parent(tsk, signal); | |
764 | } else if (tsk->ptrace) { | |
765 | do_notify_parent(tsk, SIGCHLD); | |
766 | } | |
767 | ||
768 | state = EXIT_ZOMBIE; | |
769 | if (tsk->exit_signal == -1 && | |
770 | (likely(tsk->ptrace == 0) || | |
771 | unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT))) | |
772 | state = EXIT_DEAD; | |
773 | tsk->exit_state = state; | |
774 | ||
775 | write_unlock_irq(&tasklist_lock); | |
776 | ||
777 | list_for_each_safe(_p, _n, &ptrace_dead) { | |
778 | list_del_init(_p); | |
779 | t = list_entry(_p,struct task_struct,ptrace_list); | |
780 | release_task(t); | |
781 | } | |
782 | ||
783 | /* If the process is dead, release it - nobody will wait for it */ | |
784 | if (state == EXIT_DEAD) | |
785 | release_task(tsk); | |
786 | ||
787 | /* PF_DEAD causes final put_task_struct after we schedule. */ | |
788 | preempt_disable(); | |
789 | tsk->flags |= PF_DEAD; | |
790 | } | |
791 | ||
792 | fastcall NORET_TYPE void do_exit(long code) | |
793 | { | |
794 | struct task_struct *tsk = current; | |
795 | int group_dead; | |
796 | ||
797 | profile_task_exit(tsk); | |
798 | ||
22e2c507 JA |
799 | WARN_ON(atomic_read(&tsk->fs_excl)); |
800 | ||
1da177e4 LT |
801 | if (unlikely(in_interrupt())) |
802 | panic("Aiee, killing interrupt handler!"); | |
803 | if (unlikely(!tsk->pid)) | |
804 | panic("Attempted to kill the idle task!"); | |
805 | if (unlikely(tsk->pid == 1)) | |
806 | panic("Attempted to kill init!"); | |
807 | if (tsk->io_context) | |
808 | exit_io_context(); | |
809 | ||
810 | if (unlikely(current->ptrace & PT_TRACE_EXIT)) { | |
811 | current->ptrace_message = code; | |
812 | ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP); | |
813 | } | |
814 | ||
df164db5 AN |
815 | /* |
816 | * We're taking recursive faults here in do_exit. Safest is to just | |
817 | * leave this task alone and wait for reboot. | |
818 | */ | |
819 | if (unlikely(tsk->flags & PF_EXITING)) { | |
820 | printk(KERN_ALERT | |
821 | "Fixing recursive fault but reboot is needed!\n"); | |
822 | set_current_state(TASK_UNINTERRUPTIBLE); | |
823 | schedule(); | |
824 | } | |
825 | ||
1da177e4 LT |
826 | tsk->flags |= PF_EXITING; |
827 | ||
a362f463 LT |
828 | /* |
829 | * Make sure we don't try to process any timer firings | |
830 | * while we are already exiting. | |
831 | */ | |
832 | tsk->it_virt_expires = cputime_zero; | |
833 | tsk->it_prof_expires = cputime_zero; | |
834 | tsk->it_sched_expires = 0; | |
835 | ||
1da177e4 LT |
836 | if (unlikely(in_atomic())) |
837 | printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n", | |
838 | current->comm, current->pid, | |
839 | preempt_count()); | |
840 | ||
841 | acct_update_integrals(tsk); | |
365e9c87 HD |
842 | if (tsk->mm) { |
843 | update_hiwater_rss(tsk->mm); | |
844 | update_hiwater_vm(tsk->mm); | |
845 | } | |
1da177e4 | 846 | group_dead = atomic_dec_and_test(&tsk->signal->live); |
c3068951 AM |
847 | if (group_dead) { |
848 | del_timer_sync(&tsk->signal->real_timer); | |
25f407f0 | 849 | exit_itimers(tsk->signal); |
1da177e4 | 850 | acct_process(code); |
c3068951 | 851 | } |
1da177e4 LT |
852 | exit_mm(tsk); |
853 | ||
854 | exit_sem(tsk); | |
855 | __exit_files(tsk); | |
856 | __exit_fs(tsk); | |
857 | exit_namespace(tsk); | |
858 | exit_thread(); | |
859 | cpuset_exit(tsk); | |
860 | exit_keys(tsk); | |
861 | ||
862 | if (group_dead && tsk->signal->leader) | |
863 | disassociate_ctty(1); | |
864 | ||
865 | module_put(tsk->thread_info->exec_domain->module); | |
866 | if (tsk->binfmt) | |
867 | module_put(tsk->binfmt->module); | |
868 | ||
869 | tsk->exit_code = code; | |
870 | exit_notify(tsk); | |
871 | #ifdef CONFIG_NUMA | |
872 | mpol_free(tsk->mempolicy); | |
873 | tsk->mempolicy = NULL; | |
874 | #endif | |
875 | ||
876 | BUG_ON(!(current->flags & PF_DEAD)); | |
877 | schedule(); | |
878 | BUG(); | |
879 | /* Avoid "noreturn function does return". */ | |
880 | for (;;) ; | |
881 | } | |
882 | ||
012914da RA |
883 | EXPORT_SYMBOL_GPL(do_exit); |
884 | ||
1da177e4 LT |
885 | NORET_TYPE void complete_and_exit(struct completion *comp, long code) |
886 | { | |
887 | if (comp) | |
888 | complete(comp); | |
889 | ||
890 | do_exit(code); | |
891 | } | |
892 | ||
893 | EXPORT_SYMBOL(complete_and_exit); | |
894 | ||
895 | asmlinkage long sys_exit(int error_code) | |
896 | { | |
897 | do_exit((error_code&0xff)<<8); | |
898 | } | |
899 | ||
900 | task_t fastcall *next_thread(const task_t *p) | |
901 | { | |
902 | return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID); | |
903 | } | |
904 | ||
905 | EXPORT_SYMBOL(next_thread); | |
906 | ||
907 | /* | |
908 | * Take down every thread in the group. This is called by fatal signals | |
909 | * as well as by sys_exit_group (below). | |
910 | */ | |
911 | NORET_TYPE void | |
912 | do_group_exit(int exit_code) | |
913 | { | |
914 | BUG_ON(exit_code & 0x80); /* core dumps don't get here */ | |
915 | ||
916 | if (current->signal->flags & SIGNAL_GROUP_EXIT) | |
917 | exit_code = current->signal->group_exit_code; | |
918 | else if (!thread_group_empty(current)) { | |
919 | struct signal_struct *const sig = current->signal; | |
920 | struct sighand_struct *const sighand = current->sighand; | |
921 | read_lock(&tasklist_lock); | |
922 | spin_lock_irq(&sighand->siglock); | |
923 | if (sig->flags & SIGNAL_GROUP_EXIT) | |
924 | /* Another thread got here before we took the lock. */ | |
925 | exit_code = sig->group_exit_code; | |
926 | else { | |
927 | sig->flags = SIGNAL_GROUP_EXIT; | |
928 | sig->group_exit_code = exit_code; | |
929 | zap_other_threads(current); | |
930 | } | |
931 | spin_unlock_irq(&sighand->siglock); | |
932 | read_unlock(&tasklist_lock); | |
933 | } | |
934 | ||
935 | do_exit(exit_code); | |
936 | /* NOTREACHED */ | |
937 | } | |
938 | ||
939 | /* | |
940 | * this kills every thread in the thread group. Note that any externally | |
941 | * wait4()-ing process will get the correct exit code - even if this | |
942 | * thread is not the thread group leader. | |
943 | */ | |
944 | asmlinkage void sys_exit_group(int error_code) | |
945 | { | |
946 | do_group_exit((error_code & 0xff) << 8); | |
947 | } | |
948 | ||
949 | static int eligible_child(pid_t pid, int options, task_t *p) | |
950 | { | |
951 | if (pid > 0) { | |
952 | if (p->pid != pid) | |
953 | return 0; | |
954 | } else if (!pid) { | |
955 | if (process_group(p) != process_group(current)) | |
956 | return 0; | |
957 | } else if (pid != -1) { | |
958 | if (process_group(p) != -pid) | |
959 | return 0; | |
960 | } | |
961 | ||
962 | /* | |
963 | * Do not consider detached threads that are | |
964 | * not ptraced: | |
965 | */ | |
966 | if (p->exit_signal == -1 && !p->ptrace) | |
967 | return 0; | |
968 | ||
969 | /* Wait for all children (clone and not) if __WALL is set; | |
970 | * otherwise, wait for clone children *only* if __WCLONE is | |
971 | * set; otherwise, wait for non-clone children *only*. (Note: | |
972 | * A "clone" child here is one that reports to its parent | |
973 | * using a signal other than SIGCHLD.) */ | |
974 | if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0)) | |
975 | && !(options & __WALL)) | |
976 | return 0; | |
977 | /* | |
978 | * Do not consider thread group leaders that are | |
979 | * in a non-empty thread group: | |
980 | */ | |
981 | if (current->tgid != p->tgid && delay_group_leader(p)) | |
982 | return 2; | |
983 | ||
984 | if (security_task_wait(p)) | |
985 | return 0; | |
986 | ||
987 | return 1; | |
988 | } | |
989 | ||
990 | static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid, | |
991 | int why, int status, | |
992 | struct siginfo __user *infop, | |
993 | struct rusage __user *rusagep) | |
994 | { | |
995 | int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0; | |
996 | put_task_struct(p); | |
997 | if (!retval) | |
998 | retval = put_user(SIGCHLD, &infop->si_signo); | |
999 | if (!retval) | |
1000 | retval = put_user(0, &infop->si_errno); | |
1001 | if (!retval) | |
1002 | retval = put_user((short)why, &infop->si_code); | |
1003 | if (!retval) | |
1004 | retval = put_user(pid, &infop->si_pid); | |
1005 | if (!retval) | |
1006 | retval = put_user(uid, &infop->si_uid); | |
1007 | if (!retval) | |
1008 | retval = put_user(status, &infop->si_status); | |
1009 | if (!retval) | |
1010 | retval = pid; | |
1011 | return retval; | |
1012 | } | |
1013 | ||
1014 | /* | |
1015 | * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold | |
1016 | * read_lock(&tasklist_lock) on entry. If we return zero, we still hold | |
1017 | * the lock and this task is uninteresting. If we return nonzero, we have | |
1018 | * released the lock and the system call should return. | |
1019 | */ | |
1020 | static int wait_task_zombie(task_t *p, int noreap, | |
1021 | struct siginfo __user *infop, | |
1022 | int __user *stat_addr, struct rusage __user *ru) | |
1023 | { | |
1024 | unsigned long state; | |
1025 | int retval; | |
1026 | int status; | |
1027 | ||
1028 | if (unlikely(noreap)) { | |
1029 | pid_t pid = p->pid; | |
1030 | uid_t uid = p->uid; | |
1031 | int exit_code = p->exit_code; | |
1032 | int why, status; | |
1033 | ||
1034 | if (unlikely(p->exit_state != EXIT_ZOMBIE)) | |
1035 | return 0; | |
1036 | if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) | |
1037 | return 0; | |
1038 | get_task_struct(p); | |
1039 | read_unlock(&tasklist_lock); | |
1040 | if ((exit_code & 0x7f) == 0) { | |
1041 | why = CLD_EXITED; | |
1042 | status = exit_code >> 8; | |
1043 | } else { | |
1044 | why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED; | |
1045 | status = exit_code & 0x7f; | |
1046 | } | |
1047 | return wait_noreap_copyout(p, pid, uid, why, | |
1048 | status, infop, ru); | |
1049 | } | |
1050 | ||
1051 | /* | |
1052 | * Try to move the task's state to DEAD | |
1053 | * only one thread is allowed to do this: | |
1054 | */ | |
1055 | state = xchg(&p->exit_state, EXIT_DEAD); | |
1056 | if (state != EXIT_ZOMBIE) { | |
1057 | BUG_ON(state != EXIT_DEAD); | |
1058 | return 0; | |
1059 | } | |
1060 | if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) { | |
1061 | /* | |
1062 | * This can only happen in a race with a ptraced thread | |
1063 | * dying on another processor. | |
1064 | */ | |
1065 | return 0; | |
1066 | } | |
1067 | ||
1068 | if (likely(p->real_parent == p->parent) && likely(p->signal)) { | |
1069 | /* | |
1070 | * The resource counters for the group leader are in its | |
1071 | * own task_struct. Those for dead threads in the group | |
1072 | * are in its signal_struct, as are those for the child | |
1073 | * processes it has previously reaped. All these | |
1074 | * accumulate in the parent's signal_struct c* fields. | |
1075 | * | |
1076 | * We don't bother to take a lock here to protect these | |
1077 | * p->signal fields, because they are only touched by | |
1078 | * __exit_signal, which runs with tasklist_lock | |
1079 | * write-locked anyway, and so is excluded here. We do | |
1080 | * need to protect the access to p->parent->signal fields, | |
1081 | * as other threads in the parent group can be right | |
1082 | * here reaping other children at the same time. | |
1083 | */ | |
1084 | spin_lock_irq(&p->parent->sighand->siglock); | |
1085 | p->parent->signal->cutime = | |
1086 | cputime_add(p->parent->signal->cutime, | |
1087 | cputime_add(p->utime, | |
1088 | cputime_add(p->signal->utime, | |
1089 | p->signal->cutime))); | |
1090 | p->parent->signal->cstime = | |
1091 | cputime_add(p->parent->signal->cstime, | |
1092 | cputime_add(p->stime, | |
1093 | cputime_add(p->signal->stime, | |
1094 | p->signal->cstime))); | |
1095 | p->parent->signal->cmin_flt += | |
1096 | p->min_flt + p->signal->min_flt + p->signal->cmin_flt; | |
1097 | p->parent->signal->cmaj_flt += | |
1098 | p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt; | |
1099 | p->parent->signal->cnvcsw += | |
1100 | p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw; | |
1101 | p->parent->signal->cnivcsw += | |
1102 | p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw; | |
1103 | spin_unlock_irq(&p->parent->sighand->siglock); | |
1104 | } | |
1105 | ||
1106 | /* | |
1107 | * Now we are sure this task is interesting, and no other | |
1108 | * thread can reap it because we set its state to EXIT_DEAD. | |
1109 | */ | |
1110 | read_unlock(&tasklist_lock); | |
1111 | ||
1112 | retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; | |
1113 | status = (p->signal->flags & SIGNAL_GROUP_EXIT) | |
1114 | ? p->signal->group_exit_code : p->exit_code; | |
1115 | if (!retval && stat_addr) | |
1116 | retval = put_user(status, stat_addr); | |
1117 | if (!retval && infop) | |
1118 | retval = put_user(SIGCHLD, &infop->si_signo); | |
1119 | if (!retval && infop) | |
1120 | retval = put_user(0, &infop->si_errno); | |
1121 | if (!retval && infop) { | |
1122 | int why; | |
1123 | ||
1124 | if ((status & 0x7f) == 0) { | |
1125 | why = CLD_EXITED; | |
1126 | status >>= 8; | |
1127 | } else { | |
1128 | why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED; | |
1129 | status &= 0x7f; | |
1130 | } | |
1131 | retval = put_user((short)why, &infop->si_code); | |
1132 | if (!retval) | |
1133 | retval = put_user(status, &infop->si_status); | |
1134 | } | |
1135 | if (!retval && infop) | |
1136 | retval = put_user(p->pid, &infop->si_pid); | |
1137 | if (!retval && infop) | |
1138 | retval = put_user(p->uid, &infop->si_uid); | |
1139 | if (retval) { | |
1140 | // TODO: is this safe? | |
1141 | p->exit_state = EXIT_ZOMBIE; | |
1142 | return retval; | |
1143 | } | |
1144 | retval = p->pid; | |
1145 | if (p->real_parent != p->parent) { | |
1146 | write_lock_irq(&tasklist_lock); | |
1147 | /* Double-check with lock held. */ | |
1148 | if (p->real_parent != p->parent) { | |
1149 | __ptrace_unlink(p); | |
1150 | // TODO: is this safe? | |
1151 | p->exit_state = EXIT_ZOMBIE; | |
1152 | /* | |
1153 | * If this is not a detached task, notify the parent. | |
1154 | * If it's still not detached after that, don't release | |
1155 | * it now. | |
1156 | */ | |
1157 | if (p->exit_signal != -1) { | |
1158 | do_notify_parent(p, p->exit_signal); | |
1159 | if (p->exit_signal != -1) | |
1160 | p = NULL; | |
1161 | } | |
1162 | } | |
1163 | write_unlock_irq(&tasklist_lock); | |
1164 | } | |
1165 | if (p != NULL) | |
1166 | release_task(p); | |
1167 | BUG_ON(!retval); | |
1168 | return retval; | |
1169 | } | |
1170 | ||
1171 | /* | |
1172 | * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold | |
1173 | * read_lock(&tasklist_lock) on entry. If we return zero, we still hold | |
1174 | * the lock and this task is uninteresting. If we return nonzero, we have | |
1175 | * released the lock and the system call should return. | |
1176 | */ | |
1177 | static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap, | |
1178 | struct siginfo __user *infop, | |
1179 | int __user *stat_addr, struct rusage __user *ru) | |
1180 | { | |
1181 | int retval, exit_code; | |
1182 | ||
1183 | if (!p->exit_code) | |
1184 | return 0; | |
1185 | if (delayed_group_leader && !(p->ptrace & PT_PTRACED) && | |
1186 | p->signal && p->signal->group_stop_count > 0) | |
1187 | /* | |
1188 | * A group stop is in progress and this is the group leader. | |
1189 | * We won't report until all threads have stopped. | |
1190 | */ | |
1191 | return 0; | |
1192 | ||
1193 | /* | |
1194 | * Now we are pretty sure this task is interesting. | |
1195 | * Make sure it doesn't get reaped out from under us while we | |
1196 | * give up the lock and then examine it below. We don't want to | |
1197 | * keep holding onto the tasklist_lock while we call getrusage and | |
1198 | * possibly take page faults for user memory. | |
1199 | */ | |
1200 | get_task_struct(p); | |
1201 | read_unlock(&tasklist_lock); | |
1202 | ||
1203 | if (unlikely(noreap)) { | |
1204 | pid_t pid = p->pid; | |
1205 | uid_t uid = p->uid; | |
1206 | int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED; | |
1207 | ||
1208 | exit_code = p->exit_code; | |
1209 | if (unlikely(!exit_code) || | |
14bf01bb | 1210 | unlikely(p->state & TASK_TRACED)) |
1da177e4 LT |
1211 | goto bail_ref; |
1212 | return wait_noreap_copyout(p, pid, uid, | |
1213 | why, (exit_code << 8) | 0x7f, | |
1214 | infop, ru); | |
1215 | } | |
1216 | ||
1217 | write_lock_irq(&tasklist_lock); | |
1218 | ||
1219 | /* | |
1220 | * This uses xchg to be atomic with the thread resuming and setting | |
1221 | * it. It must also be done with the write lock held to prevent a | |
1222 | * race with the EXIT_ZOMBIE case. | |
1223 | */ | |
1224 | exit_code = xchg(&p->exit_code, 0); | |
1225 | if (unlikely(p->exit_state)) { | |
1226 | /* | |
1227 | * The task resumed and then died. Let the next iteration | |
1228 | * catch it in EXIT_ZOMBIE. Note that exit_code might | |
1229 | * already be zero here if it resumed and did _exit(0). | |
1230 | * The task itself is dead and won't touch exit_code again; | |
1231 | * other processors in this function are locked out. | |
1232 | */ | |
1233 | p->exit_code = exit_code; | |
1234 | exit_code = 0; | |
1235 | } | |
1236 | if (unlikely(exit_code == 0)) { | |
1237 | /* | |
1238 | * Another thread in this function got to it first, or it | |
1239 | * resumed, or it resumed and then died. | |
1240 | */ | |
1241 | write_unlock_irq(&tasklist_lock); | |
1242 | bail_ref: | |
1243 | put_task_struct(p); | |
1244 | /* | |
1245 | * We are returning to the wait loop without having successfully | |
1246 | * removed the process and having released the lock. We cannot | |
1247 | * continue, since the "p" task pointer is potentially stale. | |
1248 | * | |
1249 | * Return -EAGAIN, and do_wait() will restart the loop from the | |
1250 | * beginning. Do _not_ re-acquire the lock. | |
1251 | */ | |
1252 | return -EAGAIN; | |
1253 | } | |
1254 | ||
1255 | /* move to end of parent's list to avoid starvation */ | |
1256 | remove_parent(p); | |
1257 | add_parent(p, p->parent); | |
1258 | ||
1259 | write_unlock_irq(&tasklist_lock); | |
1260 | ||
1261 | retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; | |
1262 | if (!retval && stat_addr) | |
1263 | retval = put_user((exit_code << 8) | 0x7f, stat_addr); | |
1264 | if (!retval && infop) | |
1265 | retval = put_user(SIGCHLD, &infop->si_signo); | |
1266 | if (!retval && infop) | |
1267 | retval = put_user(0, &infop->si_errno); | |
1268 | if (!retval && infop) | |
1269 | retval = put_user((short)((p->ptrace & PT_PTRACED) | |
1270 | ? CLD_TRAPPED : CLD_STOPPED), | |
1271 | &infop->si_code); | |
1272 | if (!retval && infop) | |
1273 | retval = put_user(exit_code, &infop->si_status); | |
1274 | if (!retval && infop) | |
1275 | retval = put_user(p->pid, &infop->si_pid); | |
1276 | if (!retval && infop) | |
1277 | retval = put_user(p->uid, &infop->si_uid); | |
1278 | if (!retval) | |
1279 | retval = p->pid; | |
1280 | put_task_struct(p); | |
1281 | ||
1282 | BUG_ON(!retval); | |
1283 | return retval; | |
1284 | } | |
1285 | ||
1286 | /* | |
1287 | * Handle do_wait work for one task in a live, non-stopped state. | |
1288 | * read_lock(&tasklist_lock) on entry. If we return zero, we still hold | |
1289 | * the lock and this task is uninteresting. If we return nonzero, we have | |
1290 | * released the lock and the system call should return. | |
1291 | */ | |
1292 | static int wait_task_continued(task_t *p, int noreap, | |
1293 | struct siginfo __user *infop, | |
1294 | int __user *stat_addr, struct rusage __user *ru) | |
1295 | { | |
1296 | int retval; | |
1297 | pid_t pid; | |
1298 | uid_t uid; | |
1299 | ||
1300 | if (unlikely(!p->signal)) | |
1301 | return 0; | |
1302 | ||
1303 | if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) | |
1304 | return 0; | |
1305 | ||
1306 | spin_lock_irq(&p->sighand->siglock); | |
1307 | /* Re-check with the lock held. */ | |
1308 | if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) { | |
1309 | spin_unlock_irq(&p->sighand->siglock); | |
1310 | return 0; | |
1311 | } | |
1312 | if (!noreap) | |
1313 | p->signal->flags &= ~SIGNAL_STOP_CONTINUED; | |
1314 | spin_unlock_irq(&p->sighand->siglock); | |
1315 | ||
1316 | pid = p->pid; | |
1317 | uid = p->uid; | |
1318 | get_task_struct(p); | |
1319 | read_unlock(&tasklist_lock); | |
1320 | ||
1321 | if (!infop) { | |
1322 | retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; | |
1323 | put_task_struct(p); | |
1324 | if (!retval && stat_addr) | |
1325 | retval = put_user(0xffff, stat_addr); | |
1326 | if (!retval) | |
1327 | retval = p->pid; | |
1328 | } else { | |
1329 | retval = wait_noreap_copyout(p, pid, uid, | |
1330 | CLD_CONTINUED, SIGCONT, | |
1331 | infop, ru); | |
1332 | BUG_ON(retval == 0); | |
1333 | } | |
1334 | ||
1335 | return retval; | |
1336 | } | |
1337 | ||
1338 | ||
1339 | static inline int my_ptrace_child(struct task_struct *p) | |
1340 | { | |
1341 | if (!(p->ptrace & PT_PTRACED)) | |
1342 | return 0; | |
1343 | if (!(p->ptrace & PT_ATTACHED)) | |
1344 | return 1; | |
1345 | /* | |
1346 | * This child was PTRACE_ATTACH'd. We should be seeing it only if | |
1347 | * we are the attacher. If we are the real parent, this is a race | |
1348 | * inside ptrace_attach. It is waiting for the tasklist_lock, | |
1349 | * which we have to switch the parent links, but has already set | |
1350 | * the flags in p->ptrace. | |
1351 | */ | |
1352 | return (p->parent != p->real_parent); | |
1353 | } | |
1354 | ||
1355 | static long do_wait(pid_t pid, int options, struct siginfo __user *infop, | |
1356 | int __user *stat_addr, struct rusage __user *ru) | |
1357 | { | |
1358 | DECLARE_WAITQUEUE(wait, current); | |
1359 | struct task_struct *tsk; | |
1360 | int flag, retval; | |
1361 | ||
1362 | add_wait_queue(¤t->signal->wait_chldexit,&wait); | |
1363 | repeat: | |
1364 | /* | |
1365 | * We will set this flag if we see any child that might later | |
1366 | * match our criteria, even if we are not able to reap it yet. | |
1367 | */ | |
1368 | flag = 0; | |
1369 | current->state = TASK_INTERRUPTIBLE; | |
1370 | read_lock(&tasklist_lock); | |
1371 | tsk = current; | |
1372 | do { | |
1373 | struct task_struct *p; | |
1374 | struct list_head *_p; | |
1375 | int ret; | |
1376 | ||
1377 | list_for_each(_p,&tsk->children) { | |
1378 | p = list_entry(_p,struct task_struct,sibling); | |
1379 | ||
1380 | ret = eligible_child(pid, options, p); | |
1381 | if (!ret) | |
1382 | continue; | |
1383 | ||
1384 | switch (p->state) { | |
1385 | case TASK_TRACED: | |
1386 | if (!my_ptrace_child(p)) | |
1387 | continue; | |
1388 | /*FALLTHROUGH*/ | |
1389 | case TASK_STOPPED: | |
1390 | /* | |
1391 | * It's stopped now, so it might later | |
1392 | * continue, exit, or stop again. | |
1393 | */ | |
1394 | flag = 1; | |
1395 | if (!(options & WUNTRACED) && | |
1396 | !my_ptrace_child(p)) | |
1397 | continue; | |
1398 | retval = wait_task_stopped(p, ret == 2, | |
1399 | (options & WNOWAIT), | |
1400 | infop, | |
1401 | stat_addr, ru); | |
1402 | if (retval == -EAGAIN) | |
1403 | goto repeat; | |
1404 | if (retval != 0) /* He released the lock. */ | |
1405 | goto end; | |
1406 | break; | |
1407 | default: | |
1408 | // case EXIT_DEAD: | |
1409 | if (p->exit_state == EXIT_DEAD) | |
1410 | continue; | |
1411 | // case EXIT_ZOMBIE: | |
1412 | if (p->exit_state == EXIT_ZOMBIE) { | |
1413 | /* | |
1414 | * Eligible but we cannot release | |
1415 | * it yet: | |
1416 | */ | |
1417 | if (ret == 2) | |
1418 | goto check_continued; | |
1419 | if (!likely(options & WEXITED)) | |
1420 | continue; | |
1421 | retval = wait_task_zombie( | |
1422 | p, (options & WNOWAIT), | |
1423 | infop, stat_addr, ru); | |
1424 | /* He released the lock. */ | |
1425 | if (retval != 0) | |
1426 | goto end; | |
1427 | break; | |
1428 | } | |
1429 | check_continued: | |
1430 | /* | |
1431 | * It's running now, so it might later | |
1432 | * exit, stop, or stop and then continue. | |
1433 | */ | |
1434 | flag = 1; | |
1435 | if (!unlikely(options & WCONTINUED)) | |
1436 | continue; | |
1437 | retval = wait_task_continued( | |
1438 | p, (options & WNOWAIT), | |
1439 | infop, stat_addr, ru); | |
1440 | if (retval != 0) /* He released the lock. */ | |
1441 | goto end; | |
1442 | break; | |
1443 | } | |
1444 | } | |
1445 | if (!flag) { | |
1446 | list_for_each(_p, &tsk->ptrace_children) { | |
1447 | p = list_entry(_p, struct task_struct, | |
1448 | ptrace_list); | |
1449 | if (!eligible_child(pid, options, p)) | |
1450 | continue; | |
1451 | flag = 1; | |
1452 | break; | |
1453 | } | |
1454 | } | |
1455 | if (options & __WNOTHREAD) | |
1456 | break; | |
1457 | tsk = next_thread(tsk); | |
1458 | if (tsk->signal != current->signal) | |
1459 | BUG(); | |
1460 | } while (tsk != current); | |
1461 | ||
1462 | read_unlock(&tasklist_lock); | |
1463 | if (flag) { | |
1464 | retval = 0; | |
1465 | if (options & WNOHANG) | |
1466 | goto end; | |
1467 | retval = -ERESTARTSYS; | |
1468 | if (signal_pending(current)) | |
1469 | goto end; | |
1470 | schedule(); | |
1471 | goto repeat; | |
1472 | } | |
1473 | retval = -ECHILD; | |
1474 | end: | |
1475 | current->state = TASK_RUNNING; | |
1476 | remove_wait_queue(¤t->signal->wait_chldexit,&wait); | |
1477 | if (infop) { | |
1478 | if (retval > 0) | |
1479 | retval = 0; | |
1480 | else { | |
1481 | /* | |
1482 | * For a WNOHANG return, clear out all the fields | |
1483 | * we would set so the user can easily tell the | |
1484 | * difference. | |
1485 | */ | |
1486 | if (!retval) | |
1487 | retval = put_user(0, &infop->si_signo); | |
1488 | if (!retval) | |
1489 | retval = put_user(0, &infop->si_errno); | |
1490 | if (!retval) | |
1491 | retval = put_user(0, &infop->si_code); | |
1492 | if (!retval) | |
1493 | retval = put_user(0, &infop->si_pid); | |
1494 | if (!retval) | |
1495 | retval = put_user(0, &infop->si_uid); | |
1496 | if (!retval) | |
1497 | retval = put_user(0, &infop->si_status); | |
1498 | } | |
1499 | } | |
1500 | return retval; | |
1501 | } | |
1502 | ||
1503 | asmlinkage long sys_waitid(int which, pid_t pid, | |
1504 | struct siginfo __user *infop, int options, | |
1505 | struct rusage __user *ru) | |
1506 | { | |
1507 | long ret; | |
1508 | ||
1509 | if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED)) | |
1510 | return -EINVAL; | |
1511 | if (!(options & (WEXITED|WSTOPPED|WCONTINUED))) | |
1512 | return -EINVAL; | |
1513 | ||
1514 | switch (which) { | |
1515 | case P_ALL: | |
1516 | pid = -1; | |
1517 | break; | |
1518 | case P_PID: | |
1519 | if (pid <= 0) | |
1520 | return -EINVAL; | |
1521 | break; | |
1522 | case P_PGID: | |
1523 | if (pid <= 0) | |
1524 | return -EINVAL; | |
1525 | pid = -pid; | |
1526 | break; | |
1527 | default: | |
1528 | return -EINVAL; | |
1529 | } | |
1530 | ||
1531 | ret = do_wait(pid, options, infop, NULL, ru); | |
1532 | ||
1533 | /* avoid REGPARM breakage on x86: */ | |
1534 | prevent_tail_call(ret); | |
1535 | return ret; | |
1536 | } | |
1537 | ||
1538 | asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr, | |
1539 | int options, struct rusage __user *ru) | |
1540 | { | |
1541 | long ret; | |
1542 | ||
1543 | if (options & ~(WNOHANG|WUNTRACED|WCONTINUED| | |
1544 | __WNOTHREAD|__WCLONE|__WALL)) | |
1545 | return -EINVAL; | |
1546 | ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru); | |
1547 | ||
1548 | /* avoid REGPARM breakage on x86: */ | |
1549 | prevent_tail_call(ret); | |
1550 | return ret; | |
1551 | } | |
1552 | ||
1553 | #ifdef __ARCH_WANT_SYS_WAITPID | |
1554 | ||
1555 | /* | |
1556 | * sys_waitpid() remains for compatibility. waitpid() should be | |
1557 | * implemented by calling sys_wait4() from libc.a. | |
1558 | */ | |
1559 | asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options) | |
1560 | { | |
1561 | return sys_wait4(pid, stat_addr, options, NULL); | |
1562 | } | |
1563 | ||
1564 | #endif |