From 263b9c6e39c98a2790167db5251379e0acfd3e89 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Fri, 21 Jul 2023 17:20:23 +0200 Subject: [PATCH] socket1: add a timeout for requests --- src/debug/HyprCtl.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 5c9b329e4..03c556db9 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -686,11 +686,10 @@ std::string dispatchRequest(std::string in) { in = in.substr(in.find_first_of(' ') + 1); const auto DISPATCHSTR = in.substr(0, in.find_first_of(' ')); - - auto DISPATCHARG = std::string(); - if ((int) in.find_first_of(' ') != -1) - DISPATCHARG = in.substr(in.find_first_of(' ') + 1); + auto DISPATCHARG = std::string(); + if ((int)in.find_first_of(' ') != -1) + DISPATCHARG = in.substr(in.find_first_of(' ') + 1); const auto DISPATCHER = g_pKeybindManager->m_mDispatchers.find(DISPATCHSTR); if (DISPATCHER == g_pKeybindManager->m_mDispatchers.end()) @@ -1312,7 +1311,18 @@ int hyprCtlFDTick(int fd, uint32_t mask, void* data) { char readBuffer[1024]; - auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); + fd_set fdset; + FD_ZERO(&fdset); + FD_SET(ACCEPTEDCONNECTION, &fdset); + timeval timeout = {.tv_sec = 0, .tv_usec = 5000}; + auto success = select(ACCEPTEDCONNECTION + 1, &fdset, nullptr, nullptr, &timeout); + + if (success <= 0) { + close(ACCEPTEDCONNECTION); + return 0; + } + + auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0'; std::string request(readBuffer);