Adapted classes from liby2util, some of them to be later replaced by
[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       ExternalProgram (const char *const *argv,
73                      Stderr_Disposition stderr_disp = Normal_Stderr,
74                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
75                      const Pathname& root = "");
76     
77       ExternalProgram (const char *const *argv, const Environment & environment,
78                      Stderr_Disposition stderr_disp = Normal_Stderr,
79                      bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
80                      const Pathname& root = "");
81     
82       ExternalProgram (const char *binpath, const char *const *argv_1,
83                      bool use_pty = false);
84     
85     
86       ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
87                      bool use_pty = false);
88     
89     
90       ~ExternalProgram();
91     
92       int close();
93     
94       /**
95        * Kill the program
96        */
97       bool kill();
98     
99       /**
100        * Return whether program is running
101        */
102       bool running();
103     
104       /**
105        * return pid
106        * */
107       pid_t getpid() { return pid; }
108     
109       /**
110        * origfd will be accessible as newfd and closed (unless they were equal)
111        */
112       static void renumber_fd (int origfd, int newfd);
113     
114     protected:
115       int checkStatus( int );
116     
117     private:
118     
119       /**
120        * Set to true, if a pair of ttys is used for communication
121        * instead of a pair of pipes.
122        */
123       bool use_pty;
124     
125       pid_t pid;
126       int _exitStatus;
127     
128       void start_program (const char *const *argv, const Environment & environment,
129                         Stderr_Disposition stderr_disp = Normal_Stderr,
130                         int stderr_fd = -1, bool default_locale = false,
131                         const char* root = NULL);
132     
133     };
134     
135 } // namespace zypp
136
137 #endif // ZYPP_EXTERNALPROGRAM_H