2021-10-02 23:27:11 +00:00
|
|
|
#include "adapters/script_runner.hpp"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include "modules/meta/base.hpp"
|
|
|
|
#include "utils/command.hpp"
|
|
|
|
#include "utils/io.hpp"
|
|
|
|
#include "utils/scope.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
script_runner::script_runner(std::function<void(void)> on_update, const string& exec, const string& exec_if, bool tail,
|
2022-03-06 08:34:12 +00:00
|
|
|
interval interval_success, interval interval_fail, const vector<pair<string, string>>& env)
|
2021-10-02 23:27:11 +00:00
|
|
|
: m_log(logger::make())
|
|
|
|
, m_on_update(on_update)
|
|
|
|
, m_exec(exec)
|
|
|
|
, m_exec_if(exec_if)
|
|
|
|
, m_tail(tail)
|
2022-03-06 08:34:12 +00:00
|
|
|
, m_interval_success(interval_success)
|
|
|
|
, m_interval_fail(interval_fail)
|
2021-10-02 23:27:11 +00:00
|
|
|
, m_env(env) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if defined condition is met
|
|
|
|
*/
|
|
|
|
bool script_runner::check_condition() const {
|
|
|
|
if (m_exec_if.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-06 16:44:48 +00:00
|
|
|
command<output_policy::IGNORED> exec_if_cmd(m_log, m_exec_if);
|
|
|
|
return exec_if_cmd.exec(true) == 0;
|
2021-10-02 23:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process mutex wrapped script handler
|
|
|
|
*/
|
|
|
|
script_runner::interval script_runner::process() {
|
|
|
|
if (m_tail) {
|
|
|
|
return run_tail();
|
|
|
|
} else {
|
|
|
|
return run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void script_runner::clear_output() {
|
|
|
|
set_output("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void script_runner::stop() {
|
|
|
|
m_stopping = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int script_runner::get_pid() const {
|
|
|
|
return m_pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int script_runner::get_counter() const {
|
|
|
|
return m_counter;
|
|
|
|
}
|
|
|
|
|
2022-03-06 08:34:12 +00:00
|
|
|
int script_runner::get_exit_status() const {
|
2022-02-23 14:01:28 +00:00
|
|
|
return m_exit_status;
|
|
|
|
}
|
|
|
|
|
2021-10-02 23:27:11 +00:00
|
|
|
string script_runner::get_output() {
|
|
|
|
std::lock_guard<std::mutex> guard(m_output_lock);
|
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool script_runner::is_stopping() const {
|
|
|
|
return m_stopping;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the current output.
|
|
|
|
*
|
|
|
|
* Returns true if the output changed.
|
|
|
|
*/
|
2022-02-23 14:01:28 +00:00
|
|
|
bool script_runner::set_output(string&& new_output) {
|
2021-10-02 23:27:11 +00:00
|
|
|
std::lock_guard<std::mutex> guard(m_output_lock);
|
|
|
|
|
|
|
|
if (m_output != new_output) {
|
|
|
|
m_output = std::move(new_output);
|
|
|
|
m_on_update();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
script_runner::interval script_runner::run() {
|
|
|
|
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
|
|
|
m_log.info("script_runner: Invoking shell command: \"%s\"", exec);
|
2022-03-06 16:44:48 +00:00
|
|
|
command<output_policy::REDIRECTED> cmd(m_log, exec);
|
2021-10-02 23:27:11 +00:00
|
|
|
|
|
|
|
try {
|
2022-03-06 16:44:48 +00:00
|
|
|
cmd.exec(false, m_env);
|
2021-10-02 23:27:11 +00:00
|
|
|
} catch (const exception& err) {
|
|
|
|
m_log.err("script_runner: %s", err.what());
|
|
|
|
throw modules::module_error("Failed to execute command, stopping module...");
|
|
|
|
}
|
|
|
|
|
2022-03-06 16:44:48 +00:00
|
|
|
int fd = cmd.get_stdout(PIPE_READ);
|
2021-10-02 23:27:11 +00:00
|
|
|
assert(fd != -1);
|
2022-03-03 22:38:51 +00:00
|
|
|
|
|
|
|
bool got_output = false;
|
2022-03-06 16:44:48 +00:00
|
|
|
while (!m_stopping && cmd.is_running() && !io_util::poll(fd, POLLHUP, 0)) {
|
2022-03-03 22:38:51 +00:00
|
|
|
/**
|
|
|
|
* For non-tailed scripts, we only use the first line. However, to ensure interruptability when the module shuts
|
|
|
|
* down, we still need to continue polling.
|
|
|
|
*/
|
|
|
|
if (io_util::poll_read(fd, 25) && !got_output) {
|
2022-04-01 18:15:35 +00:00
|
|
|
set_output(cmd.readline());
|
2022-03-03 22:38:51 +00:00
|
|
|
got_output = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_stopping) {
|
2022-03-06 16:44:48 +00:00
|
|
|
cmd.terminate();
|
2022-03-03 22:38:51 +00:00
|
|
|
return 0s;
|
|
|
|
}
|
|
|
|
|
2022-03-06 16:44:48 +00:00
|
|
|
m_exit_status = cmd.wait();
|
2021-10-02 23:27:11 +00:00
|
|
|
|
2022-02-23 14:01:28 +00:00
|
|
|
if (m_exit_status == 0) {
|
2022-03-06 08:34:12 +00:00
|
|
|
return m_interval_success;
|
2021-10-02 23:27:11 +00:00
|
|
|
} else {
|
2022-03-06 08:34:12 +00:00
|
|
|
return std::max(m_interval_fail, interval{1s});
|
2021-10-02 23:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
script_runner::interval script_runner::run_tail() {
|
|
|
|
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
|
|
|
m_log.info("script_runner: Invoking shell command: \"%s\"", exec);
|
2022-03-06 16:44:48 +00:00
|
|
|
command<output_policy::REDIRECTED> cmd(m_log, exec);
|
2021-10-02 23:27:11 +00:00
|
|
|
|
|
|
|
try {
|
2022-03-06 16:44:48 +00:00
|
|
|
cmd.exec(false, m_env);
|
2021-10-02 23:27:11 +00:00
|
|
|
} catch (const exception& err) {
|
|
|
|
throw modules::module_error("Failed to execute command: " + string(err.what()));
|
|
|
|
}
|
|
|
|
|
2022-03-06 16:02:28 +00:00
|
|
|
scope_util::on_exit pid_guard([this]() { m_pid = -1; });
|
2022-03-06 16:44:48 +00:00
|
|
|
m_pid = cmd.get_pid();
|
2021-10-02 23:27:11 +00:00
|
|
|
|
2022-03-06 16:44:48 +00:00
|
|
|
int fd = cmd.get_stdout(PIPE_READ);
|
2021-10-02 23:27:11 +00:00
|
|
|
assert(fd != -1);
|
|
|
|
|
2022-03-06 16:44:48 +00:00
|
|
|
while (!m_stopping && cmd.is_running() && !io_util::poll(fd, POLLHUP, 0)) {
|
2021-10-02 23:27:11 +00:00
|
|
|
if (io_util::poll_read(fd, 25)) {
|
2022-03-06 16:44:48 +00:00
|
|
|
set_output(cmd.readline());
|
2021-10-02 23:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_stopping) {
|
2022-03-06 16:44:48 +00:00
|
|
|
cmd.terminate();
|
2021-10-02 23:27:11 +00:00
|
|
|
return 0s;
|
|
|
|
}
|
|
|
|
|
2022-03-06 16:44:48 +00:00
|
|
|
auto exit_status = cmd.wait();
|
2021-10-02 23:27:11 +00:00
|
|
|
|
|
|
|
if (exit_status == 0) {
|
2022-03-06 08:34:12 +00:00
|
|
|
return m_interval_success;
|
2021-10-02 23:27:11 +00:00
|
|
|
} else {
|
2022-03-06 08:34:12 +00:00
|
|
|
return std::max(m_interval_fail, interval{1s});
|
2021-10-02 23:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|