include unistd.h
[platform/upstream/libzypp.git] / zypp / ExternalProgram.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ExternalProgram.h
10 */
11
12
13 #ifndef ZYPP_EXTERNALPROGRAM_H
14 #define ZYPP_EXTERNALPROGRAM_H
15
16 #include <unistd.h>
17
18 #include <map>
19 #include <string>
20 #include <vector>
21
22 #include "zypp/base/ExternalDataSource.h"
23 #include "zypp/Pathname.h"
24
25 namespace zypp {
26
27     /**
28      * @short Execute a program and give access to its io
29      * An object of this class encapsulates the execution of
30      * an external program. It starts the program using fork
31      * and some exec.. call, gives you access to the program's
32      * stdio and closes the program after use.
33      *
34      * \code
35      *
36      * const char* argv[] =
37      * {
38      *     "/usr/bin/foo,
39      *     "--option1",
40      *     "--option2",
41      *     NULL
42      * };
43      *
44      * ExternalProgram prog( argv,
45      *                        ExternalProgram::Discard_Stderr,
46      *                        false, -1, true);
47      * string line;
48      * for(line = prog.receiveLine();
49      *     ! line.empty();
50      *     line = prog.receiveLine() )
51      * {
52      *     stream << line;
53      * }
54      * prog.close();
55      *
56      * \endcode
57      */
58     class ExternalProgram : public zypp::externalprogram::ExternalDataSource
59     {
60
61     public:
62
63       typedef std::vector<std::string> Arguments;
64
65       /**
66        * Define symbols for different policies on the handling
67        * of stderr
68        */
69       enum Stderr_Disposition {
70         Normal_Stderr,
71         Discard_Stderr,
72         Stderr_To_Stdout,
73         Stderr_To_FileDesc
74       };
75
76
77       /**
78        * For passing additional environment variables to set
79        */
80       typedef std::map<std::string,std::string> Environment;
81
82       /**
83        * Start the external program by using the shell <tt>/bin/sh<tt>
84        * with the option <tt>-c</tt>. You can use io direction symbols < and >.
85        * @param commandline a shell commandline that is appended to
86        * <tt>/bin/sh -c</tt>.
87        * @param default_locale whether to set LC_ALL=C before starting
88        * @param root directory to chroot into, / or empty to not chroot
89        */
90       ExternalProgram (std::string commandline,
91                      Stderr_Disposition stderr_disp = Normal_Stderr,
92                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
93                      const Pathname& root = "");
94
95       /**
96        * Start an external program by giving the arguments as an arry of char *pointers.
97        * If environment is provided, varaiables will be added to the childs environment,
98        * overwriting existing ones.
99        *
100        * Stdin redirection: If the \b 1st argument starts with a \b '<', the remaining
101        * part is treated as file opened for reading on standard input (or \c /dev/null
102        * if empty).
103        * \code
104        *   // cat file /tmp/x
105        *   const char* argv[] = { "</tmp/x", "cat", NULL };
106        *   ExternalProgram prog( argv );
107        * \endcode
108        */
109
110       ExternalProgram();
111
112       ExternalProgram (const Arguments &argv,
113                      Stderr_Disposition stderr_disp = Normal_Stderr,
114                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
115                      const Pathname& root = "");
116
117       ExternalProgram (const Arguments &argv, const Environment & environment,
118                      Stderr_Disposition stderr_disp = Normal_Stderr,
119                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
120                      const Pathname& root = "");
121
122       ExternalProgram (const char *const *argv,
123                      Stderr_Disposition stderr_disp = Normal_Stderr,
124                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
125                      const Pathname& root = "");
126
127       ExternalProgram (const char *const *argv, const Environment & environment,
128                      Stderr_Disposition stderr_disp = Normal_Stderr,
129                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
130                      const Pathname& root = "");
131
132       ExternalProgram (const char *binpath, const char *const *argv_1,
133                      bool use_pty = false);
134
135
136       ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
137                      bool use_pty = false);
138
139
140       ~ExternalProgram();
141
142       /** Wait for the progamm to complete. */
143       int close();
144
145       /**
146        * Kill the program
147        */
148       bool kill();
149
150       /**
151        * Return whether program is running
152        */
153       bool running();
154
155       /**
156        * return pid
157        * */
158       pid_t getpid() { return pid; }
159
160       /** The command we're executing. */
161       const std::string & command() const
162       { return _command; }
163
164       /** Some detail telling why the execution failed, if it failed.
165        * Empty if the command is still running or successfully completed.
166        *
167        * \li <tt>Can't open pty (%s).</tt>
168        * \li <tt>Can't open pipe (%s).</tt>
169        * \li <tt>Can't fork (%s).</tt>
170        * \li <tt>Command exited with status %d.</tt>
171        * \li <tt>Command was killed by signal %d (%s).</tt>
172       */
173       const std::string & execError() const
174       { return _execError; }
175
176       /**
177        * origfd will be accessible as newfd and closed (unless they were equal)
178        */
179       static void renumber_fd (int origfd, int newfd);
180
181     public:
182
183       /**
184        * Redirect all command output to an \c ostream.
185        * Returns when the command has completed.
186        * \code
187        *   std::ostringstream s;
188        *   ExternalProgram("pwd") >> s;
189        *   SEC << s.str() << endl;
190        * \endcode
191        * \code
192        *   std::ostringstream s;
193        *   ExternalProgram prog("ls -l wrzl");
194        *   prog >> s;
195        *   if ( prog.close() == 0 )
196        *     MIL << s.str() << endl;
197        *   else
198        *     ERR << prog.execError() << endl;
199        * \endcode
200        */
201       std::ostream & operator>>( std::ostream & out_r );
202
203     protected:
204       int checkStatus( int );
205
206     private:
207
208       /**
209        * Set to true, if a pair of ttys is used for communication
210        * instead of a pair of pipes.
211        */
212       bool use_pty;
213
214       pid_t pid;
215       int _exitStatus;
216       /** Store the command we're executing. */
217       std::string _command;
218       /** Remember execution errors like failed fork/exec. */
219       std::string _execError;
220
221       void start_program (const char *const *argv, const Environment & environment,
222                         Stderr_Disposition stderr_disp = Normal_Stderr,
223                         int stderr_fd = -1, bool default_locale = false,
224                         const char* root = NULL);
225
226     };
227
228
229   namespace _ExternalProgram
230   {
231     /** Helper providing pipe FDs for \ref ExternalProgramWithStderr.
232      * Moved to a basse class because the pipe needs to be initialized
233      * before the \ref ExternalProgram base class is initialized.
234      * \see \ref ExternalProgramWithStderr
235      */
236     struct EarlyPipe
237     {
238       enum { R=0, W=1 };
239       EarlyPipe();
240       ~EarlyPipe();
241       void closeW()             { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
242       FILE * stderr()           { return _stderr; }
243       protected:
244         FILE * _stderr;
245         int _fds[2];
246     };
247   }
248
249   /** ExternalProgram extended to offer reading programs stderr.
250    * \see \ref ExternalProgram
251    */
252   class ExternalProgramWithStderr : private _ExternalProgram::EarlyPipe, public ExternalProgram
253   {
254     public:
255       ExternalProgramWithStderr( const Arguments & argv_r )
256         : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
257       { _initStdErr(); }
258
259       ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r )
260         : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
261       { _initStdErr(); }
262
263     public:
264       /** Return \c FILE* to read programms stderr (O_NONBLOCK set). */
265       using _ExternalProgram::EarlyPipe::stderr;
266
267       /** Read data up to \c delim_r from stderr (nonblocking).
268        * \note If \c delim_r is '\0', we read as much data as possible.
269        * \return \c false if data are not yet available (\c retval_r remains untouched then).
270        */
271       bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
272
273       /** Read next complete line from stderr (nonblocking).
274        * \return \c false if data are not yet available (\c retval_r remains untouched then).
275        */
276       bool stderrGetline( std::string & retval_r, bool returnDelim_r = false  )
277       { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
278
279     private:
280       /** Close write end of the pipe (childs end). */
281       void _initStdErr()
282       { closeW(); }
283
284     private:
285       std::string _buffer;
286   };
287
288 } // namespace zypp
289
290 #endif // ZYPP_EXTERNALPROGRAM_H