Code cleanups

This commit is contained in:
topjohnwu 2021-01-11 02:19:10 -08:00
parent 541bb53553
commit eb21c8b42e
20 changed files with 60 additions and 81 deletions

View File

@ -1,6 +1,4 @@
#include <libgen.h> #include <libgen.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>

View File

@ -13,6 +13,8 @@
#include <resetprop.hpp> #include <resetprop.hpp>
#include <selinux.hpp> #include <selinux.hpp>
#include "core.hpp"
using namespace std; using namespace std;
static bool safe_mode = false; static bool safe_mode = false;
@ -320,7 +322,7 @@ void post_fs_data(int client) {
stop_magiskhide(); stop_magiskhide();
} else { } else {
exec_common_scripts("post-fs-data"); exec_common_scripts("post-fs-data");
auto_start_magiskhide(); auto_start_magiskhide(false);
handle_modules(); handle_modules();
} }
@ -369,7 +371,7 @@ void boot_complete(int client) {
if (access(SECURE_DIR, F_OK) != 0) if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700); xmkdir(SECURE_DIR, 0700);
auto_start_magiskhide(); auto_start_magiskhide(true);
if (!check_manager()) { if (!check_manager()) {
if (access(MANAGERAPK, F_OK) == 0) { if (access(MANAGERAPK, F_OK) == 0) {

26
native/jni/core/core.hpp Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <string>
#include <vector>
extern bool RECOVERY_MODE;
extern int DAEMON_STATE;
void unlock_blocks();
void reboot();
void setup_logfile(bool reset);
// Module stuffs
void handle_modules();
void magic_mount();
void disable_modules();
void remove_modules();
void exec_module_scripts(const char *stage);
// Scripting
void exec_script(const char *script);
void exec_common_scripts(const char *stage);
void exec_module_scripts(const char *stage, const std::vector<std::string> &module_list);
void install_apk(const char *apk);
[[noreturn]] void install_module(const char *file);

View File

@ -1,6 +1,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <csignal>
#include <libgen.h> #include <libgen.h>
#include <sys/un.h> #include <sys/un.h>
#include <sys/types.h> #include <sys/types.h>
@ -16,11 +16,14 @@
#include <flags.hpp> #include <flags.hpp>
#include <stream.hpp> #include <stream.hpp>
#include "core.hpp"
using namespace std; using namespace std;
int SDK_INT = -1; int SDK_INT = -1;
bool RECOVERY_MODE = false;
string MAGISKTMP; string MAGISKTMP;
bool RECOVERY_MODE = false;
int DAEMON_STATE = STATE_NONE; int DAEMON_STATE = STATE_NONE;
static struct stat self_st; static struct stat self_st;

View File

@ -1,14 +1,10 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <magisk.hpp> #include <magisk.hpp>
#include <db.hpp> #include <db.hpp>
#include <daemon.hpp> #include <socket.hpp>
#include <utils.hpp> #include <utils.hpp>
#define DB_VERSION 10 #define DB_VERSION 10

View File

@ -7,6 +7,8 @@
#include <selinux.hpp> #include <selinux.hpp>
#include <flags.hpp> #include <flags.hpp>
#include "core.hpp"
using namespace std; using namespace std;
[[noreturn]] static void usage() { [[noreturn]] static void usage() {

View File

@ -5,9 +5,10 @@
#include <utils.hpp> #include <utils.hpp>
#include <magisk.hpp> #include <magisk.hpp>
#include <selinux.hpp> #include <selinux.hpp>
#include <daemon.hpp>
#include <resetprop.hpp> #include <resetprop.hpp>
#include "core.hpp"
using namespace std; using namespace std;
#define VLOGD(tag, from, to) LOGD("%-8s: %s <- %s\n", tag, to, from) #define VLOGD(tag, from, to) LOGD("%-8s: %s <- %s\n", tag, to, from)

View File

@ -1,7 +1,6 @@
#include <string_view> #include <string_view>
#include <magisk.hpp> #include <magisk.hpp>
#include <daemon.hpp>
#include <selinux.hpp> #include <selinux.hpp>
#include <utils.hpp> #include <utils.hpp>

View File

@ -6,6 +6,8 @@
#include <utils.hpp> #include <utils.hpp>
#include <selinux.hpp> #include <selinux.hpp>
#include "core.hpp"
using namespace std; using namespace std;
#define BBEXEC_CMD bbpath(), "sh" #define BBEXEC_CMD bbpath(), "sh"

View File

@ -1,5 +1,3 @@
#include <string.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <endian.h> #include <endian.h>

View File

@ -2,7 +2,6 @@
#include <pthread.h> #include <pthread.h>
#include <string> #include <string>
#include <vector>
#include <socket.hpp> #include <socket.hpp>
@ -39,10 +38,7 @@ enum {
STATE_BOOT_COMPLETE STATE_BOOT_COMPLETE
}; };
extern int SDK_INT; int connect_daemon(bool create = false);
extern bool RECOVERY_MODE;
extern int DAEMON_STATE;
#define APP_DATA_DIR (SDK_INT >= 24 ? "/data/user_de" : "/data/user")
// Daemon handlers // Daemon handlers
void post_fs_data(int client); void post_fs_data(int client);
@ -51,26 +47,6 @@ void boot_complete(int client);
void magiskhide_handler(int client); void magiskhide_handler(int client);
void su_daemon_handler(int client, ucred *credential); void su_daemon_handler(int client, ucred *credential);
// Misc
int connect_daemon(bool create = false);
void unlock_blocks();
void reboot();
void setup_logfile(bool reset);
// Module stuffs
void handle_modules();
void magic_mount();
void disable_modules();
void remove_modules();
void exec_module_scripts(const char *stage);
// MagiskHide // MagiskHide
void auto_start_magiskhide(); void auto_start_magiskhide(bool late_props);
int stop_magiskhide(); int stop_magiskhide();
// Scripting
void exec_script(const char *script);
void exec_common_scripts(const char *stage);
void exec_module_scripts(const char *stage, const std::vector<std::string> &module_list);
void install_apk(const char *apk);
[[noreturn]] void install_module(const char *file);

View File

@ -30,6 +30,9 @@ constexpr const char *init_applet[] = { "magiskpolicy", "supolicy", nullptr };
#define POST_FS_DATA_WAIT_TIME 40 #define POST_FS_DATA_WAIT_TIME 40
#define POST_FS_DATA_SCRIPT_MAX_TIME 35 #define POST_FS_DATA_SCRIPT_MAX_TIME 35
extern int SDK_INT;
#define APP_DATA_DIR (SDK_INT >= 24 ? "/data/user_de" : "/data/user")
// Multi-call entrypoints // Multi-call entrypoints
int magisk_main(int argc, char *argv[]); int magisk_main(int argc, char *argv[]);
int magiskhide_main(int argc, char *argv[]); int magiskhide_main(int argc, char *argv[]);

View File

@ -267,7 +267,7 @@ static void update_hide_config() {
db_err(err); db_err(err);
} }
int launch_magiskhide() { int launch_magiskhide(bool late_props) {
mutex_guard g(hide_state_lock); mutex_guard g(hide_state_lock);
if (SDK_INT < 19) if (SDK_INT < 19)
@ -289,7 +289,7 @@ int launch_magiskhide() {
return DAEMON_ERROR; return DAEMON_ERROR;
hide_sensitive_props(); hide_sensitive_props();
if (DAEMON_STATE >= STATE_BOOT_COMPLETE || DAEMON_STATE == STATE_NONE) if (late_props)
hide_late_sensitive_props(); hide_late_sensitive_props();
#if ENABLE_PTRACE_MONITOR #if ENABLE_PTRACE_MONITOR
@ -320,7 +320,7 @@ int stop_magiskhide() {
return DAEMON_SUCCESS; return DAEMON_SUCCESS;
} }
void auto_start_magiskhide() { void auto_start_magiskhide(bool late_props) {
if (hide_enabled()) { if (hide_enabled()) {
#if ENABLE_PTRACE_MONITOR #if ENABLE_PTRACE_MONITOR
pthread_kill(monitor_thread, SIGALRM); pthread_kill(monitor_thread, SIGALRM);
@ -330,7 +330,7 @@ void auto_start_magiskhide() {
db_settings dbs; db_settings dbs;
get_db_settings(dbs, HIDE_CONFIG); get_db_settings(dbs, HIDE_CONFIG);
if (dbs[HIDE_CONFIG]) if (dbs[HIDE_CONFIG])
launch_magiskhide(); launch_magiskhide(late_props);
} }
} }

View File

@ -1,14 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/types.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <daemon.hpp>
#include <utils.hpp> #include <utils.hpp>
#include "magiskhide.hpp" #include "magiskhide.hpp"
@ -53,7 +45,7 @@ void magiskhide_handler(int client) {
switch (req) { switch (req) {
case LAUNCH_MAGISKHIDE: case LAUNCH_MAGISKHIDE:
res = launch_magiskhide(); res = launch_magiskhide(true);
break; break;
case STOP_MAGISKHIDE: case STOP_MAGISKHIDE:
res = stop_magiskhide(); res = stop_magiskhide();

View File

@ -18,7 +18,7 @@
#define ENABLE_PTRACE_MONITOR 1 #define ENABLE_PTRACE_MONITOR 1
// CLI entries // CLI entries
int launch_magiskhide(); int launch_magiskhide(bool late_props);
int stop_magiskhide(); int stop_magiskhide();
int add_list(int client); int add_list(int client);
int rm_list(int client); int rm_list(int client);

View File

@ -1,7 +1,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <daemon.hpp>
#include <utils.hpp> #include <utils.hpp>
#include <selinux.hpp> #include <selinux.hpp>

View File

@ -9,13 +9,9 @@
* helper functions to handle raw input mode and terminal window resizing * helper functions to handle raw input mode and terminal window resizing
*/ */
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h>
#include <termios.h> #include <termios.h>
#include <errno.h>
#include <pthread.h>
#include <utils.hpp> #include <utils.hpp>

View File

@ -1,26 +1,19 @@
/* /*
* Copyright 2017, John Wu (@topjohnwu) * Copyright 2017 - 2021, John Wu (@topjohnwu)
* Copyright 2015, Pierre-Hugues Husson <phh@phh.me> * Copyright 2015, Pierre-Hugues Husson <phh@phh.me>
* Copyright 2010, Adam Shanks (@ChainsDD) * Copyright 2010, Adam Shanks (@ChainsDD)
* Copyright 2008, Zinx Verituse (@zinxv) * Copyright 2008, Zinx Verituse (@zinxv)
*/ */
/* su.c - The main function running in the daemon
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <fcntl.h> #include <fcntl.h>
#include <pwd.h> #include <pwd.h>
#include <errno.h>
#include <signal.h>
#include <sched.h> #include <sched.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <magisk.hpp>
#include <daemon.hpp> #include <daemon.hpp>
#include <utils.hpp> #include <utils.hpp>
#include <flags.hpp> #include <flags.hpp>

View File

@ -5,7 +5,6 @@
#include <memory> #include <memory>
#include <db.hpp> #include <db.hpp>
#include <utils.hpp>
#define DEFAULT_SHELL "/system/bin/sh" #define DEFAULT_SHELL "/system/bin/sh"

View File

@ -1,20 +1,14 @@
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <pwd.h> #include <pwd.h>
#include <time.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <daemon.hpp> #include <daemon.hpp>
#include <utils.hpp> #include <utils.hpp>
#include <selinux.hpp> #include <selinux.hpp>
#include <db.hpp>
#include "su.hpp" #include "su.hpp"
#include "pts.hpp" #include "pts.hpp"