Magisk/jni/magiskhide/proc_monitor.c

107 lines
2.3 KiB
C
Raw Normal View History

2016-12-31 02:44:24 +08:00
#include "magiskhide.h"
void monitor_proc() {
int pid, badns, i, zygote_num = 0;
2017-01-01 18:54:13 +08:00
char init_ns[32], zygote_ns[2][32];
// Get the mount namespace of init
read_namespace(1, init_ns, 32);
2017-01-01 20:31:08 +08:00
printf("%s\n", init_ns);
2017-01-01 18:54:13 +08:00
// Get the mount namespace of zygote
FILE *p = popen("ps | grep zygote | grep -v grep", "r");
2017-01-01 18:54:13 +08:00
while(fgets(buffer, sizeof(buffer), p)) {
if (zygote_num == 2) break;
sscanf(buffer, "%d", &pid);
do {
usleep(500);
read_namespace(pid, zygote_ns[zygote_num], 32);
} while (strcmp(zygote_ns[zygote_num], init_ns) == 0);
++zygote_num;
}
pclose(p);
for (i = 0; i < zygote_num; ++i)
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);
2017-01-01 18:54:13 +08:00
// Monitor am_proc_start
p = popen("logcat -b events -c; logcat -b events -v raw -s am_proc_start", "r");
2016-12-31 02:44:24 +08:00
while(!feof(p)) {
fgets(buffer, sizeof(buffer), p);
char *pos = buffer;
while(1) {
pos = strchr(pos, ',');
if(pos == NULL)
break;
pos[0] = ' ';
}
char processName[256];
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);
}
2016-12-31 02:44:24 +08:00
if(ret != 2)
continue;
2017-01-01 18:54:13 +08:00
pthread_mutex_lock(&mutex);
2016-12-31 02:44:24 +08:00
for (i = 0; i < list_size; ++i) {
if(strcmp(processName, hide_list[i]) == 0) {
2017-01-01 18:54:13 +08:00
while(1) {
badns = 0;
read_namespace(pid, buffer, 32);
for (i = 0; i < zygote_num; ++i) {
if (strcmp(buffer, zygote_ns[i]) == 0) {
usleep(500);
badns = 1;
break;
}
}
if (!badns) break;
}
// Send pause signal ASAP
if (kill(pid, SIGSTOP) == -1) continue;
fprintf(logfile, "MagiskHide: %s(PID=%d ns=%s)\n", processName, pid, buffer);
// Unmount start
2016-12-31 02:44:24 +08:00
write(pipefd[1], &pid, sizeof(pid));
2017-01-01 18:54:13 +08:00
break;
2016-12-31 02:44:24 +08:00
}
}
2017-01-01 18:54:13 +08:00
pthread_mutex_unlock(&mutex);
2016-12-31 02:44:24 +08:00
}
// Close the logcat monitor
pclose(p);
}