From e8e58f3fedbef051393f2e9a963d48c3a36e53f9 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 19 Aug 2017 19:27:51 +0800 Subject: [PATCH] Adjust run_command --- jni/daemon/bootstages.c | 18 ++++++++---------- jni/daemon/log_monitor.c | 6 ++---- jni/include/utils.h | 5 +++-- jni/magiskhide/proc_monitor.c | 6 ++---- jni/utils/img.c | 8 ++++---- jni/utils/misc.c | 22 +++++++++++++++------- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/jni/daemon/bootstages.c b/jni/daemon/bootstages.c index 219ace349..01b619ddc 100644 --- a/jni/daemon/bootstages.c +++ b/jni/daemon/bootstages.c @@ -117,7 +117,7 @@ static struct node_entry *insert_child(struct node_entry *p, struct node_entry * * Scripts * ***********/ -static void bb_path() { +static void bb_setenv() { snprintf(buf, PATH_MAX, "%s:%s", BBPATH, getenv("PATH")); setenv("PATH", buf, 1); } @@ -137,7 +137,7 @@ static void exec_common_script(const char* stage) { continue; LOGI("%s.d: exec [%s]\n", stage, entry->d_name); char *const command[] = { "sh", buf2, NULL }; - int pid = run_command(0, NULL, bb_path, "/system/bin/sh", command); + int pid = run_command2(0, NULL, bb_setenv, command); if (pid != -1) waitpid(pid, NULL, 0); } @@ -155,7 +155,7 @@ static void exec_module_script(const char* stage) { continue; LOGI("%s: exec [%s.sh]\n", module, stage); char *const command[] = { "sh", buf2, NULL }; - int pid = run_command(0, NULL, bb_path, "/system/bin/sh", command); + int pid = run_command2(0, NULL, bb_setenv, command); if (pid != -1) waitpid(pid, NULL, 0); } @@ -402,10 +402,8 @@ static void mount_mirrors() { static void link_busybox() { mkdir_p(BBPATH, 0755); - char *const command[] = { "busybox", "--install", "-s", BBPATH, NULL}; - int pid = run_command(0, NULL, NULL, MIRRDIR "/bin/busybox", command); - if (pid != -1) - waitpid(pid, NULL, 0); + char *const command[] = { MIRRDIR "/bin/busybox", "--install", "-s", BBPATH, NULL}; + run_command(command); symlink(MIRRDIR "/bin/busybox", BBPATH "/busybox"); } @@ -523,7 +521,7 @@ void post_fs_data(int client) { // Start debug logs in new process debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644); char *const command[] = { "logcat", "-v", "brief", NULL }; - debug_log_pid = run_command(0, &debug_log_fd, NULL, "/system/bin/logcat", command); + debug_log_pid = run_command2(0, &debug_log_fd, NULL, command); close(debug_log_fd); #endif @@ -565,7 +563,7 @@ void post_fs_data(int client) { // uninstaller if (access(UNINSTALLER, F_OK) == 0) { close(open(UNBLOCKFILE, O_RDONLY | O_CREAT)); - bb_path(); + bb_setenv(); system("(BOOTMODE=true sh " UNINSTALLER ") &"); return; } @@ -710,7 +708,7 @@ core_only: "/system/bin", "com.android.commands.pm.Pm", "install", "-r", MANAGERAPK, NULL }; int apk_res = -1, pid; - pid = run_command(1, &apk_res, pm_setenv, "/system/bin/app_process", command); + pid = run_command2(1, &apk_res, pm_setenv, command); if (pid != -1) { waitpid(pid, NULL, 0); fdgets(buf, PATH_MAX, apk_res); diff --git a/jni/daemon/log_monitor.c b/jni/daemon/log_monitor.c index d6ffbd00d..832ed5622 100644 --- a/jni/daemon/log_monitor.c +++ b/jni/daemon/log_monitor.c @@ -27,14 +27,12 @@ static void *logger_thread(void *args) { while (1) { // Start logcat char *const command[] = { "logcat", "-s", "Magisk", "-v", "thread", NULL }; - log_pid = run_command(0, &log_fd, NULL, "/system/bin/logcat", command); + log_pid = run_command2(0, &log_fd, NULL, command); if (log_pid > 0) waitpid(log_pid, NULL, 0); // For some reason it went here, clear buffer and restart char *const restart[] = { "logcat", "-c", NULL }; - log_pid = run_command(0, NULL, NULL, "/system/bin/logcat", restart); - if (log_pid > 0) - waitpid(log_pid, NULL, 0); + run_command(restart); } // Should never be here, but well... diff --git a/jni/include/utils.h b/jni/include/utils.h index 97ac8b368..52ff6066c 100644 --- a/jni/include/utils.h +++ b/jni/include/utils.h @@ -82,13 +82,14 @@ void ps_filter_proc_name(const char *filter, void (*func)(int)); int create_links(const char *bin, const char *path); void unlock_blocks(); void setup_sighandlers(void (*handler)(int)); -int run_command(int err, int *fd, void (*cb)(void), const char *path, char *const argv[]); +int run_command(char *const argv[]); +int run_command2(int err, int *fd, void (*cb)(void), char *const argv[]); int mkdir_p(const char *pathname, mode_t mode); int bind_mount(const char *from, const char *to); int open_new(const char *filename); int cp_afc(const char *source, const char *target); int clone_dir(const char *source, const char *target); -int rm_rf(const char *target); +void rm_rf(const char *target); void fclone_attr(const int sourcefd, const int targetfd); void clone_attr(const char *source, const char *target); void get_client_cred(int fd, struct ucred *cred); diff --git a/jni/magiskhide/proc_monitor.c b/jni/magiskhide/proc_monitor.c index 03c9408cc..cf5a36bd4 100644 --- a/jni/magiskhide/proc_monitor.c +++ b/jni/magiskhide/proc_monitor.c @@ -182,14 +182,12 @@ void proc_monitor() { while (1) { // Clear previous logcat buffer char *const restart[] = { "logcat", "-b", "events", "-c", NULL }; - log_pid = run_command(0, NULL, NULL, "/system/bin/logcat", restart); - if (log_pid > 0) - waitpid(log_pid, NULL, 0); + run_command(restart); // Monitor am_proc_start char *const command[] = { "logcat", "-b", "events", "-v", "raw", "-s", "am_proc_start", NULL }; log_fd = -1; - log_pid = run_command(0, &log_fd, NULL, "/system/bin/logcat", command); + log_pid = run_command2(0, &log_fd, NULL, command); if (log_pid < 0) continue; if (kill(log_pid, 0)) continue; diff --git a/jni/utils/img.c b/jni/utils/img.c index b27d69d72..2d0ae6490 100644 --- a/jni/utils/img.c +++ b/jni/utils/img.c @@ -15,7 +15,7 @@ static int e2fsck(const char *img) { char buffer[128]; int pid, fd = -1; char *const command[] = { "e2fsck", "-yf", (char *) img, NULL }; - pid = run_command(1, &fd, NULL, "/system/bin/e2fsck", command); + pid = run_command2(1, &fd, NULL, command); if (pid < 0) return 1; while (fdgets(buffer, sizeof(buffer), fd)) @@ -63,7 +63,7 @@ int create_img(const char *img, int size) { char buffer[16]; snprintf(buffer, sizeof(buffer), "%dM", size); char *const command[] = { "make_ext4fs", "-l", buffer, "-a", "/magisk", "-S", filename, (char *) img, NULL }; - pid = run_command(0, NULL, NULL, "/system/bin/make_ext4fs", command); + pid = run_command2(0, NULL, NULL, command); if (pid < 0) return 1; waitpid(pid, &status, 0); @@ -77,7 +77,7 @@ int get_img_size(const char *img, int *used, int *total) { char buffer[PATH_MAX]; int pid, fd = -1, status = 1; char *const command[] = { "e2fsck", "-n", (char *) img, NULL }; - pid = run_command(1, &fd, NULL, "/system/bin/e2fsck", command); + pid = run_command2(1, &fd, NULL, command); if (pid < 0) return 1; while (fdgets(buffer, sizeof(buffer), fd)) { @@ -110,7 +110,7 @@ int resize_img(const char *img, int size) { int pid, status, fd = -1; snprintf(buffer, sizeof(buffer), "%dM", size); char *const command[] = { "resize2fs", (char *) img, buffer, NULL }; - pid = run_command(1, &fd, NULL, "/system/bin/resize2fs", command); + pid = run_command2(1, &fd, NULL, command); if (pid < 0) return 1; while (fdgets(buffer, sizeof(buffer), fd)) diff --git a/jni/utils/misc.c b/jni/utils/misc.c index 5c11fc3d2..5f843094f 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -219,13 +219,22 @@ void setup_sighandlers(void (*handler)(int)) { } } +int run_command(char *const argv[]) { + int pid = run_command2(0, NULL, NULL, argv); + if (pid != -1) + waitpid(pid, NULL, 0); + else + return 1; + return 0; +} + /* fd == NULL -> Ignore output *fd < 0 -> Open pipe and set *fd to the read end *fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd *cb -> A callback function which runs after fork */ -int run_command(int err, int *fd, void (*cb)(void), const char *path, char *const argv[]) { +int run_command2(int err, int *fd, void (*cb)(void), char *const argv[]) { int pipefd[2], writeEnd = -1; if (fd) { @@ -255,7 +264,7 @@ int run_command(int err, int *fd, void (*cb)(void), const char *path, char *cons if (err) xdup2(writeEnd, STDERR_FILENO); } - execv(path, argv); + execvp(argv[0], argv); PLOGE("execv"); return -1; } @@ -359,13 +368,12 @@ int clone_dir(const char *source, const char *target) { return 0; } -int rm_rf(const char *target) { +void rm_rf(const char *target) { if (access(target, F_OK) == -1) - return 0; + return; // Use external rm command, saves a lot of headache and issues - char command[PATH_MAX]; - snprintf(command, sizeof(command), "rm -rf %s", target); - return system(command); + char *const command[] = { "rm", "-rf", (char*) target, NULL }; + run_command(command); } void clone_attr(const char *source, const char *target) {