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