Fix fd ownership

This commit is contained in:
Nullptr
2023-01-31 15:04:01 +08:00
parent b8678720fb
commit d7877ea959
5 changed files with 28 additions and 28 deletions

View File

@@ -8,8 +8,8 @@
namespace zygiskd {
UniqueFd Connect(uint8_t retry) {
UniqueFd fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
int Connect(uint8_t retry) {
int fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
struct sockaddr_un addr{
.sun_family = AF_UNIX,
.sun_path={0},
@@ -23,12 +23,14 @@ namespace zygiskd {
LOGW("retrying to connect to zygiskd, sleep 1s");
sleep(1);
}
close(fd);
return -1;
}
bool PingHeartbeat() {
LOGD("Daemon socket: %s", kZygiskSocket.data());
auto fd = Connect(5);
UniqueFd fd = Connect(5);
if (fd == -1) {
PLOGE("Connect to zygiskd");
return false;
@@ -38,7 +40,7 @@ namespace zygiskd {
}
std::string ReadNativeBridge() {
auto fd = Connect(1);
UniqueFd fd = Connect(1);
if (fd == -1) {
PLOGE("ReadNativeBridge");
return "";
@@ -49,7 +51,7 @@ namespace zygiskd {
std::vector<Module> ReadModules() {
std::vector<Module> modules;
auto fd = Connect(1);
UniqueFd fd = Connect(1);
if (fd == -1) {
PLOGE("ReadModules");
return modules;
@@ -69,8 +71,8 @@ namespace zygiskd {
return modules;
}
UniqueFd ConnectCompanion(size_t index) {
auto fd = Connect(1);
int ConnectCompanion(size_t index) {
int fd = Connect(1);
if (fd == -1) {
PLOGE("ConnectCompanion");
return -1;
@@ -84,8 +86,8 @@ namespace zygiskd {
}
}
UniqueFd GetModuleDir(size_t index) {
auto fd = Connect(1);
int GetModuleDir(size_t index) {
int fd = Connect(1);
if (fd == -1) {
PLOGE("GetModuleDir");
return -1;

View File

@@ -86,6 +86,10 @@ namespace socket_utils {
return sizeof(T) == xwrite(fd, &val, sizeof(T));
}
uint8_t read_u8(int fd) {
return read_exact_or<uint8_t>(fd, 0);
}
size_t read_usize(int fd) {
return read_exact_or<size_t>(fd, 0);
}
@@ -116,8 +120,4 @@ namespace socket_utils {
memcpy(&result, data, sizeof(int));
return result;
}
uint8_t read_u8(int fd) {
return read_exact_or<uint8_t>(fd, 0);
}
}

View File

@@ -65,7 +65,7 @@ namespace zygiskd {
std::vector<Module> ReadModules();
UniqueFd ConnectCompanion(size_t index);
int ConnectCompanion(size_t index);
UniqueFd GetModuleDir(size_t index);
int GetModuleDir(size_t index);
}

View File

@@ -10,6 +10,8 @@ namespace socket_utils {
ssize_t xwrite(int fd, const void *buf, size_t count);
uint8_t read_u8(int fd);
size_t read_usize(int fd);
std::string read_string(int fd);
@@ -19,6 +21,4 @@ namespace socket_utils {
int recv_fd(int fd);
bool write_usize(int fd, size_t val);
uint8_t read_u8(int fd);
}

View File

@@ -99,8 +99,9 @@ fn load_modules(arch: &str) -> Result<Vec<Module>> {
for entry_result in dir.into_iter() {
let entry = entry_result?;
let name = entry.file_name().into_string().unwrap();
let so_path = entry.path().join(format!("zygisk/{arch}.so"));
if !so_path.exists() {
let so_path = entry.path().join(format!("zygisksu/{arch}.so"));
let disabled = entry.path().join("disabled");
if !so_path.exists() || disabled.exists() {
continue;
}
log::info!(" Loading module `{name}`...");
@@ -203,6 +204,7 @@ fn handle_daemon_action(mut stream: UnixStream, context: &Context) -> Result<()>
let module = &context.modules[index];
log::debug!("New companion request from module {}", module.name);
// FIXME: Spawn a new process
match module.companion_entry {
Some(entry) => {
stream.write_u8(1)?;
@@ -214,15 +216,11 @@ fn handle_daemon_action(mut stream: UnixStream, context: &Context) -> Result<()>
}
}
DaemonSocketAction::GetModuleDir => {
unsafe {
let index = stream.read_usize()?;
let module = &context.modules[index];
let path = format!("{}/{}", constants::PATH_KSU_MODULE_DIR, module.name);
let filename = std::ffi::CString::new(path)?;
let fd = libc::open(filename.as_ptr(), libc::O_PATH | libc::O_CLOEXEC);
stream.send_fd(fd)?;
libc::close(fd);
}
let index = stream.read_usize()?;
let module = &context.modules[index];
let dir = format!("{}/{}", constants::PATH_KSU_MODULE_DIR, module.name);
let dir = fs::File::open(dir)?;
stream.send_fd(dir.as_raw_fd())?;
}
}
Ok(())