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