Use pidfd in more senarios

This commit is contained in:
LoveSy 2024-05-22 21:39:40 +08:00 committed by John Wu
parent 2f710a564f
commit d6e159bff9

View File

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <cinttypes> #include <cinttypes>
#include <android/log.h> #include <android/log.h>
#include <sys/syscall.h>
#include <base.hpp> #include <base.hpp>
@ -190,18 +191,27 @@ static void process_events_buffer(struct log_msg *msg) {
} }
char path[16]; char path[16];
ssprintf(path, sizeof(path), "/proc/%d", pid);
struct stat st{}; struct stat st{};
sprintf(path, "/proc/%d", pid); int fd = syscall(__NR_pidfd_open, pid, 0);
if (fd > 0 && setns(fd, CLONE_NEWNS) == 0) {
pid = getpid();
} else {
close(fd);
fd = -1;
}
while (read_ns(pid, &st) == 0 && it->second.st_ino == st.st_ino) { while (read_ns(pid, &st) == 0 && it->second.st_ino == st.st_ino) {
if (stat(path, &st) == 0 && st.st_uid == 0) { if (stat(path, &st) == 0 && st.st_uid == 0) {
usleep(10 * 1000); usleep(10 * 1000);
} else { } else {
LOGW("logcat: skip [%.*s] PID=[%d] UID=[%d]; namespace not isolated\n", LOGW("logcat: skip [%.*s] PID=[%s] UID=[%d]; namespace not isolated\n",
(int) proc.length(), proc.data(), (int) proc.length(), proc.data(),
pid, am_proc_start->uid.data); path + 6, am_proc_start->uid.data);
_exit(0); _exit(0);
} }
if (fd > 0) setns(fd, CLONE_NEWNS);
} }
close(fd);
LOGI("logcat: revert [%.*s] PID=[%d] UID=[%d]\n", LOGI("logcat: revert [%.*s] PID=[%d] UID=[%d]\n",
(int) proc.length(), proc.data(), pid, am_proc_start->uid.data); (int) proc.length(), proc.data(), pid, am_proc_start->uid.data);