ignore
[platform/upstream/libzypp.git] / zypp / ExternalProgram.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ExternalProgram.h
10  *
11  * \todo replace by Blocxx
12  *
13 */
14
15
16 #ifndef ZYPP_EXTERNALPROGRAM_H
17 #define ZYPP_EXTERNALPROGRAM_H
18
19 #include <map>
20 #include <string>
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     class ExternalProgram : public zypp::externalprogram::ExternalDataSource
35     {
36     
37     public:
38       /**
39        * Define symbols for different policies on the handling
40        * of stderr
41        */
42       enum Stderr_Disposition {
43         Normal_Stderr,
44         Discard_Stderr,
45         Stderr_To_Stdout,
46         Stderr_To_FileDesc
47       };
48     
49       /**
50        * For passing additional environment variables to set
51        */
52       typedef std::map<std::string,std::string> Environment;
53     
54       /**
55        * Start the external program by using the shell <tt>/bin/sh<tt>
56        * with the option <tt>-c</tt>. You can use io direction symbols < and >.
57        * @param commandline a shell commandline that is appended to
58        * <tt>/bin/sh -c</tt>.
59        * @param default_locale whether to set LC_ALL=C before starting
60        * @param root directory to chroot into, / or empty to not chroot
61        */
62       ExternalProgram (std::string commandline,
63                      Stderr_Disposition stderr_disp = Normal_Stderr,
64                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
65                      const Pathname& root = "");
66     
67       /**
68        * Start an external program by giving the arguments as an arry of char *pointers.
69        * If environment is provided, varaiables will be added to the childs environment,
70        * overwriting existing ones.
71        */
72       
73       ExternalProgram();
74       
75       ExternalProgram (const char *const *argv,
76                      Stderr_Disposition stderr_disp = Normal_Stderr,
77                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
78                      const Pathname& root = "");
79     
80       ExternalProgram (const char *const *argv, const Environment & environment,
81                      Stderr_Disposition stderr_disp = Normal_Stderr,
82                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
83                      const Pathname& root = "");
84     
85       ExternalProgram (const char *binpath, const char *const *argv_1,
86                      bool use_pty = false);
87     
88     
89       ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
90                      bool use_pty = false);
91     
92     
93       ~ExternalProgram();
94     
95       int close();
96     
97       /**
98        * Kill the program
99        */
100       bool kill();
101     
102       /**
103        * Return whether program is running
104        */
105       bool running();
106     
107       /**
108        * return pid
109        * */
110       pid_t getpid() { return pid; }
111     
112       /**
113        * origfd will be accessible as newfd and closed (unless they were equal)
114        */
115       static void renumber_fd (int origfd, int newfd);
116     
117     protected:
118       int checkStatus( int );
119     
120     private:
121     
122       /**
123        * Set to true, if a pair of ttys is used for communication
124        * instead of a pair of pipes.
125        */
126       bool use_pty;
127     
128       pid_t pid;
129       int _exitStatus;
130     
131       void start_program (const char *const *argv, const Environment & environment,
132                         Stderr_Disposition stderr_disp = Normal_Stderr,
133                         int stderr_fd = -1, bool default_locale = false,
134                         const char* root = NULL);
135     
136     };
137     
138 } // namespace zypp
139
140 #endif // ZYPP_EXTERNALPROGRAM_H