Magisk/native/src/core/applets.cpp

70 lines
1.5 KiB
C++
Raw Normal View History

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
using namespace std;
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;
cmdline_logging();
init_argv0(argc, argv);
2022-08-27 14:50:28 -07:00
string_view argv0 = basename(argv[0]);
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") {
if (argc > 1 && argv[1][0] != '-') {
2022-08-27 14:50:28 -07:00
// Calling applet with "magisk [applet] args..."
--argc;
++argv;
2022-08-27 14:50:28 -07:00
argv0 = argv[0];
} 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
}