From 1816ca6b0299421f864ea34586f57ba4b868488e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 14 Sep 2017 21:54:56 +0800 Subject: [PATCH] Seperate logging to another header --- jni/Android.mk | 3 +-- jni/daemon/magisk.c | 16 ++++++++++++ jni/include/logging.h | 49 +++++++++++++++++++++++++++++++++++++ jni/include/magisk.h | 32 +++--------------------- jni/include/utils.h | 1 - jni/magiskboot/magisk.h | 10 -------- jni/magiskboot/magiskboot.h | 2 +- jni/utils/misc.c | 17 +------------ jni/utils/xwrap.c | 2 +- 9 files changed, 72 insertions(+), 60 deletions(-) create mode 100644 jni/include/logging.h delete mode 100644 jni/magiskboot/magisk.h diff --git a/jni/Android.mk b/jni/Android.mk index 05b7ef7bf..c79041721 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -56,7 +56,7 @@ LOCAL_SRC_FILES := \ su/su_daemon.c \ su/su_socket.c -LOCAL_CFLAGS := -Wno-implicit-exception-spec-mismatch +LOCAL_CFLAGS := -Wno-implicit-exception-spec-mismatch -DIS_DAEMON LOCAL_CPPFLAGS := -std=c++11 LOCAL_LDLIBS := -llog include $(BUILD_EXECUTABLE) @@ -66,7 +66,6 @@ include $(CLEAR_VARS) LOCAL_MODULE := magiskboot LOCAL_STATIC_LIBRARIES := libz liblzma liblz4 libbz2 LOCAL_C_INCLUDES := \ - jni/magiskboot \ jni/include \ $(LIBZ) \ $(LIBLZMA) \ diff --git a/jni/daemon/magisk.c b/jni/daemon/magisk.c index ab5dd9a90..5ff2356f1 100644 --- a/jni/daemon/magisk.c +++ b/jni/daemon/magisk.c @@ -3,6 +3,7 @@ #include #include +#include #include "utils.h" #include "magisk.h" @@ -16,6 +17,21 @@ char *applet[] = int (*applet_main[]) (int, char *[]) = { su_client_main, resetprop_main, secilc_main, magiskpolicy_main, magiskpolicy_main, magiskpolicy_main, magiskhide_main, NULL }; +int create_links(const char *bin, const char *path) { + char self[PATH_MAX], linkpath[PATH_MAX]; + if (bin == NULL) { + xreadlink("/proc/self/exe", self, sizeof(self)); + bin = self; + } + int ret = 0; + for (int i = 0; applet[i]; ++i) { + snprintf(linkpath, sizeof(linkpath), "%s/%s", path, applet[i]); + unlink(linkpath); + ret |= symlink(bin, linkpath); + } + return ret; +} + // Global error hander function // Should be changed each thread/process __thread void (*err_handler)(void); diff --git a/jni/include/logging.h b/jni/include/logging.h new file mode 100644 index 000000000..6a95211b5 --- /dev/null +++ b/jni/include/logging.h @@ -0,0 +1,49 @@ +/* logging.h - Error handling and logging + */ + +#ifndef _LOGGING_H_ +#define _LOGGING_H_ + +#include +#include +#include + +#ifdef IS_DAEMON + +#include +#include + +#define LOG_TAG "Magisk" + +// Global handler for PLOGE +extern __thread void (*err_handler)(void); + +// Common error handlers +static inline void exit_proc() { exit(1); } +static inline void exit_thread() { pthread_exit(NULL); } +static inline void do_nothing() {} + +// Dummy function to depress debug message +static inline void stub(const char *fmt, ...) {} + +#ifdef MAGISK_DEBUG +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) +#else +#define LOGD(...) stub(__VA_ARGS__) +#endif +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) +#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) + +#define PLOGE(fmt, args...) { LOGE(fmt " failed with %d: %s", ##args, errno, strerror(errno)); err_handler(); } + +#else // IS_DAEMON + +#include + +#define LOGE(err, ...) { fprintf(stderr, __VA_ARGS__); exit(err); } +#define PLOGE(fmt, args...) { fprintf(stderr, fmt " failed with %d: %s\n\n", ##args, errno, strerror(errno)); exit(1); } + +#endif // IS_DAEMON + +#endif // _LOGGING_H_ diff --git a/jni/include/magisk.h b/jni/include/magisk.h index 83a76e670..4f4465ba6 100644 --- a/jni/include/magisk.h +++ b/jni/include/magisk.h @@ -4,11 +4,7 @@ #ifndef _MAGISK_H_ #define _MAGISK_H_ -#include -#include -#include -#include -#include +#include "logging.h" #define str(a) #a #define xstr(a) str(a) @@ -16,8 +12,6 @@ #define MAGISK_VER_STR xstr(MAGISK_VERSION) ":MAGISK" #define REQUESTOR_DAEMON_PATH "\0MAGISK" -#define LOG_TAG "Magisk" - #ifndef ARG_MAX #define ARG_MAX 4096 #endif @@ -48,33 +42,13 @@ #define MAGISKHIDE_PROP "persist.magisk.hide" -// Global handler for PLOGE -extern __thread void (*err_handler)(void); - -// Common error handlers -static inline void exit_proc() { exit(1); } -static inline void exit_thread() { pthread_exit(NULL); } -static inline void do_nothing() {} - -// Dummy function to depress debug message -static inline void stub(const char *fmt, ...) {} - -#ifdef MAGISK_DEBUG -#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) -#else -#define LOGD(...) stub(__VA_ARGS__) -#endif -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) -#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) - -#define PLOGE(fmt, args...) { LOGE(fmt " failed with %d: %s", ##args, errno, strerror(errno)); err_handler(); } - extern char *argv0; /* For changing process name */ extern char *applet[]; extern int (*applet_main[]) (int, char *[]); +int create_links(const char *bin, const char *path); + // Multi-call entrypoints int magiskhide_main(int argc, char *argv[]); int magiskpolicy_main(int argc, char *argv[]); diff --git a/jni/include/utils.h b/jni/include/utils.h index 85b708a00..308be8d1a 100644 --- a/jni/include/utils.h +++ b/jni/include/utils.h @@ -78,7 +78,6 @@ int vector_to_file(const char* filename, struct vector *v); ssize_t fdgets(char *buf, size_t size, int fd); void ps(void (*func)(int)); 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 exec_command(int err, int *fd, void (*setupenv)(struct vector*), const char *argv0, ...); diff --git a/jni/magiskboot/magisk.h b/jni/magiskboot/magisk.h deleted file mode 100644 index f8ff607f8..000000000 --- a/jni/magiskboot/magisk.h +++ /dev/null @@ -1,10 +0,0 @@ -/* magisk.h - Let MagiskBoot use the same error handling API as main magisk program - */ - -#ifndef _MAGISK_H_ -#define _MAGISK_H_ - -#define LOGE(err, ...) { fprintf(stderr, __VA_ARGS__); exit(err); } -#define PLOGE(fmt, args...) { fprintf(stderr, fmt " failed with %d: %s\n\n", ##args, errno, strerror(errno)); exit(1); } - -#endif diff --git a/jni/magiskboot/magiskboot.h b/jni/magiskboot/magiskboot.h index 7bafdbd30..ce404cdaa 100644 --- a/jni/magiskboot/magiskboot.h +++ b/jni/magiskboot/magiskboot.h @@ -14,7 +14,7 @@ #include "bootimg.h" #include "sha1.h" -#include "magisk.h" +#include "logging.h" #include "utils.h" #include "magic.h" diff --git a/jni/utils/misc.c b/jni/utils/misc.c index 653370f5b..060e55dfb 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -20,7 +20,7 @@ #include #include -#include "magisk.h" +#include "logging.h" #include "utils.h" int quit_signals[] = { SIGALRM, SIGABRT, SIGHUP, SIGPIPE, SIGQUIT, SIGTERM, SIGINT, 0 }; @@ -167,21 +167,6 @@ void ps_filter_proc_name(const char *pattern, void (*func)(int)) { ps(proc_name_filter); } -int create_links(const char *bin, const char *path) { - char self[PATH_MAX], linkpath[PATH_MAX]; - if (bin == NULL) { - xreadlink("/proc/self/exe", self, sizeof(self)); - bin = self; - } - int ret = 0; - for (int i = 0; applet[i]; ++i) { - snprintf(linkpath, sizeof(linkpath), "%s/%s", path, applet[i]); - unlink(linkpath); - ret |= symlink(bin, linkpath); - } - return ret; -} - #define DEV_BLOCK "/dev/block" void unlock_blocks() { diff --git a/jni/utils/xwrap.c b/jni/utils/xwrap.c index e5602411e..78e8eb10d 100644 --- a/jni/utils/xwrap.c +++ b/jni/utils/xwrap.c @@ -21,7 +21,7 @@ #include #include -#include "magisk.h" +#include "logging.h" #include "utils.h" FILE *xfopen(const char *pathname, const char *mode) {