Skip to content

Commit

Permalink
Exit when child process terminates, mimic strace
Browse files Browse the repository at this point in the history
  • Loading branch information
i-ky committed Jun 22, 2024
1 parent 9ee404b commit 496f255
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/basset.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <fcntl.h>
#include <regex.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

Expand Down Expand Up @@ -291,13 +292,13 @@ int main(int argc, char *argv[]) {

Pipe pipe;

if (auto pid = fork()) {
if (pid == -1) {
if (auto main_pid = fork()) {
if (main_pid == -1) {
perror("cannot fork()");
return -1;
}

if (ptrace(PTRACE_SEIZE, pid, nullptr,
if (ptrace(PTRACE_SEIZE, main_pid, nullptr,
PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK |
PTRACE_O_TRACEEXEC | PTRACE_O_EXITKILL) == -1) {
perror("cannot ptrace(PTRACE_SEIZE)");
Expand Down Expand Up @@ -327,6 +328,21 @@ int main(int argc, char *argv[]) {

if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus)) {
verbose &&cerr << pid << " exited/terminated by signal\n";

if (pid == main_pid) {
if (WIFEXITED(wstatus)) {
return WEXITSTATUS(wstatus);
}

if (raise(WTERMSIG(wstatus)) != 0) {
perror("cannot raise()");
return -1;
}

// for some reason the signal terminated the child, but did not kill
// the parent...
return -1;
}
} else if (WIFSTOPPED(wstatus)) {
verbose &&cerr << pid << " stopped\n";

Expand Down

0 comments on commit 496f255

Please sign in to comment.