libzypp  17.8.1
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 
26 namespace 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,
92  Stderr_Disposition stderr_disp = Normal_Stderr,
93  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
94  const Pathname& root = "");
95 
118  ExternalProgram();
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 running();
162 
166  pid_t getpid() { return pid; }
167 
169  const std::string & command() const
170  { return _command; }
171 
181  const std::string & execError() const
182  { return _execError; }
183 
187  static void renumber_fd (int origfd, int newfd);
188 
189  public:
190 
209  std::ostream & operator>>( std::ostream & out_r );
210 
211  protected:
212  int checkStatus( int );
213 
214  private:
215 
220  bool use_pty;
221 
222  pid_t pid;
225  std::string _command;
227  std::string _execError;
228 
229  protected:
230 
231  void start_program (const char *const *argv, const Environment & environment,
232  Stderr_Disposition stderr_disp = Normal_Stderr,
233  int stderr_fd = -1, bool default_locale = false,
234  const char* root = NULL, bool switch_pgid = false);
235 
236  };
237 
238 
239  namespace externalprogram
240  {
246  struct EarlyPipe
247  {
248  enum { R=0, W=1 };
249  EarlyPipe();
250  ~EarlyPipe();
251  void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
252  FILE * fStdErr() { return _stderr; }
253  protected:
254  FILE * _stderr;
255  int _fds[2];
256  };
257  } // namespace externalprogram
258 
263  {
264  public:
265  ExternalProgramWithStderr( const Arguments & argv_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
266  : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
267  { _initStdErr(); }
269  ExternalProgramWithStderr( const Arguments & argv_r, const Pathname & root_r )
270  : ExternalProgramWithStderr( argv_r, false, root_r )
271  {}
272 
273  ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
274  : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
275  { _initStdErr(); }
277  ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, const Pathname & root_r )
278  : ExternalProgramWithStderr( argv_r, environment_r, false, root_r )
279  {}
280  public:
283 
288  bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
289 
293  bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
294  { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
295 
296  private:
298  void _initStdErr()
299  { closeW(); }
300 
301  private:
302  std::string _buffer;
303  };
304 
309  {
310  public:
311  ExternalProgramWithSeperatePgid (const char *const *argv,
312  Stderr_Disposition stderr_disp = Normal_Stderr,
313  int stderr_fd = -1, bool default_locale = false,
314  const Pathname& root = "") : ExternalProgram()
315  {
316  start_program( argv, Environment(), stderr_disp, stderr_fd, default_locale, root.c_str(), true );
317  }
318 
319  };
320 
321 } // namespace zypp
322 
323 #endif // ZYPP_EXTERNALPROGRAM_H
#define ZYPP_LOCAL
Definition: APIConfig.h:48
const std::string & command() const
The command we&#39;re executing.
ExternalProgram()
Start an external program by giving the arguments as an arry of char *pointers.
ExternalProgramWithSeperatePgid(const char *const *argv, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
Bidirectional stream to external data.
bool use_pty
Set to true, if a pair of ttys is used for communication instead of a pair of pipes.
std::ostream & operator>>(std::ostream &out_r)
Redirect all command output to an ostream.
bool kill()
Kill the program.
void _initStdErr()
Close write end of the pipe (childs end).
ExternalProgram extended to offer reading programs stderr.
bool running()
Return whether program is running.
ExternalProgramWithStderr(const Arguments &argv_r, const Environment &environment_r, const Pathname &root_r)
Convenience taking just the root_r.
std::vector< std::string > Arguments
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
bool stderrGetline(std::string &retval_r, bool returnDelim_r=false)
Read next complete line from stderr (nonblocking).
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
ExternalProgramWithStderr(const Arguments &argv_r, bool defaultLocale_r=false, const Pathname &root_r="")
Provides API related macros.
ExternalProgramWithStderr(const Arguments &argv_r, const Environment &environment_r, bool defaultLocale_r=false, const Pathname &root_r="")
ExternalProgram extended to change the progress group ID after forking.
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
std::string _execError
Remember execution errors like failed fork/exec.
int close()
Wait for the progamm to complete.
std::string _command
Store the command we&#39;re executing.
ExternalProgramWithStderr(const Arguments &argv_r, const Pathname &root_r)
Convenience taking just the root_r.
static void renumber_fd(int origfd, int newfd)
origfd will be accessible as newfd and closed (unless they were equal)
pid_t getpid()
return pid
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)
Helper providing pipe FDs for ExternalProgramWithStderr.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1