This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix pldd not to leave process stopped after detaching

When attaching to a process we always have to wait for the tracee to
enter ptrace-stop state.  Otherwise this can result in a race where the
STOP signal is reveived after we have already detached from it again,
leaving the process in stopped state.


	[BZ #15804]
	* elf/pldd.c (wait_for_ptrace_stop): New function.
	(main): Call it after attaching.

diff --git a/elf/pldd.c b/elf/pldd.c
index d9388a1..b534aa8 100644
--- a/elf/pldd.c
+++ b/elf/pldd.c
@@ -34,6 +34,7 @@
 #include <unistd.h>
 #include <sys/ptrace.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <ldsodefs.h>
 #include <version.h>
@@ -82,6 +83,7 @@ static char *exe;
 /* Local functions.  */
 static int get_process_info (int dfd, long int pid);
+static void wait_for_ptrace_stop (long int pid);
@@ -170,6 +172,8 @@ main (int argc, char *argv[])
+      wait_for_ptrace_stop (tid);
       struct thread_list *newp = alloca (sizeof (*newp));
       newp->tid = tid;
       newp->next = thread_list;
@@ -194,6 +198,24 @@ main (int argc, char *argv[])
+/* Wait for PID to enter ptrace-stop state after being attached.  */
+static void
+wait_for_ptrace_stop (long int pid)
+  int status;
+  /* While waiting for SIGSTOP being delivered to the tracee we have to
+     reinject any other pending signal.  Ignore all other errors.  */
+  while (waitpid (pid, &status, __WALL) == pid && WIFSTOPPED (status))
+    {
+      /* The STOP signal should not be delivered to the tracee.  */
+      if (WSTOPSIG (status) == SIGSTOP)
+	return;
+      ptrace (PTRACE_CONT, pid, NULL, (void *) (uintptr_t) WSTOPSIG (status));
+    }
 /* Handle program arguments.  */
 static error_t
 parse_opt (int key, char *arg, struct argp_state *state)

Andreas Schwab, SUSE Labs,
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]