2018-09-27 18:26:41 -04:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2023-11-08 01:46:02 -08:00
|
|
|
#include <consts.hpp>
|
2020-03-09 01:50:30 -07:00
|
|
|
#include <selinux.hpp>
|
2022-05-12 02:03:42 -07:00
|
|
|
#include <base.hpp>
|
2018-09-27 18:26:41 -04:00
|
|
|
|
2021-01-06 22:21:17 -08:00
|
|
|
using namespace std;
|
2019-04-03 17:25:47 -04:00
|
|
|
|
2022-08-27 14:50:28 -07:00
|
|
|
struct Applet {
|
|
|
|
string_view name;
|
|
|
|
int (*fn)(int, char *[]);
|
|
|
|
};
|
2020-01-22 01:17:30 +08:00
|
|
|
|
2022-08-27 14:50:28 -07:00
|
|
|
constexpr Applet applets[] = {
|
|
|
|
{ "su", su_client_main },
|
|
|
|
{ "resetprop", resetprop_main },
|
|
|
|
};
|
2018-09-27 18:26:41 -04:00
|
|
|
|
2022-08-27 14:50:28 -07:00
|
|
|
constexpr Applet private_applets[] = {
|
|
|
|
{ "zygisk", zygisk_main },
|
|
|
|
};
|
2018-09-27 18:26:41 -04:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2022-08-27 14:50:28 -07:00
|
|
|
if (argc < 1)
|
|
|
|
return 1;
|
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
cmdline_logging();
|
|
|
|
init_argv0(argc, argv);
|
|
|
|
|
2022-08-27 14:50:28 -07:00
|
|
|
string_view argv0 = basename(argv[0]);
|
2022-02-11 01:26:24 -08:00
|
|
|
|
|
|
|
umask(0);
|
2022-08-27 14:50:28 -07: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-30 22:11:24 -08:00
|
|
|
if (argc > 1 && argv[1][0] != '-') {
|
2022-08-27 14:50:28 -07:00
|
|
|
// Calling applet with "magisk [applet] args..."
|
2020-12-30 22:11:24 -08:00
|
|
|
--argc;
|
|
|
|
++argv;
|
2022-08-27 14:50:28 -07:00
|
|
|
argv0 = argv[0];
|
2020-12-30 22:11:24 -08:00
|
|
|
} else {
|
|
|
|
return magisk_main(argc, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 14:50:28 -07: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 18:26:41 -04:00
|
|
|
}
|