From ff3710de66aa1afe655d97f92c6893b460134e4a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 30 Jun 2019 19:09:31 -0700 Subject: [PATCH] Minor code changes across all sources --- native/jni/core/socket.cpp | 6 ++--- native/jni/init/magiskrc.h | 2 +- native/jni/init/rootdir.cpp | 1 + native/jni/magiskhide/hide_policy.cpp | 5 ---- native/jni/magiskhide/magiskhide.cpp | 1 - native/jni/magiskhide/proc_monitor.cpp | 2 +- native/jni/magiskpolicy/magiskpolicy.cpp | 2 +- native/jni/magiskpolicy/magiskpolicy.h | 13 +++------ native/jni/magiskpolicy/rules.cpp | 4 +-- native/jni/magiskpolicy/sepolicy.h | 13 +++------ native/jni/resetprop/persist_properties.cpp | 2 +- .../{_resetprop.h => private/resetprop.h} | 13 +++------ native/jni/resetprop/resetprop.cpp | 27 ++++++++++--------- native/jni/su/connect.cpp | 1 - native/jni/su/pts.cpp | 2 +- native/jni/su/su.cpp | 25 +++++++++-------- native/jni/su/su.h | 2 +- native/jni/su/su_daemon.cpp | 2 +- native/jni/utils/file.cpp | 1 - native/jni/utils/include/selinux.h | 8 ++---- 20 files changed, 50 insertions(+), 82 deletions(-) rename native/jni/resetprop/{_resetprop.h => private/resetprop.h} (85%) diff --git a/native/jni/core/socket.cpp b/native/jni/core/socket.cpp index eb0293e83..84f6abd03 100644 --- a/native/jni/core/socket.cpp +++ b/native/jni/core/socket.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #define ABS_SOCKET_LEN(sun) (sizeof(sa_family_t) + strlen(sun->sun_path + 1) + 1) @@ -34,7 +34,7 @@ int socket_accept(int sockfd, int timeout) { .fd = sockfd, .events = POLL_IN }; - return xpoll(&pfd, 1, timeout * 1000) <= 0 ? -1 : xaccept4(sockfd, NULL, NULL, SOCK_CLOEXEC); + return xpoll(&pfd, 1, timeout * 1000) <= 0 ? -1 : xaccept4(sockfd, nullptr, nullptr, SOCK_CLOEXEC); } void get_client_cred(int fd, struct ucred *cred) { @@ -86,7 +86,7 @@ int recv_fd(int sockfd) { cmsg = CMSG_FIRSTHDR(&msg); - if (cmsg == NULL || + if (cmsg == nullptr || cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { diff --git a/native/jni/init/magiskrc.h b/native/jni/init/magiskrc.h index f0d7a7554..1e9a1b7a1 100644 --- a/native/jni/init/magiskrc.h +++ b/native/jni/init/magiskrc.h @@ -1,5 +1,5 @@ #include -#include +#include constexpr const char magiskrc[] = "\n\n" diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index bdf0fb465..a041358df 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "init.h" diff --git a/native/jni/magiskhide/hide_policy.cpp b/native/jni/magiskhide/hide_policy.cpp index 184dbf328..0ef62f76f 100644 --- a/native/jni/magiskhide/hide_policy.cpp +++ b/native/jni/magiskhide/hide_policy.cpp @@ -63,11 +63,6 @@ void hide_unmount(int pid) { chmod(SELINUX_POLICY, 0440); } - getprop([](const char *name, auto, auto) { - if (strstr(name, "magisk")) - deleteprop(name); - }, nullptr, false); - vector targets; // Unmount dummy skeletons and /sbin links diff --git a/native/jni/magiskhide/magiskhide.cpp b/native/jni/magiskhide/magiskhide.cpp index 0eef9e766..a8194c647 100644 --- a/native/jni/magiskhide/magiskhide.cpp +++ b/native/jni/magiskhide/magiskhide.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 81f1e3471..eaca2a836 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include "magiskhide.h" diff --git a/native/jni/magiskpolicy/magiskpolicy.cpp b/native/jni/magiskpolicy/magiskpolicy.cpp index 606eb44c4..9344bb886 100644 --- a/native/jni/magiskpolicy/magiskpolicy.cpp +++ b/native/jni/magiskpolicy/magiskpolicy.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include diff --git a/native/jni/magiskpolicy/magiskpolicy.h b/native/jni/magiskpolicy/magiskpolicy.h index 171e414c6..b7bd1a725 100644 --- a/native/jni/magiskpolicy/magiskpolicy.h +++ b/native/jni/magiskpolicy/magiskpolicy.h @@ -1,17 +1,14 @@ /* magiskpolicy.h - Public API for policy patching */ -#ifndef _MAGISKPOLICY_H -#define _MAGISKPOLICY_H +#pragma once #include #include #define ALL NULL -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS // policydb functions int load_policydb(const char *file); @@ -41,8 +38,4 @@ int sepol_exists(const char *source); // Built in rules void sepol_magisk_rules(); -#ifdef __cplusplus -}; -#endif - -#endif +__END_DECLS diff --git a/native/jni/magiskpolicy/rules.cpp b/native/jni/magiskpolicy/rules.cpp index 5dcbd40b5..e53867e68 100644 --- a/native/jni/magiskpolicy/rules.cpp +++ b/native/jni/magiskpolicy/rules.cpp @@ -1,8 +1,8 @@ -#include +#include +#include #include "magiskpolicy.h" #include "sepolicy.h" -#include "flags.h" static void allowSuClient(const char *target) { if (!sepol_exists(target)) diff --git a/native/jni/magiskpolicy/sepolicy.h b/native/jni/magiskpolicy/sepolicy.h index 4c5cc1c26..1ff36ee4c 100644 --- a/native/jni/magiskpolicy/sepolicy.h +++ b/native/jni/magiskpolicy/sepolicy.h @@ -1,14 +1,11 @@ /* sepolicy.h - Header for magiskpolicy non-public APIs */ -#ifndef _SEPOLICY_H -#define _SEPOLICY_H +#pragma once #include -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS // Global policydb extern policydb_t *policydb; @@ -36,8 +33,4 @@ int add_rule(const char *s, const char *t, const char *c, const char *p, int eff int add_xperm_rule(const char *s, const char *t, const char *c, const char *range, int effect, int n); int add_type_rule(const char *s, const char *t, const char *c, const char *d, int effect); -#ifdef __cplusplus -}; -#endif - -#endif +__END_DECLS diff --git a/native/jni/resetprop/persist_properties.cpp b/native/jni/resetprop/persist_properties.cpp index b6653cc2d..b4819a628 100644 --- a/native/jni/resetprop/persist_properties.cpp +++ b/native/jni/resetprop/persist_properties.cpp @@ -11,7 +11,7 @@ #include #include -#include "_resetprop.h" +#include "private/resetprop.h" using namespace std; diff --git a/native/jni/resetprop/_resetprop.h b/native/jni/resetprop/private/resetprop.h similarity index 85% rename from native/jni/resetprop/_resetprop.h rename to native/jni/resetprop/private/resetprop.h index 300d25ef7..3246917be 100644 --- a/native/jni/resetprop/_resetprop.h +++ b/native/jni/resetprop/private/resetprop.h @@ -1,14 +1,9 @@ -/* resetprop.h - Internal struct definitions - */ - - -#ifndef MAGISK_PROPS_H -#define MAGISK_PROPS_H +#pragma once #include #include -#include "private/system_properties.h" +#include "system_properties.h" struct prop_t { char *name; @@ -28,7 +23,7 @@ struct prop_t { bool operator<(const prop_t &prop) const { return strcmp(name, prop.name) < 0; } - prop_t& operator= (prop_t &&prop) { + prop_t& operator=(prop_t &&prop) { if (this != &prop) { free(name); name = prop.name; @@ -60,5 +55,3 @@ std::string persist_getprop(const char *name); void persist_getprop(read_cb_t *read_cb); bool persist_deleteprop(const char *name); void collect_props(const char *name, const char *value, void *v_plist); - -#endif //MAGISK_PROPS_H diff --git a/native/jni/resetprop/resetprop.cpp b/native/jni/resetprop/resetprop.cpp index e29b26bbf..44d424f47 100644 --- a/native/jni/resetprop/resetprop.cpp +++ b/native/jni/resetprop/resetprop.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include @@ -17,7 +17,7 @@ #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ #include "private/_system_properties.h" #include "private/system_properties.h" -#include "_resetprop.h" +#include "private/resetprop.h" using namespace std; @@ -80,15 +80,14 @@ illegal: static void read_props(const prop_info *pi, void *read_cb) { __system_property_read_callback( - pi, [](auto cb, auto name, auto value, auto) -> void - { + pi, [](auto cb, auto name, auto value, auto) { ((read_cb_t *) cb)->exec(name, value); }, read_cb); } void collect_props(const char *name, const char *value, void *v_plist) { - auto &prop_list = *static_cast *>(v_plist); - prop_list.emplace_back(name, value); + auto prop_list = static_cast *>(v_plist); + prop_list->emplace_back(name, value); } static void collect_unique_props(const char *name, const char *value, void *v_plist) { @@ -121,15 +120,18 @@ static void print_props(bool persist) { * Implementations of functions in resetprop.h (APIs) * **************************************************/ +#define ENSURE_INIT(ret) if (init_resetprop()) return ret + int prop_exist(const char *name) { - if (init_resetprop()) return 0; + ENSURE_INIT(0); return __system_property_find(name) != nullptr; } // Get prop by name, return string string getprop(const char *name, bool persist) { - if (!check_legal_property_name(name) || init_resetprop()) + if (!check_legal_property_name(name)) return string(); + ENSURE_INIT(string()); const prop_info *pi = __system_property_find(name); if (pi == nullptr) { if (persist && strncmp(name, "persist.", 8) == 0) { @@ -151,7 +153,7 @@ string getprop(const char *name, bool persist) { } void getprop(void (*callback)(const char *, const char *, void *), void *cookie, bool persist) { - if (init_resetprop()) return; + ENSURE_INIT(); read_cb_t read_cb(callback, cookie); __system_property_foreach(read_props, &read_cb); if (persist) { @@ -163,8 +165,7 @@ void getprop(void (*callback)(const char *, const char *, void *), void *cookie, int setprop(const char *name, const char *value, bool trigger) { if (!check_legal_property_name(name)) return 1; - if (init_resetprop()) - return -1; + ENSURE_INIT(-1); int ret; @@ -197,7 +198,7 @@ int setprop(const char *name, const char *value, bool trigger) { int deleteprop(const char *name, bool persist) { if (!check_legal_property_name(name)) return 1; - if (init_resetprop()) return -1; + ENSURE_INIT(-1); char path[PATH_MAX]; path[0] = '\0'; LOGD("resetprop: deleteprop [%s]\n", name); @@ -207,7 +208,7 @@ int deleteprop(const char *name, bool persist) { } void load_prop_file(const char *filename, bool trigger) { - if (init_resetprop()) return; + ENSURE_INIT(); LOGD("resetprop: Parse prop file [%s]\n", filename); parse_prop_file(filename, [=](auto key, auto val) -> bool { setprop(key.data(), val.data(), trigger); diff --git a/native/jni/su/connect.cpp b/native/jni/su/connect.cpp index 4cf8720f1..e58e50049 100644 --- a/native/jni/su/connect.cpp +++ b/native/jni/su/connect.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include diff --git a/native/jni/su/pts.cpp b/native/jni/su/pts.cpp index 7fa7fec5e..99b7c535c 100644 --- a/native/jni/su/pts.cpp +++ b/native/jni/su/pts.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include #include "pts.h" diff --git a/native/jni/su/su.cpp b/native/jni/su/su.cpp index 318452f76..3253d51ca 100644 --- a/native/jni/su/su.cpp +++ b/native/jni/su/su.cpp @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -82,7 +81,7 @@ static void sighandler(int sig) { memset(&act, 0, sizeof(act)); act.sa_handler = SIG_DFL; for (int i = 0; quit_signals[i]; ++i) { - sigaction(quit_signals[i], &act, NULL); + sigaction(quit_signals[i], &act, nullptr); } } @@ -91,7 +90,7 @@ static void setup_sighandlers(void (*handler)(int)) { memset(&act, 0, sizeof(act)); act.sa_handler = handler; for (int i = 0; quit_signals[i]; ++i) { - sigaction(quit_signals[i], &act, NULL); + sigaction(quit_signals[i], &act, nullptr); } } @@ -107,15 +106,15 @@ su_request::su_request() int su_client_main(int argc, char *argv[]) { int c; struct option long_opts[] = { - { "command", required_argument, NULL, 'c' }, - { "help", no_argument, NULL, 'h' }, - { "login", no_argument, NULL, 'l' }, - { "preserve-environment", no_argument, NULL, 'p' }, - { "shell", required_argument, NULL, 's' }, - { "version", no_argument, NULL, 'v' }, - { "context", required_argument, NULL, 'z' }, - { "mount-master", no_argument, NULL, 'M' }, - { NULL, 0, NULL, 0 }, + { "command", required_argument, nullptr, 'c' }, + { "help", no_argument, nullptr, 'h' }, + { "login", no_argument, nullptr, 'l' }, + { "preserve-environment", no_argument, nullptr, 'p' }, + { "shell", required_argument, nullptr, 's' }, + { "version", no_argument, nullptr, 'v' }, + { "context", required_argument, nullptr, 'z' }, + { "mount-master", no_argument, nullptr, 'M' }, + { nullptr, 0, nullptr, 0 }, }; su_request su_req; @@ -128,7 +127,7 @@ int su_client_main(int argc, char *argv[]) { strcpy(argv[i], "-M"); } - while ((c = getopt_long(argc, argv, "c:hlmps:Vvuz:M", long_opts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "c:hlmps:Vvuz:M", long_opts, nullptr)) != -1) { switch (c) { case 'c': su_req.command = concat_commands(argc, argv); diff --git a/native/jni/su/su.h b/native/jni/su/su.h index 33672da7e..6aa4b1fcd 100644 --- a/native/jni/su/su.h +++ b/native/jni/su/su.h @@ -8,7 +8,7 @@ #include #include -#include "db.h" +#include #define DEFAULT_SHELL "/system/bin/sh" diff --git a/native/jni/su/su_daemon.cpp b/native/jni/su/su_daemon.cpp index 1cb6298d4..c498ab2f8 100644 --- a/native/jni/su/su_daemon.cpp +++ b/native/jni/su/su_daemon.cpp @@ -12,7 +12,7 @@ #include #include -#include +#include #include #include #include diff --git a/native/jni/utils/file.cpp b/native/jni/utils/file.cpp index 51c8064a5..42f8463b5 100644 --- a/native/jni/utils/file.cpp +++ b/native/jni/utils/file.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include diff --git a/native/jni/utils/include/selinux.h b/native/jni/utils/include/selinux.h index 21b0a4096..322afaaff 100644 --- a/native/jni/utils/include/selinux.h +++ b/native/jni/utils/include/selinux.h @@ -18,9 +18,7 @@ #define SEPOL_PROC_DOMAIN "magisk" #define SEPOL_FILE_DOMAIN "magisk_file" -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS extern void (*freecon)(char *con); extern int (*setcon)(const char *con); @@ -38,6 +36,4 @@ void dload_selinux(); void restorecon(); void restore_rootcon(); -#ifdef __cplusplus -} -#endif +__END_DECLS