Minor reorganization of daemons
This commit is contained in:
parent
619d48c97a
commit
acf7c0c665
@ -492,7 +492,7 @@ static void *start_magisk_hide(void *args) {
|
||||
}
|
||||
|
||||
static void auto_start_magiskhide() {
|
||||
if (!check_and_start_logger())
|
||||
if (!start_log_daemon())
|
||||
return;
|
||||
char *hide_prop = getprop2(MAGISKHIDE_PROP, 1);
|
||||
if (hide_prop == NULL || strcmp(hide_prop, "0") != 0) {
|
||||
@ -728,6 +728,9 @@ void post_fs_data(int client) {
|
||||
|
||||
xmount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL);
|
||||
|
||||
// Start log_daemon
|
||||
start_log_daemon();
|
||||
|
||||
LOGI("** post-fs-data mode running\n");
|
||||
|
||||
// Allocate buffer
|
||||
|
@ -96,7 +96,7 @@ static void *request_handler(void *args) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void main_daemon() {
|
||||
static void main_daemon() {
|
||||
android_logging();
|
||||
#ifndef MAGISK_DEBUG
|
||||
log_cb.d = nop_log;
|
||||
@ -111,11 +111,8 @@ void main_daemon() {
|
||||
xdup2(fd, STDIN_FILENO);
|
||||
close(fd);
|
||||
|
||||
// Start the log monitor
|
||||
check_and_start_logger();
|
||||
|
||||
struct sockaddr_un sun;
|
||||
socklen_t len = setup_sockaddr(&sun, MAIN_DAEMON);
|
||||
socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET);
|
||||
fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (xbind(fd, (struct sockaddr*) &sun, len))
|
||||
exit(1);
|
||||
@ -149,39 +146,24 @@ void main_daemon() {
|
||||
}
|
||||
}
|
||||
|
||||
/* Connect the daemon, set sockfd, and return if new daemon is spawned */
|
||||
int connect_daemon2(daemon_t d, int *sockfd) {
|
||||
int connect_daemon() {
|
||||
struct sockaddr_un sun;
|
||||
socklen_t len = setup_sockaddr(&sun, d);
|
||||
*sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (connect(*sockfd, (struct sockaddr*) &sun, len)) {
|
||||
socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET);
|
||||
int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (connect(fd, (struct sockaddr*) &sun, len)) {
|
||||
if (getuid() != UID_ROOT || getgid() != UID_ROOT) {
|
||||
fprintf(stderr, "No daemon is currently running!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
LOGD("client: launching new main daemon process\n");
|
||||
if (fork_dont_care() == 0) {
|
||||
LOGD("client: connect fail, try launching new daemon process\n");
|
||||
close(*sockfd);
|
||||
switch (d) {
|
||||
case MAIN_DAEMON:
|
||||
close(fd);
|
||||
main_daemon();
|
||||
break;
|
||||
case LOG_DAEMON:
|
||||
log_daemon();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (connect(*sockfd, (struct sockaddr*) &sun, len))
|
||||
while (connect(fd, (struct sockaddr*) &sun, len))
|
||||
usleep(10000);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int connect_daemon() {
|
||||
int fd;
|
||||
connect_daemon2(MAIN_DAEMON, &fd);
|
||||
return fd;
|
||||
}
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include "daemon.h"
|
||||
#include "flags.h"
|
||||
|
||||
static int loggable = 0;
|
||||
int log_daemon_started = 0;
|
||||
static struct vector log_cmd, clear_cmd;
|
||||
static int sockfd;
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
@ -109,24 +109,10 @@ static void *logcat_thread(void *args) {
|
||||
}
|
||||
}
|
||||
|
||||
int check_and_start_logger() {
|
||||
if (!loggable) {
|
||||
int fd;
|
||||
loggable = exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null", NULL) == 0;
|
||||
chmod("/dev/null", 0666);
|
||||
if (loggable) {
|
||||
connect_daemon2(LOG_DAEMON, &fd);
|
||||
write_int(fd, HANDSHAKE);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
return loggable;
|
||||
}
|
||||
|
||||
void log_daemon() {
|
||||
static void log_daemon() {
|
||||
setsid();
|
||||
struct sockaddr_un sun;
|
||||
socklen_t len = setup_sockaddr(&sun, LOG_DAEMON);
|
||||
socklen_t len = setup_sockaddr(&sun, LOG_SOCKET);
|
||||
sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (xbind(sockfd, (struct sockaddr*) &sun, len))
|
||||
exit(1);
|
||||
@ -179,10 +165,36 @@ void log_daemon() {
|
||||
events[HIDE_EVENT].fd = fd;
|
||||
pthread_mutex_unlock(&lock);
|
||||
break;
|
||||
case HANDSHAKE:
|
||||
default:
|
||||
close(fd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int start_log_daemon() {
|
||||
if (!log_daemon_started) {
|
||||
if (exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null", NULL) == 0) {
|
||||
if (fork_dont_care() == 0)
|
||||
log_daemon();
|
||||
// Wait till we can connect to log_daemon
|
||||
int fd = connect_log_daemon();
|
||||
write_int(fd, HANDSHAKE);
|
||||
close(fd);
|
||||
log_daemon_started = 1;
|
||||
}
|
||||
chmod("/dev/null", 0666);
|
||||
}
|
||||
return log_daemon_started;
|
||||
}
|
||||
|
||||
int connect_log_daemon() {
|
||||
if (!log_daemon_started)
|
||||
return -1;
|
||||
struct sockaddr_un sun;
|
||||
socklen_t len = setup_sockaddr(&sun, LOG_SOCKET);
|
||||
int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
while (connect(fd, (struct sockaddr*) &sun, len))
|
||||
usleep(10000);
|
||||
return fd;
|
||||
}
|
||||
|
@ -13,18 +13,9 @@
|
||||
|
||||
#define ABS_SOCKET_LEN(sun) (sizeof(sun->sun_family) + strlen(sun->sun_path + 1) + 1)
|
||||
|
||||
socklen_t setup_sockaddr(struct sockaddr_un *sun, daemon_t d) {
|
||||
socklen_t setup_sockaddr(struct sockaddr_un *sun, const char *name) {
|
||||
memset(sun, 0, sizeof(*sun));
|
||||
sun->sun_family = AF_LOCAL;
|
||||
const char *name;
|
||||
switch (d) {
|
||||
case MAIN_DAEMON:
|
||||
name = MAIN_SOCKET;
|
||||
break;
|
||||
case LOG_DAEMON:
|
||||
name = LOG_SOCKET;
|
||||
break;
|
||||
}
|
||||
strcpy(sun->sun_path + 1, name);
|
||||
return ABS_SOCKET_LEN(sun);
|
||||
}
|
||||
|
@ -48,18 +48,17 @@ typedef enum {
|
||||
|
||||
// daemon.c
|
||||
|
||||
void main_daemon();
|
||||
int connect_daemon();
|
||||
int connect_daemon2(daemon_t d, int *sockfd);
|
||||
|
||||
// log_monitor.c
|
||||
|
||||
void log_daemon();
|
||||
int check_and_start_logger();
|
||||
extern int log_daemon_started;
|
||||
int connect_log_daemon();
|
||||
int start_log_daemon();
|
||||
|
||||
// socket.c
|
||||
|
||||
socklen_t setup_sockaddr(struct sockaddr_un *sun, daemon_t d);
|
||||
socklen_t setup_sockaddr(struct sockaddr_un *sun, const char *name);
|
||||
int create_rand_socket(struct sockaddr_un *sun);
|
||||
int socket_accept(int sockfd, int timeout);
|
||||
int recv_fd(int sockfd);
|
||||
|
@ -45,7 +45,7 @@ void launch_magiskhide(int client) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!check_and_start_logger()) {
|
||||
if (!log_daemon_started) {
|
||||
if (client > 0) {
|
||||
write_int(client, LOGCAT_DISABLED);
|
||||
close(client);
|
||||
|
@ -125,9 +125,10 @@ void proc_monitor() {
|
||||
term_thread(TERM_THREAD);
|
||||
}
|
||||
|
||||
while(1) {
|
||||
// Connect to the log daemon
|
||||
connect_daemon2(LOG_DAEMON, &sockfd);
|
||||
sockfd = connect_log_daemon();
|
||||
if (sockfd < 0)
|
||||
return;
|
||||
write_int(sockfd, HIDE_CONNECT);
|
||||
|
||||
FILE *log_in = fdopen(sockfd, "r");
|
||||
@ -202,6 +203,4 @@ void proc_monitor() {
|
||||
if (fork_dont_care() == 0)
|
||||
hide_daemon(pid);
|
||||
}
|
||||
// The other end EOF, restart the connection
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user