fix(command): Use execlp()

Ref #214
This commit is contained in:
Michael Carlberg 2016-12-03 20:52:42 +01:00
parent bb0cfcf033
commit 5f6d73a415
6 changed files with 15 additions and 33 deletions

View File

@ -50,7 +50,7 @@ namespace command_util {
bool is_running(); bool is_running();
int wait(); int wait();
void tail(callback<string> callback); void tail(callback<string> cb);
int writeline(string data); int writeline(string data);
string readline(); string readline();

View File

@ -9,7 +9,7 @@ namespace process_util {
bool in_forked_process(pid_t pid); bool in_forked_process(pid_t pid);
void exec(char* cmd, char** args); 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(pid_t process_id, int* status_addr, int waitflags = 0);
pid_t wait_for_completion(int* status_addr, int waitflags = 0); pid_t wait_for_completion(int* status_addr, int waitflags = 0);

View File

@ -84,14 +84,8 @@ namespace modules {
} }
try { 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)); 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 = command_util::make_command(exec);
m_command->exec(); m_command->exec();
m_command->tail([&](string output) { m_output = output; }); m_command->tail([&](string output) { m_output = output; });

View File

@ -11,7 +11,7 @@
POLYBAR_NS POLYBAR_NS
namespace command_util { 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) { if (pipe(m_stdin) != 0) {
throw command_strerror("Failed to allocate input stream"); throw command_strerror("Failed to allocate input stream");
} }
@ -76,9 +76,7 @@ namespace command_util {
process_util::unblock_signal(SIGTERM); process_util::unblock_signal(SIGTERM);
setpgid(m_forkpid, 0); setpgid(m_forkpid, 0);
process_util::exec(m_cmd); process_util::exec_sh(m_cmd.c_str());
throw command_error("Exec failed");
} else { } else {
// Close file descriptors that won't be used by the parent // Close file descriptors that won't be used by the parent
if ((m_stdin[PIPE_READ] = close(m_stdin[PIPE_READ])) == -1) { 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 * @note: This is a blocking call and will not
* end until the stream is closed * end until the stream is closed
*/ */
void command::tail(callback<string> callback) { void command::tail(callback<string> cb) {
io_util::tail(m_stdout[PIPE_READ], move(callback)); io_util::tail(m_stdout[PIPE_READ], move(cb));
} }
/** /**

View File

@ -93,7 +93,7 @@ namespace io_util {
} }
void tail(int read_fd, int writeback_fd) { 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) { bool poll(int fd, short int events, int timeout_ms) {

View File

@ -2,6 +2,7 @@
#include <unistd.h> #include <unistd.h>
#include "errors.hpp" #include "errors.hpp"
#include "utils/env.hpp"
#include "utils/process.hpp" #include "utils/process.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
@ -23,7 +24,7 @@ namespace process_util {
} }
/** /**
* Replace current process * Execute command
*/ */
void exec(char* cmd, char** args) { void exec(char* cmd, char** args) {
execvp(cmd, args); execvp(cmd, args);
@ -31,23 +32,12 @@ namespace process_util {
} }
/** /**
* Replace process with command * Execute command using shell
*/ */
void exec(const string& cmd) { void exec_sh(const char* cmd) {
vector<char*> c_args; static const char* shell = env_util::get("SHELL", "/bin/sh").c_str();
vector<string> args; execlp(shell, shell, "-c", cmd, nullptr);
throw system_error("execvp() failed");
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<char*>(argument.c_str()));
}
exec(c_args[0], c_args.data());
} }
/** /**