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