2019-03-04 21:45:18 +00:00
|
|
|
#include <sys/mount.h>
|
2017-11-05 21:41:03 +00:00
|
|
|
#include <libgen.h>
|
2017-04-08 23:25:10 +00:00
|
|
|
|
2022-05-12 09:03:42 +00:00
|
|
|
#include <base.hpp>
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <magisk.hpp>
|
|
|
|
#include <daemon.hpp>
|
|
|
|
#include <selinux.hpp>
|
2021-09-08 02:35:28 +00:00
|
|
|
#include <flags.h>
|
2017-04-04 19:44:13 +00:00
|
|
|
|
2021-01-11 10:19:10 +00:00
|
|
|
#include "core.hpp"
|
|
|
|
|
2020-06-01 11:15:37 +00:00
|
|
|
using namespace std;
|
2019-05-13 09:01:10 +00:00
|
|
|
|
2018-11-04 08:38:06 +00:00
|
|
|
[[noreturn]] static void usage() {
|
2020-12-31 06:11:24 +00:00
|
|
|
fprintf(stderr,
|
2020-06-01 11:15:37 +00:00
|
|
|
R"EOF(Magisk - Multi-purpose Utility
|
2019-09-13 07:14:21 +00:00
|
|
|
|
|
|
|
Usage: magisk [applet [arguments]...]
|
|
|
|
or: magisk [options]...
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-c print current binary version
|
|
|
|
-v print running daemon version
|
|
|
|
-V print running daemon version code
|
|
|
|
--list list all available applets
|
|
|
|
--remove-modules remove all modules and reboot
|
2020-09-26 23:50:41 +00:00
|
|
|
--install-module ZIP install a module zip file
|
2019-09-13 07:14:21 +00:00
|
|
|
|
|
|
|
Advanced Options (Internal APIs):
|
2020-09-26 23:50:41 +00:00
|
|
|
--daemon manually start magisk daemon
|
2021-08-26 10:09:56 +00:00
|
|
|
--stop remove all magisk changes and stop daemon
|
2022-05-29 05:39:44 +00:00
|
|
|
--[init trigger] callback on init triggers. Valid triggers:
|
|
|
|
post-fs-data, service, boot-complete, zygote-restart
|
2019-09-13 07:14:21 +00:00
|
|
|
--unlock-blocks set BLKROSET flag to OFF for all block devices
|
|
|
|
--restorecon restore selinux context on Magisk files
|
|
|
|
--clone-attr SRC DEST clone permission, owner, and selinux context
|
|
|
|
--clone SRC DEST clone SRC to DEST
|
|
|
|
--sqlite SQL exec SQL commands to Magisk database
|
2020-04-12 12:34:56 +00:00
|
|
|
--path print Magisk tmpfs mount path
|
2021-09-12 19:40:34 +00:00
|
|
|
--denylist ARGS denylist config CLI
|
2019-09-13 07:14:21 +00:00
|
|
|
|
2020-02-29 23:33:11 +00:00
|
|
|
Available applets:
|
2019-09-13 07:14:21 +00:00
|
|
|
)EOF");
|
2017-04-15 10:10:54 +00:00
|
|
|
|
2020-12-31 06:11:24 +00:00
|
|
|
for (int i = 0; applet_names[i]; ++i)
|
|
|
|
fprintf(stderr, i ? ", %s" : " %s", applet_names[i]);
|
|
|
|
fprintf(stderr, "\n\n");
|
|
|
|
exit(1);
|
2017-04-15 10:10:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 06:32:21 +00:00
|
|
|
int magisk_main(int argc, char *argv[]) {
|
2020-12-31 06:11:24 +00:00
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
if (argv[1] == "-c"sv) {
|
2022-05-30 08:53:07 +00:00
|
|
|
#if MAGISK_DEBUG
|
|
|
|
printf(MAGISK_VERSION ":MAGISK:D (" str(MAGISK_VER_CODE) ")\n");
|
|
|
|
#else
|
|
|
|
printf(MAGISK_VERSION ":MAGISK:R (" str(MAGISK_VER_CODE) ")\n");
|
|
|
|
#endif
|
2020-12-31 06:11:24 +00:00
|
|
|
return 0;
|
|
|
|
} else if (argv[1] == "-v"sv) {
|
2022-03-01 10:13:18 +00:00
|
|
|
int fd = connect_daemon(MainRequest::CHECK_VERSION);
|
2021-01-12 08:07:48 +00:00
|
|
|
string v = read_string(fd);
|
|
|
|
printf("%s\n", v.data());
|
2020-12-31 06:11:24 +00:00
|
|
|
return 0;
|
|
|
|
} else if (argv[1] == "-V"sv) {
|
2022-03-01 10:13:18 +00:00
|
|
|
int fd = connect_daemon(MainRequest::CHECK_VERSION_CODE);
|
2020-12-31 06:11:24 +00:00
|
|
|
printf("%d\n", read_int(fd));
|
|
|
|
return 0;
|
|
|
|
} else if (argv[1] == "--list"sv) {
|
|
|
|
for (int i = 0; applet_names[i]; ++i)
|
|
|
|
printf("%s\n", applet_names[i]);
|
|
|
|
return 0;
|
|
|
|
} else if (argv[1] == "--unlock-blocks"sv) {
|
|
|
|
unlock_blocks();
|
|
|
|
return 0;
|
|
|
|
} else if (argv[1] == "--restorecon"sv) {
|
|
|
|
restorecon();
|
|
|
|
return 0;
|
2022-02-12 15:43:36 +00:00
|
|
|
} else if (argc >= 4 && argv[1] == "--clone-attr"sv) {
|
2020-12-31 06:11:24 +00:00
|
|
|
clone_attr(argv[2], argv[3]);
|
|
|
|
return 0;
|
|
|
|
} else if (argc >= 4 && argv[1] == "--clone"sv) {
|
|
|
|
cp_afc(argv[2], argv[3]);
|
|
|
|
return 0;
|
|
|
|
} else if (argv[1] == "--daemon"sv) {
|
2022-05-28 23:53:04 +00:00
|
|
|
close(connect_daemon(MainRequest::START_DAEMON, true));
|
2020-12-31 06:11:24 +00:00
|
|
|
return 0;
|
2021-08-26 10:09:56 +00:00
|
|
|
} else if (argv[1] == "--stop"sv) {
|
2022-03-01 10:13:18 +00:00
|
|
|
int fd = connect_daemon(MainRequest::STOP_DAEMON);
|
2021-08-26 10:09:56 +00:00
|
|
|
return read_int(fd);
|
2020-12-31 06:11:24 +00:00
|
|
|
} else if (argv[1] == "--post-fs-data"sv) {
|
2022-05-28 23:53:04 +00:00
|
|
|
close(connect_daemon(MainRequest::POST_FS_DATA, true));
|
|
|
|
return 0;
|
2020-12-31 06:11:24 +00:00
|
|
|
} else if (argv[1] == "--service"sv) {
|
2022-05-28 23:53:04 +00:00
|
|
|
close(connect_daemon(MainRequest::LATE_START, true));
|
|
|
|
return 0;
|
2020-12-31 06:11:24 +00:00
|
|
|
} else if (argv[1] == "--boot-complete"sv) {
|
2022-05-28 23:53:04 +00:00
|
|
|
close(connect_daemon(MainRequest::BOOT_COMPLETE));
|
|
|
|
return 0;
|
2022-05-29 05:39:44 +00:00
|
|
|
} else if (argv[1] == "--zygote-restart"sv) {
|
|
|
|
close(connect_daemon(MainRequest::ZYGOTE_RESTART));
|
|
|
|
return 0;
|
2021-09-12 19:40:34 +00:00
|
|
|
} else if (argv[1] == "--denylist"sv) {
|
|
|
|
return denylist_cli(argc - 1, argv + 1);
|
2022-02-12 15:43:36 +00:00
|
|
|
} else if (argc >= 3 && argv[1] == "--sqlite"sv) {
|
2022-03-01 10:13:18 +00:00
|
|
|
int fd = connect_daemon(MainRequest::SQLITE_CMD);
|
2020-12-31 06:11:24 +00:00
|
|
|
write_string(fd, argv[2]);
|
2021-01-12 08:07:48 +00:00
|
|
|
string res;
|
2020-12-31 06:11:24 +00:00
|
|
|
for (;;) {
|
2021-01-12 08:07:48 +00:00
|
|
|
read_string(fd, res);
|
|
|
|
if (res.empty())
|
2020-12-31 06:11:24 +00:00
|
|
|
return 0;
|
2021-01-12 08:07:48 +00:00
|
|
|
printf("%s\n", res.data());
|
2020-12-31 06:11:24 +00:00
|
|
|
}
|
|
|
|
} else if (argv[1] == "--remove-modules"sv) {
|
2022-03-01 10:13:18 +00:00
|
|
|
int fd = connect_daemon(MainRequest::REMOVE_MODULES);
|
2020-12-31 06:11:24 +00:00
|
|
|
return read_int(fd);
|
|
|
|
} else if (argv[1] == "--path"sv) {
|
2022-03-01 10:13:18 +00:00
|
|
|
int fd = connect_daemon(MainRequest::GET_PATH);
|
2021-01-12 08:07:48 +00:00
|
|
|
string path = read_string(fd);
|
|
|
|
printf("%s\n", path.data());
|
2020-12-31 06:11:24 +00:00
|
|
|
return 0;
|
|
|
|
} else if (argc >= 3 && argv[1] == "--install-module"sv) {
|
|
|
|
install_module(argv[2]);
|
|
|
|
}
|
2019-02-10 06:05:19 +00:00
|
|
|
#if 0
|
2020-12-31 06:11:24 +00:00
|
|
|
/* Entry point for testing stuffs */
|
|
|
|
else if (argv[1] == "--test"sv) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-10 06:05:19 +00:00
|
|
|
#endif
|
2020-12-31 06:11:24 +00:00
|
|
|
usage();
|
2018-05-13 06:32:21 +00:00
|
|
|
}
|