From dead74801d167a1bd55ef387e0205ac1ce58db08 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 4 Dec 2020 01:07:47 -0800 Subject: [PATCH] Setup log file when manually starting daemon --- native/jni/core/daemon.cpp | 14 +++++++++----- native/jni/core/magisk.cpp | 2 +- native/jni/include/daemon.hpp | 3 ++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 5a909a1e0..b27fa0d1b 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -25,7 +25,7 @@ int DAEMON_STATE = STATE_UNKNOWN; static struct stat self_st; -static bool verify_client(int client, pid_t pid) { +static bool verify_client(pid_t pid) { // Verify caller is the same as server char path[32]; sprintf(path, "/proc/%d/exe", pid); @@ -71,11 +71,14 @@ static void handle_request(int client) { // Verify client credentials ucred cred; get_client_cred(client, &cred); - if (cred.uid != 0 && !verify_client(client, cred.pid)) + if (cred.uid != 0 && !verify_client(cred.pid)) + goto shortcut; + + req_code = read_int(client); + if (req_code < 0 || req_code >= DAEMON_CODE_END) goto shortcut; // Check client permissions - req_code = read_int(client); switch (req_code) { case MAGISKHIDE: case POST_FS_DATA: @@ -108,7 +111,7 @@ static void handle_request(int client) { DAEMON_STATE = STATE_BOOT_COMPLETE; break; - // Simple requests to query daemon info + // Simple requests case CHECK_VERSION: write_string(client, MAGISK_VERSION ":MAGISK"); goto shortcut; @@ -118,7 +121,8 @@ static void handle_request(int client) { case GET_PATH: write_string(client, MAGISKTMP.data()); goto shortcut; - case DO_NOTHING: + case START_DAEMON: + setup_logfile(true); goto shortcut; } diff --git a/native/jni/core/magisk.cpp b/native/jni/core/magisk.cpp index 73e3127c9..c050ada90 100644 --- a/native/jni/core/magisk.cpp +++ b/native/jni/core/magisk.cpp @@ -81,7 +81,7 @@ int magisk_main(int argc, char *argv[]) { return 0; } else if (argv[1] == "--daemon"sv) { int fd = connect_daemon(true); - write_int(fd, DO_NOTHING); + write_int(fd, START_DAEMON); return 0; } else if (argv[1] == "--post-fs-data"sv) { int fd = connect_daemon(true); diff --git a/native/jni/include/daemon.hpp b/native/jni/include/daemon.hpp index be676ed65..7aeacecf2 100644 --- a/native/jni/include/daemon.hpp +++ b/native/jni/include/daemon.hpp @@ -8,7 +8,7 @@ // Daemon command codes enum { - DO_NOTHING = 0, + START_DAEMON, SUPERUSER, CHECK_VERSION, CHECK_VERSION_CODE, @@ -19,6 +19,7 @@ enum { SQLITE_CMD, REMOVE_MODULES, GET_PATH, + DAEMON_CODE_END, }; // Return codes for daemon