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