From d75fa62cab1d15106b2e21855242578cfebdc0eb Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 10 Jul 2017 22:29:53 +0800 Subject: [PATCH] Adjust run_command function --- jni/daemon/bootstages.c | 2 +- jni/magiskhide/proc_monitor.c | 2 +- jni/utils/img.c | 6 +++--- jni/utils/misc.c | 12 ++++++------ jni/utils/utils.h | 2 +- jni/utils/xwrap.c | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/jni/daemon/bootstages.c b/jni/daemon/bootstages.c index 296ebf867..28b60770b 100644 --- a/jni/daemon/bootstages.c +++ b/jni/daemon/bootstages.c @@ -749,7 +749,7 @@ void late_start(int client) { "CLASSPATH=/system/framework/pm.jar " "/system/bin/app_process /system/bin " "com.android.commands.pm.Pm install -r " MANAGERAPK, NULL }; - int apk_res = 0, pid; + int apk_res = -1, pid; pid = run_command(1, &apk_res, "/system/bin/sh", command); waitpid(pid, NULL, 0); fdgets(buf, PATH_MAX, apk_res); diff --git a/jni/magiskhide/proc_monitor.c b/jni/magiskhide/proc_monitor.c index e628d935e..4293d2fda 100644 --- a/jni/magiskhide/proc_monitor.c +++ b/jni/magiskhide/proc_monitor.c @@ -127,7 +127,7 @@ void proc_monitor() { // Monitor am_proc_start char *const command[] = { "logcat", "-b", "events", "-v", "raw", "-s", "am_proc_start", NULL }; - log_fd = 0; + log_fd = -1; log_pid = run_command(0, &log_fd, "/system/bin/logcat", command); if (log_pid < 0) continue; diff --git a/jni/utils/img.c b/jni/utils/img.c index 11147506d..2823a0165 100644 --- a/jni/utils/img.c +++ b/jni/utils/img.c @@ -13,7 +13,7 @@ static int e2fsck(const char *img) { // Check and repair ext4 image char buffer[128]; - int pid, fd = 0; + int pid, fd = -1; char *const command[] = { "e2fsck", "-yf", (char *) img, NULL }; pid = run_command(1, &fd, "/system/bin/e2fsck", command); if (pid < 0) @@ -75,7 +75,7 @@ int get_img_size(const char *img, int *used, int *total) { if (access(img, R_OK) == -1) return 1; char buffer[PATH_MAX]; - int pid, fd = 0, status = 1; + int pid, fd = -1, status = 1; char *const command[] = { "e2fsck", "-n", (char *) img, NULL }; pid = run_command(1, &fd, "/system/bin/e2fsck", command); if (pid < 0) @@ -107,7 +107,7 @@ int resize_img(const char *img, int size) { if (e2fsck(img)) return 1; char buffer[128]; - int pid, status, fd = 0; + int pid, status, fd = -1; snprintf(buffer, sizeof(buffer), "%dM", size); char *const command[] = { "resize2fs", (char *) img, buffer, NULL }; pid = run_command(1, &fd, "/system/bin/resize2fs", command); diff --git a/jni/utils/misc.c b/jni/utils/misc.c index fe49c5343..58d7fef0a 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -221,21 +221,21 @@ void setup_sighandlers(void (*handler)(int)) { /* 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 + *fd < 0 -> Open pipe and set *fd to the read end + *fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd */ int run_command(int err, int *fd, const char *path, char *const argv[]) { int pipefd[2], writeEnd = -1; if (fd) { - if (*fd) { - writeEnd = *fd; - } else { - if (pipe(pipefd) == -1) + if (*fd < 0) { + if (xpipe2(pipefd, O_CLOEXEC) == -1) return -1; writeEnd = pipefd[1]; // Give the read end of the pipe *fd = pipefd[0]; + } else { + writeEnd = *fd; } } diff --git a/jni/utils/utils.h b/jni/utils/utils.h index ad95b8c69..fb5414eef 100644 --- a/jni/utils/utils.h +++ b/jni/utils/utils.h @@ -31,7 +31,7 @@ int xopen3(const char *pathname, int flags, mode_t mode); ssize_t xwrite(int fd, const void *buf, size_t count); ssize_t xread(int fd, void *buf, size_t count); ssize_t xxread(int fd, void *buf, size_t count); -int xpipe(int pipefd[2]); +int xpipe2(int pipefd[2], int flags); int xsetns(int fd, int nstype); DIR *xopendir(const char *name); struct dirent *xreaddir(DIR *dirp); diff --git a/jni/utils/xwrap.c b/jni/utils/xwrap.c index cd5e6e7d2..e5602411e 100644 --- a/jni/utils/xwrap.c +++ b/jni/utils/xwrap.c @@ -82,10 +82,10 @@ ssize_t xxread(int fd, void *buf, size_t count) { return ret; } -int xpipe(int pipefd[2]) { - int ret = pipe(pipefd); +int xpipe2(int pipefd[2], int flags) { + int ret = pipe2(pipefd, flags); if (ret == -1) { - PLOGE("pipe"); + PLOGE("pipe2"); } return ret; }