diff --git a/utils/include/dinput_utils_tool.h b/utils/include/dinput_utils_tool.h index 1a9e7ccddfb0c658d18100c0479e99831e22a5a8..01d0d6cd31ed5f50d1c158be19c214ee8e828656 100644 --- a/utils/include/dinput_utils_tool.h +++ b/utils/include/dinput_utils_tool.h @@ -51,7 +51,7 @@ std::string GetNodeDesc(std::string parameters); std::string GetAnonyString(const std::string &value); std::string GetAnonyInt32(const int32_t value); std::string Sha256(const std::string &string); -void CloseFd(int fd); +void CloseFd(int &fd); int BitIsSet(const unsigned long *array, int bit); void SplitStringToVector(const std::string &str, const char split, std::vector &vecStr); int OpenInputDeviceFdByPath(const std::string &devicePath); diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index a6e99fa83a377b93bfa2911240857fb66a445bf5..0ddfe371e4cf276f4213ec49d0b5ea4367cae3ca 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -268,7 +268,7 @@ std::string Sha256(const std::string &in) return reinterpret_cast(out); } -void CloseFd(int fd) +void CloseFd(int &fd) { if (fd < 0) { DHLOGE("No fd need to beclosed."); @@ -403,6 +403,7 @@ void ResetVirtualDevicePressedKeys(const std::vector &nodePaths) int rc = ioctl(fd, EVIOCGKEY(sizeof(keyState)), keyState); if (rc < 0) { DHLOGE("Read all key state failed, rc: %{public}d, path: %{public}s", rc, path.c_str()); + CloseFd(fd); continue; } @@ -414,6 +415,7 @@ void ResetVirtualDevicePressedKeys(const std::vector &nodePaths) } if (pressedKeys.empty()) { + CloseFd(fd); continue; } @@ -430,6 +432,7 @@ void ResetVirtualDevicePressedKeys(const std::vector &nodePaths) event.code = 0; WriteEventToDevice(fd, event); } + CloseFd(fd); } }