libzypp 17.25.7
ExternalProgram.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
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/APIConfig.h>
24#include <zypp/Pathname.h>
25
26namespace zypp {
27
60 {
61
62 public:
63
64 typedef std::vector<std::string> Arguments;
65
75 };
76
77
81 typedef std::map<std::string,std::string> Environment;
82
91 ExternalProgram (std::string commandline,
93 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
94 const Pathname& root = "");
95
119
120 ExternalProgram (const Arguments &argv,
121 Stderr_Disposition stderr_disp = Normal_Stderr,
122 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
123 const Pathname& root = "");
124
125 ExternalProgram (const Arguments &argv, const Environment & environment,
126 Stderr_Disposition stderr_disp = Normal_Stderr,
127 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
128 const Pathname& root = "");
129
130 ExternalProgram (const char *const *argv,
131 Stderr_Disposition stderr_disp = Normal_Stderr,
132 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
133 const Pathname& root = "");
134
135 ExternalProgram (const char *const *argv, const Environment & environment,
136 Stderr_Disposition stderr_disp = Normal_Stderr,
137 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
138 const Pathname& root = "");
139
140 ExternalProgram (const char *binpath, const char *const *argv_1,
141 bool use_pty = false);
142
143
144 ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
145 bool use_pty = false);
146
147
149
151 int close();
152
156 bool kill();
157
161 bool kill( int sig );
162
166 bool running();
167
171 pid_t getpid() { return pid; }
172
174 const std::string & command() const
175 { return _command; }
176
186 const std::string & execError() const
187 { return _execError; }
188
192 static void renumber_fd (int origfd, int newfd);
193
194 public:
195
214 std::ostream & operator>>( std::ostream & out_r );
215
216 protected:
217 int checkStatus( int );
218
219 private:
220
226
227 pid_t pid;
230 std::string _command;
232 std::string _execError;
233
234 protected:
235
236 void start_program (const char *const *argv, const Environment & environment,
237 Stderr_Disposition stderr_disp = Normal_Stderr,
238 int stderr_fd = -1, bool default_locale = false,
239 const char* root = NULL, bool switch_pgid = false, bool die_with_parent = false );
240
241 };
242
243
244 namespace externalprogram
245 {
252 {
253 enum { R=0, W=1 };
254 EarlyPipe();
255 ~EarlyPipe();
256 void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
257 FILE * fStdErr() { return _stderr; }
258 protected:
259 FILE * _stderr;
260 int _fds[2];
261 };
262 } // namespace externalprogram
263
268 {
269 public:
270 ExternalProgramWithStderr( const Arguments & argv_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
271 : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
272 { _initStdErr(); }
274 ExternalProgramWithStderr( const Arguments & argv_r, const Pathname & root_r )
275 : ExternalProgramWithStderr( argv_r, false, root_r )
276 {}
277
278 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
279 : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
280 { _initStdErr(); }
282 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, const Pathname & root_r )
283 : ExternalProgramWithStderr( argv_r, environment_r, false, root_r )
284 {}
285 public:
288
293 bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
294
298 bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
299 { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
300
301 private:
304 { closeW(); }
305
306 private:
307 std::string _buffer;
308 };
309
314 {
315 public:
316 ExternalProgramWithSeperatePgid (const char *const *argv,
317 Stderr_Disposition stderr_disp = Normal_Stderr,
318 int stderr_fd = -1, bool default_locale = false,
319 const Pathname& root = "") : ExternalProgram()
320 {
321 start_program( argv, Environment(), stderr_disp, stderr_fd, default_locale, root.c_str(), true );
322 }
323
324 };
325
326} // namespace zypp
327
328#endif // ZYPP_EXTERNALPROGRAM_H
ExternalProgram extended to change the progress group ID after forking.
ExternalProgramWithSeperatePgid(const char *const *argv, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
ExternalProgram extended to offer reading programs stderr.
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
ExternalProgramWithStderr(const Arguments &argv_r, const Environment &environment_r, const Pathname &root_r)
\overlocad Convenience taking just the root_r.
ExternalProgramWithStderr(const Arguments &argv_r, bool defaultLocale_r=false, const Pathname &root_r="")
void _initStdErr()
Close write end of the pipe (childs end).
bool stderrGetline(std::string &retval_r, bool returnDelim_r=false)
Read next complete line from stderr (nonblocking).
ExternalProgramWithStderr(const Arguments &argv_r, const Pathname &root_r)
\overlocad Convenience taking just the root_r.
ExternalProgramWithStderr(const Arguments &argv_r, const Environment &environment_r, bool defaultLocale_r=false, const Pathname &root_r="")
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
ExternalProgram()
Start an external program by giving the arguments as an arry of char *pointers.
const std::string & command() const
The command we're executing.
std::ostream & operator>>(std::ostream &out_r)
Redirect all command output to an ostream.
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
std::string _execError
Remember execution errors like failed fork/exec.
static void renumber_fd(int origfd, int newfd)
origfd will be accessible as newfd and closed (unless they were equal)
std::vector< std::string > Arguments
void start_program(const char *const *argv, const Environment &environment, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const char *root=NULL, bool switch_pgid=false, bool die_with_parent=false)
bool kill()
Kill the program.
std::string _command
Store the command we're executing.
pid_t getpid()
return pid
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
bool running()
Return whether program is running.
int close()
Wait for the progamm to complete.
bool use_pty
Set to true, if a pair of ttys is used for communication instead of a pair of pipes.
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
Bidirectional stream to external data.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
Helper providing pipe FDs for ExternalProgramWithStderr.