Adjust run_command function

This commit is contained in:
topjohnwu 2017-07-10 22:29:53 +08:00
parent 3d43c3c5bc
commit d75fa62cab
6 changed files with 15 additions and 15 deletions

View File

@ -749,7 +749,7 @@ void late_start(int client) {
"CLASSPATH=/system/framework/pm.jar " "CLASSPATH=/system/framework/pm.jar "
"/system/bin/app_process /system/bin " "/system/bin/app_process /system/bin "
"com.android.commands.pm.Pm install -r " MANAGERAPK, NULL }; "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); pid = run_command(1, &apk_res, "/system/bin/sh", command);
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
fdgets(buf, PATH_MAX, apk_res); fdgets(buf, PATH_MAX, apk_res);

View File

@ -127,7 +127,7 @@ void proc_monitor() {
// Monitor am_proc_start // Monitor am_proc_start
char *const command[] = { "logcat", "-b", "events", "-v", "raw", "-s", "am_proc_start", NULL }; 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); log_pid = run_command(0, &log_fd, "/system/bin/logcat", command);
if (log_pid < 0) continue; if (log_pid < 0) continue;

View File

@ -13,7 +13,7 @@
static int e2fsck(const char *img) { static int e2fsck(const char *img) {
// Check and repair ext4 image // Check and repair ext4 image
char buffer[128]; char buffer[128];
int pid, fd = 0; int pid, fd = -1;
char *const command[] = { "e2fsck", "-yf", (char *) img, NULL }; char *const command[] = { "e2fsck", "-yf", (char *) img, NULL };
pid = run_command(1, &fd, "/system/bin/e2fsck", command); pid = run_command(1, &fd, "/system/bin/e2fsck", command);
if (pid < 0) if (pid < 0)
@ -75,7 +75,7 @@ int get_img_size(const char *img, int *used, int *total) {
if (access(img, R_OK) == -1) if (access(img, R_OK) == -1)
return 1; return 1;
char buffer[PATH_MAX]; char buffer[PATH_MAX];
int pid, fd = 0, status = 1; int pid, fd = -1, status = 1;
char *const command[] = { "e2fsck", "-n", (char *) img, NULL }; char *const command[] = { "e2fsck", "-n", (char *) img, NULL };
pid = run_command(1, &fd, "/system/bin/e2fsck", command); pid = run_command(1, &fd, "/system/bin/e2fsck", command);
if (pid < 0) if (pid < 0)
@ -107,7 +107,7 @@ int resize_img(const char *img, int size) {
if (e2fsck(img)) if (e2fsck(img))
return 1; return 1;
char buffer[128]; char buffer[128];
int pid, status, fd = 0; int pid, status, fd = -1;
snprintf(buffer, sizeof(buffer), "%dM", size); snprintf(buffer, sizeof(buffer), "%dM", size);
char *const command[] = { "resize2fs", (char *) img, buffer, NULL }; char *const command[] = { "resize2fs", (char *) img, buffer, NULL };
pid = run_command(1, &fd, "/system/bin/resize2fs", command); pid = run_command(1, &fd, "/system/bin/resize2fs", command);

View File

@ -221,21 +221,21 @@ void setup_sighandlers(void (*handler)(int)) {
/* /*
fd == NULL -> Ignore output fd == NULL -> Ignore output
*fd == 0 -> Open pipe and set *fd to the read end *fd < 0 -> Open pipe and set *fd to the read end
*fd != 0 -> STDOUT (or STDERR) will be redirected to *fd *fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd
*/ */
int run_command(int err, int *fd, const char *path, char *const argv[]) { int run_command(int err, int *fd, const char *path, char *const argv[]) {
int pipefd[2], writeEnd = -1; int pipefd[2], writeEnd = -1;
if (fd) { if (fd) {
if (*fd) { if (*fd < 0) {
writeEnd = *fd; if (xpipe2(pipefd, O_CLOEXEC) == -1)
} else {
if (pipe(pipefd) == -1)
return -1; return -1;
writeEnd = pipefd[1]; writeEnd = pipefd[1];
// Give the read end of the pipe // Give the read end of the pipe
*fd = pipefd[0]; *fd = pipefd[0];
} else {
writeEnd = *fd;
} }
} }

View File

@ -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 xwrite(int fd, const void *buf, size_t count);
ssize_t xread(int fd, 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); 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); int xsetns(int fd, int nstype);
DIR *xopendir(const char *name); DIR *xopendir(const char *name);
struct dirent *xreaddir(DIR *dirp); struct dirent *xreaddir(DIR *dirp);

View File

@ -82,10 +82,10 @@ ssize_t xxread(int fd, void *buf, size_t count) {
return ret; return ret;
} }
int xpipe(int pipefd[2]) { int xpipe2(int pipefd[2], int flags) {
int ret = pipe(pipefd); int ret = pipe2(pipefd, flags);
if (ret == -1) { if (ret == -1) {
PLOGE("pipe"); PLOGE("pipe2");
} }
return ret; return ret;
} }