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