fix(script): Poll file descriptors

This commit is contained in:
Michael Carlberg 2017-01-11 18:15:46 +01:00
parent d7d0fbcae4
commit 5773b08cf8
3 changed files with 19 additions and 14 deletions

View File

@ -14,9 +14,9 @@ namespace io_util {
void tail(int read_fd, const function<void(string)>& callback); void tail(int read_fd, const function<void(string)>& callback);
void tail(int read_fd, int writeback_fd); void tail(int read_fd, int writeback_fd);
bool poll(int fd, short int events, int timeout_ms = 1); bool poll(int fd, short int events, int timeout_ms = 0);
bool poll_read(int fd, int timeout_ms = 1); bool poll_read(int fd, int timeout_ms = 0);
bool poll_write(int fd, int timeout_ms = 1); bool poll_write(int fd, int timeout_ms = 0);
bool interrupt_read(int write_fd); bool interrupt_read(int write_fd);

View File

@ -1,6 +1,5 @@
#include "modules/script.hpp" #include "modules/script.hpp"
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
POLYBAR_NS POLYBAR_NS
@ -31,14 +30,19 @@ namespace modules {
} }
} }
if (io_util::poll(m_command->get_stdout(PIPE_READ), POLLIN, 0)) { int fd = m_command->get_stdout(PIPE_READ);
if ((m_output = m_command->readline()) != m_prev) { 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; m_prev = m_output;
broadcast(); broadcast();
} }
} }
if (m_command && !m_command->is_running()) { if (m_stopping) {
return chrono::duration<double>{0};
} else if (m_command && !m_command->is_running()) {
return std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval); return std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval);
} else { } else {
return m_interval; return m_interval;
@ -60,9 +64,14 @@ namespace modules {
throw module_error("Failed to execute command, stopping module..."); 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(); broadcast();
m_prev = m_output; 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); 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 * Stop the module worker by terminating any running commands
*/ */
void script_module::stop() { void script_module::stop() {
std::lock_guard<decltype(m_handler)> guard(m_handler);
m_stopping = true; m_stopping = true;
wakeup(); wakeup();
if (m_command && m_command->is_running()) { std::lock_guard<decltype(m_handler)> guard(m_handler);
m_log.warn("%s: Stopping shell command", name());
m_command->terminate();
}
m_command.reset(); m_command.reset();
module::stop(); module::stop();

View File

@ -73,7 +73,7 @@ namespace io_util {
} }
bool interrupt_read(int write_fd) { bool interrupt_read(int write_fd) {
return writeline(write_fd, "") > 0; return write(write_fd, 1, {'\n'}) > 0;
} }
void set_block(int fd) { void set_block(int fd) {