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