libzypp 17.31.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-core/Globals.h>
23#include <zypp-core/base/ExternalDataSource.h>
24#include <zypp-core/Pathname.h>
25
26namespace zyppng {
27 class AbstractSpawnEngine;
28}
29
30namespace zypp {
31
63 class ExternalProgram : public zypp::externalprogram::ExternalDataSource
64 {
65
66 public:
67
68 typedef std::vector<std::string> Arguments;
69
79 };
80
84 typedef std::map<std::string,std::string> Environment;
85
94 ExternalProgram (std::string commandline,
96 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
97 const Pathname& root = "");
98
122
123 ExternalProgram (const Arguments &argv,
124 Stderr_Disposition stderr_disp = Normal_Stderr,
125 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
126 const Pathname& root = "");
127
128 ExternalProgram (const Arguments &argv, const Environment & environment,
129 Stderr_Disposition stderr_disp = Normal_Stderr,
130 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
131 const Pathname& root = "");
132
133 ExternalProgram (const char *const *argv,
134 Stderr_Disposition stderr_disp = Normal_Stderr,
135 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
136 const Pathname& root = "");
137
138 ExternalProgram (const char *const *argv, const Environment & environment,
139 Stderr_Disposition stderr_disp = Normal_Stderr,
140 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
141 const Pathname& root = "");
142
143 ExternalProgram (const char *binpath, const char *const *argv_1,
144 bool use_pty = false);
145
146
147 ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
148 bool use_pty = false);
149
150
152
154 int close();
155
159 bool kill();
160
164 bool kill( int sig );
165
169 bool running();
170
174 pid_t getpid();
175
177 const std::string & command() const;
178
188 const std::string & execError() const;
189
193 static void renumber_fd (int origfd, int newfd);
194
195 public:
196
215 std::ostream & operator>>( std::ostream & out_r );
216
217 private:
218 std::unique_ptr<zyppng::AbstractSpawnEngine> _backend;
219
220 protected:
221
222 void start_program (const char *const *argv, const Environment & environment,
223 Stderr_Disposition stderr_disp = Normal_Stderr,
224 int stderr_fd = -1, bool default_locale = false,
225 const char* root = NULL, bool switch_pgid = false, bool die_with_parent = false, bool usePty = false );
226
227 };
228
229
230 namespace externalprogram
231 {
237 struct EarlyPipe
238 {
239 enum { R=0, W=1 };
240 EarlyPipe();
241 ~EarlyPipe();
242 void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
243 FILE * fStdErr() { return _stderr; }
244 protected:
245 FILE * _stderr;
246 int _fds[2];
247 };
248 } // namespace externalprogram
249
253 class ExternalProgramWithStderr : private externalprogram::EarlyPipe, public ExternalProgram
254 {
255 public:
256 ExternalProgramWithStderr( const Arguments & argv_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
257 : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
258 { _initStdErr(); }
260 ExternalProgramWithStderr( const Arguments & argv_r, const Pathname & root_r )
261 : ExternalProgramWithStderr( argv_r, false, root_r )
262 {}
263
264 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
265 : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
266 { _initStdErr(); }
268 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, const Pathname & root_r )
269 : ExternalProgramWithStderr( argv_r, environment_r, false, root_r )
270 {}
271 public:
274
279 bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
280
284 bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
285 { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
286
287 private:
289 void _initStdErr()
290 { closeW(); }
291
292 private:
293 std::string _buffer;
294 };
295
299 class ZYPP_LOCAL ExternalProgramWithSeperatePgid : public ExternalProgram
300 {
301 public:
302 ExternalProgramWithSeperatePgid (const char *const *argv,
303 Stderr_Disposition stderr_disp = Normal_Stderr,
304 int stderr_fd = -1, bool default_locale = false,
305 const Pathname& root = "") : ExternalProgram()
306 {
307 start_program( argv, Environment(), stderr_disp, stderr_fd, default_locale, root.c_str(), true );
308 }
309
310 };
311
312} // namespace zypp
313
314#endif // ZYPP_EXTERNALPROGRAM_H
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, 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).
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.
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 usePty=false)
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
bool kill()
Kill the program.
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.
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
std::unique_ptr< zyppng::AbstractSpawnEngine > _backend
Bidirectional stream to external data.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
#define ZYPP_LOCAL
Definition: Globals.h:59