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