From 2a694596b548e60189ca7f9c08ff8dfba828ad7a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 5 Dec 2020 10:23:49 -0800 Subject: [PATCH] Better error handling and logging --- native/jni/init/mount.cpp | 1 + native/jni/init/raw_data.cpp | 8 +++++++- native/jni/utils/files.cpp | 11 +++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/native/jni/init/mount.cpp b/native/jni/init/mount.cpp index 9671f2263..e5e6fe0aa 100644 --- a/native/jni/init/mount.cpp +++ b/native/jni/init/mount.cpp @@ -346,6 +346,7 @@ void SARInit::early_mount() { auto init = raw_data::mmap_ro("/init"); is_two_stage = init.contains("selinux_setup"); } + LOGD("is_two_stage: [%d]\n", is_two_stage); if (!is_two_stage) { // Make dev writable diff --git a/native/jni/init/raw_data.cpp b/native/jni/init/raw_data.cpp index 7f1d9bcc6..01ae06372 100644 --- a/native/jni/init/raw_data.cpp +++ b/native/jni/init/raw_data.cpp @@ -3,6 +3,8 @@ using namespace std; int data_holder::patch(str_pairs list) { + if (buf == nullptr) + return 0; int count = 0; for (uint8_t *p = buf, *eof = buf + sz; p < eof; ++p) { for (auto [from, to] : list) { @@ -19,9 +21,13 @@ int data_holder::patch(str_pairs list) { } bool data_holder::contains(string_view pattern) { + if (buf == nullptr) + return false; for (uint8_t *p = buf, *eof = buf + sz; p < eof; ++p) { - if (memcmp(p, pattern.data(), pattern.length() + 1) == 0) + if (memcmp(p, pattern.data(), pattern.length() + 1) == 0) { + LOGD("Found pattern [%s]\n", pattern.data()); return true; + } } return false; } diff --git a/native/jni/utils/files.cpp b/native/jni/utils/files.cpp index 098e487b4..685038a51 100644 --- a/native/jni/utils/files.cpp +++ b/native/jni/utils/files.cpp @@ -273,10 +273,17 @@ void fclone_attr(int src, int dest) { } void *__mmap(const char *filename, size_t *size, bool rw) { + int fd = xopen(filename, (rw ? O_RDWR : O_RDONLY) | O_CLOEXEC); + if (fd < 0) { + *size = 0; + return nullptr; + } struct stat st; void *buf; - int fd = xopen(filename, (rw ? O_RDWR : O_RDONLY) | O_CLOEXEC); - fstat(fd, &st); + if (fstat(fd, &st)) { + *size = 0; + return nullptr; + } if (S_ISBLK(st.st_mode)) ioctl(fd, BLKGETSIZE64, size); else