Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc_monitor: Support older & newer kernels am_proc_start format #142

Merged
merged 3 commits into from
Mar 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jni/magiskhide/list_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void *monitor_list(void *path) {
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
exit(1);
}
if (inotify_add_watch(inotifyFd, listpath, IN_MODIFY) == -1) {
if (inotify_add_watch(inotifyFd, listpath, IN_CLOSE_WRITE) == -1) {
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
exit(1);
}
Expand Down
36 changes: 30 additions & 6 deletions jni/magiskhide/proc_monitor.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "magiskhide.h"

void monitor_proc() {
int pid, badns, zygote_num = 0;
int pid, badns, i, zygote_num = 0;
char init_ns[32], zygote_ns[2][32];

// Get the mount namespace of init
Expand All @@ -26,12 +26,30 @@ void monitor_proc() {
fprintf(logfile, "Zygote(%d) ns=%s ", i, zygote_ns[i]);
fprintf(logfile, "\n");

// get a sample line from am_proc_start
p = popen("logcat -b events -v raw -s am_proc_start -t 1", "r");

/**
* Format of am_proc_start is (as of Android 5.1 and 6.0)
* UserID, pid, unix uid, processName, hostingType, hostingName
* but sometimes can have 7 fields, with processName as 5th field
*/
fgets(buffer, sizeof(buffer), p);
int commas = 0;
char *s = buffer;
for (i = 0;s[i] != '\0';i++) {
if (s[i] == ',')
commas++;

}
int numFields = commas + 1;

pclose(p);

// Monitor am_proc_start
p = popen("while true; do logcat -b events -c; logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
p = popen("logcat -b events -c; logcat -b events -v raw -s am_proc_start", "r");

while(!feof(p)) {
//Format of am_proc_start is (as of Android 5.1 and 6.0)
//UserID, pid, unix uid, processName, hostingType, hostingName
fgets(buffer, sizeof(buffer), p);

char *pos = buffer;
Expand All @@ -43,7 +61,13 @@ void monitor_proc() {
}

char processName[256];
int ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName);
int ret;

if (numFields == 7) {
ret = sscanf(buffer, "[%*d %d %*d %*d %256s", &pid, processName);
} else {
ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName);
}

if(ret != 2)
continue;
Expand Down Expand Up @@ -79,4 +103,4 @@ void monitor_proc() {

// Close the logcat monitor
pclose(p);
}
}