1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ExternalProgram.cc
12 #define _GNU_SOURCE 1 // for ::getline
19 #include <pty.h> // openpty
20 #include <stdlib.h> // setenv
22 #include <cstring> // strsignal
26 #include "zypp/base/Logger.h"
27 #include "zypp/base/String.h"
28 #include "zypp/base/Gettext.h"
29 #include "zypp/ExternalProgram.h"
35 ExternalProgram::ExternalProgram()
41 ExternalProgram::ExternalProgram( std::string commandline,
42 Stderr_Disposition stderr_disp,
46 const Pathname & root )
52 argv[2] = commandline.c_str();
55 const char* rootdir = NULL;
56 if(!root.empty() && root != "/")
58 rootdir = root.asString().c_str();
60 Environment environment;
61 start_program (argv, environment, stderr_disp, stderr_fd, default_locale, rootdir);
65 ExternalProgram::ExternalProgram (const Arguments &argv,
66 Stderr_Disposition stderr_disp,
67 bool use_pty, int stderr_fd,
72 const char * argvp[argv.size() + 1];
74 for_( i, argv.begin(), argv.end() )
76 argvp[c] = i->c_str();
81 Environment environment;
82 const char* rootdir = NULL;
83 if(!root.empty() && root != "/")
85 rootdir = root.asString().c_str();
87 start_program (argvp, environment, stderr_disp, stderr_fd, default_locale, rootdir);
91 ExternalProgram::ExternalProgram (const Arguments &argv,
92 const Environment & environment,
93 Stderr_Disposition stderr_disp,
94 bool use_pty, int stderr_fd,
99 const char * argvp[argv.size() + 1];
101 for_( i, argv.begin(), argv.end() )
103 argvp[c] = i->c_str();
108 const char* rootdir = NULL;
109 if(!root.empty() && root != "/")
111 rootdir = root.asString().c_str();
113 start_program (argvp, environment, stderr_disp, stderr_fd, default_locale, rootdir);
120 ExternalProgram::ExternalProgram( const char *const *argv,
121 Stderr_Disposition stderr_disp,
125 const Pathname & root )
128 const char* rootdir = NULL;
129 if(!root.empty() && root != "/")
131 rootdir = root.asString().c_str();
133 Environment environment;
134 start_program (argv, environment, stderr_disp, stderr_fd, default_locale, rootdir);
138 ExternalProgram::ExternalProgram (const char *const *argv, const Environment & environment,
139 Stderr_Disposition stderr_disp, bool use_pty,
140 int stderr_fd, bool default_locale,
141 const Pathname& root)
144 const char* rootdir = NULL;
145 if(!root.empty() && root != "/")
147 rootdir = root.asString().c_str();
149 start_program (argv, environment, stderr_disp, stderr_fd, default_locale, rootdir);
153 ExternalProgram::ExternalProgram (const char *binpath, const char *const *argv_1,
160 const char *argv[i + 1];
162 memcpy (&argv[1], argv_1, (i - 1) * sizeof (char *));
163 Environment environment;
164 start_program (argv, environment);
168 ExternalProgram::ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
175 const char *argv[i + 1];
177 memcpy (&argv[1], argv_1, (i - 1) * sizeof (char *));
178 start_program (argv, environment);
182 ExternalProgram::~ExternalProgram()
188 ExternalProgram::start_program (const char *const *argv, const Environment & environment,
189 Stderr_Disposition stderr_disp,
190 int stderr_fd, bool default_locale, const char* root)
194 int to_external[2], from_external[2]; // fds for pair of pipes
195 int master_tty, slave_tty; // fds for pair of ttys
197 // do not remove the single quotes around every argument, copy&paste of
198 // command to shell will not work otherwise!
201 for (int i = 0; argv[i]; i++)
203 if (i>0) cmdstr << ' ';
208 _command = cmdstr.str();
210 DBG << "Executing " << _command << endl;
215 // Create pair of ttys
216 DBG << "Using ttys for communication with " << argv[0] << endl;
217 if (openpty (&master_tty, &slave_tty, 0, 0, 0) != 0)
219 _execError = str::form( _("Can't open pty (%s)."), strerror(errno) );
221 ERR << _execError << endl;
227 // Create pair of pipes
228 if (pipe (to_external) != 0 || pipe (from_external) != 0)
230 _execError = str::form( _("Can't open pipe (%s)."), strerror(errno) );
232 ERR << _execError << endl;
237 // Create module process
238 if ((pid = fork()) == 0)
244 dup2 (slave_tty, 1); // set new stdout
245 renumber_fd (slave_tty, 0); // set new stdin
246 ::close(master_tty); // Belongs to father process
248 // We currently have no controlling terminal (due to setsid).
249 // The first open call will also set the new ctty (due to historical
250 // unix guru knowledge ;-) )
253 ttyname_r(slave_tty, name, sizeof(name));
254 ::close(open(name, O_RDONLY));
258 renumber_fd (to_external[0], 0); // set new stdin
259 ::close(from_external[0]); // Belongs to father process
261 renumber_fd (from_external[1], 1); // set new stdout
262 ::close(to_external [1]); // Belongs to father process
266 if (stderr_disp == Discard_Stderr)
268 int null_fd = open("/dev/null", O_WRONLY);
272 else if (stderr_disp == Stderr_To_Stdout)
276 else if (stderr_disp == Stderr_To_FileDesc)
278 // Note: We don't have to close anything regarding stderr_fd.
279 // Our caller is responsible for that.
283 for ( Environment::const_iterator it = environment.begin(); it != environment.end(); ++it ) {
284 setenv( it->first.c_str(), it->second.c_str(), 1 );
288 setenv("LC_ALL","C",1);
292 if(chroot(root) == -1)
294 _execError = str::form( _("Can't chroot to '%s' (%s)."), root, strerror(errno) );
295 ERR << _execError << endl;
296 std::cerr << _execError << endl;// After fork log on stderr too
297 _exit (128); // No sense in returning! I am forked away!!
301 _execError = str::form( _("Can't chdir to '/' inside chroot (%s)."), strerror(errno) );
302 ERR << _execError << endl;
303 std::cerr << _execError << endl;// After fork log on stderr too
304 _exit (128); // No sense in returning! I am forked away!!
308 // close all filedesctiptors above stderr
309 for ( int i = ::getdtablesize() - 1; i > 2; --i ) {
313 execvp(argv[0], const_cast<char *const *>(argv));
314 // don't want to get here
315 _execError = str::form( _("Can't exec '%s' (%s)."), argv[0], strerror(errno) );
316 ERR << _execError << endl;
317 std::cerr << _execError << endl;// After fork log on stderr too
318 _exit (129); // No sense in returning! I am forked away!!
321 else if (pid == -1) // Fork failed, close everything.
323 _execError = str::form( _("Can't fork (%s)."), strerror(errno) );
325 ERR << _execError << endl;
332 ::close(to_external[0]);
333 ::close(to_external[1]);
334 ::close(from_external[0]);
335 ::close(from_external[1]);
342 ::close(slave_tty); // belongs to child process
343 inputfile = fdopen(master_tty, "r");
344 outputfile = fdopen(master_tty, "w");
348 ::close(to_external[0]); // belongs to child process
349 ::close(from_external[1]); // belongs to child process
350 inputfile = fdopen(from_external[0], "r");
351 outputfile = fdopen(to_external[1], "w");
354 DBG << "pid " << pid << " launched" << endl;
356 if (!inputfile || !outputfile)
358 ERR << "Cannot create streams to external program " << argv[0] << endl;
366 ExternalProgram::close()
371 while ( receiveLine().length() )
372 ; // discard any output instead of closing the pipe
373 //ExternalDataSource::close();
375 // Wait for child to exit
380 ret = waitpid(pid, &status, 0);
382 while (ret == -1 && errno == EINTR);
386 status = checkStatus( status );
398 int ExternalProgram::checkStatus( int status )
400 if (WIFEXITED (status))
402 status = WEXITSTATUS (status);
405 DBG << "Pid " << pid << " exited with status " << status << endl;
406 _execError = str::form( _("Command exited with status %d."), status );
410 // if 'launch' is logged, completion should be logged,
411 // even if successfull.
412 DBG << "Pid " << pid << " successfully completed" << endl;
413 //_execError = _("Command successfully completed.");
416 else if (WIFSIGNALED (status))
418 status = WTERMSIG (status);
419 WAR << "Pid " << pid << " was killed by signal " << status
420 << " (" << strsignal(status);
421 if (WCOREDUMP (status))
423 WAR << ", core dumped";
426 _execError = str::form( _("Command was killed by signal %d (%s)."), status, strsignal(status) );
430 ERR << "Pid " << pid << " exited with unknown error" << endl;
431 _execError = _("Command exited with unknown error.");
438 ExternalProgram::kill()
442 ::kill(pid, SIGKILL);
450 ExternalProgram::running()
452 if ( pid < 0 ) return false;
455 int p = waitpid( pid, &status, WNOHANG );
459 ERR << "waitpid( " << pid << ") returned error '" << strerror(errno) << "'" << endl;
463 return true; // still running
467 // Here: completed...
468 _exitStatus = checkStatus( status );
473 // origfd will be accessible as newfd and closed (unless they were equal)
474 void ExternalProgram::renumber_fd (int origfd, int newfd)
476 // It may happen that origfd is already the one we want
477 // (Although in our circumstances, that would mean somebody has closed
478 // our stdin or stdout... weird but has appened to Cray, #49797)
481 dup2 (origfd, newfd);