54 std::vector<std::string> params;
56 params.push_back(command);
86 const int status = ::execv(argv[0], argv);
87 throw OksSystem::ExecutionIssue(
ERS_HERE, errno, argv[0], status);
103 const int status = ::execve(argv[0], argv, env);
104 throw OksSystem::ExecutionIssue(
ERS_HERE, errno, argv[0], status);
115 const int argc = params.size();
116 const int argclen = argc + 2;
117 char** argv = (
char**)calloc(
sizeof(
char*), argclen);
119 const char* name = *
this;
120 argv[0] = strdup(name);
122 for (
int i = 0; i < argc; i++) {
123 argv[i + 1] = strdup(params[i].c_str());
128 }
catch (OksSystem::ExecutionIssue& ex) {
129 for (
int i = 0; argv[i] != 0; i++) {
135 for (
int i = 0; argv[i] != 0; i++) {
153 const unsigned int envArraySize = envs.size() + 1;
154 char**
const env =
new char*[envArraySize];
157 const env_collection::const_iterator b = envs.begin();
158 const env_collection::const_iterator e = envs.end();
159 env_collection::const_iterator it;
161 unsigned int counter = 0;
162 for (it = b; it != e; ++it) {
163 const std::string entry = it->first +
"=" + it->second;
164 env[counter] =
new char[entry.size() + 1];
165 ::strcpy(env[counter], entry.c_str());
169 env[envArraySize - 1] = (
char*)0;
173 const unsigned int paramArraySize = params.size() + 2;
174 char**
const par =
new char*[paramArraySize];
177 const std::string& binName = this->
full_name();
178 par[0] =
new char[binName.size() + 1];
179 ::strcpy(par[0], binName.c_str());
181 const param_collection::const_iterator b = params.begin();
182 const param_collection::const_iterator e = params.end();
183 param_collection::const_iterator it;
185 unsigned int counter = 1;
186 for (it = b; it != e; ++it) {
187 const std::string& value = *it;
188 par[counter] =
new char[value.size() + 1];
189 ::strcpy(par[counter], value.c_str());
193 par[paramArraySize - 1] = (
char*)0;
198 }
catch (OksSystem::ExecutionIssue& ex) {
201 for (
unsigned int i = 0; i < envArraySize; ++i) {
206 for (
unsigned int i = 0; i < paramArraySize; ++i) {
228 sigfillset(&new_set);
229 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
231 const pid_t child_id = fork();
235 signal(SIGTERM, SIG_DFL);
236 signal(SIGINT, SIG_DFL);
239 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
243 }
catch (OksSystem::ExecutionIssue& ex) {
255 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
262 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
264 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"");
280 sigfillset(&new_set);
281 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
283 signal(SIGCLD, SIG_IGN);
285 const pid_t child_id = fork();
289 signal(SIGTERM, SIG_DFL);
290 signal(SIGINT, SIG_DFL);
291 signal(SIGCLD, SIG_DFL);
294 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
298 }
catch (OksSystem::ExecutionIssue& ex) {
310 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
316 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
318 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"");
332 long status = read(fd, buffer,
sizeof(buffer));
336 for (
int i = 0; i < status; i++) {
337 target << (char)buffer[i];
351 const int input_pipe_status = pipe(input_pipe);
352 if (input_pipe_status < 0)
353 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"");
354 const int data_pipe_status = pipe(data_pipe);
355 if (data_pipe_status < 0)
356 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"");
357 const int error_pipe_status = pipe(error_pipe);
358 if (error_pipe_status < 0)
359 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"pipe",
"");
364 sigfillset(&new_set);
365 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
367 const pid_t child_id = fork();
371 signal(SIGTERM, SIG_DFL);
372 signal(SIGINT, SIG_DFL);
375 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
377 dup2(input_pipe[1], fileno(stdin));
378 dup2(data_pipe[1], fileno(stdout));
379 dup2(error_pipe[1], fileno(stderr));
383 }
catch (OksSystem::ExecutionIssue& ex) {
395 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
398 close(input_pipe[1]);
400 close(error_pipe[1]);
401 const int child_status = child_process.
join();
402 const int in_fd = input_pipe[0];
403 const int err_fd = error_pipe[0];
404 const int out_fd = data_pipe[0];
405 std::ostringstream in_stream;
406 std::ostringstream out_stream;
407 std::ostringstream err_stream;
414 if (0 == child_status) {
415 return out_stream.str();
418 std::string error_str = err_stream.str();
419 throw OksSystem::ExecutionIssue(
ERS_HERE, errno, command.c_str(), child_status);
423 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
425 return std::string();
451 const File& input_file,
452 const File& output_file,
453 const File& error_file,
460 sigfillset(&new_set);
461 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
468 const pid_t child_id = fork();
472 signal(SIGTERM, SIG_DFL);
473 signal(SIGINT, SIG_DFL);
476 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
479 ::dup2(in, ::fileno(stdin));
480 ::dup2(out, ::fileno(stdout));
481 ::dup2(err, ::fileno(stderr));
486 }
catch (OksSystem::ExecutionIssue& ex) {
489 }
catch (OksSystem::PosixIssue& ex) {
501 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
507 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
509 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"");
524 const File& input_file,
525 const File& output_file,
526 const File& error_file,
533 sigfillset(&new_set);
534 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
541 const pid_t child_id = fork();
545 signal(SIGTERM, SIG_DFL);
546 signal(SIGINT, SIG_DFL);
549 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
552 ::dup2(in, ::fileno(stdin));
553 ::dup2(out, ::fileno(stdout));
554 ::dup2(err, ::fileno(stderr));
558 this->
exec(params, envs);
559 }
catch (OksSystem::ExecutionIssue& ex) {
562 }
catch (OksSystem::PosixIssue& ex) {
574 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
580 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
582 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"");
598 sigfillset(&new_set);
599 pthread_sigmask(SIG_SETMASK, &new_set, &old_set);
601 const pid_t child_id = fork();
605 signal(SIGTERM, SIG_DFL);
606 signal(SIGINT, SIG_DFL);
609 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
613 }
catch (OksSystem::ExecutionIssue& ex) {
625 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
631 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
633 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"fork",
"");
645 std::ostringstream stream;
647 for (param_collection::const_iterator pos = params.begin(); pos != params.end(); ++pos) {
648 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.
Process()
Builds a process representing the current process.
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)