blob: 83beb1e93b18c77d3f2fb1d2cfb21e3f9e66c28e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/uaccess.h>
33#include <asm/unistd.h>
34#include <asm/pgtable.h>
35#include <asm/mmu_context.h>
36
37extern void sem_exit (void);
38extern struct task_struct *child_reaper;
39
40int getrusage(struct task_struct *, int, struct rusage __user *);
41
Adrian Bunk408b6642005-05-01 08:59:29 -070042static void exit_mm(struct task_struct * tsk);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static 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
59void release_task(struct task_struct * p)
60{
61 int zap_leader;
62 task_t *leader;
63 struct dentry *proc_dentry;
64
65repeat:
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);
Christoph Lameter71a22242005-06-23 00:10:05 -070075 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 __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
117void 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 */
135int 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;
150out:
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 */
164static 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
183int 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
194static 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/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700220 * reparent_to_init - Reparent the calling kernel thread to the init task.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
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 */
Coywolf Qi Hunt6c46ada2005-04-16 15:26:01 -0700231static inline void reparent_to_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
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
258void __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
274void 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 */
286int allow_signal(int sig)
287{
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700288 if (!valid_signal(sig) || sig < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -EINVAL;
290
291 spin_lock_irq(&current->sighand->siglock);
292 sigdelset(&current->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(&current->sighand->siglock);
302 return 0;
303}
304
305EXPORT_SYMBOL(allow_signal);
306
307int disallow_signal(int sig)
308{
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700309 if (!valid_signal(sig) || sig < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -EINVAL;
311
312 spin_lock_irq(&current->sighand->siglock);
313 sigaddset(&current->blocked, sig);
314 recalc_sigpending();
315 spin_unlock_irq(&current->sighand->siglock);
316 return 0;
317}
318
319EXPORT_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
326void 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(&current->files->count);
362
363 reparent_to_init();
364}
365
366EXPORT_SYMBOL(daemonize);
367
368static inline void close_files(struct files_struct * files)
369{
370 int i, j;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700371 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 j = 0;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700374 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 for (;;) {
376 unsigned long set;
377 i = j * __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700378 if (i >= fdt->max_fdset || i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700380 set = fdt->open_fds->fds_bits[j++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 while (set) {
382 if (set & 1) {
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700383 struct file * file = xchg(&fdt->fd[i], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (file)
385 filp_close(file, files);
386 }
387 i++;
388 set >>= 1;
389 }
390 }
391}
392
393struct files_struct *get_files_struct(struct task_struct *task)
394{
395 struct files_struct *files;
396
397 task_lock(task);
398 files = task->files;
399 if (files)
400 atomic_inc(&files->count);
401 task_unlock(task);
402
403 return files;
404}
405
406void fastcall put_files_struct(struct files_struct *files)
407{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700408 struct fdtable *fdt;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (atomic_dec_and_test(&files->count)) {
411 close_files(files);
412 /*
413 * Free the fd and fdset arrays if we expanded them.
414 */
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700415 fdt = files_fdtable(files);
416 if (fdt->fd != &files->fd_array[0])
417 free_fd_array(fdt->fd, fdt->max_fds);
418 if (fdt->max_fdset > __FD_SETSIZE) {
419 free_fdset(fdt->open_fds, fdt->max_fdset);
420 free_fdset(fdt->close_on_exec, fdt->max_fdset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 kmem_cache_free(files_cachep, files);
423 }
424}
425
426EXPORT_SYMBOL(put_files_struct);
427
428static inline void __exit_files(struct task_struct *tsk)
429{
430 struct files_struct * files = tsk->files;
431
432 if (files) {
433 task_lock(tsk);
434 tsk->files = NULL;
435 task_unlock(tsk);
436 put_files_struct(files);
437 }
438}
439
440void exit_files(struct task_struct *tsk)
441{
442 __exit_files(tsk);
443}
444
445static inline void __put_fs_struct(struct fs_struct *fs)
446{
447 /* No need to hold fs->lock if we are killing it */
448 if (atomic_dec_and_test(&fs->count)) {
449 dput(fs->root);
450 mntput(fs->rootmnt);
451 dput(fs->pwd);
452 mntput(fs->pwdmnt);
453 if (fs->altroot) {
454 dput(fs->altroot);
455 mntput(fs->altrootmnt);
456 }
457 kmem_cache_free(fs_cachep, fs);
458 }
459}
460
461void put_fs_struct(struct fs_struct *fs)
462{
463 __put_fs_struct(fs);
464}
465
466static inline void __exit_fs(struct task_struct *tsk)
467{
468 struct fs_struct * fs = tsk->fs;
469
470 if (fs) {
471 task_lock(tsk);
472 tsk->fs = NULL;
473 task_unlock(tsk);
474 __put_fs_struct(fs);
475 }
476}
477
478void exit_fs(struct task_struct *tsk)
479{
480 __exit_fs(tsk);
481}
482
483EXPORT_SYMBOL_GPL(exit_fs);
484
485/*
486 * Turn us into a lazy TLB process if we
487 * aren't already..
488 */
Adrian Bunk408b6642005-05-01 08:59:29 -0700489static void exit_mm(struct task_struct * tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct mm_struct *mm = tsk->mm;
492
493 mm_release(tsk, mm);
494 if (!mm)
495 return;
496 /*
497 * Serialize with any possible pending coredump.
498 * We must hold mmap_sem around checking core_waiters
499 * and clearing tsk->mm. The core-inducing thread
500 * will increment core_waiters for each thread in the
501 * group with ->mm != NULL.
502 */
503 down_read(&mm->mmap_sem);
504 if (mm->core_waiters) {
505 up_read(&mm->mmap_sem);
506 down_write(&mm->mmap_sem);
507 if (!--mm->core_waiters)
508 complete(mm->core_startup_done);
509 up_write(&mm->mmap_sem);
510
511 wait_for_completion(&mm->core_done);
512 down_read(&mm->mmap_sem);
513 }
514 atomic_inc(&mm->mm_count);
515 if (mm != tsk->active_mm) BUG();
516 /* more a memory barrier than a real lock */
517 task_lock(tsk);
518 tsk->mm = NULL;
519 up_read(&mm->mmap_sem);
520 enter_lazy_tlb(mm, current);
521 task_unlock(tsk);
522 mmput(mm);
523}
524
525static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
526{
527 /*
528 * Make sure we're not reparenting to ourselves and that
529 * the parent is not a zombie.
530 */
531 BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE);
532 p->real_parent = reaper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535static inline void reparent_thread(task_t *p, task_t *father, int traced)
536{
537 /* We don't want people slaying init. */
538 if (p->exit_signal != -1)
539 p->exit_signal = SIGCHLD;
540
541 if (p->pdeath_signal)
542 /* We already hold the tasklist_lock here. */
543 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
544
545 /* Move the child from its dying parent to the new one. */
546 if (unlikely(traced)) {
547 /* Preserve ptrace links if someone else is tracing this child. */
548 list_del_init(&p->ptrace_list);
549 if (p->parent != p->real_parent)
550 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
551 } else {
552 /* If this child is being traced, then we're the one tracing it
553 * anyway, so let go of it.
554 */
555 p->ptrace = 0;
556 list_del_init(&p->sibling);
557 p->parent = p->real_parent;
558 list_add_tail(&p->sibling, &p->parent->children);
559
560 /* If we'd notified the old parent about this child's death,
561 * also notify the new parent.
562 */
563 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
564 thread_group_empty(p))
565 do_notify_parent(p, p->exit_signal);
566 else if (p->state == TASK_TRACED) {
567 /*
568 * If it was at a trace stop, turn it into
569 * a normal stop since it's no longer being
570 * traced.
571 */
572 ptrace_untrace(p);
573 }
574 }
575
576 /*
577 * process group orphan check
578 * Case ii: Our child is in a different pgrp
579 * than we are, and it was the only connection
580 * outside, so the child pgrp is now orphaned.
581 */
582 if ((process_group(p) != process_group(father)) &&
583 (p->signal->session == father->signal->session)) {
584 int pgrp = process_group(p);
585
586 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
587 __kill_pg_info(SIGHUP, (void *)1, pgrp);
588 __kill_pg_info(SIGCONT, (void *)1, pgrp);
589 }
590 }
591}
592
593/*
594 * When we die, we re-parent all our children.
595 * Try to give them to another thread in our thread
596 * group, and if no such member exists, give it to
597 * the global child reaper process (ie "init")
598 */
599static inline void forget_original_parent(struct task_struct * father,
600 struct list_head *to_release)
601{
602 struct task_struct *p, *reaper = father;
603 struct list_head *_p, *_n;
604
605 do {
606 reaper = next_thread(reaper);
607 if (reaper == father) {
608 reaper = child_reaper;
609 break;
610 }
611 } while (reaper->exit_state);
612
613 /*
614 * There are only two places where our children can be:
615 *
616 * - in our child list
617 * - in our ptraced child list
618 *
619 * Search them and reparent children.
620 */
621 list_for_each_safe(_p, _n, &father->children) {
622 int ptrace;
623 p = list_entry(_p,struct task_struct,sibling);
624
625 ptrace = p->ptrace;
626
627 /* if father isn't the real parent, then ptrace must be enabled */
628 BUG_ON(father != p->real_parent && !ptrace);
629
630 if (father == p->real_parent) {
631 /* reparent with a reaper, real father it's us */
632 choose_new_parent(p, reaper, child_reaper);
633 reparent_thread(p, father, 0);
634 } else {
635 /* reparent ptraced task to its real parent */
636 __ptrace_unlink (p);
637 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
638 thread_group_empty(p))
639 do_notify_parent(p, p->exit_signal);
640 }
641
642 /*
643 * if the ptraced child is a zombie with exit_signal == -1
644 * we must collect it before we exit, or it will remain
645 * zombie forever since we prevented it from self-reap itself
646 * while it was being traced by us, to be able to see it in wait4.
647 */
648 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
649 list_add(&p->ptrace_list, to_release);
650 }
651 list_for_each_safe(_p, _n, &father->ptrace_children) {
652 p = list_entry(_p,struct task_struct,ptrace_list);
653 choose_new_parent(p, reaper, child_reaper);
654 reparent_thread(p, father, 1);
655 }
656}
657
658/*
659 * Send signals to all our closest relatives so that they know
660 * to properly mourn us..
661 */
662static void exit_notify(struct task_struct *tsk)
663{
664 int state;
665 struct task_struct *t;
666 struct list_head ptrace_dead, *_p, *_n;
667
668 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
669 && !thread_group_empty(tsk)) {
670 /*
671 * This occurs when there was a race between our exit
672 * syscall and a group signal choosing us as the one to
673 * wake up. It could be that we are the only thread
674 * alerted to check for pending signals, but another thread
675 * should be woken now to take the signal since we will not.
676 * Now we'll wake all the threads in the group just to make
677 * sure someone gets all the pending signals.
678 */
679 read_lock(&tasklist_lock);
680 spin_lock_irq(&tsk->sighand->siglock);
681 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
682 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
683 recalc_sigpending_tsk(t);
684 if (signal_pending(t))
685 signal_wake_up(t, 0);
686 }
687 spin_unlock_irq(&tsk->sighand->siglock);
688 read_unlock(&tasklist_lock);
689 }
690
691 write_lock_irq(&tasklist_lock);
692
693 /*
694 * This does two things:
695 *
696 * A. Make init inherit all the child processes
697 * B. Check to see if any process groups have become orphaned
698 * as a result of our exiting, and if they have any stopped
699 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
700 */
701
702 INIT_LIST_HEAD(&ptrace_dead);
703 forget_original_parent(tsk, &ptrace_dead);
704 BUG_ON(!list_empty(&tsk->children));
705 BUG_ON(!list_empty(&tsk->ptrace_children));
706
707 /*
708 * Check to see if any process groups have become orphaned
709 * as a result of our exiting, and if they have any stopped
710 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
711 *
712 * Case i: Our father is in a different pgrp than we are
713 * and we were the only connection outside, so our pgrp
714 * is about to become orphaned.
715 */
716
717 t = tsk->real_parent;
718
719 if ((process_group(t) != process_group(tsk)) &&
720 (t->signal->session == tsk->signal->session) &&
721 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
722 has_stopped_jobs(process_group(tsk))) {
723 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
724 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
725 }
726
727 /* Let father know we died
728 *
729 * Thread signals are configurable, but you aren't going to use
730 * that to send signals to arbitary processes.
731 * That stops right now.
732 *
733 * If the parent exec id doesn't match the exec id we saved
734 * when we started then we know the parent has changed security
735 * domain.
736 *
737 * If our self_exec id doesn't match our parent_exec_id then
738 * we have changed execution domain as these two values started
739 * the same after a fork.
740 *
741 */
742
743 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
744 ( tsk->parent_exec_id != t->self_exec_id ||
745 tsk->self_exec_id != tsk->parent_exec_id)
746 && !capable(CAP_KILL))
747 tsk->exit_signal = SIGCHLD;
748
749
750 /* If something other than our normal parent is ptracing us, then
751 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
752 * only has special meaning to our real parent.
753 */
754 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
755 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
756 do_notify_parent(tsk, signal);
757 } else if (tsk->ptrace) {
758 do_notify_parent(tsk, SIGCHLD);
759 }
760
761 state = EXIT_ZOMBIE;
762 if (tsk->exit_signal == -1 &&
763 (likely(tsk->ptrace == 0) ||
764 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
765 state = EXIT_DEAD;
766 tsk->exit_state = state;
767
768 write_unlock_irq(&tasklist_lock);
769
770 list_for_each_safe(_p, _n, &ptrace_dead) {
771 list_del_init(_p);
772 t = list_entry(_p,struct task_struct,ptrace_list);
773 release_task(t);
774 }
775
776 /* If the process is dead, release it - nobody will wait for it */
777 if (state == EXIT_DEAD)
778 release_task(tsk);
779
780 /* PF_DEAD causes final put_task_struct after we schedule. */
781 preempt_disable();
782 tsk->flags |= PF_DEAD;
783}
784
785fastcall NORET_TYPE void do_exit(long code)
786{
787 struct task_struct *tsk = current;
788 int group_dead;
789
790 profile_task_exit(tsk);
791
Jens Axboe22e2c502005-06-27 10:55:12 +0200792 WARN_ON(atomic_read(&tsk->fs_excl));
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (unlikely(in_interrupt()))
795 panic("Aiee, killing interrupt handler!");
796 if (unlikely(!tsk->pid))
797 panic("Attempted to kill the idle task!");
798 if (unlikely(tsk->pid == 1))
799 panic("Attempted to kill init!");
800 if (tsk->io_context)
801 exit_io_context();
802
803 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
804 current->ptrace_message = code;
805 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
806 }
807
Alexander Nybergdf164db2005-06-23 00:09:13 -0700808 /*
809 * We're taking recursive faults here in do_exit. Safest is to just
810 * leave this task alone and wait for reboot.
811 */
812 if (unlikely(tsk->flags & PF_EXITING)) {
813 printk(KERN_ALERT
814 "Fixing recursive fault but reboot is needed!\n");
815 set_current_state(TASK_UNINTERRUPTIBLE);
816 schedule();
817 }
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 tsk->flags |= PF_EXITING;
820
821 /*
822 * Make sure we don't try to process any timer firings
823 * while we are already exiting.
824 */
825 tsk->it_virt_expires = cputime_zero;
826 tsk->it_prof_expires = cputime_zero;
827 tsk->it_sched_expires = 0;
828
829 if (unlikely(in_atomic()))
830 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
831 current->comm, current->pid,
832 preempt_count());
833
834 acct_update_integrals(tsk);
835 update_mem_hiwater(tsk);
836 group_dead = atomic_dec_and_test(&tsk->signal->live);
Andrew Mortonc3068952005-08-04 16:49:32 -0700837 if (group_dead) {
838 del_timer_sync(&tsk->signal->real_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 acct_process(code);
Andrew Mortonc3068952005-08-04 16:49:32 -0700840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 exit_mm(tsk);
842
843 exit_sem(tsk);
844 __exit_files(tsk);
845 __exit_fs(tsk);
846 exit_namespace(tsk);
847 exit_thread();
848 cpuset_exit(tsk);
849 exit_keys(tsk);
850
851 if (group_dead && tsk->signal->leader)
852 disassociate_ctty(1);
853
854 module_put(tsk->thread_info->exec_domain->module);
855 if (tsk->binfmt)
856 module_put(tsk->binfmt->module);
857
858 tsk->exit_code = code;
859 exit_notify(tsk);
860#ifdef CONFIG_NUMA
861 mpol_free(tsk->mempolicy);
862 tsk->mempolicy = NULL;
863#endif
864
865 BUG_ON(!(current->flags & PF_DEAD));
866 schedule();
867 BUG();
868 /* Avoid "noreturn function does return". */
869 for (;;) ;
870}
871
Russ Anderson012914d2005-04-23 00:08:00 -0700872EXPORT_SYMBOL_GPL(do_exit);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874NORET_TYPE void complete_and_exit(struct completion *comp, long code)
875{
876 if (comp)
877 complete(comp);
878
879 do_exit(code);
880}
881
882EXPORT_SYMBOL(complete_and_exit);
883
884asmlinkage long sys_exit(int error_code)
885{
886 do_exit((error_code&0xff)<<8);
887}
888
889task_t fastcall *next_thread(const task_t *p)
890{
891 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
892}
893
894EXPORT_SYMBOL(next_thread);
895
896/*
897 * Take down every thread in the group. This is called by fatal signals
898 * as well as by sys_exit_group (below).
899 */
900NORET_TYPE void
901do_group_exit(int exit_code)
902{
903 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
904
905 if (current->signal->flags & SIGNAL_GROUP_EXIT)
906 exit_code = current->signal->group_exit_code;
907 else if (!thread_group_empty(current)) {
908 struct signal_struct *const sig = current->signal;
909 struct sighand_struct *const sighand = current->sighand;
910 read_lock(&tasklist_lock);
911 spin_lock_irq(&sighand->siglock);
912 if (sig->flags & SIGNAL_GROUP_EXIT)
913 /* Another thread got here before we took the lock. */
914 exit_code = sig->group_exit_code;
915 else {
916 sig->flags = SIGNAL_GROUP_EXIT;
917 sig->group_exit_code = exit_code;
918 zap_other_threads(current);
919 }
920 spin_unlock_irq(&sighand->siglock);
921 read_unlock(&tasklist_lock);
922 }
923
924 do_exit(exit_code);
925 /* NOTREACHED */
926}
927
928/*
929 * this kills every thread in the thread group. Note that any externally
930 * wait4()-ing process will get the correct exit code - even if this
931 * thread is not the thread group leader.
932 */
933asmlinkage void sys_exit_group(int error_code)
934{
935 do_group_exit((error_code & 0xff) << 8);
936}
937
938static int eligible_child(pid_t pid, int options, task_t *p)
939{
940 if (pid > 0) {
941 if (p->pid != pid)
942 return 0;
943 } else if (!pid) {
944 if (process_group(p) != process_group(current))
945 return 0;
946 } else if (pid != -1) {
947 if (process_group(p) != -pid)
948 return 0;
949 }
950
951 /*
952 * Do not consider detached threads that are
953 * not ptraced:
954 */
955 if (p->exit_signal == -1 && !p->ptrace)
956 return 0;
957
958 /* Wait for all children (clone and not) if __WALL is set;
959 * otherwise, wait for clone children *only* if __WCLONE is
960 * set; otherwise, wait for non-clone children *only*. (Note:
961 * A "clone" child here is one that reports to its parent
962 * using a signal other than SIGCHLD.) */
963 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
964 && !(options & __WALL))
965 return 0;
966 /*
967 * Do not consider thread group leaders that are
968 * in a non-empty thread group:
969 */
970 if (current->tgid != p->tgid && delay_group_leader(p))
971 return 2;
972
973 if (security_task_wait(p))
974 return 0;
975
976 return 1;
977}
978
979static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
980 int why, int status,
981 struct siginfo __user *infop,
982 struct rusage __user *rusagep)
983{
984 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
985 put_task_struct(p);
986 if (!retval)
987 retval = put_user(SIGCHLD, &infop->si_signo);
988 if (!retval)
989 retval = put_user(0, &infop->si_errno);
990 if (!retval)
991 retval = put_user((short)why, &infop->si_code);
992 if (!retval)
993 retval = put_user(pid, &infop->si_pid);
994 if (!retval)
995 retval = put_user(uid, &infop->si_uid);
996 if (!retval)
997 retval = put_user(status, &infop->si_status);
998 if (!retval)
999 retval = pid;
1000 return retval;
1001}
1002
1003/*
1004 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1005 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1006 * the lock and this task is uninteresting. If we return nonzero, we have
1007 * released the lock and the system call should return.
1008 */
1009static int wait_task_zombie(task_t *p, int noreap,
1010 struct siginfo __user *infop,
1011 int __user *stat_addr, struct rusage __user *ru)
1012{
1013 unsigned long state;
1014 int retval;
1015 int status;
1016
1017 if (unlikely(noreap)) {
1018 pid_t pid = p->pid;
1019 uid_t uid = p->uid;
1020 int exit_code = p->exit_code;
1021 int why, status;
1022
1023 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1024 return 0;
1025 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1026 return 0;
1027 get_task_struct(p);
1028 read_unlock(&tasklist_lock);
1029 if ((exit_code & 0x7f) == 0) {
1030 why = CLD_EXITED;
1031 status = exit_code >> 8;
1032 } else {
1033 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1034 status = exit_code & 0x7f;
1035 }
1036 return wait_noreap_copyout(p, pid, uid, why,
1037 status, infop, ru);
1038 }
1039
1040 /*
1041 * Try to move the task's state to DEAD
1042 * only one thread is allowed to do this:
1043 */
1044 state = xchg(&p->exit_state, EXIT_DEAD);
1045 if (state != EXIT_ZOMBIE) {
1046 BUG_ON(state != EXIT_DEAD);
1047 return 0;
1048 }
1049 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1050 /*
1051 * This can only happen in a race with a ptraced thread
1052 * dying on another processor.
1053 */
1054 return 0;
1055 }
1056
1057 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1058 /*
1059 * The resource counters for the group leader are in its
1060 * own task_struct. Those for dead threads in the group
1061 * are in its signal_struct, as are those for the child
1062 * processes it has previously reaped. All these
1063 * accumulate in the parent's signal_struct c* fields.
1064 *
1065 * We don't bother to take a lock here to protect these
1066 * p->signal fields, because they are only touched by
1067 * __exit_signal, which runs with tasklist_lock
1068 * write-locked anyway, and so is excluded here. We do
1069 * need to protect the access to p->parent->signal fields,
1070 * as other threads in the parent group can be right
1071 * here reaping other children at the same time.
1072 */
1073 spin_lock_irq(&p->parent->sighand->siglock);
1074 p->parent->signal->cutime =
1075 cputime_add(p->parent->signal->cutime,
1076 cputime_add(p->utime,
1077 cputime_add(p->signal->utime,
1078 p->signal->cutime)));
1079 p->parent->signal->cstime =
1080 cputime_add(p->parent->signal->cstime,
1081 cputime_add(p->stime,
1082 cputime_add(p->signal->stime,
1083 p->signal->cstime)));
1084 p->parent->signal->cmin_flt +=
1085 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1086 p->parent->signal->cmaj_flt +=
1087 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1088 p->parent->signal->cnvcsw +=
1089 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1090 p->parent->signal->cnivcsw +=
1091 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1092 spin_unlock_irq(&p->parent->sighand->siglock);
1093 }
1094
1095 /*
1096 * Now we are sure this task is interesting, and no other
1097 * thread can reap it because we set its state to EXIT_DEAD.
1098 */
1099 read_unlock(&tasklist_lock);
1100
1101 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1102 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1103 ? p->signal->group_exit_code : p->exit_code;
1104 if (!retval && stat_addr)
1105 retval = put_user(status, stat_addr);
1106 if (!retval && infop)
1107 retval = put_user(SIGCHLD, &infop->si_signo);
1108 if (!retval && infop)
1109 retval = put_user(0, &infop->si_errno);
1110 if (!retval && infop) {
1111 int why;
1112
1113 if ((status & 0x7f) == 0) {
1114 why = CLD_EXITED;
1115 status >>= 8;
1116 } else {
1117 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1118 status &= 0x7f;
1119 }
1120 retval = put_user((short)why, &infop->si_code);
1121 if (!retval)
1122 retval = put_user(status, &infop->si_status);
1123 }
1124 if (!retval && infop)
1125 retval = put_user(p->pid, &infop->si_pid);
1126 if (!retval && infop)
1127 retval = put_user(p->uid, &infop->si_uid);
1128 if (retval) {
1129 // TODO: is this safe?
1130 p->exit_state = EXIT_ZOMBIE;
1131 return retval;
1132 }
1133 retval = p->pid;
1134 if (p->real_parent != p->parent) {
1135 write_lock_irq(&tasklist_lock);
1136 /* Double-check with lock held. */
1137 if (p->real_parent != p->parent) {
1138 __ptrace_unlink(p);
1139 // TODO: is this safe?
1140 p->exit_state = EXIT_ZOMBIE;
1141 /*
1142 * If this is not a detached task, notify the parent.
1143 * If it's still not detached after that, don't release
1144 * it now.
1145 */
1146 if (p->exit_signal != -1) {
1147 do_notify_parent(p, p->exit_signal);
1148 if (p->exit_signal != -1)
1149 p = NULL;
1150 }
1151 }
1152 write_unlock_irq(&tasklist_lock);
1153 }
1154 if (p != NULL)
1155 release_task(p);
1156 BUG_ON(!retval);
1157 return retval;
1158}
1159
1160/*
1161 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1162 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1163 * the lock and this task is uninteresting. If we return nonzero, we have
1164 * released the lock and the system call should return.
1165 */
1166static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1167 struct siginfo __user *infop,
1168 int __user *stat_addr, struct rusage __user *ru)
1169{
1170 int retval, exit_code;
1171
1172 if (!p->exit_code)
1173 return 0;
1174 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1175 p->signal && p->signal->group_stop_count > 0)
1176 /*
1177 * A group stop is in progress and this is the group leader.
1178 * We won't report until all threads have stopped.
1179 */
1180 return 0;
1181
1182 /*
1183 * Now we are pretty sure this task is interesting.
1184 * Make sure it doesn't get reaped out from under us while we
1185 * give up the lock and then examine it below. We don't want to
1186 * keep holding onto the tasklist_lock while we call getrusage and
1187 * possibly take page faults for user memory.
1188 */
1189 get_task_struct(p);
1190 read_unlock(&tasklist_lock);
1191
1192 if (unlikely(noreap)) {
1193 pid_t pid = p->pid;
1194 uid_t uid = p->uid;
1195 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1196
1197 exit_code = p->exit_code;
1198 if (unlikely(!exit_code) ||
1199 unlikely(p->state > TASK_STOPPED))
1200 goto bail_ref;
1201 return wait_noreap_copyout(p, pid, uid,
1202 why, (exit_code << 8) | 0x7f,
1203 infop, ru);
1204 }
1205
1206 write_lock_irq(&tasklist_lock);
1207
1208 /*
1209 * This uses xchg to be atomic with the thread resuming and setting
1210 * it. It must also be done with the write lock held to prevent a
1211 * race with the EXIT_ZOMBIE case.
1212 */
1213 exit_code = xchg(&p->exit_code, 0);
1214 if (unlikely(p->exit_state)) {
1215 /*
1216 * The task resumed and then died. Let the next iteration
1217 * catch it in EXIT_ZOMBIE. Note that exit_code might
1218 * already be zero here if it resumed and did _exit(0).
1219 * The task itself is dead and won't touch exit_code again;
1220 * other processors in this function are locked out.
1221 */
1222 p->exit_code = exit_code;
1223 exit_code = 0;
1224 }
1225 if (unlikely(exit_code == 0)) {
1226 /*
1227 * Another thread in this function got to it first, or it
1228 * resumed, or it resumed and then died.
1229 */
1230 write_unlock_irq(&tasklist_lock);
1231bail_ref:
1232 put_task_struct(p);
1233 /*
1234 * We are returning to the wait loop without having successfully
1235 * removed the process and having released the lock. We cannot
1236 * continue, since the "p" task pointer is potentially stale.
1237 *
1238 * Return -EAGAIN, and do_wait() will restart the loop from the
1239 * beginning. Do _not_ re-acquire the lock.
1240 */
1241 return -EAGAIN;
1242 }
1243
1244 /* move to end of parent's list to avoid starvation */
1245 remove_parent(p);
1246 add_parent(p, p->parent);
1247
1248 write_unlock_irq(&tasklist_lock);
1249
1250 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1251 if (!retval && stat_addr)
1252 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1253 if (!retval && infop)
1254 retval = put_user(SIGCHLD, &infop->si_signo);
1255 if (!retval && infop)
1256 retval = put_user(0, &infop->si_errno);
1257 if (!retval && infop)
1258 retval = put_user((short)((p->ptrace & PT_PTRACED)
1259 ? CLD_TRAPPED : CLD_STOPPED),
1260 &infop->si_code);
1261 if (!retval && infop)
1262 retval = put_user(exit_code, &infop->si_status);
1263 if (!retval && infop)
1264 retval = put_user(p->pid, &infop->si_pid);
1265 if (!retval && infop)
1266 retval = put_user(p->uid, &infop->si_uid);
1267 if (!retval)
1268 retval = p->pid;
1269 put_task_struct(p);
1270
1271 BUG_ON(!retval);
1272 return retval;
1273}
1274
1275/*
1276 * Handle do_wait work for one task in a live, non-stopped state.
1277 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1278 * the lock and this task is uninteresting. If we return nonzero, we have
1279 * released the lock and the system call should return.
1280 */
1281static int wait_task_continued(task_t *p, int noreap,
1282 struct siginfo __user *infop,
1283 int __user *stat_addr, struct rusage __user *ru)
1284{
1285 int retval;
1286 pid_t pid;
1287 uid_t uid;
1288
1289 if (unlikely(!p->signal))
1290 return 0;
1291
1292 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1293 return 0;
1294
1295 spin_lock_irq(&p->sighand->siglock);
1296 /* Re-check with the lock held. */
1297 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1298 spin_unlock_irq(&p->sighand->siglock);
1299 return 0;
1300 }
1301 if (!noreap)
1302 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1303 spin_unlock_irq(&p->sighand->siglock);
1304
1305 pid = p->pid;
1306 uid = p->uid;
1307 get_task_struct(p);
1308 read_unlock(&tasklist_lock);
1309
1310 if (!infop) {
1311 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1312 put_task_struct(p);
1313 if (!retval && stat_addr)
1314 retval = put_user(0xffff, stat_addr);
1315 if (!retval)
1316 retval = p->pid;
1317 } else {
1318 retval = wait_noreap_copyout(p, pid, uid,
1319 CLD_CONTINUED, SIGCONT,
1320 infop, ru);
1321 BUG_ON(retval == 0);
1322 }
1323
1324 return retval;
1325}
1326
1327
1328static inline int my_ptrace_child(struct task_struct *p)
1329{
1330 if (!(p->ptrace & PT_PTRACED))
1331 return 0;
1332 if (!(p->ptrace & PT_ATTACHED))
1333 return 1;
1334 /*
1335 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1336 * we are the attacher. If we are the real parent, this is a race
1337 * inside ptrace_attach. It is waiting for the tasklist_lock,
1338 * which we have to switch the parent links, but has already set
1339 * the flags in p->ptrace.
1340 */
1341 return (p->parent != p->real_parent);
1342}
1343
1344static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1345 int __user *stat_addr, struct rusage __user *ru)
1346{
1347 DECLARE_WAITQUEUE(wait, current);
1348 struct task_struct *tsk;
1349 int flag, retval;
1350
1351 add_wait_queue(&current->signal->wait_chldexit,&wait);
1352repeat:
1353 /*
1354 * We will set this flag if we see any child that might later
1355 * match our criteria, even if we are not able to reap it yet.
1356 */
1357 flag = 0;
1358 current->state = TASK_INTERRUPTIBLE;
1359 read_lock(&tasklist_lock);
1360 tsk = current;
1361 do {
1362 struct task_struct *p;
1363 struct list_head *_p;
1364 int ret;
1365
1366 list_for_each(_p,&tsk->children) {
1367 p = list_entry(_p,struct task_struct,sibling);
1368
1369 ret = eligible_child(pid, options, p);
1370 if (!ret)
1371 continue;
1372
1373 switch (p->state) {
1374 case TASK_TRACED:
1375 if (!my_ptrace_child(p))
1376 continue;
1377 /*FALLTHROUGH*/
1378 case TASK_STOPPED:
1379 /*
1380 * It's stopped now, so it might later
1381 * continue, exit, or stop again.
1382 */
1383 flag = 1;
1384 if (!(options & WUNTRACED) &&
1385 !my_ptrace_child(p))
1386 continue;
1387 retval = wait_task_stopped(p, ret == 2,
1388 (options & WNOWAIT),
1389 infop,
1390 stat_addr, ru);
1391 if (retval == -EAGAIN)
1392 goto repeat;
1393 if (retval != 0) /* He released the lock. */
1394 goto end;
1395 break;
1396 default:
1397 // case EXIT_DEAD:
1398 if (p->exit_state == EXIT_DEAD)
1399 continue;
1400 // case EXIT_ZOMBIE:
1401 if (p->exit_state == EXIT_ZOMBIE) {
1402 /*
1403 * Eligible but we cannot release
1404 * it yet:
1405 */
1406 if (ret == 2)
1407 goto check_continued;
1408 if (!likely(options & WEXITED))
1409 continue;
1410 retval = wait_task_zombie(
1411 p, (options & WNOWAIT),
1412 infop, stat_addr, ru);
1413 /* He released the lock. */
1414 if (retval != 0)
1415 goto end;
1416 break;
1417 }
1418check_continued:
1419 /*
1420 * It's running now, so it might later
1421 * exit, stop, or stop and then continue.
1422 */
1423 flag = 1;
1424 if (!unlikely(options & WCONTINUED))
1425 continue;
1426 retval = wait_task_continued(
1427 p, (options & WNOWAIT),
1428 infop, stat_addr, ru);
1429 if (retval != 0) /* He released the lock. */
1430 goto end;
1431 break;
1432 }
1433 }
1434 if (!flag) {
1435 list_for_each(_p, &tsk->ptrace_children) {
1436 p = list_entry(_p, struct task_struct,
1437 ptrace_list);
1438 if (!eligible_child(pid, options, p))
1439 continue;
1440 flag = 1;
1441 break;
1442 }
1443 }
1444 if (options & __WNOTHREAD)
1445 break;
1446 tsk = next_thread(tsk);
1447 if (tsk->signal != current->signal)
1448 BUG();
1449 } while (tsk != current);
1450
1451 read_unlock(&tasklist_lock);
1452 if (flag) {
1453 retval = 0;
1454 if (options & WNOHANG)
1455 goto end;
1456 retval = -ERESTARTSYS;
1457 if (signal_pending(current))
1458 goto end;
1459 schedule();
1460 goto repeat;
1461 }
1462 retval = -ECHILD;
1463end:
1464 current->state = TASK_RUNNING;
1465 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1466 if (infop) {
1467 if (retval > 0)
1468 retval = 0;
1469 else {
1470 /*
1471 * For a WNOHANG return, clear out all the fields
1472 * we would set so the user can easily tell the
1473 * difference.
1474 */
1475 if (!retval)
1476 retval = put_user(0, &infop->si_signo);
1477 if (!retval)
1478 retval = put_user(0, &infop->si_errno);
1479 if (!retval)
1480 retval = put_user(0, &infop->si_code);
1481 if (!retval)
1482 retval = put_user(0, &infop->si_pid);
1483 if (!retval)
1484 retval = put_user(0, &infop->si_uid);
1485 if (!retval)
1486 retval = put_user(0, &infop->si_status);
1487 }
1488 }
1489 return retval;
1490}
1491
1492asmlinkage long sys_waitid(int which, pid_t pid,
1493 struct siginfo __user *infop, int options,
1494 struct rusage __user *ru)
1495{
1496 long ret;
1497
1498 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1499 return -EINVAL;
1500 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1501 return -EINVAL;
1502
1503 switch (which) {
1504 case P_ALL:
1505 pid = -1;
1506 break;
1507 case P_PID:
1508 if (pid <= 0)
1509 return -EINVAL;
1510 break;
1511 case P_PGID:
1512 if (pid <= 0)
1513 return -EINVAL;
1514 pid = -pid;
1515 break;
1516 default:
1517 return -EINVAL;
1518 }
1519
1520 ret = do_wait(pid, options, infop, NULL, ru);
1521
1522 /* avoid REGPARM breakage on x86: */
1523 prevent_tail_call(ret);
1524 return ret;
1525}
1526
1527asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1528 int options, struct rusage __user *ru)
1529{
1530 long ret;
1531
1532 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1533 __WNOTHREAD|__WCLONE|__WALL))
1534 return -EINVAL;
1535 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1536
1537 /* avoid REGPARM breakage on x86: */
1538 prevent_tail_call(ret);
1539 return ret;
1540}
1541
1542#ifdef __ARCH_WANT_SYS_WAITPID
1543
1544/*
1545 * sys_waitpid() remains for compatibility. waitpid() should be
1546 * implemented by calling sys_wait4() from libc.a.
1547 */
1548asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1549{
1550 return sys_wait4(pid, stat_addr, options, NULL);
1551}
1552
1553#endif