12 #define _GNU_SOURCE 1 // for ::getline
35 ExternalProgram::ExternalProgram()
47 const Pathname & root )
54 argv[2] = commandline.c_str();
57 const char* rootdir = NULL;
58 if(!root.empty() && root !=
"/")
60 rootdir = root.asString().c_str();
63 start_program (argv, environment, stderr_disp, stderr_fd, default_locale, rootdir);
69 bool use_pty,
int stderr_fd,
75 const char * argvp[argv.size() + 1];
77 for_( i, argv.begin(), argv.end() )
79 argvp[c] = i->c_str();
85 const char* rootdir = NULL;
86 if(!root.empty() && root !=
"/")
88 rootdir = root.asString().c_str();
90 start_program (argvp, environment, stderr_disp, stderr_fd, default_locale, rootdir);
97 bool use_pty,
int stderr_fd,
103 const char * argvp[argv.size() + 1];
105 for_( i, argv.begin(), argv.end() )
107 argvp[c] = i->c_str();
112 const char* rootdir = NULL;
113 if(!root.empty() && root !=
"/")
115 rootdir = root.asString().c_str();
117 start_program (argvp, environment, stderr_disp, stderr_fd, default_locale, rootdir);
129 const Pathname & root )
133 const char* rootdir = NULL;
134 if(!root.empty() && root !=
"/")
136 rootdir = root.asString().c_str();
139 start_program (argv, environment, stderr_disp, stderr_fd, default_locale, rootdir);
145 int stderr_fd,
bool default_locale,
146 const Pathname& root)
150 const char* rootdir = NULL;
151 if(!root.empty() && root !=
"/")
153 rootdir = root.asString().c_str();
155 start_program (argv, environment, stderr_disp, stderr_fd, default_locale, rootdir);
167 const char *argv[i + 1];
169 memcpy (&argv[1], argv_1, (i - 1) *
sizeof (
char *));
183 const char *argv[i + 1];
185 memcpy (&argv[1], argv_1, (i - 1) *
sizeof (
char *));
198 int stderr_fd,
bool default_locale,
const char* root)
202 int to_external[2], from_external[2];
203 int master_tty, slave_tty;
206 const char * redirectStdin =
nullptr;
207 const char * chdirTo =
nullptr;
209 for (
bool strip =
false; argv[0]; ++argv )
212 switch ( argv[0][0] )
216 redirectStdin = argv[0]+1;
217 if ( *redirectStdin ==
'\0' )
218 redirectStdin =
"/dev/null";
223 if ( argv[0][1] ==
'/' )
235 for (
int i = 0; argv[i]; i++)
237 if (i>0) cmdstr <<
' ';
243 cmdstr <<
" < '" << redirectStdin <<
"'";
252 DBG <<
"Using ttys for communication with " << argv[0] << endl;
253 if (openpty (&master_tty, &slave_tty, 0, 0, 0) != 0)
264 if (pipe (to_external) != 0 || pipe (from_external) != 0)
274 if ((
pid = fork()) == 0)
292 ttyname_r(slave_tty, name,
sizeof(name));
307 int inp_fd = open( redirectStdin, O_RDONLY );
314 int null_fd = open(
"/dev/null", O_WRONLY);
329 for ( Environment::const_iterator it = environment.begin(); it != environment.end(); ++it ) {
330 setenv( it->first.c_str(), it->second.c_str(), 1 );
334 setenv(
"LC_ALL",
"C",1);
338 if(chroot(root) == -1)
348 if ( chdirTo && chdir( chdirTo ) == -1 )
357 for (
int i = ::getdtablesize() - 1; i > 2; --i ) {
361 execvp(argv[0], const_cast<char *const *>(argv));
398 inputfile = fdopen(from_external[0],
"r");
402 DBG <<
"pid " <<
pid <<
" launched" << endl;
406 ERR <<
"Cannot create streams to external program " << argv[0] << endl;
425 int inputfileFd = ::fileno( inputfile );
432 FD_SET( inputfileFd, &rfds );
436 tv.tv_sec = (delay < 0 ? 1 : 0);
437 tv.tv_usec = (delay < 0 ? 0 : delay*100000);
438 if ( delay >= 0 && ++delay > 9 )
440 int retval = select( inputfileFd+1, &rfds, NULL, NULL, &tv );
444 ERR <<
"select error: " <<
strerror(errno) << endl;
445 if ( errno != EINTR )
453 getline( &linebuffer, &linebuffer_size, inputfile );
456 if ( ::feof( inputfile ) )
458 clearerr( inputfile );
474 ret = waitpid(
pid, &status, 0);
476 while (ret == -1 && errno == EINTR);
491 if (WIFEXITED (status))
493 status = WEXITSTATUS (status);
496 DBG <<
"Pid " <<
pid <<
" exited with status " << status << endl;
503 DBG <<
"Pid " <<
pid <<
" successfully completed" << endl;
507 else if (WIFSIGNALED (status))
509 status = WTERMSIG (status);
510 WAR <<
"Pid " <<
pid <<
" was killed by signal " << status
511 <<
" (" << strsignal(status);
512 if (WCOREDUMP (status))
514 WAR <<
", core dumped";
517 _execError =
str::form(
_(
"Command was killed by signal %d (%s)."), status, strsignal(status) );
521 ERR <<
"Pid " <<
pid <<
" exited with unknown error" << endl;
522 _execError =
_(
"Command exited with unknown error.");
543 if (
pid < 0 )
return false;
546 int p = waitpid(
pid, &status, WNOHANG );
550 ERR <<
"waitpid( " <<
pid <<
") returned error '" <<
strerror(errno) <<
"'" << endl;
572 dup2 (origfd, newfd);
591 namespace _ExternalProgram
597 ::pipe2(
_fds, O_NONBLOCK );
600 ::fcntl(
_fds[
R], F_SETFD, O_NONBLOCK );
601 ::fcntl(
_fds[
W], F_SETFD, O_NONBLOCK );
618 if ( delim_r && !
_buffer.empty() )
622 if ( pos != std::string::npos )
624 retval_r =
_buffer.substr( 0, returnDelim_r ? pos+1 : pos );
634 if ( ch != delim_r || ! delim_r )
649 else if ( errno != EINTR )
ExternalProgram()
Start an external program by giving the arguments as an arry of char *pointers.
bool use_pty
Set to true, if a pair of ttys is used for communication instead of a pair of pipes.
std::ostream & operator>>(std::ostream &out_r)
Redirect all command output to an ostream.
bool kill()
Kill the program.
void start_program(const char *const *argv, const Environment &environment, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const char *root=NULL)
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
bool running()
Return whether program is running.
std::vector< std::string > Arguments
std::string getline(std::istream &str)
Read one line from stream.
#define _(MSG)
Return translated text.
std::string receiveLine()
Read one line from the input stream.
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
FILE * inputFile() const
Return the input stream.
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
std::string _execError
Remember execution errors like failed fork/exec.
int close()
Wait for the progamm to complete.
std::string form(const char *format,...)
Printf style construction of std::string.
std::string _command
Store the command we're executing.
static void renumber_fd(int origfd, int newfd)
origfd will be accessible as newfd and closed (unless they were equal)
void setBlocking(bool mode)
Set the blocking mode of the input stream.
std::string strerror(int errno_r)
Return string describing the error_r code.