12 #define _GNU_SOURCE 1 // for ::getline 34 #undef ZYPP_BASE_LOGGER_LOGGROUP 35 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::exec" 39 ExternalProgram::ExternalProgram()
57 argv[2] = commandline.c_str();
73 const char * argvp[argv.size() + 1];
75 for_( i, argv.begin(), argv.end() )
77 argvp[c] = i->c_str();
96 const char * argvp[argv.size() + 1];
98 for_( i, argv.begin(), argv.end() )
100 argvp[c] = i->c_str();
105 start_program( argvp, environment, stderr_disp, stderr_fd, default_locale, root.
c_str() );
133 start_program( argv, environment, stderr_disp, stderr_fd, default_locale, root.
c_str() );
138 const char *
const *argv_1,
146 const char *argv[i + 1];
148 memcpy( &argv[1], argv_1, (i - 1) *
sizeof (
char *) );
154 const char *
const *argv_1,
163 const char *argv[i + 1];
165 memcpy( &argv[1], argv_1, (i - 1) *
sizeof (
char *) );
186 const char * root ,
bool switch_pgid)
190 int to_external[2], from_external[2];
191 int master_tty, slave_tty;
194 const char * redirectStdin =
nullptr;
195 const char * redirectStdout =
nullptr;
196 const char * chdirTo =
nullptr;
200 if ( root[0] ==
'\0' )
204 else if ( root[0] ==
'/' && root[1] ==
'\0' )
213 for (
bool strip =
false; argv[0]; ++argv )
216 switch ( argv[0][0] )
220 redirectStdin = argv[0]+1;
221 if ( *redirectStdin ==
'\0' )
222 redirectStdin =
"/dev/null";
227 redirectStdout = argv[0]+1;
228 if ( *redirectStdout ==
'\0' )
229 redirectStdout =
"/dev/null";
234 if ( argv[0][1] ==
'/' )
246 for (
int i = 0; argv[i]; i++)
248 if (i>0) cmdstr <<
' ';
254 cmdstr <<
" < '" << redirectStdin <<
"'";
255 if ( redirectStdout )
256 cmdstr <<
" > '" << redirectStdout <<
"'";
265 DBG <<
"Using ttys for communication with " << argv[0] << endl;
266 if (openpty (&master_tty, &slave_tty, 0, 0, 0) != 0)
277 if (pipe (to_external) != 0 || pipe (from_external) != 0)
287 if ((
pid = fork()) == 0)
305 ttyname_r(slave_tty, name,
sizeof(name));
322 int inp_fd = open( redirectStdin, O_RDONLY );
326 if ( redirectStdout )
329 int inp_fd = open( redirectStdout, O_WRONLY|O_CREAT|O_APPEND, 0600 );
336 int null_fd = open(
"/dev/null", O_WRONLY);
351 for ( Environment::const_iterator it = environment.begin(); it != environment.end(); ++it ) {
352 setenv( it->first.c_str(), it->second.c_str(), 1 );
356 setenv(
"LC_ALL",
"C",1);
360 if(chroot(root) == -1)
370 if ( chdirTo && chdir( chdirTo ) == -1 )
379 for (
int i = ::getdtablesize() - 1; i > 2; --i ) {
383 execvp(argv[0], const_cast<char *const *>(argv));
420 inputfile = fdopen(from_external[0],
"r");
424 DBG <<
"pid " <<
pid <<
" launched" << endl;
428 ERR <<
"Cannot create streams to external program " << argv[0] << endl;
454 FD_SET( inputfileFd, &rfds );
458 tv.tv_sec = (delay < 0 ? 1 : 0);
459 tv.tv_usec = (delay < 0 ? 0 : delay*100000);
460 if ( delay >= 0 && ++delay > 9 )
462 int retval = select( inputfileFd+1, &rfds, NULL, NULL, &tv );
466 ERR <<
"select error: " <<
strerror(errno) << endl;
467 if ( errno != EINTR )
498 ret = waitpid(
pid, &status, 0);
500 while (ret == -1 && errno == EINTR);
516 if (WIFEXITED (status))
518 status = WEXITSTATUS (status);
521 DBG <<
"Pid " <<
pid <<
" exited with status " << status << endl;
528 DBG <<
"Pid " <<
pid <<
" successfully completed" << endl;
532 else if (WIFSIGNALED (status))
534 status = WTERMSIG (status);
535 WAR <<
"Pid " <<
pid <<
" was killed by signal " << status
536 <<
" (" << strsignal(status);
537 if (WCOREDUMP (status))
539 WAR <<
", core dumped";
542 _execError =
str::form(
_(
"Command was killed by signal %d (%s)."), status, strsignal(status) );
546 ERR <<
"Pid " <<
pid <<
" exited with unknown error" << endl;
547 _execError =
_(
"Command exited with unknown error.");
568 if (
pid < 0 )
return false;
571 int p = waitpid(
pid, &status, WNOHANG );
575 ERR <<
"waitpid( " <<
pid <<
") returned error '" <<
strerror(errno) <<
"'" << endl;
597 dup2 (origfd, newfd);
616 namespace externalprogram
622 ::pipe2(
_fds, O_NONBLOCK );
625 ::fcntl(
_fds[
R], F_SETFD, O_NONBLOCK );
626 ::fcntl(
_fds[
W], F_SETFD, O_NONBLOCK );
643 if ( delim_r && !
_buffer.empty() )
647 if ( pos != std::string::npos )
649 retval_r =
_buffer.substr( 0, returnDelim_r ? pos+1 : pos );
659 if ( ch != delim_r || ! delim_r )
674 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.
const char * c_str() const
String representation.
static void watchPID(pid_t pid_r)
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
bool running()
Return whether program is running.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
std::vector< std::string > Arguments
std::string getline(std::istream &str)
Read one line from stream.
std::string receiveLine()
Read one line from the input stream.
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
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.
This file contains private API, it will change without notice.
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.
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, bool switch_pgid=false)
FILE * inputFile() const
Return the input stream.
std::string strerror(int errno_r)
Return string describing the error_r code.
Easy-to use interface to the ZYPP dependency resolver.