improve: magisk binary check code

This commit improves and simplifies the code that checks in which path the "magisk" binary is by using loops instead of hardcoded "if"s.
This commit is contained in:
ThePedroo
2025-04-16 02:15:08 -03:00
parent 8f70a1a451
commit 032cd68916

View File

@@ -30,53 +30,36 @@ char *magisk_managers[] = {
enum magisk_variants variant = Official; enum magisk_variants variant = Official;
/* INFO: Longest path */ /* INFO: Longest path */
static char path_to_magisk[sizeof(DEBUG_RAMDISK_MAGISK)]; static char path_to_magisk[sizeof(DEBUG_RAMDISK_MAGISK)] = { 0 };
bool is_using_sulist = false; bool is_using_sulist = false;
void magisk_get_existence(struct root_impl_state *state) { void magisk_get_existence(struct root_impl_state *state) {
struct stat s; const char *magisk_files[] = {
if (stat(SBIN_MAGISK, &s) != 0) { SBIN_MAGISK,
if (errno != ENOENT) { BITLESS_SBIN_MAGISK,
LOGE("Failed to stat Magisk /sbin/magisk binary: %s\n", strerror(errno)); DEBUG_RAMDISK_MAGISK,
} BITLESS_DEBUG_RAMDISK_MAGISK
errno = 0; };
if (stat(BITLESS_SBIN_MAGISK, &s) != 0) { for (size_t i = 0; i < sizeof(magisk_files) / sizeof(magisk_files[0]); i++) {
if (access(magisk_files[i], F_OK) != 0) {
if (errno != ENOENT) { if (errno != ENOENT) {
LOGE("Failed to stat Magisk %s binary: %s\n", BITLESS_SBIN_MAGISK, strerror(errno)); LOGE("Failed to access Magisk binary: %s\n", strerror(errno));
} }
errno = 0; errno = 0;
if (stat(DEBUG_RAMDISK_MAGISK, &s) != 0) { continue;
if (errno != ENOENT) {
LOGE("Failed to stat Magisk %s binary: %s\n", DEBUG_RAMDISK_MAGISK, strerror(errno));
}
errno = 0;
if (stat(BITLESS_DEBUG_RAMDISK_MAGISK, &s) != 0) {
if (errno != ENOENT) {
LOGE("Failed to stat Magisk /debug_ramdisk/magisk binary: %s\n", strerror(errno));
}
errno = 0;
state->state = Inexistent;
return;
}
/* INFO: /debug_ramdisk/magisk64 (or 32) doesn't exist but /debug_ramdisk/magisk does */
strcpy(path_to_magisk, BITLESS_DEBUG_RAMDISK_MAGISK);
} else {
/* INFO: /sbin/magisk doesn't exist but /debug_ramdisk/magisk does */
strcpy(path_to_magisk, DEBUG_RAMDISK_MAGISK);
}
} else {
/* INFO: /sbin/magisk64 (or 32) doesn't exist but /sbin/magisk does */
strcpy(path_to_magisk, BITLESS_SBIN_MAGISK);
} }
} else {
/* INFO: /sbin/magisk64 (or 32) exists */ strcpy(path_to_magisk, magisk_files[i]);
strcpy(path_to_magisk, SBIN_MAGISK);
break;
}
if (path_to_magisk[0] == '\0') {
state->state = Inexistent;
return;
} }
char *argv[4] = { "magisk", "-v", NULL, NULL }; char *argv[4] = { "magisk", "-v", NULL, NULL };