53 std::vector<std::string> params;
55 params.push_back(command);
75 const int status = ::execv(argv[0],argv);
76 throw OksSystem::ExecutionIssue(
ERS_HERE, errno, argv[0], status );
90 const int status = ::execve(argv[0],argv,env);
91 throw OksSystem::ExecutionIssue(
ERS_HERE, errno, argv[0], status );
100 const int argc = params.size();
101 const int argclen = argc+2;
102 char **argv = (
char **) calloc(
sizeof(
char*),argclen);
104 const char* name = *
this;
105 argv[0] = strdup(name);
107 for(
int i=0;i<argc;i++) {
108 argv[i+1] = strdup(params[i].c_str());
114 catch(OksSystem::ExecutionIssue &ex) {
115 for(
int i=0;argv[i]!=0;i++) {
122 for(
int i=0;argv[i]!=0;i++) {
138 const unsigned int envArraySize = envs.size() + 1;
139 char**
const env =
new char*[envArraySize];
142 const env_collection::const_iterator b = envs.begin();
143 const env_collection::const_iterator e = envs.end();
144 env_collection::const_iterator it;
146 unsigned int counter = 0;
147 for(it = b; it != e; ++it) {
148 const std::string entry = it->first +
"=" + it->second;
149 env[counter] =
new char[entry.size() + 1];
150 ::strcpy(env[counter], entry.c_str());
154 env[envArraySize - 1] = (
char*) 0;
158 const unsigned int paramArraySize = params.size() + 2;
159 char**
const par =
new char*[paramArraySize];
162 const std::string& binName = this->
full_name();
163 par[0] =
new char[binName.size() + 1];
164 ::strcpy(par[0], binName.c_str());
166 const param_collection::const_iterator b = params.begin();
167 const param_collection::const_iterator e = params.end();
168 param_collection::const_iterator it;
170 unsigned int counter = 1;
171 for(it = b; it != e; ++it) {
172 const std::string& value = *it;
173 par[counter] =
new char[value.size() + 1];
174 ::strcpy(par[counter], value.c_str());
178 par[paramArraySize - 1] = (
char*) 0;
184 catch(OksSystem::ExecutionIssue& ex) {
187 for(
unsigned int i = 0; i < envArraySize; ++i) {
192 for(
unsigned int i = 0; i < paramArraySize; ++i) {
214 sigfillset(&new_set);
215 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
217 const pid_t child_id = fork();
221 signal(SIGTERM, SIG_DFL);
222 signal(SIGINT, SIG_DFL);
225 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
230 catch(OksSystem::ExecutionIssue &ex) {
243 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
250 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
252 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
266 sigfillset(&new_set);
267 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
269 signal(SIGCLD, SIG_IGN);
271 const pid_t child_id = fork();
275 signal(SIGTERM, SIG_DFL);
276 signal(SIGINT, SIG_DFL);
277 signal(SIGCLD, SIG_DFL);
280 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
285 catch(OksSystem::ExecutionIssue &ex) {
298 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
304 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
306 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
319 long status = read(fd,buffer,
sizeof(buffer));
320 if (status <= 0)
return;
322 for(
int i=0;i<status;i++) {
323 target << (char) buffer[i];
335 const int input_pipe_status = pipe(input_pipe);
336 if (input_pipe_status<0)
throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"" );
337 const int data_pipe_status = pipe(data_pipe);
338 if (data_pipe_status<0)
throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"" );
339 const int error_pipe_status = pipe(error_pipe);
340 if (error_pipe_status<0)
throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"" );
345 sigfillset(&new_set);
346 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
348 const pid_t child_id = fork();
352 signal(SIGTERM, SIG_DFL);
353 signal(SIGINT, SIG_DFL);
356 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
358 dup2(input_pipe[1], fileno(stdin));
359 dup2(data_pipe[1], fileno(stdout));
360 dup2(error_pipe[1], fileno(stderr));
365 catch(OksSystem::ExecutionIssue &ex) {
378 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
381 close(input_pipe[1]);
383 close(error_pipe[1]);
384 const int child_status = child_process.
join();
385 const int in_fd = input_pipe[0];
386 const int err_fd = error_pipe[0];
387 const int out_fd = data_pipe[0];
388 std::ostringstream in_stream;
389 std::ostringstream out_stream;
390 std::ostringstream err_stream;
397 if (0==child_status) {
398 return out_stream.str();
401 std::string error_str = err_stream.str();
402 throw OksSystem::ExecutionIssue(
ERS_HERE,errno,command.c_str(),child_status);
406 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
408 return std::string();
435 sigfillset(&new_set);
436 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
442 const pid_t child_id = fork();
446 signal(SIGTERM, SIG_DFL);
447 signal(SIGINT, SIG_DFL);
450 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
453 ::dup2(in,::fileno(stdin));
454 ::dup2(out,::fileno(stdout));
455 ::dup2(err,::fileno(stderr));
461 catch(OksSystem::ExecutionIssue &ex) {
465 catch(OksSystem::PosixIssue &ex) {
478 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
484 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
486 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
503 sigfillset(&new_set);
504 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
510 const pid_t child_id = fork();
514 signal(SIGTERM, SIG_DFL);
515 signal(SIGINT, SIG_DFL);
518 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
521 ::dup2(in,::fileno(stdin));
522 ::dup2(out,::fileno(stdout));
523 ::dup2(err,::fileno(stderr));
527 this->
exec(params,envs);
529 catch(OksSystem::ExecutionIssue &ex) {
533 catch(OksSystem::PosixIssue &ex) {
546 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
552 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
554 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
568 sigfillset(&new_set);
569 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
571 const pid_t child_id = fork();
575 signal(SIGTERM, SIG_DFL);
576 signal(SIGINT, SIG_DFL);
579 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
584 catch(OksSystem::ExecutionIssue &ex) {
597 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
603 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
605 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
615 std::ostringstream stream;
617 for(param_collection::const_iterator pos=params.begin();pos!=params.end();++pos) {
618 stream <<
" " << (*pos);
#define ERS_PRECONDITION(expression)
#define ERS_ASSERT(expression)
File descriptor / Socket wrapper.
int fd() const
file descritptor
static int flags(bool read_mode, bool write_mode)
Wrapper for executable file manipulation.
Process start_and_forget(const param_collection ¶ms) const
start the executable in another process; do not wait for termination of child
Executable(const OksSystem::File &file)
std::string pipe_in(const param_collection ¶ms) const
run the executable and pipe results back
Process pipe_out(const param_collection ¶ms, const File &input_file, const File &output_file, const File &error_file, mode_t perm) const
static const char *const SHELL_COMMAND
command to execute in a shell
std::vector< std::string > param_collection
std::map< std::string, std::string > env_collection
Process start(const param_collection ¶ms) const
start the executable in another process
void exec(char **argv) const
does the actual exec
static std::string okssystem(const std::string &command)
execute a command in a shell
static const char *const SHELL_COMMAND_PARAM
parameter to execute in a shell
std::string to_string(const param_collection ¶ms) const
converts executable name and a parameter sequence into a string
void exec() const
run the executable
static void copy_fd(int fd, std::ostream &target)
copies the content of a file descriptor into a STL stream
Wrapper for file operations.
File(const std::string &name)
const std::string & full_name() const
full name for file */
std::string m_full_name
full name (path) of the file */
Wrapper for process manipulation.
int join(bool throw_non_zero=false) const
Waits for the process to terminate.
Base class for any user define issue.
void warning(const Issue &issue)
static void set(const std::string &key, const std::string &value)
sets an environnement variable
#define OKSSYSTEM_ALLOC_CHECK(p, size)