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