Fix several things regarding scripting

This commit is contained in:
topjohnwu 2020-11-07 14:36:13 -08:00
parent e21a78164e
commit afebe734b8
3 changed files with 53 additions and 33 deletions

View File

@ -148,12 +148,10 @@ static bool magisk_env() {
if (access(DATABIN "/busybox", X_OK) == -1) if (access(DATABIN "/busybox", X_OK) == -1)
return false; return false;
// TODO: Remove. Backwards compatibility for old manager
LOGI("* Setting up internal busybox\n");
sprintf(buf, "%s/" BBPATH "/busybox", MAGISKTMP.data()); sprintf(buf, "%s/" BBPATH "/busybox", MAGISKTMP.data());
mkdir(dirname(buf), 0755); mkdir(dirname(buf), 0755);
cp_afc(DATABIN "/busybox", buf); cp_afc(DATABIN "/busybox", buf);
exec_command_sync(buf, "--install", "-s", dirname(buf)); exec_command_async(buf, "--install", "-s", dirname(buf));
return true; return true;
} }

View File

@ -10,15 +10,25 @@
using namespace std; using namespace std;
#define BBEXEC_CMD DATABIN "/busybox", "sh" #define BBEXEC_CMD bbpath(), "sh"
static void set_standalone() { static const char *bbpath() {
static string path;
if (path.empty())
path = MAGISKTMP + "/" BBPATH "/busybox";
return path.data();
}
static void set_script_env() {
setenv("ASH_STANDALONE", "1", 1); setenv("ASH_STANDALONE", "1", 1);
char new_path[4096];
sprintf(new_path, "%s:%s", getenv("PATH"), MAGISKTMP.data());
setenv("PATH", new_path, 1);
}; };
void exec_script(const char *script) { void exec_script(const char *script) {
exec_t exec { exec_t exec {
.pre_exec = set_standalone, .pre_exec = set_script_env,
.fork = fork_no_zombie .fork = fork_no_zombie
}; };
exec_command_sync(exec, BBEXEC_CMD, script); exec_command_sync(exec, BBEXEC_CMD, script);
@ -43,7 +53,7 @@ void exec_common_scripts(const char *stage) {
LOGI("%s.d: exec [%s]\n", stage, entry->d_name); LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
strcpy(name, entry->d_name); strcpy(name, entry->d_name);
exec_t exec { exec_t exec {
.pre_exec = set_standalone, .pre_exec = set_script_env,
.fork = pfs ? fork_no_zombie : fork_dont_care .fork = pfs ? fork_no_zombie : fork_dont_care
}; };
if (pfs) if (pfs)
@ -59,13 +69,13 @@ void exec_module_scripts(const char *stage, const vector<string> &module_list) {
char path[4096]; char path[4096];
bool pfs = stage == "post-fs-data"sv; bool pfs = stage == "post-fs-data"sv;
for (auto &m : module_list) { for (auto &m : module_list) {
const char* module = m.c_str(); const char* module = m.data();
sprintf(path, MODULEROOT "/%s/%s.sh", module, stage); sprintf(path, MODULEROOT "/%s/%s.sh", module, stage);
if (access(path, F_OK) == -1) if (access(path, F_OK) == -1)
continue; continue;
LOGI("%s: exec [%s.sh]\n", module, stage); LOGI("%s: exec [%s.sh]\n", module, stage);
exec_t exec { exec_t exec {
.pre_exec = set_standalone, .pre_exec = set_script_env,
.fork = pfs ? fork_no_zombie : fork_dont_care .fork = pfs ? fork_no_zombie : fork_dont_care
}; };
if (pfs) if (pfs)
@ -78,30 +88,35 @@ void exec_module_scripts(const char *stage, const vector<string> &module_list) {
constexpr char install_script[] = R"EOF( constexpr char install_script[] = R"EOF(
APK=%s APK=%s
log -t Magisk "apk_install: $APK" log -t Magisk "apk_install: $APK"
log -t Magisk "apk_install: `pm install -r $APK 2>&1`" log -t Magisk "apk_install: $(pm install -r $APK 2>&1)"
rm -f $APK rm -f $APK
)EOF"; )EOF";
void install_apk(const char *apk) { void install_apk(const char *apk) {
setfilecon(apk, "u:object_r:" SEPOL_FILE_TYPE ":s0"); setfilecon(apk, "u:object_r:" SEPOL_FILE_TYPE ":s0");
exec_t exec { exec_t exec {
.pre_exec = set_standalone,
.fork = fork_no_zombie .fork = fork_no_zombie
}; };
char cmds[sizeof(install_script) + 4096]; char cmds[sizeof(install_script) + 4096];
sprintf(cmds, install_script, apk); sprintf(cmds, install_script, apk);
exec_command_sync(exec, BBEXEC_CMD, "-c", cmds); exec_command_sync(exec, "/system/bin/sh", "-c", cmds);
} }
[[noreturn]] static void abort(const char *msg) { [[noreturn]] __printflike(1, 2)
fprintf(stderr, "%s\n\n", msg); static void abort(const char *fmt, ...) {
va_list valist;
va_start(valist, fmt);
vfprintf(stderr, fmt, valist);
fprintf(stderr, "\n\n");
va_end(valist);
exit(1); exit(1);
} }
constexpr char install_module_script[] = R"EOF( constexpr char install_module_script[] = R"EOF(
exec $(magisk --path)/.magisk/busybox/busybox sh -c '
. /data/adb/magisk/util_functions.sh . /data/adb/magisk/util_functions.sh
install_module install_module
exit 0 exit 0'
)EOF"; )EOF";
void install_module(const char *file) { void install_module(const char *file) {
@ -112,20 +127,18 @@ void install_module(const char *file) {
access(DATABIN "/util_functions.sh", F_OK)) access(DATABIN "/util_functions.sh", F_OK))
abort("Incomplete Magisk install"); abort("Incomplete Magisk install");
if (access(file, F_OK)) { if (access(file, F_OK)) {
char msg[4096]; abort("'%s' does not exist", file);
sprintf(msg, "'%s' does not exist", file);
abort(msg);
} }
setenv("OUTFD", "1", true); setenv("OUTFD", "1", 1);
setenv("ZIPFILE", file, true); setenv("ZIPFILE", file, 1);
setenv("ASH_STANDALONE", "1", 1); setenv("ASH_STANDALONE", "1", 1);
int fd = xopen("/dev/null", O_RDONLY); int fd = xopen("/dev/null", O_RDONLY);
xdup2(fd, STDERR_FILENO); xdup2(fd, STDERR_FILENO);
close(fd); close(fd);
const char *argv[] = { BBEXEC_CMD, "-c", install_module_script }; const char *argv[] = { "/system/bin/sh", "-c", install_module_script };
execve(argv[0], (char **) argv, environ); execve(argv[0], (char **) argv, environ);
abort("Failed to execute BusyBox shell"); abort("Failed to execute BusyBox shell");
} }

View File

@ -58,13 +58,24 @@ reversed_container<T> reversed(T &base) {
} }
int parse_int(const char *s); int parse_int(const char *s);
static inline int parse_int(std::string s) { return parse_int(s.data()); } static inline int parse_int(const std::string &s) { return parse_int(s.data()); }
static inline int parse_int(std::string_view s) { return parse_int(s.data()); } static inline int parse_int(std::string_view s) { return parse_int(s.data()); }
using thread_entry = void *(*)(void *); using thread_entry = void *(*)(void *);
int new_daemon_thread(thread_entry entry, void *arg = nullptr, const pthread_attr_t *attr = nullptr); int new_daemon_thread(thread_entry entry, void *arg = nullptr, const pthread_attr_t *attr = nullptr);
int new_daemon_thread(std::function<void()> &&entry); int new_daemon_thread(std::function<void()> &&entry);
bool ends_with(const std::string_view &s1, const std::string_view &s2);
int fork_dont_care();
int fork_no_zombie();
int strend(const char *s1, const char *s2);
void init_argv0(int argc, char **argv);
void set_nice_name(const char *name);
uint32_t binary_gcd(uint32_t u, uint32_t v);
int switch_mnt_ns(int pid);
int gen_rand_str(char *buf, int len, bool varlen = true);
std::string &replace_all(std::string &str, std::string_view from, std::string_view to);
struct exec_t { struct exec_t {
bool err = false; bool err = false;
int fd = -2; int fd = -2;
@ -92,14 +103,12 @@ int exec_command_sync(Args &&...args) {
exec_t exec{}; exec_t exec{};
return exec_command_sync(exec, args...); return exec_command_sync(exec, args...);
} }
template <class ...Args>
bool ends_with(const std::string_view &s1, const std::string_view &s2); void exec_command_async(Args &&...args) {
int fork_dont_care(); const char *argv[] = {args..., nullptr};
int fork_no_zombie(); exec_t exec {
int strend(const char *s1, const char *s2); .argv = argv,
void init_argv0(int argc, char **argv); .fork = fork_dont_care
void set_nice_name(const char *name); };
uint32_t binary_gcd(uint32_t u, uint32_t v); exec_command(exec);
int switch_mnt_ns(int pid); }
int gen_rand_str(char *buf, int len, bool varlen = true);
std::string &replace_all(std::string &str, std::string_view from, std::string_view to);