diff --git a/native/jni/daemon/bootstages.cpp b/native/jni/daemon/bootstages.cpp index e5233418a..1c38e019f 100644 --- a/native/jni/daemon/bootstages.cpp +++ b/native/jni/daemon/bootstages.cpp @@ -441,17 +441,17 @@ static bool magisk_env() { if (str_contains(line, " /system_root ")) { bind_mount("/system_root/system", MIRRDIR "/system"); sscanf(line.c_str(), "%s", buf); - system_block = strdup2(buf); + system_block = strdup(buf); system_as_root = true; } else if (!system_as_root && str_contains(line, " /system ")) { sscanf(line.c_str(), "%s %*s %s", buf, buf2); - system_block = strdup2(buf); + system_block = strdup(buf); xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr); VLOGI("mount", system_block, MIRRDIR "/system"); } else if (str_contains(line, " /vendor ")) { seperate_vendor = true; sscanf(line.c_str(), "%s %*s %s", buf, buf2); - vendor_block = strdup2(buf); + vendor_block = strdup(buf); xmkdir(MIRRDIR "/vendor", 0755); xmount(vendor_block, MIRRDIR "/vendor", buf2, MS_RDONLY, nullptr); VLOGI("mount", vendor_block, MIRRDIR "/system"); diff --git a/native/jni/daemon/log_daemon.cpp b/native/jni/daemon/log_daemon.cpp index 5b00b42b5..e2196b5d3 100644 --- a/native/jni/daemon/log_daemon.cpp +++ b/native/jni/daemon/log_daemon.cpp @@ -82,33 +82,35 @@ static void *monitor_thread(void *) { } static void *logcat_thread(void *) { - int log_fd = -1, log_pid; + int log_pid; char line[4096]; - while (1) { + while (true) { // Start logcat - log_pid = exec_command(false, &log_fd, nullptr, log_cmd.data()); - FILE *logs = fdopen(log_fd, "r"); + exec_t exec { + .fd = -1, + .argv = log_cmd.data() + }; + log_pid = exec_command(exec); + FILE *logs = fdopen(exec.fd, "r"); while (fgets(line, sizeof(line), logs)) { if (line[0] == '-') continue; size_t len = strlen(line); pthread_mutex_lock(&lock); - for (int i = 0; i < EVENT_NUM; ++i) { - if (events[i].fd > 0 && events[i].filter(line)) - write(events[i].fd, line, len); + for (auto &event : events) { + if (event.fd > 0 && event.filter(line)) + write(event.fd, line, len); } pthread_mutex_unlock(&lock); } fclose(logs); - log_fd = -1; kill(log_pid, SIGTERM); waitpid(log_pid, nullptr, 0); LOGI("magisklogd: logcat output EOF"); // Clear buffer - log_pid = exec_command(false, nullptr, nullptr, clear_cmd.data()); - waitpid(log_pid, nullptr, 0); + exec_command_sync(clear_cmd.data()); } } @@ -131,10 +133,10 @@ static void log_daemon() { log_cmd.push_back(MIRRDIR "/system/bin/logcat"); // Test whether these buffers actually works const char *b[] = { "main", "events", "crash" }; - for (int i = 0; i < 3; ++i) { - if (exec_command_sync(MIRRDIR "/system/bin/logcat", "-b", b[i], "-d", "-f", "/dev/null", nullptr) == 0) { + for (auto &buffer : b) { + if (exec_command_sync(MIRRDIR "/system/bin/logcat", "-b", buffer, "-d", "-f", "/dev/null", nullptr) == 0) { log_cmd.push_back("-b"); - log_cmd.push_back(b[i]); + log_cmd.push_back(buffer); } } chmod("/dev/null", 0666); diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index 788654ea4..16bd1d69f 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -142,10 +142,19 @@ void write_zero(int fd, size_t size); #define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0) std::vector file_to_vector(const char *filename); -char *strdup2(const char *s, size_t *size = nullptr); +struct exec_t { + bool err = false; + int fd = -2; + void (*pre_exec)() = nullptr; + const char **argv = nullptr; + int (*fork)() = xfork; +}; + +int exec_command(exec_t &exec); int exec_command(bool err, int *fd, void (*pre_exec)(), const char **argv); int exec_command(bool err, int *fd, void (*cb)(), const char *argv0, ...); +int exec_command_sync(const char **argv); int exec_command_sync(const char *argv0, ...); #endif diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index f38d271ff..1f912f5a7 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -155,30 +155,25 @@ int __fsetxattr(int fd, const char *name, const void *value, size_t size, int fl return (int) syscall(__NR_fsetxattr, fd, name, value, size, flags); } -/* - fd == nullptr -> Ignore output - *fd < 0 -> Open pipe and set *fd to the read end - *fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd - *pre_exec -> A callback function called after forking, before execvp -*/ -int exec_command(bool err, int *fd, void (*pre_exec)(void), const char **argv) { - int pipefd[2], outfd = -1; +int exec_command(exec_t &exec) { + int pipefd[2] = {-1, -1}, outfd = -1; - if (fd) { - if (*fd < 0) { - if (xpipe2(pipefd, O_CLOEXEC) == -1) - return -1; - outfd = pipefd[1]; - } else { - outfd = *fd; - } + if (exec.fd == -1) { + if (xpipe2(pipefd, O_CLOEXEC) == -1) + return -1; + outfd = pipefd[1]; + } else if (exec.fd >= 0) { + outfd = exec.fd; } - int pid = xfork(); - if (pid != 0) { - if (fd && *fd < 0) { - // Give the read end and close write end - *fd = pipefd[0]; + int pid = exec.fork(); + if (pid < 0) { + close(pipefd[0]); + close(pipefd[1]); + return -1; + } else if (pid) { + if (exec.fd == -1) { + exec.fd = pipefd[0]; close(pipefd[1]); } return pid; @@ -186,21 +181,39 @@ int exec_command(bool err, int *fd, void (*pre_exec)(void), const char **argv) { if (outfd >= 0) { xdup2(outfd, STDOUT_FILENO); - if (err) + if (exec.err) xdup2(outfd, STDERR_FILENO); close(outfd); } // Call the pre-exec callback - if (pre_exec) - pre_exec(); + if (exec.pre_exec) + exec.pre_exec(); - execve(argv[0], (char **) argv, environ); - PLOGE("execve %s", argv[0]); + execve(exec.argv[0], (char **) exec.argv, environ); + PLOGE("execve %s", exec.argv[0]); exit(-1); } -static int v_exec_command(bool err, int *fd, void (*cb)(void), const char *argv0, va_list argv) { +/* + fd == nullptr -> Ignore output + *fd < 0 -> Open pipe and set *fd to the read end + *fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd + *pre_exec -> A callback function called after forking, before execvp +*/ +int exec_command(bool err, int *fd, void (*pre_exec)(), const char **argv) { + exec_t exec { + .err = err, + .fd = fd ? *fd : -2, + .pre_exec = pre_exec, + .argv = argv + }; + int pid = exec_command(exec); + if (fd) *fd = exec.fd; + return pid; +} + +static int v_exec_command(bool err, int *fd, void (*cb)(), const char *argv0, va_list argv) { // Collect va_list into vector vector args; args.push_back(argv0); @@ -211,6 +224,18 @@ static int v_exec_command(bool err, int *fd, void (*cb)(void), const char *argv0 return pid; } +int exec_command_sync(const char **argv) { + exec_t exec { + .argv = argv + }; + int pid, status; + pid = exec_command(exec); + if (pid < 0) + return -1; + waitpid(pid, &status, 0); + return WEXITSTATUS(status); +} + int exec_command_sync(const char *argv0, ...) { va_list argv; va_start(argv, argv0); @@ -230,11 +255,3 @@ int exec_command(bool err, int *fd, void (*cb)(void), const char *argv0, ...) { va_end(argv); return pid; } - -char *strdup2(const char *s, size_t *size) { - size_t len = strlen(s) + 1; - char *buf = new char[len]; - memcpy(buf, s, len); - if (size) *size = len; - return buf; -}