RSC  0.17.1
UnixSubprocess.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of RSC project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "UnixSubprocess.h"
28 
29 #include <stdexcept>
30 #include <sstream>
31 
32 #include <errno.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <signal.h>
37 
38 using namespace std;
39 
40 namespace rsc {
41 namespace subprocess {
42 
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) {
46 
47  if (logger->isDebugEnabled()) {
48  stringstream argStream;
49  argStream << "[";
50  for (size_t i = 0; i < args.size(); ++i) {
51  argStream << args[i];
52  if (i != args.size() - 1) {
53  argStream << ", ";
54  } else {
55  argStream << "]";
56  }
57  }
58  RSCDEBUG(logger, "Creating a subprocess for command '" << command
59  << "' with arguments " << argStream.str());
60  }
61 
62  // convert arguments to char pointer array
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());
69  }
70  this->args[args.size() + 1] = 0;
71 
72  pid_t pid = vfork();
73  if (pid == 0) {
74 
75  // child process
76  int ret = execv(command.c_str(), this->args);
77  if (ret == -1) {
78  RSCERROR(logger, "Error starting subprocess command '" << command
79  << "': " << strerror(errno));
80  }
81 
82  } else if (pid < 0) {
83 
84  // failed to fork
85  throw runtime_error("Forking new subprocess failed.");
86 
87  } else {
88 
89  // code executed by parent process
90  this->pid = pid;
91 
92  }
93 
94 }
95 
97 
98  RSCDEBUG(logger, "Killing subprocess with command '" << command << "'");
99  int killed = kill(pid, SIGINT);
100  if (killed == 0) {
101  // successful signal sent
102 
103  int status;
104  pid_t childStatus;
105  RSCDEBUG(logger, "Waiting for command to finish: '" << command << "'");
106  do {
107  childStatus = waitpid(pid, &status, 0);
108  if (childStatus == -1) {
109  RSCERROR(logger, "Error while waiting for child to finish: "
110  << strerror(errno));
111  break;
112  }
113 
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));
118  }
119  } while (!WIFEXITED(status) && !WIFSIGNALED(status));
120  RSCDEBUG(logger, "Command finished: '" << command << "'");
121 
122  } else {
123 
124  RSCERROR(logger, "Problem killing the child command '" << command
125  << "': " << strerror(errno));
126 
127  }
128 
129  // clean up argument structure
130  for (size_t i = 0; i < argLen - 1; ++i) {
131  delete[] args[i];
132  }
133  delete[] args;
134 
135 }
136 
137 }
138 }
#define RSCDEBUG(logger, msg)
Definition: Logger.h:217
STL namespace.
LoggerPtr getLogger()
#define RSCERROR(logger, msg)
Definition: Logger.h:238