Process monitor minor tweaks

This commit is contained in:
topjohnwu 2019-05-26 02:35:12 -07:00
parent 23d3e56967
commit 92400ebcab

View File

@ -159,8 +159,8 @@ static void setup_inotify() {
// Setup inotify asynchronous I/O
fcntl(inotify_fd, F_SETFL, O_ASYNC);
struct f_owner_ex ex = {
.type = F_OWNER_TID,
.pid = gettid()
.type = F_OWNER_TID,
.pid = gettid()
};
fcntl(inotify_fd, F_SETOWN_EX, &ex);
@ -285,6 +285,7 @@ static void term_thread(int) {
static void detach_pid(int pid, int signal = 0) {
char path[128];
attaches[pid] = false;
xptrace(PTRACE_DETACH, pid, nullptr, signal);
// Detach all child threads too
@ -318,10 +319,6 @@ static bool check_pid(int pid) {
if (strncmp(cmdline, "zygote", 6) == 0)
return false;
/* This process is fully initialized, we will stop
* tracing it no matter if it is a target or not. */
attaches[pid] = false;
sprintf(path, "/proc/%d", pid);
struct stat st;
lstat(path, &st);
@ -415,11 +412,11 @@ void proc_monitor() {
/* This mean we have nothing to wait, sleep
* and wait till signal interruption */
LOGD("proc_monitor: nothing to monitor, wait for signal\n");
struct timespec timespec = {
struct timespec ts = {
.tv_sec = INT_MAX,
.tv_nsec = 0
};
nanosleep(&timespec, nullptr);
nanosleep(&ts, nullptr);
}
continue;
}
@ -430,12 +427,13 @@ void proc_monitor() {
attaches[pid] = false;
detaches[pid] = false;
ptrace(PTRACE_DETACH, pid, 0, 0);
PTRACE_LOG("detach\n");
}
});
if (!WIFSTOPPED(status) || detaches[pid]) {
PTRACE_LOG("detached\n");
if (!WIFSTOPPED(status) /* Ignore if not ptrace-stop */ || detaches[pid])
DETACH_AND_CONT;
}
if (WSTOPSIG(status) == SIGTRAP && WEVENT(status)) {
unsigned long msg;
xptrace(PTRACE_GETEVENTMSG, pid, nullptr, &msg);
@ -449,13 +447,11 @@ void proc_monitor() {
break;
case PTRACE_EVENT_EXIT:
PTRACE_LOG("zygote exited with status: [%d]\n", msg);
[[fallthrough]];
default:
zygote_map.erase(pid);
DETACH_AND_CONT;
default:
PTRACE_LOG("unknown event: %d\n", WEVENT(status));
break;
}
xptrace(PTRACE_CONT, pid);
} else {
switch (WEVENT(status)) {
case PTRACE_EVENT_CLONE:
@ -465,14 +461,13 @@ void proc_monitor() {
break;
case PTRACE_EVENT_EXEC:
case PTRACE_EVENT_EXIT:
PTRACE_LOG("exited or execve\n");
DETACH_AND_CONT;
PTRACE_LOG("exit or execve\n");
[[fallthrough]];
default:
PTRACE_LOG("unknown event: %d\n", WEVENT(status));
break;
DETACH_AND_CONT;
}
xptrace(PTRACE_CONT, pid);
}
xptrace(PTRACE_CONT, pid);
} else if (WSTOPSIG(status) == SIGSTOP) {
PTRACE_LOG("SIGSTOP from child\n");
xptrace(PTRACE_SETOPTIONS, pid, nullptr,