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 <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

View File

@ -13,6 +13,8 @@
#include <resetprop.hpp>
#include <selinux.hpp>
#include "core.hpp"
using namespace std;
static bool safe_mode = false;
@ -320,7 +322,7 @@ void post_fs_data(int client) {
stop_magiskhide();
} else {
exec_common_scripts("post-fs-data");
auto_start_magiskhide();
auto_start_magiskhide(false);
handle_modules();
}
@ -369,7 +371,7 @@ void boot_complete(int client) {
if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700);
auto_start_magiskhide();
auto_start_magiskhide(true);
if (!check_manager()) {
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 <pthread.h>
#include <signal.h>
#include <csignal>
#include <libgen.h>
#include <sys/un.h>
#include <sys/types.h>
@ -16,11 +16,14 @@
#include <flags.hpp>
#include <stream.hpp>
#include "core.hpp"
using namespace std;
int SDK_INT = -1;
bool RECOVERY_MODE = false;
string MAGISKTMP;
bool RECOVERY_MODE = false;
int DAEMON_STATE = STATE_NONE;
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 <dlfcn.h>
#include <sys/stat.h>
#include <magisk.hpp>
#include <db.hpp>
#include <daemon.hpp>
#include <socket.hpp>
#include <utils.hpp>
#define DB_VERSION 10

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,6 @@
#include <pthread.h>
#include <string>
#include <vector>
#include <socket.hpp>
@ -39,10 +38,7 @@ enum {
STATE_BOOT_COMPLETE
};
extern int SDK_INT;
extern bool RECOVERY_MODE;
extern int DAEMON_STATE;
#define APP_DATA_DIR (SDK_INT >= 24 ? "/data/user_de" : "/data/user")
int connect_daemon(bool create = false);
// Daemon handlers
void post_fs_data(int client);
@ -51,26 +47,6 @@ void boot_complete(int client);
void magiskhide_handler(int client);
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
void auto_start_magiskhide();
void auto_start_magiskhide(bool late_props);
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_SCRIPT_MAX_TIME 35
extern int SDK_INT;
#define APP_DATA_DIR (SDK_INT >= 24 ? "/data/user_de" : "/data/user")
// Multi-call entrypoints
int magisk_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);
}
int launch_magiskhide() {
int launch_magiskhide(bool late_props) {
mutex_guard g(hide_state_lock);
if (SDK_INT < 19)
@ -289,7 +289,7 @@ int launch_magiskhide() {
return DAEMON_ERROR;
hide_sensitive_props();
if (DAEMON_STATE >= STATE_BOOT_COMPLETE || DAEMON_STATE == STATE_NONE)
if (late_props)
hide_late_sensitive_props();
#if ENABLE_PTRACE_MONITOR
@ -320,7 +320,7 @@ int stop_magiskhide() {
return DAEMON_SUCCESS;
}
void auto_start_magiskhide() {
void auto_start_magiskhide(bool late_props) {
if (hide_enabled()) {
#if ENABLE_PTRACE_MONITOR
pthread_kill(monitor_thread, SIGALRM);
@ -330,7 +330,7 @@ void auto_start_magiskhide() {
db_settings dbs;
get_db_settings(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/types.h>
#include <sys/mount.h>
#include <daemon.hpp>
#include <utils.hpp>
#include "magiskhide.hpp"
@ -53,7 +45,7 @@ void magiskhide_handler(int client) {
switch (req) {
case LAUNCH_MAGISKHIDE:
res = launch_magiskhide();
res = launch_magiskhide(true);
break;
case STOP_MAGISKHIDE:
res = stop_magiskhide();

View File

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

View File

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

View File

@ -9,13 +9,9 @@
* helper functions to handle raw input mode and terminal window resizing
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <errno.h>
#include <pthread.h>
#include <utils.hpp>
@ -116,7 +112,7 @@ static int stdin_is_raw = 0;
/**
* set_stdin_raw
*
* Changes stdin to raw unbuffered mode, disables echo,
* Changes stdin to raw unbuffered mode, disables echo,
* auto carriage return, etc.
*
* Return Value
@ -180,7 +176,7 @@ int restore_stdin(void) {
static volatile bool close_sigwinch_watcher = false;
/**
* Thread process. Wait for a SIGWINCH to be received, then update
* Thread process. Wait for a SIGWINCH to be received, then update
* the terminal size.
*/
static void *watch_sigwinch(void *data) {
@ -214,7 +210,7 @@ static void *watch_sigwinch(void *data) {
* watch_sigwinch_async
*
* After calling this function, if the application receives
* SIGWINCH, the terminal window size will be read from
* SIGWINCH, the terminal window size will be read from
* "input" and set on "output".
*
* NOTE: This function blocks SIGWINCH and spawns a thread.
@ -228,7 +224,7 @@ static void *watch_sigwinch(void *data) {
*
* Return Value
* on failure, -1 and errno will be set. In this case, no
* thread has been spawned and SIGWINCH will not be
* thread has been spawned and SIGWINCH will not be
* blocked.
* on success, 0
*/

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 2010, Adam Shanks (@ChainsDD)
* 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 <getopt.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <signal.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <magisk.hpp>
#include <daemon.hpp>
#include <utils.hpp>
#include <flags.hpp>

View File

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

View File

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