Cleanup file descriptors and add more info

This commit is contained in:
topjohnwu 2017-07-08 23:51:58 +08:00
parent b9968aa1e6
commit b570b363d9
12 changed files with 41 additions and 53 deletions

View File

@ -23,7 +23,6 @@
#include "magiskpolicy.h" #include "magiskpolicy.h"
pthread_t sepol_patch; pthread_t sepol_patch;
int null_fd;
static void *request_handler(void *args) { static void *request_handler(void *args) {
// Setup the default error handler for threads // Setup the default error handler for threads
@ -89,17 +88,12 @@ static void *request_handler(void *args) {
default: default:
break; break;
} }
// Just in case
close(client);
return NULL; return NULL;
} }
/* Setup the address and return socket fd */ /* Setup the address and return socket fd */
static int setup_socket(struct sockaddr_un *sun) { static int setup_socket(struct sockaddr_un *sun) {
int fd = xsocket(AF_LOCAL, SOCK_STREAM, 0); int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fcntl(fd, F_SETFD, FD_CLOEXEC))
PLOGE("fcntl FD_CLOEXEC");
memset(sun, 0, sizeof(*sun)); memset(sun, 0, sizeof(*sun));
sun->sun_family = AF_LOCAL; sun->sun_family = AF_LOCAL;
memcpy(sun->sun_path, REQUESTOR_DAEMON_PATH, REQUESTOR_DAEMON_PATH_LEN); memcpy(sun->sun_path, REQUESTOR_DAEMON_PATH, REQUESTOR_DAEMON_PATH_LEN);
@ -137,10 +131,11 @@ void start_daemon(int client) {
xsetsid(); xsetsid();
setcon("u:r:su:s0"); setcon("u:r:su:s0");
umask(022); umask(022);
null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC); int fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
xdup2(null_fd, STDIN_FILENO); xdup2(fd, STDIN_FILENO);
xdup2(null_fd, STDOUT_FILENO); xdup2(fd, STDOUT_FILENO);
xdup2(null_fd, STDERR_FILENO); xdup2(fd, STDERR_FILENO);
close(fd);
// Patch selinux with medium patch before we do anything // Patch selinux with medium patch before we do anything
load_policydb(SELINUX_POLICY); load_policydb(SELINUX_POLICY);
@ -151,7 +146,7 @@ void start_daemon(int client) {
pthread_create(&sepol_patch, NULL, large_sepol_patch, NULL); pthread_create(&sepol_patch, NULL, large_sepol_patch, NULL);
struct sockaddr_un sun; struct sockaddr_un sun;
int fd = setup_socket(&sun); fd = setup_socket(&sun);
xbind(fd, (struct sockaddr*) &sun, sizeof(sun)); xbind(fd, (struct sockaddr*) &sun, sizeof(sun));
xlisten(fd, 10); xlisten(fd, 10);
@ -162,7 +157,7 @@ void start_daemon(int client) {
// It should stay intact under any circumstances // It should stay intact under any circumstances
err_handler = do_nothing; err_handler = do_nothing;
LOGI("Magisk v" xstr(MAGISK_VERSION) " daemon started\n"); LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") daemon started\n");
// Unlock all blocks for rw // Unlock all blocks for rw
unlock_blocks(); unlock_blocks();
@ -178,9 +173,7 @@ void start_daemon(int client) {
// Loop forever to listen for requests // Loop forever to listen for requests
while(1) { while(1) {
int *client = xmalloc(sizeof(int)); int *client = xmalloc(sizeof(int));
*client = xaccept(fd, NULL, NULL); *client = xaccept4(fd, NULL, NULL, SOCK_CLOEXEC);
// Just in case, set to close on exec
fcntl(*client, F_SETFD, FD_CLOEXEC);
pthread_t thread; pthread_t thread;
xpthread_create(&thread, NULL, request_handler, client); xpthread_create(&thread, NULL, request_handler, client);
// Detach the thread, we will never join it // Detach the thread, we will never join it
@ -192,7 +185,6 @@ void start_daemon(int client) {
int connect_daemon() { int connect_daemon() {
struct sockaddr_un sun; struct sockaddr_un sun;
int fd = setup_socket(&sun); int fd = setup_socket(&sun);
// LOGD("client: trying to connect socket\n");
if (connect(fd, (struct sockaddr*) &sun, sizeof(sun))) { if (connect(fd, (struct sockaddr*) &sun, sizeof(sun))) {
/* If we cannot access the daemon, we start the daemon /* If we cannot access the daemon, we start the daemon
* since there is no clear entry point when the daemon should be started * since there is no clear entry point when the daemon should be started

View File

@ -75,7 +75,6 @@ extern char *argv0; /* For changing process name */
extern char *applet[]; extern char *applet[];
extern int (*applet_main[]) (int, char *[]); extern int (*applet_main[]) (int, char *[]);
extern int null_fd;
// Multi-call entrypoints // Multi-call entrypoints
int magiskhide_main(int argc, char *argv[]); int magiskhide_main(int argc, char *argv[]);

View File

@ -58,7 +58,7 @@ static void usage(char *arg0) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
printf("MagiskBoot v" xstr(MAGISK_VERSION) " (by topjohnwu) - Boot Image Modification Tool\n\n"); printf("MagiskBoot v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Boot Image Modification Tool\n\n");
if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) { if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) {
cleanup(); cleanup();

View File

@ -53,6 +53,7 @@ int hide_daemon() {
// Set the process name // Set the process name
strcpy(argv0, "magiskhide_daemon"); strcpy(argv0, "magiskhide_daemon");
LOGD("hide_daemon: listening for hide requests");
// When an error occurs, report its failure to main process // When an error occurs, report its failure to main process
err_handler = hide_daemon_err; err_handler = hide_daemon_err;

View File

@ -16,7 +16,6 @@
#include "daemon.h" #include "daemon.h"
#include "resetprop.h" #include "resetprop.h"
int sv[2], hide_pid = -1;
struct vector *hide_list = NULL; struct vector *hide_list = NULL;
int hideEnabled = 0; int hideEnabled = 0;
@ -29,7 +28,7 @@ void kill_proc(int pid) {
static void usage(char *arg0) { static void usage(char *arg0) {
fprintf(stderr, fprintf(stderr,
"MagiskHide v" xstr(MAGISK_VERSION) " (by topjohnwu) - Hide Magisk!\n\n" "MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n"
"%s [--options [arguments...] ]\n\n" "%s [--options [arguments...] ]\n\n"
"Options:\n" "Options:\n"
" --enable: Start the magiskhide daemon\n" " --enable: Start the magiskhide daemon\n"
@ -63,19 +62,6 @@ void launch_magiskhide(int client) {
hide_sensitive_props(); hide_sensitive_props();
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1)
goto error;
/*
* The setns system call do not support multithread processes
* We have to fork a new process, and communicate with sockets
*/
if (hide_daemon())
goto error;
close(sv[1]);
// Initialize the mutex lock // Initialize the mutex lock
pthread_mutex_init(&hide_lock, NULL); pthread_mutex_init(&hide_lock, NULL);
pthread_mutex_init(&file_lock, NULL); pthread_mutex_init(&file_lock, NULL);
@ -87,8 +73,10 @@ void launch_magiskhide(int client) {
// Add SafetyNet by default // Add SafetyNet by default
add_list(strdup("com.google.android.gms.unstable")); add_list(strdup("com.google.android.gms.unstable"));
write_int(client, DAEMON_SUCCESS); if (client > 0) {
close(client); write_int(client, DAEMON_SUCCESS);
close(client);
}
// Get thread reference // Get thread reference
proc_monitor_thread = pthread_self(); proc_monitor_thread = pthread_self();
@ -98,15 +86,9 @@ void launch_magiskhide(int client) {
error: error:
hideEnabled = 0; hideEnabled = 0;
write_int(client, DAEMON_ERROR); if (client > 0) {
close(client); write_int(client, DAEMON_ERROR);
if (hide_pid != -1) { close(client);
int kill = -1;
// Kill hide daemon
write(sv[0], &kill, sizeof(kill));
close(sv[0]);
waitpid(hide_pid, NULL, 0);
hide_pid = -1;
} }
return; return;
} }

View File

@ -22,6 +22,8 @@ static char init_ns[32], zygote_ns[2][32];
static int log_pid, log_fd; static int log_pid, log_fd;
static char *buffer; static char *buffer;
int sv[2], hide_pid = -1;
// Workaround for the lack of pthread_cancel // Workaround for the lack of pthread_cancel
static void quit_pthread(int sig) { static void quit_pthread(int sig) {
err_handler = do_nothing; err_handler = do_nothing;
@ -103,10 +105,22 @@ void proc_monitor() {
LOGI("proc_monitor: zygote ns=%s\n", zygote_ns[0]); LOGI("proc_monitor: zygote ns=%s\n", zygote_ns[0]);
break; break;
case 2: case 2:
LOGI("proc_monitor: zygote (32-bit) ns=%s (64-bit) ns=%s\n", zygote_ns[0], zygote_ns[1]); LOGI("proc_monitor: zygote ns=%s zygote64 ns=%s\n", zygote_ns[0], zygote_ns[1]);
break; break;
} }
if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1)
quit_pthread(SIGUSR1);
/*
* The setns system call do not support multithread processes
* We have to fork a new process, and communicate with sockets
*/
if (hide_daemon())
quit_pthread(SIGUSR1);
close(sv[1]);
while (1) { while (1) {
// Clear previous buffer // Clear previous buffer
system("logcat -b events -c"); system("logcat -b events -c");

@ -1 +1 @@
Subproject commit 193d160bed24f75d2dd440063bfc00d7920cf789 Subproject commit 5529dab84e44856679406edd6560d26c61e1e715

View File

@ -22,7 +22,7 @@ __thread void (*err_handler)(void);
static void usage() { static void usage() {
fprintf(stderr, fprintf(stderr,
"Magisk v" xstr(MAGISK_VERSION) " multi-call binary\n" "Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) multi-call binary\n"
"\n" "\n"
"Usage: %s [applet [arguments]...]\n" "Usage: %s [applet [arguments]...]\n"
" or: %s --install [SOURCE] <DIR> \n" " or: %s --install [SOURCE] <DIR> \n"

View File

@ -108,7 +108,7 @@ static bool is_legal_property_name(const char* name, size_t namelen) {
static int usage(char* arg0) { static int usage(char* arg0) {
fprintf(stderr, fprintf(stderr,
"resetprop v" xstr(MAGISK_VERSION) " (by topjohnwu & nkk71) - System Props Modification Tool\n\n" "resetprop v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu & nkk71) - System Props Modification Tool\n\n"
"Usage: %s [options] [args...]\n" "Usage: %s [options] [args...]\n"
"%s <name> <value>: Set property entry <name> with <value>\n" "%s <name> <value>: Set property entry <name> with <value>\n"
"%s --file <prop file>: Load props from <prop file>\n" "%s --file <prop file>: Load props from <prop file>\n"

2
jni/su

@ -1 +1 @@
Subproject commit 91ea6801679ddd0b05c893065520acb29f713e6f Subproject commit e2821025ef7348085958ac8f76e51b08b0e9647f

View File

@ -40,7 +40,7 @@ int xsocket(int domain, int type, int protocol);
int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int xlisten(int sockfd, int backlog); int xlisten(int sockfd, int backlog);
int xaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
void *xmalloc(size_t size); void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size); void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t size); void *xrealloc(void *ptr, size_t size);

View File

@ -155,8 +155,8 @@ int xlisten(int sockfd, int backlog) {
return ret; return ret;
} }
int xaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
int fd = accept(sockfd, addr, addrlen); int fd = accept4(sockfd, addr, addrlen, flags);
if (fd == -1) { if (fd == -1) {
PLOGE("accept"); PLOGE("accept");
} }