libzypp  17.23.3
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 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 
225  bool use_pty;
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  {
251  struct EarlyPipe
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:
303  void _initStdErr()
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
zypp::ExternalProgram::operator>>
std::ostream & operator>>(std::ostream &out_r)
Redirect all command output to an ostream.
Definition: ExternalProgram.cc:630
zypp::ExternalProgram::Stderr_To_Stdout
Definition: ExternalProgram.h:73
zypp::ExternalProgram::start_program
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)
Definition: ExternalProgram.cc:182
zypp::ExternalProgramWithStderr
ExternalProgram extended to offer reading programs stderr.
Definition: ExternalProgram.h:267
zypp::externalprogram::EarlyPipe::closeW
void closeW()
Definition: ExternalProgram.h:256
zypp::ExternalProgram::use_pty
bool use_pty
Set to true, if a pair of ttys is used for communication instead of a pair of pipes.
Definition: ExternalProgram.h:225
zypp::ExternalProgram::kill
bool kill()
Kill the program.
Definition: ExternalProgram.cc:574
zypp::ExternalProgramWithStderr::_initStdErr
void _initStdErr()
Close write end of the pipe (childs end).
Definition: ExternalProgram.h:303
zypp::ExternalProgram::running
bool running()
Return whether program is running.
Definition: ExternalProgram.cc:594
zypp::externalprogram::EarlyPipe::_stderr
FILE * _stderr
Definition: ExternalProgram.h:259
zypp::externalprogram::EarlyPipe
Helper providing pipe FDs for ExternalProgramWithStderr.
Definition: ExternalProgram.h:251
zypp::externalprogram::EarlyPipe::_fds
int _fds[2]
Definition: ExternalProgram.h:260
zypp::ExternalProgram
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
Definition: ExternalProgram.h:59
Pathname.h
zypp::ExternalProgram::execError
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
Definition: ExternalProgram.h:186
zypp::ExternalProgram::Arguments
std::vector< std::string > Arguments
Definition: ExternalProgram.h:64
zypp::ExternalProgramWithStderr::_buffer
std::string _buffer
Definition: ExternalProgram.h:307
zypp::ExternalProgram::checkStatus
int checkStatus(int)
Definition: ExternalProgram.cc:534
zypp::ExternalProgram::~ExternalProgram
~ExternalProgram()
Definition: ExternalProgram.cc:171
zypp::ExternalProgramWithStderr::ExternalProgramWithStderr
ExternalProgramWithStderr(const Arguments &argv_r, bool defaultLocale_r=false, const Pathname &root_r="")
Definition: ExternalProgram.h:270
zypp::ExternalProgramWithStderr::ExternalProgramWithStderr
ExternalProgramWithStderr(const Arguments &argv_r, const Environment &environment_r, bool defaultLocale_r=false, const Pathname &root_r="")
Definition: ExternalProgram.h:278
zypp::externalprogram::EarlyPipe::~EarlyPipe
~EarlyPipe()
Definition: ExternalProgram.cc:659
zypp::externalprogram::EarlyPipe::EarlyPipe
EarlyPipe()
Definition: ExternalProgram.cc:646
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::ExternalProgram::Discard_Stderr
Definition: ExternalProgram.h:72
zypp::ExternalProgramWithStderr::ExternalProgramWithStderr
ExternalProgramWithStderr(const Arguments &argv_r, const Pathname &root_r)
\overlocad Convenience taking just the root_r.
Definition: ExternalProgram.h:274
zypp::ExternalProgram::Stderr_Disposition
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
Definition: ExternalProgram.h:70
zypp::ExternalProgram::close
int close()
Wait for the progamm to complete.
Definition: ExternalProgram.cc:456
zypp::ExternalProgram::_execError
std::string _execError
Remember execution errors like failed fork/exec.
Definition: ExternalProgram.h:232
zypp::ExternalProgramWithSeperatePgid
ExternalProgram extended to change the progress group ID after forking.
Definition: ExternalProgram.h:313
zypp::ExternalProgram::Environment
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
Definition: ExternalProgram.h:81
zypp::ExternalProgram::pid
pid_t pid
Definition: ExternalProgram.h:227
zypp::ExternalProgramWithStderr::stderrGetUpTo
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
Definition: ExternalProgram.cc:667
zypp::ExternalProgram::_exitStatus
int _exitStatus
Definition: ExternalProgram.h:228
zypp::ExternalProgram::_command
std::string _command
Store the command we're executing.
Definition: ExternalProgram.h:230
ExternalDataSource.h
zypp::ExternalProgram::Normal_Stderr
Definition: ExternalProgram.h:71
zypp::ExternalProgramWithStderr::ExternalProgramWithStderr
ExternalProgramWithStderr(const Arguments &argv_r, const Environment &environment_r, const Pathname &root_r)
\overlocad Convenience taking just the root_r.
Definition: ExternalProgram.h:282
zypp::ExternalProgram::getpid
pid_t getpid()
return pid
Definition: ExternalProgram.h:171
zypp::ExternalProgramWithSeperatePgid::ExternalProgramWithSeperatePgid
ExternalProgramWithSeperatePgid(const char *const *argv, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
Definition: ExternalProgram.h:316
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:44
zypp::ExternalProgram::renumber_fd
static void renumber_fd(int origfd, int newfd)
origfd will be accessible as newfd and closed (unless they were equal)
Definition: ExternalProgram.cc:618
zypp::externalprogram::EarlyPipe::R
Definition: ExternalProgram.h:253
zypp::externalprogram::EarlyPipe::W
Definition: ExternalProgram.h:253
zypp::ExternalProgramWithStderr::stderrGetline
bool stderrGetline(std::string &retval_r, bool returnDelim_r=false)
Read next complete line from stderr (nonblocking).
Definition: ExternalProgram.h:298
zypp::externalprogram::EarlyPipe::fStdErr
FILE * fStdErr()
Definition: ExternalProgram.h:257
zypp::externalprogram::ExternalDataSource
Bidirectional stream to external data.
Definition: ExternalDataSource.h:25
zypp::ExternalProgram::command
const std::string & command() const
The command we're executing.
Definition: ExternalProgram.h:174
zypp::ExternalProgram::ExternalProgram
ExternalProgram()
Start an external program by giving the arguments as an arry of char *pointers.
Definition: ExternalProgram.cc:40
zypp::ExternalProgram::Stderr_To_FileDesc
Definition: ExternalProgram.h:74