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