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