From 5773b08cf8709a2b8642f45b5e2f5560df332e22 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Wed, 11 Jan 2017 18:15:46 +0100 Subject: [PATCH] fix(script): Poll file descriptors --- include/utils/io.hpp | 6 +++--- src/modules/script.cpp | 25 +++++++++++++++---------- src/utils/io.cpp | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/utils/io.hpp b/include/utils/io.hpp index 8360172d..921e9d12 100644 --- a/include/utils/io.hpp +++ b/include/utils/io.hpp @@ -14,9 +14,9 @@ namespace io_util { void tail(int read_fd, const function& callback); void tail(int read_fd, int writeback_fd); - bool poll(int fd, short int events, int timeout_ms = 1); - bool poll_read(int fd, int timeout_ms = 1); - bool poll_write(int fd, int timeout_ms = 1); + bool poll(int fd, short int events, int timeout_ms = 0); + bool poll_read(int fd, int timeout_ms = 0); + bool poll_write(int fd, int timeout_ms = 0); bool interrupt_read(int write_fd); diff --git a/src/modules/script.cpp b/src/modules/script.cpp index 6c641904..8a334305 100644 --- a/src/modules/script.cpp +++ b/src/modules/script.cpp @@ -1,6 +1,5 @@ #include "modules/script.hpp" #include "drawtypes/label.hpp" - #include "modules/meta/base.inl" POLYBAR_NS @@ -31,14 +30,19 @@ namespace modules { } } - if (io_util::poll(m_command->get_stdout(PIPE_READ), POLLIN, 0)) { - if ((m_output = m_command->readline()) != m_prev) { + int fd = m_command->get_stdout(PIPE_READ); + while (!m_stopping && fd != -1 && m_command->is_running() && !io_util::poll(fd, POLLHUP, 0)) { + if (!io_util::poll_read(fd, 25)) { + continue; + } else if ((m_output = m_command->readline()) != m_prev) { m_prev = m_output; broadcast(); } } - if (m_command && !m_command->is_running()) { + if (m_stopping) { + return chrono::duration{0}; + } else if (m_command && !m_command->is_running()) { return std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval); } else { return m_interval; @@ -60,9 +64,14 @@ namespace modules { throw module_error("Failed to execute command, stopping module..."); } - if ((m_output = m_command->readline()) != m_prev) { + int fd = m_command->get_stdout(PIPE_READ); + if (fd != -1 && io_util::poll_read(fd) && (m_output = m_command->readline()) != m_prev) { broadcast(); m_prev = m_output; + } else if (m_command->get_exit_status() != 0) { + m_output.clear(); + m_prev.clear(); + broadcast(); } return std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval); @@ -114,14 +123,10 @@ namespace modules { * Stop the module worker by terminating any running commands */ void script_module::stop() { - std::lock_guard guard(m_handler); m_stopping = true; wakeup(); - if (m_command && m_command->is_running()) { - m_log.warn("%s: Stopping shell command", name()); - m_command->terminate(); - } + std::lock_guard guard(m_handler); m_command.reset(); module::stop(); diff --git a/src/utils/io.cpp b/src/utils/io.cpp index f0c22d2e..8e3dc017 100644 --- a/src/utils/io.cpp +++ b/src/utils/io.cpp @@ -73,7 +73,7 @@ namespace io_util { } bool interrupt_read(int write_fd) { - return writeline(write_fd, "") > 0; + return write(write_fd, 1, {'\n'}) > 0; } void set_block(int fd) {