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 *
***********/
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);

View File

@ -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...

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);
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);

View File

@ -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;

View File

@ -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))

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 < 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) {