Skip to content

Commit

Permalink
truss(1): detach more carefully
Browse files Browse the repository at this point in the history
(cherry picked from commit 12f747e)
  • Loading branch information
kostikbel committed Jan 19, 2022
1 parent 7545bb2 commit 29887e6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions usr.bin/truss/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,24 @@ restore_proc(int signo __unused)
static void
detach_proc(pid_t pid)
{
int sig, status;

/* stop the child so that we can detach */
/*
* Stop the child so that we can detach. Filter out possible
* lingering SIGTRAP events buffered in the threads.
*/
kill(pid, SIGSTOP);
if (waitpid(pid, NULL, 0) < 0)
err(1, "Unexpected stop in waitpid");
for (;;) {
if (waitpid(pid, &status, 0) < 0)
err(1, "Unexpected error in waitpid");
sig = WIFSTOPPED(status) ? WSTOPSIG(status) : 0;
if (sig == SIGSTOP)
break;
if (sig == SIGTRAP)
sig = 0;
if (ptrace(PT_CONTINUE, pid, (caddr_t)1, sig) < 0)
err(1, "Can not continue for detach");
}

if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
err(1, "Can not detach the process");
Expand Down

0 comments on commit 29887e6

Please sign in to comment.