978e96
commit b04acb2651e0aaf615de50e9138cddfd5c24021f
978e96
Author: Andreas Schwab <schwab@suse.de>
978e96
Date:   Tue Jul 30 11:58:45 2013 +0200
978e96
978e96
    Fix race conditions in pldd that may leave the process stopped after detaching
978e96
    
978e96
    Fixes bug 15804
978e96
978e96
diff --git a/elf/pldd.c b/elf/pldd.c
978e96
index 684aff4..75f7812 100644
978e96
--- a/elf/pldd.c
978e96
+++ b/elf/pldd.c
978e96
@@ -34,6 +34,7 @@
978e96
 #include <unistd.h>
978e96
 #include <sys/ptrace.h>
978e96
 #include <sys/stat.h>
978e96
+#include <sys/wait.h>
978e96
 
978e96
 #include <ldsodefs.h>
978e96
 #include <version.h>
978e96
@@ -82,6 +83,7 @@ static char *exe;
978e96
 
978e96
 /* Local functions.  */
978e96
 static int get_process_info (int dfd, long int pid);
978e96
+static void wait_for_ptrace_stop (long int pid);
978e96
 
978e96
 
978e96
 int
978e96
@@ -170,6 +172,8 @@ main (int argc, char *argv[])
978e96
 		 tid);
978e96
 	}
978e96
 
978e96
+      wait_for_ptrace_stop (tid);
978e96
+
978e96
       struct thread_list *newp = alloca (sizeof (*newp));
978e96
       newp->tid = tid;
978e96
       newp->next = thread_list;
978e96
@@ -194,6 +198,27 @@ main (int argc, char *argv[])
978e96
 }
978e96
 
978e96
 
978e96
+/* Wait for PID to enter ptrace-stop state after being attached.  */
978e96
+static void
978e96
+wait_for_ptrace_stop (long int pid)
978e96
+{
978e96
+  int status;
978e96
+
978e96
+  /* While waiting for SIGSTOP being delivered to the tracee we have to
978e96
+     reinject any other pending signal.  Ignore all other errors.  */
978e96
+  while (waitpid (pid, &status, __WALL) == pid && WIFSTOPPED (status))
978e96
+    {
978e96
+      /* The STOP signal should not be delivered to the tracee.  */
978e96
+      if (WSTOPSIG (status) == SIGSTOP)
978e96
+	return;
978e96
+      if (ptrace (PTRACE_CONT, pid, NULL,
978e96
+		  (void *) (uintptr_t) WSTOPSIG (status)))
978e96
+	/* The only possible error is that the process died.  */
978e96
+	return;
978e96
+    }
978e96
+}
978e96
+
978e96
+
978e96
 /* Handle program arguments.  */
978e96
 static error_t
978e96
 parse_opt (int key, char *arg, struct argp_state *state)