libzypp 17.37.17
ExternalProgram.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11
12
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>
25#include <zypp-core/Pathname.h>
26
27namespace zyppng {
29}
30
31namespace zypp {
32
63 */
65 {
66
67 public:
69 using Arguments = std::vector<std::string>;
70
80 };
81
85 using Environment = std::map<std::string, std::string>;
86
95 ExternalProgram (const std::string& commandline,
97 bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
98 const Pathname& root = "");
99
121
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
152 ~ExternalProgram() override;
153
154#ifdef __cpp_lib_optional // YAST/PK explicitly use c++11 until 15-SP3
163 bool waitForExit ( std::optional<uint64_t> timeout = {} );
164#endif
165
167 int close() override;
168
172 bool kill();
173
177 bool kill( int sig );
178
182 bool running();
183
187 pid_t getpid();
188
190 const std::string & command() const;
191
201 const std::string & execError() const;
202
206 static void renumber_fd (int origfd, int newfd);
207
208 public:
209
228 std::ostream & operator>>( std::ostream & out_r );
229
230 private:
231 std::unique_ptr<zyppng::AbstractSpawnEngine> _backend;
232
233 protected:
234
235 void start_program (const char *const *argv, const Environment & environment,
236 Stderr_Disposition stderr_disp = Normal_Stderr,
237 int stderr_fd = -1, bool default_locale = false,
238 const char* root = NULL, bool switch_pgid = false, bool die_with_parent = false, bool usePty = false );
239
240 };
241
242
243 namespace externalprogram
244 {
249 */
250 struct EarlyPipe
252 enum { R=0, W=1 };
253 EarlyPipe();
255 void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
256 FILE * fStdErr() { return _stderr; }
257 protected:
258 FILE * _stderr;
259 int _fds[2];
260 };
261 } // namespace externalprogram
262
265 */
267 {
268 public:
269 ExternalProgramWithStderr( const Arguments & argv_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
270 : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
271 { _initStdErr(); }
272 /** \overlocad Convenience taking just the \a root_r. */
273 ExternalProgramWithStderr( const Arguments & argv_r, const Pathname & root_r )
274 : ExternalProgramWithStderr( argv_r, false, root_r )
275 {}
277 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
278 : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
279 { _initStdErr(); }
280 /** \overlocad Convenience taking just the \a root_r. */
281 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, const Pathname & root_r )
282 : ExternalProgramWithStderr( argv_r, environment_r, false, root_r )
283 {}
284 public:
287
292 bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
293
296 */
297 bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
298 { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
299
300 private:
301 /** Close write end of the pipe (childs end). */
302 void _initStdErr()
303 { closeW(); }
304
305 private:
306 std::string _buffer;
307 };
308
311 */
313 {
314 public:
315 ExternalProgramWithSeperatePgid (const char *const *argv,
316 Stderr_Disposition stderr_disp = Normal_Stderr,
317 int stderr_fd = -1, bool default_locale = false,
318 const Pathname& root = "") : ExternalProgram()
319 {
320 start_program( argv, Environment(), stderr_disp, stderr_fd, default_locale, root.c_str(), true );
321 }
322
323 };
324
325} // namespace zypp
326
327#endif // ZYPP_EXTERNALPROGRAM_H
ExternalProgramWithSeperatePgid(const char *const *argv, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
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).
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)
std::vector< std::string > Arguments
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
ExternalProgram(const std::string &commandline, Stderr_Disposition stderr_disp=Normal_Stderr, bool use_pty=false, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
Start the external program by using the shell /bin/sh with the option -c.
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
std::unique_ptr< zyppng::AbstractSpawnEngine > _backend
std::istream & operator>>(std::istream &str, PluginFrame &obj)
Construct from stream.
Bidirectional stream to external data.
Easy-to use interface to the ZYPP dependency resolver.
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
Helper providing pipe FDs for ExternalProgramWithStderr.
Provides API related macros.
#define ZYPP_LOCAL
Definition Globals.h:71