Invincible mode implemented in magisklogd

This commit is contained in:
topjohnwu 2018-07-03 01:38:19 +08:00
parent c88dc8795b
commit 6c4d81b1e9
5 changed files with 46 additions and 9 deletions

View File

@ -662,7 +662,7 @@ void startup() {
}
// Start post-fs-data mode
execl("/sbin/magisk", "magisk", "--post-fs-data", NULL);
execl("/sbin/magisk.bin", "magisk", "--post-fs-data", NULL);
}
void post_fs_data(int client) {

View File

@ -83,6 +83,9 @@ static void *request_handler(void *args) {
case LATE_START:
late_start(client);
break;
case MONITOR:
/* Do NOT close the client, make it hold */
break;
default:
close(client);
break;
@ -117,13 +120,6 @@ void main_daemon() {
xdup2(fd, STDIN_FILENO);
close(fd);
// Block user signals
sigset_t block_set;
sigemptyset(&block_set);
sigaddset(&block_set, SIGUSR1);
sigaddset(&block_set, SIGUSR2);
pthread_sigmask(SIG_SETMASK, &block_set, NULL);
// Start the log monitor
monitor_logs();
@ -138,6 +134,19 @@ void main_daemon() {
// Change process name
strcpy(argv0, "magiskd");
// Block all user signals
sigset_t block_set;
sigemptyset(&block_set);
sigaddset(&block_set, SIGUSR1);
sigaddset(&block_set, SIGUSR2);
pthread_sigmask(SIG_SETMASK, &block_set, NULL);
// Ignore SIGPIPE
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, NULL);
// Loop forever to listen for requests
while(1) {
int *client = xmalloc(sizeof(int));

View File

@ -85,6 +85,26 @@ static void *socket_thread(void *args) {
}
}
static void *monitor_thread(void *args) {
// Block SIGPIPE to prevent interruption
sigset_t block_set;
sigemptyset(&block_set);
sigaddset(&block_set, SIGPIPE);
pthread_sigmask(SIG_SETMASK, &block_set, NULL);
// Give the main daemon some time before we monitor it
sleep(5);
int fd;
char b[1];
do {
fd = connect_daemon();
write_int(fd, MONITOR);
// This should hold unless the daemon is killed
read(fd, b, sizeof(b));
// The main daemon crashed, spawn a new one
close(fd);
} while (1);
}
void log_daemon() {
setsid();
strcpy(argv0, "magisklogd");
@ -96,6 +116,13 @@ void log_daemon() {
xlisten(sockfd, 1);
LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") logger started\n");
// Start invincible mode monitor
// TODO: Remove this when all crashes are fixed
pthread_t t;
pthread_create(&t, NULL, monitor_thread, NULL);
pthread_detach(t);
// Set SIGPIPE handler
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sigpipe_handler;

View File

@ -25,6 +25,7 @@ enum {
ADD_HIDELIST,
RM_HIDELIST,
LS_HIDELIST,
MONITOR,
HIDE_CONNECT
};

@ -1 +1 @@
Subproject commit ead8c03a3a6eed6621ffcf4bc2fd12429da3596e
Subproject commit 69b226b005e3f87c4c872fa3382e05006969118e