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