2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
|
|
|
#include "utils/io.hpp"
|
|
|
|
#include "utils/process.hpp"
|
|
|
|
#include "utils/threading.hpp"
|
|
|
|
|
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
2016-10-15 07:15:56 -04:00
|
|
|
DEFINE_ERROR(command_error);
|
|
|
|
DEFINE_CHILD_ERROR(command_strerror, command_error);
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
namespace command_util {
|
|
|
|
/**
|
2016-10-15 07:15:56 -04:00
|
|
|
* Wrapper used to execute command in a subprocess.
|
|
|
|
* In-/output streams are opened to enable ipc.
|
|
|
|
*
|
2016-06-14 23:32:35 -04:00
|
|
|
* Example usage:
|
2016-10-15 07:15:56 -04:00
|
|
|
*
|
2016-06-14 23:32:35 -04:00
|
|
|
* @code cpp
|
2016-10-15 07:15:56 -04:00
|
|
|
* auto cmd = command_util::make_command("cat /etc/rc.local");
|
2016-06-14 23:32:35 -04:00
|
|
|
* cmd->exec();
|
2016-10-15 07:15:56 -04:00
|
|
|
* cmd->tail([](string s) { std::cout << s << std::endl; });
|
|
|
|
* @endcode
|
2016-06-14 23:32:35 -04:00
|
|
|
*
|
2016-10-15 07:15:56 -04:00
|
|
|
* @code cpp
|
|
|
|
* auto cmd = command_util::make_command(
|
2016-06-14 23:32:35 -04:00
|
|
|
* "/bin/sh\n-c\n while read -r line; do echo data from parent process: $line; done");
|
2016-10-15 07:15:56 -04:00
|
|
|
* cmd->exec(false);
|
2016-06-14 23:32:35 -04:00
|
|
|
* cmd->writeline("Test");
|
2016-10-15 07:15:56 -04:00
|
|
|
* cout << cmd->readline();
|
|
|
|
* cmd->wait();
|
|
|
|
* @endcode
|
2016-06-14 23:32:35 -04:00
|
|
|
*
|
2016-10-15 07:15:56 -04:00
|
|
|
* @code cpp
|
|
|
|
* vector<string> exec{{"/bin/sh"}, {"-c"}, {"for i in 1 2 3; do echo $i; done"}};
|
|
|
|
* auto cmd = command_util::make_command(exec);
|
|
|
|
* cmd->exec();
|
|
|
|
* cout << cmd->readline(); // 1
|
|
|
|
* cout << cmd->readline() << cmd->readline(); // 23
|
|
|
|
* @endcode
|
2016-06-14 23:32:35 -04:00
|
|
|
*/
|
|
|
|
class command {
|
|
|
|
public:
|
2016-10-15 07:15:56 -04:00
|
|
|
explicit command(const logger& logger, vector<string> cmd)
|
|
|
|
: command(logger, string_util::join(cmd, "\n")) {}
|
|
|
|
|
|
|
|
explicit 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");
|
|
|
|
if (pipe(m_stdout) != 0)
|
|
|
|
throw command_strerror("Failed to allocate output stream");
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
~command() {
|
|
|
|
if (is_running()) {
|
2016-10-15 07:15:56 -04:00
|
|
|
try {
|
|
|
|
m_log.trace("command: Sending SIGTERM to running child process (%d)", m_forkpid);
|
|
|
|
killpg(m_forkpid, SIGTERM);
|
|
|
|
wait();
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
m_log.err("command: %s", err.what());
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
2016-10-15 07:15:56 -04:00
|
|
|
|
|
|
|
if (m_stdin[PIPE_READ] > 0)
|
|
|
|
close(m_stdin[PIPE_READ]);
|
|
|
|
if (m_stdin[PIPE_WRITE] > 0)
|
|
|
|
close(m_stdin[PIPE_WRITE]);
|
|
|
|
if (m_stdout[PIPE_READ] > 0)
|
|
|
|
close(m_stdout[PIPE_READ]);
|
|
|
|
if (m_stdout[PIPE_WRITE] > 0)
|
|
|
|
close(m_stdout[PIPE_WRITE]);
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the command
|
|
|
|
*/
|
|
|
|
int exec(bool wait_for_completion = true) {
|
|
|
|
if ((m_forkpid = fork()) == -1)
|
|
|
|
throw system_error("Failed to fork process");
|
|
|
|
|
|
|
|
if (process_util::in_forked_process(m_forkpid)) {
|
|
|
|
if (dup2(m_stdin[PIPE_READ], STDIN_FILENO) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to redirect stdin in child process");
|
2016-06-14 23:32:35 -04:00
|
|
|
if (dup2(m_stdout[PIPE_WRITE], STDOUT_FILENO) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to redirect stdout in child process");
|
2016-06-14 23:32:35 -04:00
|
|
|
if (dup2(m_stdout[PIPE_WRITE], STDERR_FILENO) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to redirect stderr in child process");
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-15 07:15:56 -04:00
|
|
|
// Close file descriptors that won't be used by the child
|
2016-06-14 23:32:35 -04:00
|
|
|
if ((m_stdin[PIPE_READ] = close(m_stdin[PIPE_READ])) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to close fd");
|
2016-06-14 23:32:35 -04:00
|
|
|
if ((m_stdin[PIPE_WRITE] = close(m_stdin[PIPE_WRITE])) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to close fd");
|
2016-06-14 23:32:35 -04:00
|
|
|
if ((m_stdout[PIPE_READ] = close(m_stdout[PIPE_READ])) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to close fd");
|
2016-06-14 23:32:35 -04:00
|
|
|
if ((m_stdout[PIPE_WRITE] = close(m_stdout[PIPE_WRITE])) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to close fd");
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-15 07:15:56 -04:00
|
|
|
// Make sure SIGTERM is raised
|
|
|
|
process_util::unblock_signal(SIGTERM);
|
|
|
|
|
|
|
|
setpgid(m_forkpid, 0);
|
2016-06-14 23:32:35 -04:00
|
|
|
process_util::exec(m_cmd);
|
2016-10-15 07:15:56 -04:00
|
|
|
|
|
|
|
throw command_error("Exec failed");
|
2016-06-14 23:32:35 -04:00
|
|
|
} else {
|
2016-10-15 07:15:56 -04:00
|
|
|
// Close file descriptors that won't be used by the parent
|
2016-06-14 23:32:35 -04:00
|
|
|
if ((m_stdin[PIPE_READ] = close(m_stdin[PIPE_READ])) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to close fd");
|
2016-06-14 23:32:35 -04:00
|
|
|
if ((m_stdout[PIPE_WRITE] = close(m_stdout[PIPE_WRITE])) == -1)
|
2016-10-15 07:15:56 -04:00
|
|
|
throw command_strerror("Failed to close fd");
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
if (wait_for_completion)
|
|
|
|
return wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for the child processs to finish
|
|
|
|
*/
|
|
|
|
int wait() {
|
2016-10-15 07:15:56 -04:00
|
|
|
auto waitflags = WCONTINUED | WUNTRACED;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-10-15 07:15:56 -04:00
|
|
|
do {
|
|
|
|
if (process_util::wait_for_completion(m_forkpid, &m_forkstatus, waitflags) == -1)
|
|
|
|
throw command_error("Process did not finish successfully");
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
if (WIFEXITED(m_forkstatus))
|
|
|
|
m_log.trace("command: Exited with status %d", WEXITSTATUS(m_forkstatus));
|
|
|
|
else if (WIFSIGNALED(m_forkstatus))
|
2016-10-15 07:15:56 -04:00
|
|
|
m_log.trace("command: killed by signal %d", WTERMSIG(m_forkstatus));
|
2016-06-14 23:32:35 -04:00
|
|
|
else if (WIFSTOPPED(m_forkstatus))
|
2016-10-15 07:15:56 -04:00
|
|
|
m_log.trace("command: Stopped by signal %d", WSTOPSIG(m_forkstatus));
|
2016-06-14 23:32:35 -04:00
|
|
|
else if (WIFCONTINUED(m_forkstatus) == true)
|
|
|
|
m_log.trace("command: Continued");
|
|
|
|
} while (!WIFEXITED(m_forkstatus) && !WIFSIGNALED(m_forkstatus));
|
|
|
|
|
|
|
|
return m_forkstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tail command output
|
|
|
|
*/
|
|
|
|
void tail(function<void(string)> callback) {
|
|
|
|
io_util::tail(m_stdout[PIPE_READ], callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write line to command input channel
|
|
|
|
*/
|
|
|
|
int writeline(string data) {
|
|
|
|
std::lock_guard<threading_util::spin_lock> lck(m_pipelock);
|
|
|
|
return io_util::writeline(m_stdin[PIPE_WRITE], data);
|
|
|
|
}
|
|
|
|
|
2016-10-15 07:15:56 -04:00
|
|
|
/**
|
|
|
|
* Read a line from the commands output stream
|
|
|
|
*/
|
|
|
|
string readline() {
|
|
|
|
std::lock_guard<threading_util::spin_lock> lck(m_pipelock);
|
|
|
|
return io_util::readline(m_stdout[PIPE_READ]);
|
|
|
|
}
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
/**
|
|
|
|
* Get command output channel
|
|
|
|
*/
|
|
|
|
int get_stdout(int c) {
|
|
|
|
return m_stdout[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get command input channel
|
|
|
|
*/
|
|
|
|
int get_stdin(int c) {
|
|
|
|
return m_stdin[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get command pid
|
|
|
|
*/
|
|
|
|
pid_t get_pid() {
|
|
|
|
return m_forkpid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if command is running
|
|
|
|
*/
|
|
|
|
bool is_running() {
|
|
|
|
return process_util::wait_for_completion_nohang(m_forkpid, &m_forkstatus) > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get command exit status
|
|
|
|
*/
|
|
|
|
int get_exit_status() {
|
|
|
|
return m_forkstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const logger& m_log;
|
|
|
|
|
|
|
|
string m_cmd;
|
|
|
|
|
|
|
|
int m_stdout[2];
|
|
|
|
int m_stdin[2];
|
|
|
|
|
|
|
|
pid_t m_forkpid;
|
|
|
|
int m_forkstatus;
|
|
|
|
|
|
|
|
threading_util::spin_lock m_pipelock;
|
|
|
|
};
|
|
|
|
|
2016-10-15 07:15:56 -04:00
|
|
|
using command_t = unique_ptr<command>;
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
template <typename... Args>
|
2016-10-15 07:15:56 -04:00
|
|
|
command_t make_command(Args&&... args) {
|
2016-06-14 23:32:35 -04:00
|
|
|
return make_unique<command>(
|
|
|
|
logger::configure().create<const logger&>(), forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|