12 #define _GNU_SOURCE 1 // for ::getline
34 #undef ZYPP_BASE_LOGGER_LOGGROUP
35 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::exec"
39 ExternalProgram::ExternalProgram()
50 const Pathname & root )
57 argv[2] = commandline.c_str();
69 const Pathname & root )
73 const char * argvp[argv.size() + 1];
75 for_( i, argv.begin(), argv.end() )
77 argvp[c] = i->c_str();
92 const Pathname & root )
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() );
115 const Pathname & root )
129 const Pathname & root )
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;
447 int inputfileFd = ::fileno( inputfile );
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 )
475 getline( &linebuffer, &linebuffer_size, inputfile );
478 if ( ::feof( inputfile ) )
480 clearerr( inputfile );
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 )