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