13 #include <Poco/Thread.h>
14 #include <Poco/PipeStream.h>
25 pid = waitpid (WAIT_ANY, &status, WNOHANG | WUNTRACED);
27 while (pid <= 0 && errno == EINTR);
34 std::istream& in, std::ostream& os, std::ostream& es,
const Poco::Process::Env&
env,
CallbackFunction fn)
36 std::stringstream outMsg, errMsg;
43 if (pipe(pipeOut) != 0)
44 throw std::runtime_error(strerror(errno));
46 if (pipe(pipeErr) != 0)
47 throw std::runtime_error(strerror(errno));
53 sa.sa_flags = SA_NOCLDWAIT;
54 sigaction (SIGCHLD, &sa, NULL);
59 throw std::runtime_error(strerror(errno));
72 dup2(pipeOut[1], STDOUT_FILENO);
73 dup2(pipeErr[1], STDERR_FILENO);
82 Poco::PipeOutputStream istr(inPipe);
93 Poco::Thread::sleep(500);
104 readFromPipe(pipeOut[0], outMsg);
105 readFromPipe(pipeErr[0], errMsg);
120 void Process::writeToPipe(
int fd,
const std::string& inputData)
122 FILE* stream =
nullptr;
124 if((stream = fdopen( fd,
"w" ))==
nullptr)
127 if (!inputData.empty())
128 fwrite(inputData.c_str(),
sizeof(char), inputData.length(), stream);
134 void Process::readFromPipe(
int fd, std::stringstream& os)
136 FILE* stream =
nullptr;
138 if((stream = fdopen( fd,
"r" ))==
nullptr)
142 while((ch = getc(stream)) != std::char_traits<char>::eof())
149 std::string Process::toString(std::istream& in)
152 in.seekg(0, std::ios::end);
153 str.resize(in.tellg());
154 in.seekg(0, std::ios::beg);
155 in.read(const_cast<char*>(str.c_str()), str.size());
166 if(kill(pid, signum) != 0)
167 throw std::runtime_error(strerror(errno));
200 char procPath[PATH_MAX]={0};
202 sprintf(procPath,
"/proc/%d/status", pid);
204 FILE* proc = fopen(procPath,
"r");
207 const unsigned int SZ = 256;
211 fgets(buff, SZ, proc);
212 fgets(buff, SZ, proc);
213 sscanf(buff,
"State:\t%20s", state);
227 char procPath[PATH_MAX]={0};
230 sprintf(procPath,
"/proc/%d/status", pid);
232 FILE* proc = fopen(procPath,
"r");
235 const unsigned int SZ = 256;
237 const char name[] =
"PPid";
241 fgets(buff, SZ, proc);
242 if (strncmp(buff, name, strlen(name))==0)
244 sscanf(buff,
"PPid:\t%10d", &ppid);
261 char procPath[PATH_MAX]={0};
263 sprintf(procPath,
"/proc/%d/status", pid);
265 FILE* proc = fopen(procPath,
"r");
281 std::istream& in, std::ostream& out, std::ostream& err,
const Poco::Process::Env&
env)
283 return Process::launch(command, args, initialDirectory, in, out, err, env,
nullptr);
287 std::istream& in, std::ostream& out, std::ostream& err,
const Poco::Process::Env&
env)
290 return Process::launch(command, args, initialDirectory, in, out, err, env);
294 std::istream& in, std::ostream& out, std::ostream& err)
296 Poco::Process::Env
env;
297 return Process::launch(command, args, initialDirectory, in, out, err, env);
301 std::istream& in, std::ostream& out, std::ostream& err)
309 std::stringstream
inMsg, outMsg, errMsg;
310 return Process::launch(command, args, initialDirectory, inMsg, outMsg, errMsg);