34 #include <sys/types.h>
41 namespace subprocess {
43 UnixSubprocess::UnixSubprocess(
const string& command,
const vector<string>& args) :
44 logger(logging::Logger::getLogger(
"rsc.subprocess.UnixSubprocess")),
45 command(command), argLen(args.size() + 2) {
47 if (
logger->isDebugEnabled()) {
48 stringstream argStream;
50 for (
size_t i = 0; i < args.size(); ++i) {
52 if (i != args.size() - 1) {
59 <<
"' with arguments " << argStream.str());
63 this->args =
new char* [
argLen];
64 this->args[0] =
new char[strlen(command.c_str()) + 1];
65 strcpy(this->args[0], command.c_str());
66 for (
size_t i = 0; i < args.size(); ++i) {
67 this->args[i + 1] =
new char[strlen(args[i].c_str()) + 1];
68 strcpy(this->args[i + 1], args[i].c_str());
70 this->args[args.size() + 1] = 0;
76 int ret = execv(command.c_str(), this->
args);
79 <<
"': " << strerror(errno));
85 throw runtime_error(
"Forking new subprocess failed.");
99 int killed = kill(
pid, SIGINT);
107 childStatus = waitpid(
pid, &status, 0);
108 if (childStatus == -1) {
114 if (WIFEXITED(status)) {
115 RSCDEBUG(
logger,
"Child exited with status " << WEXITSTATUS(status));
116 }
else if (WIFSIGNALED(status)) {
117 RSCDEBUG(
logger,
"Child was killed by signal " << WTERMSIG(status));
119 }
while (!WIFEXITED(status) && !WIFSIGNALED(status));
125 <<
"': " << strerror(errno));
130 for (
size_t i = 0; i <
argLen - 1; ++i) {
logging::LoggerPtr logger
virtual ~UnixSubprocess()
#define RSCDEBUG(logger, msg)
#define RSCERROR(logger, msg)