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 )
496 ret = waitpid(
pid, &status, 0);
498 while (ret == -1 && errno == EINTR);
513 if (WIFEXITED (status))
515 status = WEXITSTATUS (status);
518 DBG <<
"Pid " <<
pid <<
" exited with status " << status << endl;
525 DBG <<
"Pid " <<
pid <<
" successfully completed" << endl;
529 else if (WIFSIGNALED (status))
531 status = WTERMSIG (status);
532 WAR <<
"Pid " <<
pid <<
" was killed by signal " << status
533 <<
" (" << strsignal(status);
534 if (WCOREDUMP (status))
536 WAR <<
", core dumped";
539 _execError =
str::form(
_(
"Command was killed by signal %d (%s)."), status, strsignal(status) );
543 ERR <<
"Pid " <<
pid <<
" exited with unknown error" << endl;
544 _execError =
_(
"Command exited with unknown error.");
565 if (
pid < 0 )
return false;
568 int p = waitpid(
pid, &status, WNOHANG );
572 ERR <<
"waitpid( " <<
pid <<
") returned error '" <<
strerror(errno) <<
"'" << endl;
594 dup2 (origfd, newfd);
613 namespace externalprogram
619 ::pipe2(
_fds, O_NONBLOCK );
622 ::fcntl(
_fds[
R], F_SETFD, O_NONBLOCK );
623 ::fcntl(
_fds[
W], F_SETFD, O_NONBLOCK );
640 if ( delim_r && !
_buffer.empty() )
644 if ( pos != std::string::npos )
646 retval_r =
_buffer.substr( 0, returnDelim_r ? pos+1 : pos );
656 if ( ch != delim_r || ! delim_r )
671 else if ( errno != EINTR )