50 if ( (cmd_ptr = popen(cmd,
"r")) != NULL)
54 char buf[MX_IPC_PI_BUFSZ];
56 while (fgets(buf, MX_IPC_PI_BUFSZ, cmd_ptr) != NULL && written+1 < respsz)
59 written = strlen(resp);
72 std::vector<std::string> & commandOutput,
73 std::vector<std::string> & commandStderr,
74 const std::vector<std::string> & commandList
84 mxPError(
"mx::ipc::runCommand",errno,
"piping stdout");
88 if (pipe(errlink)==-1)
90 mxPError(
"mx::ipc::runCommand",errno,
"piping stderr");
94 if ((pid = fork()) == -1)
96 mxPError(
"mx::ipc::runCommand",errno,
"forking");
102 if(dup2 (link[1], STDOUT_FILENO) < 0)
104 mxPError(
"mx::ipc::runCommand",errno,
"dup2");
108 if(close(link[0]) < 0)
110 mxPError(
"mx::ipc::runCommand",errno,
"close");
114 if(close(link[1]) < 0)
116 mxPError(
"mx::ipc::runCommand",errno,
"close");
120 if(dup2(errlink[1], STDERR_FILENO) < 0)
122 mxPError(
"mx::ipc::runCommand",errno,
"dup2");
126 if(close(errlink[0]) < 0)
128 mxPError(
"mx::ipc::runCommand",errno,
"close");
132 if(close(errlink[1]) < 0)
134 mxPError(
"mx::ipc::runCommand",errno,
"close");
138 std::vector<const char *> charCommandList( commandList.size()+1, NULL);
140 for(
int index = 0; index < (int) commandList.size(); ++index)
142 charCommandList[index]=commandList[index].c_str();
144 execvp( charCommandList[0],
const_cast<char**
>(charCommandList.data()));
146 mxPError(
"mx::ipc::runCommand", errno,
"execvp returned");
152 char commandOutput_c[4096];
156 pid_t rvid = waitpid(pid, &status, 0);
160 mxPError(
"mx::ipc::runCommand", errno,
"waitpid");
164 if(WIFEXITED(status))
166 retVal = WEXITSTATUS(status);
170 mxError(
"mx::ipc::runCommand", MXE_PROCERR,
"child did not exit");
174 if( close(link[1]) < 0 )
176 mxPError(
"mx::ipc::runCommand", errno,
"close");
180 if( close(errlink[1]) < 0 )
182 mxPError(
"mx::ipc::runCommand", errno,
"close");
187 if ( (rd = read(link[0], commandOutput_c,
sizeof(commandOutput_c))) < 0)
189 mxPError(
"mx::ipc::runCommand", errno,
"read");
191 if(close(errlink[0])< 0) mxPError(
"mx::ipc::runCommand", errno,
"close");
192 if(close(link[0]) < 0) mxPError(
"mx::ipc::runCommand", errno,
"close");
197 if( close(link[0]) < 0)
199 mxPError(
"mx::ipc::runCommand", errno,
"close");
200 if(close(errlink[0])< 0) mxPError(
"mx::ipc::runCommand", errno,
"close");
206 commandOutput_c[rd] =
'\0';
207 std::string commandOutputString(commandOutput_c);
209 std::istringstream iss(commandOutputString);
211 while (getline(iss, line))
213 commandOutput.push_back(line);
217 if ( (rd = read(errlink[0], commandOutput_c,
sizeof(commandOutput_c))) < 0)
219 commandStderr.push_back(std::string(
"Read error on stderr: ") + strerror(errno));
220 if(close(errlink[0])< 0) mxPError(
"mx::ipc::runCommand", errno,
"close");
224 if(close(errlink[0]) < 0)
226 mxPError(
"mx::ipc::runCommand", errno,
"close");
230 commandOutput_c[rd] =
'\0';
231 commandOutputString = commandOutput_c;
233 std::istringstream iss2(commandOutputString);
235 while (getline(iss2, line))
237 commandStderr.push_back(line);
int command_response(const char *cmd, char *resp, size_t respsz)
Run a process and copy the output to a string.
int runCommand(int &retVal, std::vector< std::string > &commandOutput, std::vector< std::string > &commandStderr, const std::vector< std::string > &commandList)
Runs a command (with parameters) passed in using fork/exec.
Declares and defines the mxlib error reporting system.
Process interface facilities.