From a19c7215d2bf5bba14c01d3d8e222647e247e4ce Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 15 Feb 2019 04:31:39 -0500 Subject: [PATCH] Better nice name --- native/jni/core/applets.cpp | 8 +++----- native/jni/core/daemon.cpp | 2 +- native/jni/include/magisk.h | 1 - native/jni/utils/include/utils.h | 2 ++ native/jni/utils/misc.cpp | 12 ++++++++++++ 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/native/jni/core/applets.cpp b/native/jni/core/applets.cpp index eed645048..f130a8dab 100644 --- a/native/jni/core/applets.cpp +++ b/native/jni/core/applets.cpp @@ -6,17 +6,15 @@ #include #include +#include static int (*applet_main[]) (int, char *[]) = { magisk_main, su_client_main, resetprop_main, magiskhide_main, nullptr }; -char *argv0; - __attribute__((noreturn)) static void call_applets(int argc, char *argv[]) { // Applets for (int i = 0; applet_names[i]; ++i) { if (strcmp(basename(argv[0]), applet_names[i]) == 0) { - strcpy(argv0, basename(argv[0])); exit((*applet_main[i])(argc, argv)); } } @@ -26,11 +24,11 @@ __attribute__((noreturn)) static void call_applets(int argc, char *argv[]) { int main(int argc, char *argv[]) { umask(0); - argv0 = argv[0]; dload_selinux(); cmdline_logging(); + init_argv0(argc, argv); - if (strcmp(basename(argv0), "magisk.bin") == 0 || + if (strcmp(basename(argv[0]), "magisk.bin") == 0 || (strcmp(basename(argv[0]), "magisk") == 0 && argc > 1 && argv[1][0] != '-')) { --argc; diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 7144d701e..589f7a324 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -132,7 +132,7 @@ static void main_daemon() { xlisten(fd, 10); // Change process name - strcpy(argv0, "magiskd"); + set_nice_name("magiskd"); // Block all signals sigset_t block_set; diff --git a/native/jni/include/magisk.h b/native/jni/include/magisk.h index 94b3b5262..ec31181ab 100644 --- a/native/jni/include/magisk.h +++ b/native/jni/include/magisk.h @@ -39,7 +39,6 @@ #define SEPOL_PROC_DOMAIN "magisk" #define SEPOL_FILE_DOMAIN "magisk_file" -extern char *argv0; /* For changing process name */ extern int SDK_INT; #define applet_names ((const char *[]) { "magisk", "su", "resetprop", "magiskhide", nullptr }) diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index 11e56d36a..22bac5886 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -85,6 +85,8 @@ int fork_dont_care(); int fork_no_zombie(); void gen_rand_str(char *buf, int len); int strend(const char *s1, const char *s2); +void init_argv0(int argc, char **argv); +void set_nice_name(const char *name); #define getline __getline #define getdelim __getdelim diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index 95d41e56f..8eac4b48a 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -224,3 +224,15 @@ int new_daemon_thread(void *(*start_routine) (void *), void *arg, const pthread_ return ret; } +static char *argv0; +static size_t name_len; +void init_argv0(int argc, char **argv) { + argv0 = argv[0]; + name_len = (argv[argc - 1] - argv[0]) + strlen(argv[argc - 1]) + 1; +} + +void set_nice_name(const char *name) { + memset(argv0, 0, name_len); + strlcpy(argv0, name, name_len); + prctl(PR_SET_NAME, name); +}