Magisk/native/src/core/applets.cpp

76 lines
1.7 KiB
C++
Raw Normal View History

2018-09-27 22:26:41 +00:00
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
2020-03-09 08:50:30 +00:00
#include <magisk.hpp>
#include <selinux.hpp>
2022-05-12 09:03:42 +00:00
#include <base.hpp>
2018-09-27 22:26:41 +00:00
using namespace std;
2022-08-27 21:50:28 +00:00
struct Applet {
string_view name;
int (*fn)(int, char *[]);
};
2020-01-21 17:17:30 +00:00
2022-08-27 21:50:28 +00:00
constexpr Applet applets[] = {
{ "su", su_client_main },
{ "resetprop", resetprop_main },
};
2018-09-27 22:26:41 +00:00
2022-08-27 21:50:28 +00:00
constexpr Applet private_applets[] = {
{ "zygisk", zygisk_main },
};
2018-09-27 22:26:41 +00:00
int main(int argc, char *argv[]) {
2022-08-27 21:50:28 +00:00
if (argc < 1)
return 1;
2021-04-10 04:29:42 +00:00
enable_selinux();
cmdline_logging();
init_argv0(argc, argv);
2022-08-27 21:50:28 +00:00
string_view argv0 = basename(argv[0]);
// app_process is actually not an applet
2022-08-27 21:50:28 +00:00
if (argv0.starts_with("app_process")) {
return app_process_main(argc, argv);
}
umask(0);
2022-08-27 21:50:28 +00:00
if (argv[0][0] == '\0') {
// When argv[0] is an empty string, we're calling private applets
if (argc < 2)
return 1;
--argc;
++argv;
for (const auto &app : private_applets) {
if (argv[0] == app.name) {
return app.fn(argc, argv);
}
}
fprintf(stderr, "%s: applet not found\n", argv[0]);
return 1;
}
if (argv0 == "magisk" || argv0 == "magisk32" || argv0 == "magisk64") {
if (argc > 1 && argv[1][0] != '-') {
2022-08-27 21:50:28 +00:00
// Calling applet with "magisk [applet] args..."
--argc;
++argv;
2022-08-27 21:50:28 +00:00
argv0 = argv[0];
} else {
return magisk_main(argc, argv);
}
}
2022-08-27 21:50:28 +00:00
for (const auto &app : applets) {
if (argv0 == app.name) {
return app.fn(argc, argv);
}
}
fprintf(stderr, "%s: applet not found\n", argv0.data());
return 1;
2018-09-27 22:26:41 +00:00
}