1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ExternalProgram.h
13 #ifndef ZYPP_EXTERNALPROGRAM_H
14 #define ZYPP_EXTERNALPROGRAM_H
19 #include "zypp/base/ExternalDataSource.h"
20 #include "zypp/Pathname.h"
25 * @short Execute a program and give access to its io
26 * An object of this class encapsulates the execution of
27 * an external program. It starts the program using fork
28 * and some exec.. call, gives you access to the program's
29 * stdio and closes the program after use.
31 class ExternalProgram : public zypp::externalprogram::ExternalDataSource
36 * Define symbols for different policies on the handling
39 enum Stderr_Disposition {
48 * For passing additional environment variables to set
50 typedef std::map<std::string,std::string> Environment;
53 * Start the external program by using the shell <tt>/bin/sh<tt>
54 * with the option <tt>-c</tt>. You can use io direction symbols < and >.
55 * @param commandline a shell commandline that is appended to
56 * <tt>/bin/sh -c</tt>.
57 * @param default_locale whether to set LC_ALL=C before starting
58 * @param root directory to chroot into, / or empty to not chroot
60 ExternalProgram (std::string commandline,
61 Stderr_Disposition stderr_disp = Normal_Stderr,
62 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
63 const Pathname& root = "");
66 * Start an external program by giving the arguments as an arry of char *pointers.
67 * If environment is provided, varaiables will be added to the childs environment,
68 * overwriting existing ones.
69 * \throws ExternalProgramException if fork fails.
74 ExternalProgram (const char *const *argv,
75 Stderr_Disposition stderr_disp = Normal_Stderr,
76 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
77 const Pathname& root = "");
79 ExternalProgram (const char *const *argv, const Environment & environment,
80 Stderr_Disposition stderr_disp = Normal_Stderr,
81 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
82 const Pathname& root = "");
84 ExternalProgram (const char *binpath, const char *const *argv_1,
85 bool use_pty = false);
88 ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
89 bool use_pty = false);
102 * Return whether program is running
109 pid_t getpid() { return pid; }
111 /** The command we're executing. */
112 const std::string & command() const
115 /** Some detail telling why the execution failed, if it failed.
116 * Empty if the command is still running or successfully completed.
118 * \li <tt>Can't open pty (%s).</tt>
119 * \li <tt>Can't open pipe (%s).</tt>
120 * \li <tt>Can't fork (%s).</tt>
121 * \li <tt>Command exited with status %d.</tt>
122 * \li <tt>Command was killed by signal %d (%s).</tt>
124 const std::string & execError() const
125 { return _execError; }
128 * origfd will be accessible as newfd and closed (unless they were equal)
130 static void renumber_fd (int origfd, int newfd);
133 int checkStatus( int );
138 * Set to true, if a pair of ttys is used for communication
139 * instead of a pair of pipes.
145 /** Store the command we're executing. */
146 std::string _command;
147 /** Remember execution errors like failed fork/exec. */
148 std::string _execError;
150 void start_program (const char *const *argv, const Environment & environment,
151 Stderr_Disposition stderr_disp = Normal_Stderr,
152 int stderr_fd = -1, bool default_locale = false,
153 const char* root = NULL);
159 #endif // ZYPP_EXTERNALPROGRAM_H