libzypp  17.31.20
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 #include <optional>
22 
23 #include <zypp-core/Globals.h>
24 #include <zypp-core/base/ExternalDataSource.h>
25 #include <zypp-core/Pathname.h>
26 
27 namespace zyppng {
28  class AbstractSpawnEngine;
29 }
30 
31 namespace zypp {
32 
65  {
66 
67  public:
68 
69  typedef std::vector<std::string> Arguments;
70 
80  };
81 
85  typedef std::map<std::string,std::string> Environment;
86 
95  ExternalProgram (std::string commandline,
96  Stderr_Disposition stderr_disp = Normal_Stderr,
97  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
98  const Pathname& root = "");
99 
122  ExternalProgram();
123 
124  ExternalProgram (const Arguments &argv,
125  Stderr_Disposition stderr_disp = Normal_Stderr,
126  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
127  const Pathname& root = "");
128 
129  ExternalProgram (const Arguments &argv, const Environment & environment,
130  Stderr_Disposition stderr_disp = Normal_Stderr,
131  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
132  const Pathname& root = "");
133 
134  ExternalProgram (const char *const *argv,
135  Stderr_Disposition stderr_disp = Normal_Stderr,
136  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
137  const Pathname& root = "");
138 
139  ExternalProgram (const char *const *argv, const Environment & environment,
140  Stderr_Disposition stderr_disp = Normal_Stderr,
141  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
142  const Pathname& root = "");
143 
144  ExternalProgram (const char *binpath, const char *const *argv_1,
145  bool use_pty = false);
146 
147 
148  ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
149  bool use_pty = false);
150 
151 
153 
162  bool waitForExit ( std::optional<uint64_t> timeout = {} );
163 
165  int close();
166 
170  bool kill();
171 
175  bool kill( int sig );
176 
180  bool running();
181 
185  pid_t getpid();
186 
188  const std::string & command() const;
189 
199  const std::string & execError() const;
200 
204  static void renumber_fd (int origfd, int newfd);
205 
206  public:
207 
226  std::ostream & operator>>( std::ostream & out_r );
227 
228  private:
229  std::unique_ptr<zyppng::AbstractSpawnEngine> _backend;
230 
231  protected:
232 
233  void start_program (const char *const *argv, const Environment & environment,
234  Stderr_Disposition stderr_disp = Normal_Stderr,
235  int stderr_fd = -1, bool default_locale = false,
236  const char* root = NULL, bool switch_pgid = false, bool die_with_parent = false, bool usePty = false );
237 
238  };
239 
240 
241  namespace externalprogram
242  {
248  struct EarlyPipe
249  {
250  enum { R=0, W=1 };
251  EarlyPipe();
252  ~EarlyPipe();
253  void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
254  FILE * fStdErr() { return _stderr; }
255  protected:
256  FILE * _stderr;
257  int _fds[2];
258  };
259  } // namespace externalprogram
260 
265  {
266  public:
267  ExternalProgramWithStderr( const Arguments & argv_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
268  : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
269  { _initStdErr(); }
271  ExternalProgramWithStderr( const Arguments & argv_r, const Pathname & root_r )
272  : ExternalProgramWithStderr( argv_r, false, root_r )
273  {}
274 
275  ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
276  : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
277  { _initStdErr(); }
279  ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, const Pathname & root_r )
280  : ExternalProgramWithStderr( argv_r, environment_r, false, root_r )
281  {}
282  public:
285 
290  bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
291 
295  bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
296  { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
297 
298  private:
300  void _initStdErr()
301  { closeW(); }
302 
303  private:
304  std::string _buffer;
305  };
306 
311  {
312  public:
313  ExternalProgramWithSeperatePgid (const char *const *argv,
314  Stderr_Disposition stderr_disp = Normal_Stderr,
315  int stderr_fd = -1, bool default_locale = false,
316  const Pathname& root = "") : ExternalProgram()
317  {
318  start_program( argv, Environment(), stderr_disp, stderr_fd, default_locale, root.c_str(), true );
319  }
320 
321  };
322 
323 } // namespace zypp
324 
325 #endif // ZYPP_EXTERNALPROGRAM_H
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.
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.
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
std::unique_ptr< zyppng::AbstractSpawnEngine > _backend
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
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="")
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.
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)
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
int close()
Wait for the progamm to complete.
const std::string & command() const
The command we&#39;re executing.
bool waitForExit(std::optional< uint64_t > timeout={})
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
Helper providing pipe FDs for ExternalProgramWithStderr.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
#define ZYPP_LOCAL
Definition: Globals.h:59