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);
}