mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-01-04 16:27:39 +00:00
Reorganization
This commit is contained in:
parent
60b3b8ddce
commit
cce636224c
@ -32,12 +32,14 @@ LOCAL_C_INCLUDES := \
|
||||
$(LIBUTILS)
|
||||
|
||||
LOCAL_SRC_FILES := \
|
||||
core/magisk.c \
|
||||
core/daemon.c \
|
||||
core/log_daemon.c \
|
||||
core/bootstages.c \
|
||||
core/socket.c \
|
||||
core/db.c \
|
||||
main.c \
|
||||
img.c \
|
||||
daemon/magisk.c \
|
||||
daemon/daemon.c \
|
||||
daemon/log_daemon.c \
|
||||
daemon/bootstages.c \
|
||||
daemon/socket.c \
|
||||
daemon/db.c \
|
||||
magiskhide/magiskhide.c \
|
||||
magiskhide/proc_monitor.c \
|
||||
magiskhide/hide_utils.c \
|
||||
@ -48,8 +50,7 @@ LOCAL_SRC_FILES := \
|
||||
su/su.c \
|
||||
su/connect.c \
|
||||
su/pts.c \
|
||||
su/su_daemon.c \
|
||||
utils/img.c
|
||||
su/su_daemon.c
|
||||
|
||||
LOCAL_LDLIBS := -llog
|
||||
include $(BUILD_EXECUTABLE)
|
||||
@ -72,7 +73,7 @@ LOCAL_C_INCLUDES := \
|
||||
$(LIBUTILS)
|
||||
|
||||
LOCAL_SRC_FILES := \
|
||||
core/magiskinit.c \
|
||||
init.c \
|
||||
magiskpolicy/api.c \
|
||||
magiskpolicy/magiskpolicy.c \
|
||||
magiskpolicy/rules.c \
|
||||
|
@ -593,21 +593,23 @@ void startup() {
|
||||
setfilecon("/sbin", "u:object_r:rootfs:s0");
|
||||
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
|
||||
|
||||
// Create wrapper
|
||||
fd = creat("/sbin/magisk", 0755);
|
||||
xwrite(fd, wrapper, sizeof(wrapper) - 1);
|
||||
close(fd);
|
||||
setfilecon("/sbin/magisk", "u:object_r:"SEPOL_FILE_DOMAIN":s0");
|
||||
// Setup magisk symlinks
|
||||
// Create applet symlinks
|
||||
for (int i = 0; applet_names[i]; ++i) {
|
||||
snprintf(buf, PATH_MAX, "/sbin/%s", applet_names[i]);
|
||||
xsymlink("/sbin/magisk", buf);
|
||||
}
|
||||
|
||||
// Setup binary and wrapper
|
||||
fd = creat("/sbin/magisk.bin", 0755);
|
||||
xwrite(fd, magisk, magisk_size);
|
||||
close(fd);
|
||||
free(magisk);
|
||||
unlink("/sbin/magisk");
|
||||
fd = creat("/sbin/magisk", 0755);
|
||||
xwrite(fd, wrapper, sizeof(wrapper) - 1);
|
||||
close(fd);
|
||||
setfilecon("/sbin/magisk.bin", "u:object_r:"SEPOL_FILE_DOMAIN":s0");
|
||||
for (int i = 0; applet[i]; ++i) {
|
||||
snprintf(buf, PATH_MAX, "/sbin/%s", applet[i]);
|
||||
xsymlink("/sbin/magisk", buf);
|
||||
}
|
||||
setfilecon("/sbin/magisk", "u:object_r:"SEPOL_FILE_DOMAIN":s0");
|
||||
|
||||
// Setup magiskinit symlinks
|
||||
fd = creat("/sbin/magiskinit", 0755);
|
@ -1,6 +1,3 @@
|
||||
/* main.c - The multicall entry point
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
@ -13,11 +10,6 @@
|
||||
#include "selinux.h"
|
||||
#include "flags.h"
|
||||
|
||||
char *argv0;
|
||||
|
||||
int (*applet_main[]) (int, char *[]) =
|
||||
{ su_client_main, resetprop_main, magiskhide_main, imgtool_main, NULL };
|
||||
|
||||
int create_links(const char *bin, const char *path) {
|
||||
char self[PATH_MAX], linkpath[PATH_MAX];
|
||||
if (bin == NULL) {
|
||||
@ -25,8 +17,8 @@ int create_links(const char *bin, const char *path) {
|
||||
bin = self;
|
||||
}
|
||||
int ret = 0;
|
||||
for (int i = 0; applet[i]; ++i) {
|
||||
snprintf(linkpath, sizeof(linkpath), "%s/%s", path, applet[i]);
|
||||
for (int i = 0; applet_names[i]; ++i) {
|
||||
snprintf(linkpath, sizeof(linkpath), "%s/%s", path, applet_names[i]);
|
||||
unlink(linkpath);
|
||||
ret |= symlink(bin, linkpath);
|
||||
}
|
||||
@ -57,8 +49,8 @@ static void usage() {
|
||||
"\n"
|
||||
"Supported applets:\n");
|
||||
|
||||
for (int i = 0; applet[i]; ++i)
|
||||
fprintf(stderr, i ? ", %s" : " %s", applet[i]);
|
||||
for (int i = 0; applet_names[i]; ++i)
|
||||
fprintf(stderr, i ? ", %s" : " %s", applet_names[i]);
|
||||
fprintf(stderr, "\n\n");
|
||||
exit(1);
|
||||
}
|
||||
@ -86,8 +78,8 @@ int magisk_main(int argc, char *argv[]) {
|
||||
if (argc == 3) return create_links(NULL, argv[2]);
|
||||
else return create_links(argv[2], argv[3]);
|
||||
} else if (strcmp(argv[1], "--list") == 0) {
|
||||
for (int i = 0; applet[i]; ++i)
|
||||
printf("%s\n", applet[i]);
|
||||
for (int i = 0; applet_names[i]; ++i)
|
||||
printf("%s\n", applet_names[i]);
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "--unlock-blocks") == 0) {
|
||||
unlock_blocks();
|
||||
@ -120,41 +112,6 @@ int magisk_main(int argc, char *argv[]) {
|
||||
return read_int(fd);
|
||||
}
|
||||
|
||||
// Applets
|
||||
argc--;
|
||||
argv++;
|
||||
for (int i = 0; applet[i]; ++i) {
|
||||
if (strcmp(basename(argv[0]), applet[i]) == 0) {
|
||||
strcpy(argv0, basename(argv[0]));
|
||||
return (*applet_main[i])(argc, argv);
|
||||
}
|
||||
}
|
||||
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
umask(0);
|
||||
argv0 = argv[0];
|
||||
setup_selinux();
|
||||
|
||||
if (strcmp(basename(argv0), "magisk.bin") == 0) {
|
||||
if (argc >= 2) {
|
||||
// It's calling applets
|
||||
--argc;
|
||||
++argv;
|
||||
}
|
||||
}
|
||||
|
||||
// Applets
|
||||
for (int i = 0; applet[i]; ++i) {
|
||||
if (strcmp(basename(argv[0]), applet[i]) == 0) {
|
||||
strcpy(argv0, basename(argv[0]));
|
||||
return (*applet_main[i])(argc, argv);
|
||||
}
|
||||
}
|
||||
|
||||
// Not an applet
|
||||
return magisk_main(argc, argv);
|
||||
}
|
@ -42,7 +42,7 @@
|
||||
|
||||
extern char *argv0; /* For changing process name */
|
||||
|
||||
#define applet ((char *[]) { "su", "resetprop", "magiskhide", "imgtool", NULL })
|
||||
#define applet_names ((char *[]) { "magisk", "su", "resetprop", "magiskhide", "imgtool", NULL })
|
||||
#define init_applet ((char *[]) { "magiskpolicy", "supolicy", NULL })
|
||||
|
||||
extern int (*applet_main[]) (int, char *[]), (*init_applet_main[]) (int, char *[]);
|
||||
@ -50,6 +50,7 @@ extern int (*applet_main[]) (int, char *[]), (*init_applet_main[]) (int, char *[
|
||||
int create_links(const char *bin, const char *path);
|
||||
|
||||
// Multi-call entrypoints
|
||||
int magisk_main(int argc, char *argv[]);
|
||||
int magiskhide_main(int argc, char *argv[]);
|
||||
int magiskpolicy_main(int argc, char *argv[]);
|
||||
int su_client_main(int argc, char *argv[]);
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* magiskinit.c - Pre-init Magisk support
|
||||
/* init.c - Pre-init Magisk support
|
||||
*
|
||||
* This code has to be compiled statically to work properly.
|
||||
*
|
41
native/jni/main.c
Normal file
41
native/jni/main.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <libgen.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "magisk.h"
|
||||
#include "selinux.h"
|
||||
|
||||
int (*applet_main[]) (int, char *[]) =
|
||||
{ magisk_main, su_client_main, resetprop_main, magiskhide_main, imgtool_main, NULL };
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "%s: applet not found\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
umask(0);
|
||||
argv0 = argv[0];
|
||||
dload_selinux();
|
||||
|
||||
if (strcmp(basename(argv0), "magisk.bin") == 0 ||
|
||||
(strcmp(basename(argv[0]), "magisk") == 0
|
||||
&& argc > 1 && argv[1][0] != '-')) {
|
||||
--argc;
|
||||
++argv;
|
||||
}
|
||||
|
||||
call_applets(argc, argv);
|
||||
}
|
||||
|
@ -8,5 +8,5 @@ extern int (*lgetfilecon)(const char *path, char ** con);
|
||||
extern int (*setfilecon)(const char *path, const char * con);
|
||||
extern int (*lsetfilecon)(const char *path, const char * con);
|
||||
|
||||
void setup_selinux();
|
||||
void dload_selinux();
|
||||
void restorecon();
|
||||
|
@ -28,14 +28,14 @@ static int i_ssp(const char *s, char ** sp) {
|
||||
|
||||
// Function pointers
|
||||
|
||||
void (*freecon)(char * con) = v_s;
|
||||
int (*setcon)(const char * con) = i_s;
|
||||
int (*getfilecon)(const char *path, char ** con) = i_ssp;
|
||||
int (*lgetfilecon)(const char *path, char ** con) = i_ssp;
|
||||
int (*setfilecon)(const char *path, const char * con) = i_ss;
|
||||
int (*lsetfilecon)(const char *path, const char * con) = i_ss;
|
||||
void (*freecon)(char *) = v_s;
|
||||
int (*setcon)(const char *) = i_s;
|
||||
int (*getfilecon)(const char *, char **) = i_ssp;
|
||||
int (*lgetfilecon)(const char *, char **) = i_ssp;
|
||||
int (*setfilecon)(const char *, const char *) = i_ss;
|
||||
int (*lsetfilecon)(const char *, const char *) = i_ss;
|
||||
|
||||
void setup_selinux() {
|
||||
void dload_selinux() {
|
||||
void *handle = dlopen("libselinux.so", RTLD_LAZY);
|
||||
if (handle == NULL)
|
||||
return;
|
||||
@ -112,9 +112,8 @@ static void restore_magiskcon(int dirfd) {
|
||||
void restorecon() {
|
||||
int fd;
|
||||
fd = xopen(SELINUX_CONTEXT, O_WRONLY | O_CLOEXEC);
|
||||
if (write(fd, ADB_CON, sizeof(ADB_CON)) >= 0) {
|
||||
if (write(fd, ADB_CON, sizeof(ADB_CON)) >= 0)
|
||||
lsetfilecon(SECURE_DIR, ADB_CON);
|
||||
}
|
||||
close(fd);
|
||||
fd = xopen(MOUNTPOINT, O_RDONLY | O_CLOEXEC);
|
||||
restore_syscon(fd);
|
||||
|
Loading…
x
Reference in New Issue
Block a user