Prettify code

This commit is contained in:
topjohnwu 2016-12-11 05:10:48 +08:00
parent 8d6d619eed
commit b2431b982f

View File

@ -194,88 +194,92 @@ void *monitor_list(void *path) {
return NULL; return NULL;
} }
void run_as_daemon() {
switch(fork()) {
case -1:
exit(-1);
case 0:
if (setsid() < 0)
exit(-1);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
break;
default:
exit(0);
}
}
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp) {
pid_t forkpid = fork(); run_as_daemon();
if (forkpid < 0) logfile = fopen(LOGFILE, "a+");
return 1; setbuf(logfile, NULL);
if (forkpid == 0) { // Fork a child to handle namespace switches and unmounts
if (setsid() < 0) pipe(pipefd);
return 1; switch(fork()) {
case -1:
close(STDIN_FILENO); exit(-1);
close(STDOUT_FILENO); case 0:
close(STDERR_FILENO);
logfile = fopen(LOGFILE, "a+");
setbuf(logfile, NULL);
// Fork a child to handle namespace switches and unmounts
pipe(pipefd);
forkpid = fork();
if (forkpid < 0)
return 1;
if (forkpid == 0)
return hideMagisk(); return hideMagisk();
default:
break;
}
close(pipefd[0]);
close(pipefd[0]); // Start a thread to constantly check the hide list
pthread_t list_monitor;
pthread_mutex_init(&mutex, NULL);
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
// Start a thread to constantly check the hide list char buffer[512];
pthread_t list_monitor; FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
pthread_mutex_init(&mutex, NULL);
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
char buffer[512]; while(!feof(p)) {
FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r"); //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);
while(!feof(p)) { char *pos = buffer;
//Format of am_proc_start is (as of Android 5.1 and 6.0) while(1) {
//UserID, pid, unix uid, processName, hostingType, hostingName pos = strchr(pos, ',');
fgets(buffer, sizeof(buffer), p); if(pos == NULL)
break;
char *pos = buffer; pos[0] = ' ';
while(1) {
pos = strchr(pos, ',');
if(pos == NULL)
break;
pos[0] = ' ';
}
int user, pid, uid;
char processName[256], hostingType[16], hostingName[256];
int ret = sscanf(buffer, "[%d %d %d %256s %16s %256s]",
&user, &pid, &uid,
processName, hostingType, hostingName);
if(ret != 6)
continue;
for (i = 0; i < list_size; ++i) {
if(strstr(processName, hide_list[i])) {
fprintf(logfile, "MagiskHide: Disabling for process=%s, PID=%d, UID=%d\n", processName, pid, uid);
write(pipefd[1], &pid, sizeof(pid));
}
}
} }
// Close the logcat monitor int user, pid, uid;
pclose(p); char processName[256], hostingType[16], hostingName[256];
int ret = sscanf(buffer, "[%d %d %d %256s %16s %256s]",
&user, &pid, &uid,
processName, hostingType, hostingName);
// Close the config list monitor if(ret != 6)
pthread_kill(list_monitor, SIGQUIT); continue;
pthread_mutex_destroy(&mutex);
// Terminate our children for (i = 0; i < list_size; ++i) {
i = -1; if(strstr(processName, hide_list[i])) {
write(pipefd[1], &i, sizeof(i)); fprintf(logfile, "MagiskHide: Disabling for process=%s, PID=%d, UID=%d\n", processName, pid, uid);
write(pipefd[1], &pid, sizeof(pid));
fprintf(logfile, "MagiskHide: Cannot read from logcat, abort...\n"); }
fclose(logfile); }
return 1;
} }
return 0; // Close the logcat monitor
pclose(p);
// Close the config list monitor
pthread_kill(list_monitor, SIGQUIT);
pthread_mutex_destroy(&mutex);
// Terminate our children
i = -1;
write(pipefd[1], &i, sizeof(i));
fprintf(logfile, "MagiskHide: Cannot read from logcat, abort...\n");
fclose(logfile);
return 1;
} }