mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-03 11:31:53 +00:00
Integrate MagiskSU into Magisk daemon
This commit is contained in:
@@ -36,23 +36,31 @@ static void request_handler(int client) {
|
||||
// TODO: Remove hidelist
|
||||
break;
|
||||
case SUPERUSER:
|
||||
// TODO: Run su
|
||||
su_daemon_receiver(client);
|
||||
break;
|
||||
case CHECK_VERSION:
|
||||
write_string(client, VERSION_STR);
|
||||
close(client);
|
||||
break;
|
||||
case CHECK_VERSION_CODE:
|
||||
write_int(client, VERSION_CODE);
|
||||
close(client);
|
||||
break;
|
||||
case TEST:
|
||||
s = read_string(client);
|
||||
LOGI("%s\n", s);
|
||||
free(s);
|
||||
write_int(client, 0);
|
||||
close(client);
|
||||
break;
|
||||
}
|
||||
close(client);
|
||||
}
|
||||
|
||||
/* Setup the address and return socket fd */
|
||||
static int setup_socket(struct sockaddr_un *sun) {
|
||||
int fd = xsocket(AF_LOCAL, SOCK_STREAM, 0);
|
||||
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
|
||||
PLOGE("fcntl FD_CLOEXEC");
|
||||
PLOGE("fcntl FD_CLOEXEC");
|
||||
}
|
||||
|
||||
memset(sun, 0, sizeof(*sun));
|
||||
@@ -66,7 +74,7 @@ static void do_nothing() {}
|
||||
|
||||
void start_daemon() {
|
||||
// Launch the daemon, create new session, set proper context
|
||||
if (getuid() != AID_ROOT || getgid() != AID_ROOT) {
|
||||
if (getuid() != UID_ROOT || getgid() != UID_ROOT) {
|
||||
fprintf(stderr, "Starting daemon requires root: %s\n", strerror(errno));
|
||||
PLOGE("start daemon");
|
||||
}
|
||||
@@ -90,7 +98,7 @@ void start_daemon() {
|
||||
// Change process name
|
||||
strcpy(argv0, "magisk_daemon");
|
||||
// The root daemon should not do anything if an error occurs
|
||||
// It should stay intact in any circumstances
|
||||
// It should stay intact under any circumstances
|
||||
err_handler = do_nothing;
|
||||
|
||||
// Start log monitor
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
/* daemon.h - Utility functions for daemon-client communication
|
||||
*/
|
||||
|
||||
#ifndef _DAEMON_H_
|
||||
#define _DAEMON_H_
|
||||
|
||||
|
||||
// Commands require connecting to daemon
|
||||
typedef enum {
|
||||
@@ -9,6 +12,8 @@ typedef enum {
|
||||
ADD_HIDELIST,
|
||||
RM_HIDELIST,
|
||||
SUPERUSER,
|
||||
CHECK_VERSION,
|
||||
CHECK_VERSION_CODE,
|
||||
TEST
|
||||
} client_request;
|
||||
|
||||
@@ -25,3 +30,9 @@ int read_int(int fd);
|
||||
void write_int(int fd, int val);
|
||||
char* read_string(int fd);
|
||||
void write_string(int fd, const char* val);
|
||||
|
||||
// log_monitor.c
|
||||
|
||||
void monitor_logs();
|
||||
|
||||
#endif
|
||||
|
||||
34
jni/daemon/log_monitor.c
Normal file
34
jni/daemon/log_monitor.c
Normal file
@@ -0,0 +1,34 @@
|
||||
/* log_monitor.c - New thread to monitor logcat
|
||||
*
|
||||
* Open a new thread to call logcat and get logs with tag "Magisk"
|
||||
* Also, write the logs to a log file for debugging purpose
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "magisk.h"
|
||||
#include "utils.h"
|
||||
#include "daemon.h"
|
||||
|
||||
static void *logger_thread(void *args) {
|
||||
char buffer[PATH_MAX];
|
||||
// rename("/cache/magisk.log", "/cache/last_magisk.log");
|
||||
FILE *logfile = xfopen("/cache/magisk_test.log", "w");
|
||||
// Disable buffering
|
||||
setbuf(logfile, NULL);
|
||||
// Start logcat
|
||||
FILE *p = popen("logcat -s Magisk", "r");
|
||||
while (fgets(buffer, sizeof(buffer), p)) {
|
||||
fprintf(logfile, "%s", buffer);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Start a new thread to monitor logcat and dump to logfile */
|
||||
void monitor_logs() {
|
||||
pthread_t log_monitor;
|
||||
pthread_create(&log_monitor, NULL, logger_thread, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user