Adjust run_command

This commit is contained in:
topjohnwu 2017-08-19 19:27:51 +08:00
parent 1ca9ec384b
commit e8e58f3fed
6 changed files with 34 additions and 31 deletions

View File

@ -117,7 +117,7 @@ static struct node_entry *insert_child(struct node_entry *p, struct node_entry *
* Scripts * * Scripts *
***********/ ***********/
static void bb_path() { static void bb_setenv() {
snprintf(buf, PATH_MAX, "%s:%s", BBPATH, getenv("PATH")); snprintf(buf, PATH_MAX, "%s:%s", BBPATH, getenv("PATH"));
setenv("PATH", buf, 1); setenv("PATH", buf, 1);
} }
@ -137,7 +137,7 @@ static void exec_common_script(const char* stage) {
continue; continue;
LOGI("%s.d: exec [%s]\n", stage, entry->d_name); LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
char *const command[] = { "sh", buf2, NULL }; 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) if (pid != -1)
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
} }
@ -155,7 +155,7 @@ static void exec_module_script(const char* stage) {
continue; continue;
LOGI("%s: exec [%s.sh]\n", module, stage); LOGI("%s: exec [%s.sh]\n", module, stage);
char *const command[] = { "sh", buf2, NULL }; 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) if (pid != -1)
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
} }
@ -402,10 +402,8 @@ static void mount_mirrors() {
static void link_busybox() { static void link_busybox() {
mkdir_p(BBPATH, 0755); mkdir_p(BBPATH, 0755);
char *const command[] = { "busybox", "--install", "-s", BBPATH, NULL}; char *const command[] = { MIRRDIR "/bin/busybox", "--install", "-s", BBPATH, NULL};
int pid = run_command(0, NULL, NULL, MIRRDIR "/bin/busybox", command); run_command(command);
if (pid != -1)
waitpid(pid, NULL, 0);
symlink(MIRRDIR "/bin/busybox", BBPATH "/busybox"); symlink(MIRRDIR "/bin/busybox", BBPATH "/busybox");
} }
@ -523,7 +521,7 @@ void post_fs_data(int client) {
// Start debug logs in new process // Start debug logs in new process
debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644); debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644);
char *const command[] = { "logcat", "-v", "brief", NULL }; 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); close(debug_log_fd);
#endif #endif
@ -565,7 +563,7 @@ void post_fs_data(int client) {
// uninstaller // uninstaller
if (access(UNINSTALLER, F_OK) == 0) { if (access(UNINSTALLER, F_OK) == 0) {
close(open(UNBLOCKFILE, O_RDONLY | O_CREAT)); close(open(UNBLOCKFILE, O_RDONLY | O_CREAT));
bb_path(); bb_setenv();
system("(BOOTMODE=true sh " UNINSTALLER ") &"); system("(BOOTMODE=true sh " UNINSTALLER ") &");
return; return;
} }
@ -710,7 +708,7 @@ core_only:
"/system/bin", "com.android.commands.pm.Pm", "/system/bin", "com.android.commands.pm.Pm",
"install", "-r", MANAGERAPK, NULL }; "install", "-r", MANAGERAPK, NULL };
int apk_res = -1, pid; 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) { if (pid != -1) {
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
fdgets(buf, PATH_MAX, apk_res); fdgets(buf, PATH_MAX, apk_res);

View File

@ -27,14 +27,12 @@ static void *logger_thread(void *args) {
while (1) { while (1) {
// Start logcat // Start logcat
char *const command[] = { "logcat", "-s", "Magisk", "-v", "thread", NULL }; 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) if (log_pid > 0)
waitpid(log_pid, NULL, 0); waitpid(log_pid, NULL, 0);
// For some reason it went here, clear buffer and restart // For some reason it went here, clear buffer and restart
char *const restart[] = { "logcat", "-c", NULL }; char *const restart[] = { "logcat", "-c", NULL };
log_pid = run_command(0, NULL, NULL, "/system/bin/logcat", restart); run_command(restart);
if (log_pid > 0)
waitpid(log_pid, NULL, 0);
} }
// Should never be here, but well... // Should never be here, but well...

View File

@ -82,13 +82,14 @@ void ps_filter_proc_name(const char *filter, void (*func)(int));
int create_links(const char *bin, const char *path); int create_links(const char *bin, const char *path);
void unlock_blocks(); void unlock_blocks();
void setup_sighandlers(void (*handler)(int)); 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 mkdir_p(const char *pathname, mode_t mode);
int bind_mount(const char *from, const char *to); int bind_mount(const char *from, const char *to);
int open_new(const char *filename); int open_new(const char *filename);
int cp_afc(const char *source, const char *target); int cp_afc(const char *source, const char *target);
int clone_dir(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 fclone_attr(const int sourcefd, const int targetfd);
void clone_attr(const char *source, const char *target); void clone_attr(const char *source, const char *target);
void get_client_cred(int fd, struct ucred *cred); void get_client_cred(int fd, struct ucred *cred);

View File

@ -182,14 +182,12 @@ void proc_monitor() {
while (1) { while (1) {
// Clear previous logcat buffer // Clear previous logcat buffer
char *const restart[] = { "logcat", "-b", "events", "-c", NULL }; char *const restart[] = { "logcat", "-b", "events", "-c", NULL };
log_pid = run_command(0, NULL, NULL, "/system/bin/logcat", restart); run_command(restart);
if (log_pid > 0)
waitpid(log_pid, NULL, 0);
// 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 = -1; 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 (log_pid < 0) continue;
if (kill(log_pid, 0)) continue; if (kill(log_pid, 0)) continue;

View File

@ -15,7 +15,7 @@ static int e2fsck(const char *img) {
char buffer[128]; char buffer[128];
int pid, fd = -1; 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, NULL, "/system/bin/e2fsck", command); pid = run_command2(1, &fd, NULL, command);
if (pid < 0) if (pid < 0)
return 1; return 1;
while (fdgets(buffer, sizeof(buffer), fd)) while (fdgets(buffer, sizeof(buffer), fd))
@ -63,7 +63,7 @@ int create_img(const char *img, int size) {
char buffer[16]; char buffer[16];
snprintf(buffer, sizeof(buffer), "%dM", size); snprintf(buffer, sizeof(buffer), "%dM", size);
char *const command[] = { "make_ext4fs", "-l", buffer, "-a", "/magisk", "-S", filename, (char *) img, NULL }; 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) if (pid < 0)
return 1; return 1;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
@ -77,7 +77,7 @@ int get_img_size(const char *img, int *used, int *total) {
char buffer[PATH_MAX]; char buffer[PATH_MAX];
int pid, fd = -1, 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, NULL, "/system/bin/e2fsck", command); pid = run_command2(1, &fd, NULL, command);
if (pid < 0) if (pid < 0)
return 1; return 1;
while (fdgets(buffer, sizeof(buffer), fd)) { while (fdgets(buffer, sizeof(buffer), fd)) {
@ -110,7 +110,7 @@ int resize_img(const char *img, int size) {
int pid, status, fd = -1; 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, NULL, "/system/bin/resize2fs", command); pid = run_command2(1, &fd, NULL, command);
if (pid < 0) if (pid < 0)
return 1; return 1;
while (fdgets(buffer, sizeof(buffer), fd)) while (fdgets(buffer, sizeof(buffer), fd))

View File

@ -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 == 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
*cb -> A callback function which runs after fork *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; int pipefd[2], writeEnd = -1;
if (fd) { 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); if (err) xdup2(writeEnd, STDERR_FILENO);
} }
execv(path, argv); execvp(argv[0], argv);
PLOGE("execv"); PLOGE("execv");
return -1; return -1;
} }
@ -359,13 +368,12 @@ int clone_dir(const char *source, const char *target) {
return 0; return 0;
} }
int rm_rf(const char *target) { void rm_rf(const char *target) {
if (access(target, F_OK) == -1) if (access(target, F_OK) == -1)
return 0; return;
// Use external rm command, saves a lot of headache and issues // Use external rm command, saves a lot of headache and issues
char command[PATH_MAX]; char *const command[] = { "rm", "-rf", (char*) target, NULL };
snprintf(command, sizeof(command), "rm -rf %s", target); run_command(command);
return system(command);
} }
void clone_attr(const char *source, const char *target) { void clone_attr(const char *source, const char *target) {