48 std::vector<std::string> params;
50 params.push_back(command);
70 const int status = ::execv(argv[0],argv);
71 throw OksSystem::ExecutionIssue(
ERS_HERE, errno, argv[0], status );
85 const int status = ::execve(argv[0],argv,env);
86 throw OksSystem::ExecutionIssue(
ERS_HERE, errno, argv[0], status );
95 const int argc = params.size();
96 const int argclen = argc+2;
97 char **argv = (
char **) calloc(
sizeof(
char*),argclen);
99 const char* name = *
this;
100 argv[0] = strdup(name);
102 for(
int i=0;i<argc;i++) {
103 argv[i+1] = strdup(params[i].c_str());
109 catch(OksSystem::ExecutionIssue &ex) {
110 for(
int i=0;argv[i]!=0;i++) {
117 for(
int i=0;argv[i]!=0;i++) {
133 const unsigned int envArraySize = envs.size() + 1;
134 char**
const env =
new char*[envArraySize];
137 const env_collection::const_iterator b = envs.begin();
138 const env_collection::const_iterator e = envs.end();
139 env_collection::const_iterator it;
141 unsigned int counter = 0;
142 for(it = b; it != e; ++it) {
143 const std::string entry = it->first +
"=" + it->second;
144 env[counter] =
new char[entry.size() + 1];
145 ::strcpy(env[counter], entry.c_str());
149 env[envArraySize - 1] = (
char*) 0;
153 const unsigned int paramArraySize = params.size() + 2;
154 char**
const par =
new char*[paramArraySize];
157 const std::string& binName = this->
full_name();
158 par[0] =
new char[binName.size() + 1];
159 ::strcpy(par[0], binName.c_str());
161 const param_collection::const_iterator b = params.begin();
162 const param_collection::const_iterator e = params.end();
163 param_collection::const_iterator it;
165 unsigned int counter = 1;
166 for(it = b; it != e; ++it) {
167 const std::string& value = *it;
168 par[counter] =
new char[value.size() + 1];
169 ::strcpy(par[counter], value.c_str());
173 par[paramArraySize - 1] = (
char*) 0;
179 catch(OksSystem::ExecutionIssue& ex) {
182 for(
unsigned int i = 0; i < envArraySize; ++i) {
187 for(
unsigned int i = 0; i < paramArraySize; ++i) {
209 sigfillset(&new_set);
210 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
212 const pid_t child_id = fork();
216 signal(SIGTERM, SIG_DFL);
217 signal(SIGINT, SIG_DFL);
220 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
225 catch(OksSystem::ExecutionIssue &ex) {
238 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
245 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
247 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
261 sigfillset(&new_set);
262 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
264 signal(SIGCLD, SIG_IGN);
266 const pid_t child_id = fork();
270 signal(SIGTERM, SIG_DFL);
271 signal(SIGINT, SIG_DFL);
272 signal(SIGCLD, SIG_DFL);
275 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
280 catch(OksSystem::ExecutionIssue &ex) {
293 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
299 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
301 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
314 long status = read(fd,buffer,
sizeof(buffer));
315 if (status <= 0)
return;
317 for(
int i=0;i<status;i++) {
318 target << (char) buffer[i];
330 const int input_pipe_status = pipe(input_pipe);
331 if (input_pipe_status<0)
throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"" );
332 const int data_pipe_status = pipe(data_pipe);
333 if (data_pipe_status<0)
throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"" );
334 const int error_pipe_status = pipe(error_pipe);
335 if (error_pipe_status<0)
throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"" );
340 sigfillset(&new_set);
341 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
343 const pid_t child_id = fork();
347 signal(SIGTERM, SIG_DFL);
348 signal(SIGINT, SIG_DFL);
351 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
353 dup2(input_pipe[1], fileno(stdin));
354 dup2(data_pipe[1], fileno(stdout));
355 dup2(error_pipe[1], fileno(stderr));
360 catch(OksSystem::ExecutionIssue &ex) {
373 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
376 close(input_pipe[1]);
378 close(error_pipe[1]);
379 const int child_status = child_process.
join();
380 const int in_fd = input_pipe[0];
381 const int err_fd = error_pipe[0];
382 const int out_fd = data_pipe[0];
383 std::ostringstream in_stream;
384 std::ostringstream out_stream;
385 std::ostringstream err_stream;
392 if (0==child_status) {
393 return out_stream.str();
396 std::string error_str = err_stream.str();
397 throw OksSystem::ExecutionIssue(
ERS_HERE,errno,command.c_str(),child_status);
401 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
403 return std::string();
430 sigfillset(&new_set);
431 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
437 const pid_t child_id = fork();
441 signal(SIGTERM, SIG_DFL);
442 signal(SIGINT, SIG_DFL);
445 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
448 ::dup2(in,::fileno(stdin));
449 ::dup2(out,::fileno(stdout));
450 ::dup2(err,::fileno(stderr));
456 catch(OksSystem::ExecutionIssue &ex) {
460 catch(OksSystem::PosixIssue &ex) {
473 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
479 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
481 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
498 sigfillset(&new_set);
499 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
505 const pid_t child_id = fork();
509 signal(SIGTERM, SIG_DFL);
510 signal(SIGINT, SIG_DFL);
513 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
516 ::dup2(in,::fileno(stdin));
517 ::dup2(out,::fileno(stdout));
518 ::dup2(err,::fileno(stderr));
522 this->
exec(params,envs);
524 catch(OksSystem::ExecutionIssue &ex) {
528 catch(OksSystem::PosixIssue &ex) {
541 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
547 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
549 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
563 sigfillset(&new_set);
564 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
566 const pid_t child_id = fork();
570 signal(SIGTERM, SIG_DFL);
571 signal(SIGINT, SIG_DFL);
574 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
579 catch(OksSystem::ExecutionIssue &ex) {
592 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
598 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
600 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"" );
610 std::ostringstream stream;
612 for(param_collection::const_iterator pos=params.begin();pos!=params.end();++pos) {
613 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)