- cleanup of Aria2C and implementation of some cases like simple auth and proxy.
[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 <map>
17 #include <string>
18 #include <vector>
19
20 #include "zypp/base/ExternalDataSource.h"
21 #include "zypp/Pathname.h"
22
23 namespace zypp {
24
25     /**
26      * @short Execute a program and give access to its io
27      * An object of this class encapsulates the execution of
28      * an external program. It starts the program using fork
29      * and some exec.. call, gives you access to the program's
30      * stdio and closes the program after use.
31      *
32      * \code
33      *
34      * const char* argv[] =
35      * {
36      *     "/usr/bin/foo,
37      *     "--option1",
38      *     "--option2",
39      *     NULL
40      * };
41      *
42      * ExternalProgram prog( argv,
43      *                        ExternalProgram::Discard_Stderr,
44      *                        false, -1, true);
45      * string line;
46      * for(line = prog.receiveLine();
47      *     ! line.empty();
48      *     line = prog.receiveLine() )
49      * {
50      *     stream << line;
51      * }
52      * prog.close();
53      *
54      * \endcode
55      */
56     class ExternalProgram : public zypp::externalprogram::ExternalDataSource
57     {
58
59     public:
60
61       typedef std::vector<std::string> Arguments;
62
63       /**
64        * Define symbols for different policies on the handling
65        * of stderr
66        */
67       enum Stderr_Disposition {
68         Normal_Stderr,
69         Discard_Stderr,
70         Stderr_To_Stdout,
71         Stderr_To_FileDesc
72       };
73
74
75       /**
76        * For passing additional environment variables to set
77        */
78       typedef std::map<std::string,std::string> Environment;
79
80       /**
81        * Start the external program by using the shell <tt>/bin/sh<tt>
82        * with the option <tt>-c</tt>. You can use io direction symbols < and >.
83        * @param commandline a shell commandline that is appended to
84        * <tt>/bin/sh -c</tt>.
85        * @param default_locale whether to set LC_ALL=C before starting
86        * @param root directory to chroot into, / or empty to not chroot
87        */
88       ExternalProgram (std::string commandline,
89                      Stderr_Disposition stderr_disp = Normal_Stderr,
90                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
91                      const Pathname& root = "");
92
93       /**
94        * Start an external program by giving the arguments as an arry of char *pointers.
95        * If environment is provided, varaiables will be added to the childs environment,
96        * overwriting existing ones.
97        * \throws ExternalProgramException if fork fails.
98        */
99
100       ExternalProgram();
101
102       ExternalProgram (const Arguments &argv,
103                      Stderr_Disposition stderr_disp = Normal_Stderr,
104                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
105                      const Pathname& root = "");
106
107       ExternalProgram (const Arguments &argv, const Environment & environment,
108                      Stderr_Disposition stderr_disp = Normal_Stderr,
109                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
110                      const Pathname& root = "");
111
112       ExternalProgram (const char *const *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 char *const *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 *binpath, const char *const *argv_1,
123                      bool use_pty = false);
124
125
126       ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
127                      bool use_pty = false);
128
129
130       ~ExternalProgram();
131
132       int close();
133
134       /**
135        * Kill the program
136        */
137       bool kill();
138
139       /**
140        * Return whether program is running
141        */
142       bool running();
143
144       /**
145        * return pid
146        * */
147       pid_t getpid() { return pid; }
148
149       /** The command we're executing. */
150       const std::string & command() const
151       { return _command; }
152
153       /** Some detail telling why the execution failed, if it failed.
154        * Empty if the command is still running or successfully completed.
155        *
156        * \li <tt>Can't open pty (%s).</tt>
157        * \li <tt>Can't open pipe (%s).</tt>
158        * \li <tt>Can't fork (%s).</tt>
159        * \li <tt>Command exited with status %d.</tt>
160        * \li <tt>Command was killed by signal %d (%s).</tt>
161       */
162       const std::string & execError() const
163       { return _execError; }
164
165       /**
166        * origfd will be accessible as newfd and closed (unless they were equal)
167        */
168       static void renumber_fd (int origfd, int newfd);
169
170     protected:
171       int checkStatus( int );
172
173     private:
174
175       /**
176        * Set to true, if a pair of ttys is used for communication
177        * instead of a pair of pipes.
178        */
179       bool use_pty;
180
181       pid_t pid;
182       int _exitStatus;
183       /** Store the command we're executing. */
184       std::string _command;
185       /** Remember execution errors like failed fork/exec. */
186       std::string _execError;
187
188       void start_program (const char *const *argv, const Environment & environment,
189                         Stderr_Disposition stderr_disp = Normal_Stderr,
190                         int stderr_fd = -1, bool default_locale = false,
191                         const char* root = NULL);
192
193     };
194
195 } // namespace zypp
196
197 #endif // ZYPP_EXTERNALPROGRAM_H