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
|
|
|
|
2021-01-07 06:21:17 +00:00
|
|
|
using namespace std;
|
2019-04-03 21:25:47 +00:00
|
|
|
|
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;
|
|
|
|
|
2020-12-31 06:11:24 +00:00
|
|
|
cmdline_logging();
|
|
|
|
init_argv0(argc, argv);
|
|
|
|
|
2022-08-27 21:50:28 +00:00
|
|
|
string_view argv0 = basename(argv[0]);
|
2022-02-11 09:26:24 +00:00
|
|
|
|
|
|
|
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") {
|
2020-12-31 06:11:24 +00:00
|
|
|
if (argc > 1 && argv[1][0] != '-') {
|
2022-08-27 21:50:28 +00:00
|
|
|
// Calling applet with "magisk [applet] args..."
|
2020-12-31 06:11:24 +00:00
|
|
|
--argc;
|
|
|
|
++argv;
|
2022-08-27 21:50:28 +00:00
|
|
|
argv0 = argv[0];
|
2020-12-31 06:11:24 +00:00
|
|
|
} 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
|
|
|
}
|