Directly filter '.' and '..' in xreaddir

This commit is contained in:
topjohnwu 2020-04-18 04:20:21 -07:00
parent ed6cdb2eb4
commit dcf07ad8c7
6 changed files with 15 additions and 35 deletions

View File

@ -164,7 +164,7 @@ void remove_modules() {
int dfd = dirfd(dir.get()); int dfd = dirfd(dir.get());
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv || entry->d_name == ".core"sv) if (entry->d_name == ".core"sv)
continue; continue;
int modfd = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC); int modfd = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC);

View File

@ -11,11 +11,6 @@
using namespace std; using namespace std;
#define SKIP_DOTS {\
if (entry->d_name == "."sv || entry->d_name == ".."sv) \
continue;\
}
#ifdef MAGISK_DEBUG #ifdef MAGISK_DEBUG
#define VLOGI(tag, from, to) LOGI("%-8s: %s <- %s\n", tag, to, from) #define VLOGI(tag, from, to) LOGI("%-8s: %s <- %s\n", tag, to, from)
#else #else
@ -440,7 +435,6 @@ bool dir_node::prepare() {
string mirror = skel->mirror_path(); string mirror = skel->mirror_path();
auto dir = xopen_dir(mirror.data()); auto dir = xopen_dir(mirror.data());
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
// Insert mirror nodes // Insert mirror nodes
skel->emplace<mirror_node>(entry->d_name, entry); skel->emplace<mirror_node>(entry->d_name, entry);
} }
@ -457,7 +451,6 @@ bool dir_node::collect_files(const char *module, int dfd) {
return true; return true;
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (entry->d_name == ".replace"sv) { if (entry->d_name == ".replace"sv) {
// Stop traversing and tell parent to upgrade self to module // Stop traversing and tell parent to upgrade self to module
return false; return false;
@ -550,8 +543,6 @@ static void prepare_modules() {
int mfd = xopen(MODULEROOT, O_RDONLY | O_CLOEXEC); int mfd = xopen(MODULEROOT, O_RDONLY | O_CLOEXEC);
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
// Cleanup old module if exists // Cleanup old module if exists
if (faccessat(mfd, entry->d_name, F_OK, 0) == 0) { if (faccessat(mfd, entry->d_name, F_OK, 0) == 0) {
frm_rf(xopenat(mfd, entry->d_name, O_RDONLY | O_CLOEXEC)); frm_rf(xopenat(mfd, entry->d_name, O_RDONLY | O_CLOEXEC));
@ -579,7 +570,7 @@ static void collect_modules() {
int dfd = dirfd(dir.get()); int dfd = dirfd(dir.get());
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv || entry->d_name == ".core"sv) if (entry->d_name == ".core"sv)
continue; continue;
int modfd = xopenat(dfd, entry->d_name, O_RDONLY); int modfd = xopenat(dfd, entry->d_name, O_RDONLY);

View File

@ -24,8 +24,6 @@ static void restore_syscon(int dirfd) {
dir = xfdopendir(dirfd); dir = xfdopendir(dirfd);
while ((entry = xreaddir(dir))) { while ((entry = xreaddir(dir))) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
int fd = openat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC); int fd = openat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
restore_syscon(fd); restore_syscon(fd);
@ -53,8 +51,6 @@ static void restore_magiskcon(int dirfd) {
dir = xfdopendir(dirfd); dir = xfdopendir(dirfd);
while ((entry = xreaddir(dir))) { while ((entry = xreaddir(dir))) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
int fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC); int fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
restore_magiskcon(fd); restore_magiskcon(fd);
@ -87,8 +83,6 @@ void restore_rootcon() {
int dfd = dirfd(dir.get()); int dfd = dirfd(dir.get());
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
if (entry->d_name == "magisk"sv || entry->d_name == "magiskinit"sv) if (entry->d_name == "magisk"sv || entry->d_name == "magiskinit"sv)
setfilecon_at(dfd, entry->d_name, MAGISK_CON); setfilecon_at(dfd, entry->d_name, MAGISK_CON);
else else

View File

@ -143,8 +143,6 @@ bool MagiskInit::patch_sepolicy(const char *file) {
// Custom rules // Custom rules
if (auto dir = xopen_dir(persist_dir.data()); dir) { if (auto dir = xopen_dir(persist_dir.data()); dir) {
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
auto rule = persist_dir + "/" + entry->d_name + "/sepolicy.rule"; auto rule = persist_dir + "/" + entry->d_name + "/sepolicy.rule";
if (access(rule.data(), R_OK) == 0) { if (access(rule.data(), R_OK) == 0) {
LOGD("Loading custom sepolicy patch: %s\n", rule.data()); LOGD("Loading custom sepolicy patch: %s\n", rule.data());
@ -170,8 +168,6 @@ static void recreate_sbin(const char *mirror, bool use_bind_mount) {
int src = dirfd(dp.get()); int src = dirfd(dp.get());
char buf[4096]; char buf[4096];
for (dirent *entry; (entry = xreaddir(dp.get()));) { for (dirent *entry; (entry = xreaddir(dp.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
string sbin_path = "/sbin/"s + entry->d_name; string sbin_path = "/sbin/"s + entry->d_name;
struct stat st; struct stat st;
fstatat(src, entry->d_name, &st, AT_SYMLINK_NOFOLLOW); fstatat(src, entry->d_name, &st, AT_SYMLINK_NOFOLLOW);

View File

@ -45,17 +45,11 @@ int mkdirs(string path, mode_t mode) {
return 0; return 0;
} }
#define SKIP_DOTS {\
if (entry->d_name == "."sv || entry->d_name == ".."sv) \
continue;\
}
static void post_order_walk(int dirfd, const function<void(int, dirent *)> &&fn) { static void post_order_walk(int dirfd, const function<void(int, dirent *)> &&fn) {
auto dir = xopen_dir(dirfd); auto dir = xopen_dir(dirfd);
if (!dir) return; if (!dir) return;
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (entry->d_type == DT_DIR) if (entry->d_type == DT_DIR)
post_order_walk(xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC), std::move(fn)); post_order_walk(xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC), std::move(fn));
fn(dirfd, entry); fn(dirfd, entry);
@ -67,7 +61,6 @@ static void pre_order_walk(int dirfd, const function<bool(int, dirent *)> &&fn)
if (!dir) return; if (!dir) return;
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (!fn(dirfd, entry)) if (!fn(dirfd, entry))
continue; continue;
if (entry->d_type == DT_DIR) if (entry->d_type == DT_DIR)
@ -109,7 +102,6 @@ void mv_dir(int src, int dest) {
auto dir = xopen_dir(src); auto dir = xopen_dir(src);
run_finally f([&]{ close(dest); }); run_finally f([&]{ close(dest); });
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
switch (entry->d_type) { switch (entry->d_type) {
case DT_DIR: case DT_DIR:
if (faccessat(dest, entry->d_name, F_OK, 0) == 0) { if (faccessat(dest, entry->d_name, F_OK, 0) == 0) {
@ -157,7 +149,6 @@ void clone_dir(int src, int dest) {
auto dir = xopen_dir(src); auto dir = xopen_dir(src);
run_finally f([&]{ close(dest); }); run_finally f([&]{ close(dest); });
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
file_attr a; file_attr a;
getattrat(src, entry->d_name, &a); getattrat(src, entry->d_name, &a);
switch (entry->d_type) { switch (entry->d_type) {
@ -197,7 +188,6 @@ void link_dir(int src, int dest) {
auto dir = xopen_dir(src); auto dir = xopen_dir(src);
run_finally f([&]{ close(dest); }); run_finally f([&]{ close(dest); });
for (dirent *entry; (entry = xreaddir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
file_attr a; file_attr a;
getattrat(src, entry->d_name, &a); getattrat(src, entry->d_name, &a);

View File

@ -13,6 +13,8 @@
#include <logging.hpp> #include <logging.hpp>
#include <utils.hpp> #include <utils.hpp>
using namespace std;
FILE *xfopen(const char *pathname, const char *mode) { FILE *xfopen(const char *pathname, const char *mode) {
FILE *fp = fopen(pathname, mode); FILE *fp = fopen(pathname, mode);
if (fp == nullptr) { if (fp == nullptr) {
@ -129,12 +131,19 @@ DIR *xfdopendir(int fd) {
struct dirent *xreaddir(DIR *dirp) { struct dirent *xreaddir(DIR *dirp) {
errno = 0; errno = 0;
struct dirent *e = readdir(dirp); for (dirent *e;;) {
if (errno && e == nullptr) { e = readdir(dirp);
if (e == nullptr) {
if (errno)
PLOGE("readdir"); PLOGE("readdir");
return nullptr;
} else if (e->d_name == "."sv || e->d_name == ".."sv) {
// Filter . and .. for users
continue;
} }
return e; return e;
} }
}
pid_t xsetsid() { pid_t xsetsid() {
pid_t pid = setsid(); pid_t pid = setsid();