do_wait: fix waiting for the group stop with the dead leader

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Linux Kernel Mailing List
Date: Thursday, April 2, 2009 - 10:01 pm

Gitweb:     http://git.kernel.org/linus/90bc8d8b1a38f1ab131a2399a202e1889db95de8
Commit:     90bc8d8b1a38f1ab131a2399a202e1889db95de8
Parent:     6d7b2f5f9e88902b19f91d0c8a7ef58a5455f1a2
Author:     Oleg Nesterov <oleg@redhat.com>
AuthorDate: Thu Apr 2 16:57:58 2009 -0700
Committer:  Linus Torvalds <torvalds@linux-foundation.org>
CommitDate: Thu Apr 2 19:04:57 2009 -0700

    do_wait: fix waiting for the group stop with the dead leader
    
    do_wait(WSTOPPED) assumes that p->state must be == TASK_STOPPED, this is
    not true if the leader is already dead.  Check SIGNAL_STOP_STOPPED instead
    and use signal->group_exit_code.
    
    Trivial test-case:
    
    	void *tfunc(void *arg)
    	{
    		pause();
    		return NULL;
    	}
    
    	int main(void)
    	{
    		pthread_t thr;
    		pthread_create(&thr, NULL, tfunc, NULL);
    		pthread_exit(NULL);
    		return 0;
    	}
    
    It doesn't react to ^Z (and then to ^C or ^\). The task is stopped, but
    bash can't see this.
    
    The bug is very old, and it was reported multiple times. This patch was sent
    more than a year ago (http://marc.info/?t=119713920000003) but it was ignored.
    
    This change also fixes other oddities (but not all) in this area.  For
    example, before this patch:
    
    	$ sleep 100
    	^Z
    	[1]+  Stopped                 sleep 100
    	$ strace -p `pidof sleep`
    	Process 11442 attached - interrupt to quit
    
    strace hangs in do_wait(), because ->exit_code was already consumed by
    bash.  After this patch, strace happily proceeds:
    
    	--- SIGTSTP (Stopped) @ 0 (0) ---
    	restart_syscall(<... resuming interrupted call ...>
    
    To me, this looks much more "natural" and correct.
    
    Another example.  Let's suppose we have the main thread M and sub-thread
    T, the process is stopped, and its parent did wait(WSTOPPED).  Now we can
    ptrace T but not M.  This looks at least strange to me.
    
    Imho, do_wait() should not confuse the per-thread ptrace stops with the
    per-process job control stops.
    
    Signed-off-by: Oleg Nesterov <oleg@redhat.com>
    Cc: Denys Vlasenko <dvlasenk@redhat.com>
    Cc: "Eric W. Biederman" <ebiederm@xmission.com>
    Cc: Jan Kratochvil <jan.kratochvil@redhat.com>
    Cc: Kaz Kylheku <kkylheku@gmail.com>
    Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
    Cc: Roland McGrath <roland@redhat.com>
    Cc: Ulrich Drepper <drepper@redhat.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---
 kernel/exit.c |   30 ++++++++++++++++++------------
 1 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/kernel/exit.c b/kernel/exit.c
index 167e1e3..0c06b9e 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -1417,6 +1417,18 @@ static int wait_task_zombie(struct task_struct *p, int options,
 	return retval;
 }
 
+static int *task_stopped_code(struct task_struct *p, bool ptrace)
+{
+	if (ptrace) {
+		if (task_is_stopped_or_traced(p))
+			return &p->exit_code;
+	} else {
+		if (p->signal->flags & SIGNAL_STOP_STOPPED)
+			return &p->signal->group_exit_code;
+	}
+	return NULL;
+}
+
 /*
  * Handle sys_wait4 work for one task in state TASK_STOPPED.  We hold
  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
@@ -1427,7 +1439,7 @@ static int wait_task_stopped(int ptrace, struct task_struct *p,
 			     int options, struct siginfo __user *infop,
 			     int __user *stat_addr, struct rusage __user *ru)
 {
-	int retval, exit_code, why;
+	int retval, exit_code, *p_code, why;
 	uid_t uid = 0; /* unneeded, required by compiler */
 	pid_t pid;
 
@@ -1437,22 +1449,16 @@ static int wait_task_stopped(int ptrace, struct task_struct *p,
 	exit_code = 0;
 	spin_lock_irq(&p->sighand->siglock);
 
-	if (unlikely(!task_is_stopped_or_traced(p)))
-		goto unlock_sig;
-
-	if (!ptrace && p->signal->group_stop_count > 0)
-		/*
-		 * A group stop is in progress and this is the group leader.
-		 * We won't report until all threads have stopped.
-		 */
+	p_code = task_stopped_code(p, ptrace);
+	if (unlikely(!p_code))
 		goto unlock_sig;
 
-	exit_code = p->exit_code;
+	exit_code = *p_code;
 	if (!exit_code)
 		goto unlock_sig;
 
 	if (!unlikely(options & WNOWAIT))
-		p->exit_code = 0;
+		*p_code = 0;
 
 	/* don't need the RCU readlock here as we're holding a spinlock */
 	uid = __task_cred(p)->uid;
@@ -1608,7 +1614,7 @@ static int wait_consider_task(struct task_struct *parent, int ptrace,
 	 */
 	*notask_error = 0;
 
-	if (task_is_stopped_or_traced(p))
+	if (task_stopped_code(p, ptrace))
 		return wait_task_stopped(ptrace, p, options,
 					 infop, stat_addr, ru);
 
--
To unsubscribe from this list: send the line "unsubscribe git-commits-head" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
do_wait: fix waiting for the group stop with the dead leader, Linux Kernel Mailing ..., (Thu Apr 2, 10:01 pm)