mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-05 03:20:46 -05:00
Make Utf8CStr a first class citizen in C++ codebase
Utf8CStr is in many cases a better string view class than std::string_view, because it provides "view" access to a string buffer that is guaranteed to be null terminated. It also has the additional benefit of being UTF-8 verified and can seemlessly cross FFI boundaries. We would want to start use more Utf8CStr in our existing C++ codebase.
This commit is contained in:
@@ -15,23 +15,23 @@
|
||||
#include "init-rs.hpp"
|
||||
|
||||
int magisk_proxy_main(int, char *argv[]);
|
||||
rust::Utf8CStr backup_init();
|
||||
Utf8CStr backup_init();
|
||||
|
||||
// Expose some constants to Rust
|
||||
|
||||
static inline rust::Utf8CStr split_plat_cil() {
|
||||
static inline Utf8CStr split_plat_cil() {
|
||||
return SPLIT_PLAT_CIL;
|
||||
};
|
||||
|
||||
static inline rust::Utf8CStr preload_lib() {
|
||||
static inline Utf8CStr preload_lib() {
|
||||
return PRELOAD_LIB;
|
||||
}
|
||||
|
||||
static inline rust::Utf8CStr preload_policy() {
|
||||
static inline Utf8CStr preload_policy() {
|
||||
return PRELOAD_POLICY;
|
||||
}
|
||||
|
||||
static inline rust::Utf8CStr preload_ack() {
|
||||
static inline Utf8CStr preload_ack() {
|
||||
return PRELOAD_ACK;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,6 @@ pub mod ffi {
|
||||
unsafe extern "C++" {
|
||||
include!("init.hpp");
|
||||
|
||||
#[namespace = "rust"]
|
||||
#[cxx_name = "Utf8CStr"]
|
||||
type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>;
|
||||
|
||||
|
||||
@@ -30,15 +30,15 @@ static void parse_device(devinfo *dev, const char *uevent) {
|
||||
dev->devpath[0] = '\0';
|
||||
dev->dmname[0] = '\0';
|
||||
dev->devname[0] = '\0';
|
||||
parse_prop_file(uevent, [=](string_view key, string_view value) -> bool {
|
||||
parse_prop_file(uevent, [=](Utf8CStr key, Utf8CStr value) -> bool {
|
||||
if (key == "MAJOR")
|
||||
dev->major = parse_int(value.data());
|
||||
dev->major = parse_int(value);
|
||||
else if (key == "MINOR")
|
||||
dev->minor = parse_int(value.data());
|
||||
dev->minor = parse_int(value);
|
||||
else if (key == "DEVNAME")
|
||||
strscpy(dev->devname, value.data(), sizeof(dev->devname));
|
||||
strscpy(dev->devname, value.c_str(), sizeof(dev->devname));
|
||||
else if (key == "PARTNAME")
|
||||
strscpy(dev->partname, value.data(), sizeof(dev->devname));
|
||||
strscpy(dev->partname, value.c_str(), sizeof(dev->devname));
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
+19
-17
@@ -67,26 +67,26 @@ static bool patch_rc_scripts(const char *src_path, const char *tmp_path, bool wr
|
||||
xopenat(dest_fd, INIT_RC, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0), "we");
|
||||
if (!dest_rc) return false;
|
||||
LOGD("Patching " INIT_RC " in %s\n", src_path);
|
||||
file_readline(src_rc, [&dest_rc](string_view line) -> bool {
|
||||
file_readline(src_rc, [&dest_rc](Utf8CStr line) -> bool {
|
||||
// Do not start vaultkeeper
|
||||
if (str_contains(line, "start vaultkeeper")) {
|
||||
if (line.sv().contains("start vaultkeeper")) {
|
||||
LOGD("Remove vaultkeeper\n");
|
||||
return true;
|
||||
}
|
||||
// Do not run flash_recovery
|
||||
if (line.starts_with("service flash_recovery")) {
|
||||
if (line.sv().starts_with("service flash_recovery")) {
|
||||
LOGD("Remove flash_recovery\n");
|
||||
fprintf(dest_rc.get(), "service flash_recovery /system/bin/true\n");
|
||||
return true;
|
||||
}
|
||||
// Samsung's persist.sys.zygote.early will cause Zygote to start before post-fs-data
|
||||
if (line.starts_with("on property:persist.sys.zygote.early=")) {
|
||||
if (line.sv().starts_with("on property:persist.sys.zygote.early=")) {
|
||||
LOGD("Invalidate persist.sys.zygote.early\n");
|
||||
fprintf(dest_rc.get(), "on property:persist.sys.zygote.early.xxxxx=true\n");
|
||||
return true;
|
||||
}
|
||||
// Else just write the line
|
||||
fprintf(dest_rc.get(), "%s", line.data());
|
||||
fprintf(dest_rc.get(), "%s", line.c_str());
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -108,24 +108,26 @@ static bool patch_rc_scripts(const char *src_path, const char *tmp_path, bool wr
|
||||
|
||||
// Then patch init.zygote*.rc
|
||||
for (dirent *entry; (entry = readdir(src_dir.get()));) {
|
||||
auto name = std::string_view(entry->d_name);
|
||||
if (!name.starts_with("init.zygote") || !name.ends_with(".rc")) continue;
|
||||
owned_fd src_rc = xopenat(src_fd, name.data(), O_RDONLY | O_CLOEXEC, 0);
|
||||
{
|
||||
auto name = std::string_view(entry->d_name);
|
||||
if (!name.starts_with("init.zygote") || !name.ends_with(".rc")) continue;
|
||||
}
|
||||
owned_fd src_rc = xopenat(src_fd, entry->d_name, O_RDONLY | O_CLOEXEC, 0);
|
||||
if (src_rc < 0) continue;
|
||||
if (writable) unlinkat(src_fd, name.data(), 0);
|
||||
if (writable) unlinkat(src_fd, entry->d_name, 0);
|
||||
auto dest_rc = xopen_file(
|
||||
xopenat(dest_fd, name.data(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0), "we");
|
||||
xopenat(dest_fd, entry->d_name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0), "we");
|
||||
if (!dest_rc) continue;
|
||||
LOGD("Patching %s in %s\n", name.data(), src_path);
|
||||
file_readline(src_rc, [&dest_rc, &tmp_path](string_view line) -> bool {
|
||||
if (line.starts_with("service zygote ")) {
|
||||
LOGD("Patching %s in %s\n", entry->d_name, src_path);
|
||||
file_readline(src_rc, [&dest_rc, &tmp_path](Utf8CStr line) -> bool {
|
||||
if (line.sv().starts_with("service zygote ")) {
|
||||
LOGD("Inject zygote restart\n");
|
||||
fprintf(dest_rc.get(), "%s", line.data());
|
||||
fprintf(dest_rc.get(), "%s", line.c_str());
|
||||
fprintf(dest_rc.get(),
|
||||
" onrestart exec " MAGISK_PROC_CON " 0 0 -- %s/magisk --zygote-restart\n", tmp_path);
|
||||
return true;
|
||||
}
|
||||
fprintf(dest_rc.get(), "%s", line.data());
|
||||
fprintf(dest_rc.get(), "%s", line.c_str());
|
||||
return true;
|
||||
});
|
||||
fclone_attr(src_rc, fileno(dest_rc.get()));
|
||||
@@ -184,7 +186,7 @@ static void load_overlay_rc(const char *overlay) {
|
||||
char buf[NAME_MAX + 2];
|
||||
buf[0] = '/';
|
||||
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||
if (!str_ends(entry->d_name, ".rc")) {
|
||||
if (!string_view(entry->d_name).ends_with(".rc")) {
|
||||
continue;
|
||||
}
|
||||
strscpy(buf + 1, entry->d_name, sizeof(buf) - 1);
|
||||
@@ -407,7 +409,7 @@ static void unxz_init(const char *init_xz, const char *init) {
|
||||
unlink(init_xz);
|
||||
}
|
||||
|
||||
rust::Utf8CStr backup_init() {
|
||||
Utf8CStr backup_init() {
|
||||
if (access("/.backup/init.xz", F_OK) == 0)
|
||||
unxz_init("/.backup/init.xz", "/.backup/init");
|
||||
return "/.backup/init";
|
||||
|
||||
Reference in New Issue
Block a user