diff --git a/include/utils/command.hpp b/include/utils/command.hpp index 0d939507..f80aeb58 100644 --- a/include/utils/command.hpp +++ b/include/utils/command.hpp @@ -50,7 +50,7 @@ namespace command_util { bool is_running(); int wait(); - void tail(callback callback); + void tail(callback cb); int writeline(string data); string readline(); diff --git a/include/utils/process.hpp b/include/utils/process.hpp index c0d6e4e7..84fa1d91 100644 --- a/include/utils/process.hpp +++ b/include/utils/process.hpp @@ -9,7 +9,7 @@ namespace process_util { bool in_forked_process(pid_t pid); void exec(char* cmd, char** args); - void exec(const string& cmd); + void exec_sh(const char* cmd); pid_t wait_for_completion(pid_t process_id, int* status_addr, int waitflags = 0); pid_t wait_for_completion(int* status_addr, int waitflags = 0); diff --git a/src/modules/script.cpp b/src/modules/script.cpp index 41166677..7e07e50d 100644 --- a/src/modules/script.cpp +++ b/src/modules/script.cpp @@ -84,14 +84,8 @@ namespace modules { } try { - if (m_command && m_command->is_running()) { - m_log.warn("%s: Previous shell command is still running...", name()); - return false; - } - auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter)); - m_log.trace("%s: Executing '%s'", name(), exec); - + m_log.info("%s: Executing \"%s\"", name(), exec); m_command = command_util::make_command(exec); m_command->exec(); m_command->tail([&](string output) { m_output = output; }); diff --git a/src/utils/command.cpp b/src/utils/command.cpp index 65fbb95b..56266d74 100644 --- a/src/utils/command.cpp +++ b/src/utils/command.cpp @@ -11,7 +11,7 @@ POLYBAR_NS namespace command_util { - command::command(const logger& logger, string cmd) : m_log(logger), m_cmd("/usr/bin/env\nsh\n-c\n" + cmd) { + command::command(const logger& logger, string cmd) : m_log(logger), m_cmd(cmd) { if (pipe(m_stdin) != 0) { throw command_strerror("Failed to allocate input stream"); } @@ -76,9 +76,7 @@ namespace command_util { process_util::unblock_signal(SIGTERM); setpgid(m_forkpid, 0); - process_util::exec(m_cmd); - - throw command_error("Exec failed"); + process_util::exec_sh(m_cmd.c_str()); } else { // Close file descriptors that won't be used by the parent if ((m_stdin[PIPE_READ] = close(m_stdin[PIPE_READ])) == -1) { @@ -155,8 +153,8 @@ namespace command_util { * @note: This is a blocking call and will not * end until the stream is closed */ - void command::tail(callback callback) { - io_util::tail(m_stdout[PIPE_READ], move(callback)); + void command::tail(callback cb) { + io_util::tail(m_stdout[PIPE_READ], move(cb)); } /** diff --git a/src/utils/io.cpp b/src/utils/io.cpp index 3df95eef..654103c7 100644 --- a/src/utils/io.cpp +++ b/src/utils/io.cpp @@ -93,7 +93,7 @@ namespace io_util { } void tail(int read_fd, int writeback_fd) { - tail(read_fd, [&writeback_fd](string data) { io_util::writeline(writeback_fd, data); }); + tail(read_fd, [&](string data) { io_util::writeline(writeback_fd, data); }); } bool poll(int fd, short int events, int timeout_ms) { diff --git a/src/utils/process.cpp b/src/utils/process.cpp index 39817b23..63cdd40d 100644 --- a/src/utils/process.cpp +++ b/src/utils/process.cpp @@ -2,6 +2,7 @@ #include #include "errors.hpp" +#include "utils/env.hpp" #include "utils/process.hpp" #include "utils/string.hpp" @@ -23,7 +24,7 @@ namespace process_util { } /** - * Replace current process + * Execute command */ void exec(char* cmd, char** args) { execvp(cmd, args); @@ -31,23 +32,12 @@ namespace process_util { } /** - * Replace process with command + * Execute command using shell */ - void exec(const string& cmd) { - vector c_args; - vector args; - - if (string_util::contains(cmd, "\n")) { - string_util::split_into(cmd, '\n', args); - } else { - string_util::split_into(cmd, ' ', args); - } - - for (auto&& argument : args) { - c_args.emplace_back(const_cast(argument.c_str())); - } - - exec(c_args[0], c_args.data()); + void exec_sh(const char* cmd) { + static const char* shell = env_util::get("SHELL", "/bin/sh").c_str(); + execlp(shell, shell, "-c", cmd, nullptr); + throw system_error("execvp() failed"); } /**