move the related code from exit_notify() to exit_signals()
[safe/jmp/linux-2.6] / kernel / exit.c
index 9ee229e..2b332d1 100644 (file)
@@ -293,26 +293,27 @@ static void reparent_to_kthreadd(void)
        switch_uid(INIT_USER);
 }
 
-void __set_special_pids(pid_t session, pid_t pgrp)
+void __set_special_pids(struct pid *pid)
 {
        struct task_struct *curr = current->group_leader;
+       pid_t nr = pid_nr(pid);
 
-       if (task_session_nr(curr) != session) {
+       if (task_session(curr) != pid) {
                detach_pid(curr, PIDTYPE_SID);
-               set_task_session(curr, session);
-               attach_pid(curr, PIDTYPE_SID, find_pid(session));
+               attach_pid(curr, PIDTYPE_SID, pid);
+               set_task_session(curr, nr);
        }
-       if (task_pgrp_nr(curr) != pgrp) {
+       if (task_pgrp(curr) != pid) {
                detach_pid(curr, PIDTYPE_PGID);
-               set_task_pgrp(curr, pgrp);
-               attach_pid(curr, PIDTYPE_PGID, find_pid(pgrp));
+               attach_pid(curr, PIDTYPE_PGID, pid);
+               set_task_pgrp(curr, nr);
        }
 }
 
-static void set_special_pids(pid_t session, pid_t pgrp)
+static void set_special_pids(struct pid *pid)
 {
        write_lock_irq(&tasklist_lock);
-       __set_special_pids(session, pgrp);
+       __set_special_pids(pid);
        write_unlock_irq(&tasklist_lock);
 }
 
@@ -383,7 +384,11 @@ void daemonize(const char *name, ...)
         */
        current->flags |= PF_NOFREEZE;
 
-       set_special_pids(1, 1);
+       if (current->nsproxy != &init_nsproxy) {
+               get_nsproxy(&init_nsproxy);
+               switch_task_namespaces(current, &init_nsproxy);
+       }
+       set_special_pids(&init_struct_pid);
        proc_clear_tty(current);
 
        /* Block and flush all signals */
@@ -398,11 +403,6 @@ void daemonize(const char *name, ...)
        current->fs = fs;
        atomic_inc(&fs->count);
 
-       if (current->nsproxy != init_task.nsproxy) {
-               get_nsproxy(init_task.nsproxy);
-               switch_task_namespaces(current, init_task.nsproxy);
-       }
-
        exit_files(current);
        current->files = init_task.files;
        atomic_inc(&current->files->count);
@@ -745,24 +745,6 @@ static void exit_notify(struct task_struct *tsk)
        struct task_struct *t;
        struct pid *pgrp;
 
-       if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
-           && !thread_group_empty(tsk)) {
-               /*
-                * This occurs when there was a race between our exit
-                * syscall and a group signal choosing us as the one to
-                * wake up.  It could be that we are the only thread
-                * alerted to check for pending signals, but another thread
-                * should be woken now to take the signal since we will not.
-                * Now we'll wake all the threads in the group just to make
-                * sure someone gets all the pending signals.
-                */
-               spin_lock_irq(&tsk->sighand->siglock);
-               for (t = next_thread(tsk); t != tsk; t = next_thread(t))
-                       if (!signal_pending(t) && !(t->flags & PF_EXITING))
-                               recalc_sigpending_and_wake(t);
-               spin_unlock_irq(&tsk->sighand->siglock);
-       }
-
        /*
         * This does two things:
         *
@@ -947,7 +929,7 @@ fastcall NORET_TYPE void do_exit(long code)
                schedule();
        }
 
-       tsk->flags |= PF_EXITING;
+       exit_signals(tsk);  /* sets PF_EXITING */
        /*
         * tsk->flags are checked in the futex code to protect against
         * an exiting task cleaning up the robust pi futexes.
@@ -1142,10 +1124,14 @@ static int eligible_child(pid_t pid, int options, struct task_struct *p)
                return 0;
 
        err = security_task_wait(p);
-       if (err)
-               return err;
+       if (likely(!err))
+               return 1;
 
-       return 1;
+       if (pid <= 0)
+               return 0;
+       /* This child was explicitly requested, abort */
+       read_unlock(&tasklist_lock);
+       return err;
 }
 
 static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
@@ -1185,20 +1171,13 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
 {
        unsigned long state;
        int retval, status, traced;
-       struct pid_namespace *ns;
-
-       ns = current->nsproxy->pid_ns;
+       pid_t pid = task_pid_nr_ns(p, current->nsproxy->pid_ns);
 
        if (unlikely(noreap)) {
-               pid_t pid = task_pid_nr_ns(p, ns);
                uid_t uid = p->uid;
                int exit_code = p->exit_code;
                int why, status;
 
-               if (unlikely(p->exit_state != EXIT_ZOMBIE))
-                       return 0;
-               if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
-                       return 0;
                get_task_struct(p);
                read_unlock(&tasklist_lock);
                if ((exit_code & 0x7f) == 0) {
@@ -1309,11 +1288,11 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
                        retval = put_user(status, &infop->si_status);
        }
        if (!retval && infop)
-               retval = put_user(task_pid_nr_ns(p, ns), &infop->si_pid);
+               retval = put_user(pid, &infop->si_pid);
        if (!retval && infop)
                retval = put_user(p->uid, &infop->si_uid);
        if (!retval)
-               retval = task_pid_nr_ns(p, ns);
+               retval = pid;
 
        if (traced) {
                write_lock_irq(&tasklist_lock);
@@ -1432,7 +1411,6 @@ static int wait_task_continued(struct task_struct *p, int noreap,
        int retval;
        pid_t pid;
        uid_t uid;
-       struct pid_namespace *ns;
 
        if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
                return 0;
@@ -1447,8 +1425,7 @@ static int wait_task_continued(struct task_struct *p, int noreap,
                p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
        spin_unlock_irq(&p->sighand->siglock);
 
-       ns = current->nsproxy->pid_ns;
-       pid = task_pid_nr_ns(p, ns);
+       pid = task_pid_nr_ns(p, current->nsproxy->pid_ns);
        uid = p->uid;
        get_task_struct(p);
        read_unlock(&tasklist_lock);
@@ -1459,7 +1436,7 @@ static int wait_task_continued(struct task_struct *p, int noreap,
                if (!retval && stat_addr)
                        retval = put_user(0xffff, stat_addr);
                if (!retval)
-                       retval = task_pid_nr_ns(p, ns);
+                       retval = pid;
        } else {
                retval = wait_noreap_copyout(p, pid, uid,
                                             CLD_CONTINUED, SIGCONT,
@@ -1476,7 +1453,6 @@ static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
        DECLARE_WAITQUEUE(wait, current);
        struct task_struct *tsk;
        int flag, retval;
-       int allowed, denied;
 
        add_wait_queue(&current->signal->wait_chldexit,&wait);
 repeat:
@@ -1484,8 +1460,7 @@ repeat:
         * We will set this flag if we see any child that might later
         * match our criteria, even if we are not able to reap it yet.
         */
-       flag = 0;
-       allowed = denied = 0;
+       flag = retval = 0;
        current->state = TASK_INTERRUPTIBLE;
        read_lock(&tasklist_lock);
        tsk = current;
@@ -1498,13 +1473,8 @@ repeat:
                                continue;
 
                        if (unlikely(ret < 0)) {
-                               denied = ret;
-                               continue;
-                       }
-                       allowed = 1;
-
-                       retval = 0;
-                       if (task_is_stopped_or_traced(p)) {
+                               retval = ret;
+                       } else if (task_is_stopped_or_traced(p)) {
                                /*
                                 * It's stopped now, so it might later
                                 * continue, exit, or stop again.
@@ -1544,11 +1514,14 @@ repeat:
                }
                if (!flag) {
                        list_for_each_entry(p, &tsk->ptrace_children,
-                                           ptrace_list) {
-                               if (!eligible_child(pid, options, p))
+                                                               ptrace_list) {
+                               flag = eligible_child(pid, options, p);
+                               if (!flag)
                                        continue;
-                               flag = 1;
-                               break;
+                               if (likely(flag > 0))
+                                       break;
+                               retval = flag;
+                               goto end;
                        }
                }
                if (options & __WNOTHREAD)
@@ -1556,10 +1529,9 @@ repeat:
                tsk = next_thread(tsk);
                BUG_ON(tsk->signal != current->signal);
        } while (tsk != current);
-
        read_unlock(&tasklist_lock);
+
        if (flag) {
-               retval = 0;
                if (options & WNOHANG)
                        goto end;
                retval = -ERESTARTSYS;
@@ -1569,8 +1541,6 @@ repeat:
                goto repeat;
        }
        retval = -ECHILD;
-       if (unlikely(denied) && !allowed)
-               retval = denied;
 end:
        current->state = TASK_RUNNING;
        remove_wait_queue(&current->signal->wait_chldexit,&wait);