Skip to content

Commit

Permalink
Add wait() loop
Browse files Browse the repository at this point in the history
  • Loading branch information
i-ky committed Aug 25, 2022
1 parent 40e2cab commit a924159
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/basset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <unistd.h>

#include <sys/ptrace.h>
#include <sys/wait.h>

#include <iostream>

using std::cerr;

int main(int argc, char *argv[], char *envp[]) {
argv++;
Expand All @@ -12,7 +17,26 @@ int main(int argc, char *argv[], char *envp[]) {
return -1;
}

// TODO wait()
int wstatus;

while (auto pid = wait(&wstatus)) {
if (pid == -1) {
perror("cannot wait()");
return -1;
}

if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus)) {
cerr << pid << " exited/terminated by signal\n";
} else if (WIFSTOPPED(wstatus)) {
cerr << pid << " stopped\n";

if (ptrace(PTRACE_CONT, pid, nullptr, nullptr) == -1) {
perror("cannot ptrace(PTRACE_CONT)");
}
} else if (WIFCONTINUED(wstatus)) {
cerr << pid << " continued\n";
}
}

return 0;
}
Expand Down

0 comments on commit a924159

Please sign in to comment.