libzypp: zypp-core/ExternalProgram.h Source File

libzypp 17.38.10

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>
24#include <zypp-core/base/ExternalDataSource.h>
25#include <zypp-core/Pathname.h>
26
27namespace zyppng {
28 class AbstractSpawnEngine;
29}
30
31namespace zypp {
32
63 */
64 class ZYPP_API ExternalProgram : public zypp::externalprogram::ExternalDataSource
65 {
66
67 public:
68
69 using Arguments = std::vector<std::string>;
70
74 */
75 enum Stderr_Disposition {
76 Normal_Stderr,
77 Discard_Stderr,
78 Stderr_To_Stdout,
79 Stderr_To_FileDesc
80 };
81
85 using Environment = std::map<std::string, std::string>;
86
95 ExternalProgram (const 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
121
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
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 Pathname chroot() const;
188
192 pid_t getpid();
193
195 const std::string & command() const;
196
206 const std::string & execError() const;
207
211 static void renumber_fd (int origfd, int newfd);
212
213 public:
214
233 std::ostream & operator>>( std::ostream & out_r );
234
235 private:
236 std::unique_ptr<zyppng::AbstractSpawnEngine> _backend;
237
238 protected:
239
240 void start_program (const char *const *argv, const Environment & environment,
241 Stderr_Disposition stderr_disp = Normal_Stderr,
242 int stderr_fd = -1, bool default_locale = false,
243 const char* root = NULL, bool switch_pgid = false, bool die_with_parent = false, bool usePty = false );
244
245 };
246
247
248 namespace externalprogram
249 {
254 */
255 struct EarlyPipe
256 {
257 enum { R=0, W=1 };
258 EarlyPipe();
259 ~EarlyPipe();
260 void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
261 FILE * fStdErr() { return _stderr; }
262 protected:
263 FILE * _stderr;
264 int _fds[2];
265 };
266 } // namespace externalprogram
267
270 */
271 class ExternalProgramWithStderr : private externalprogram::EarlyPipe, public ExternalProgram
272 {
273 public:
274 ExternalProgramWithStderr( const Arguments & argv_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
275 : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
276 { _initStdErr(); }
277 /** \overlocad Convenience taking just the \a root_r. */
278 ExternalProgramWithStderr( const Arguments & argv_r, const Pathname & root_r )
279 : ExternalProgramWithStderr( argv_r, false, root_r )
280 {}
281
282 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, bool defaultLocale_r = false, const Pathname & root_r = "" )
283 : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W], defaultLocale_r, root_r )
284 { _initStdErr(); }
285 /** \overlocad Convenience taking just the \a root_r. */
286 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r, const Pathname & root_r )
287 : ExternalProgramWithStderr( argv_r, environment_r, false, root_r )
288 {}
289 public:
291 using externalprogram::EarlyPipe::fStdErr;
292
297 bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
298
301 */
302 bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
303 { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
304
305 private:
306 /** Close write end of the pipe (childs end). */
307 void _initStdErr()
308 { closeW(); }
309
310 private:
311 std::string _buffer;
312 };
313
316 */
317 class ExternalProgramWithSeperatePgid : public ExternalProgram
318 {
319 public:
320 ExternalProgramWithSeperatePgid (const char *const *argv,
321 Stderr_Disposition stderr_disp = Normal_Stderr,
322 int stderr_fd = -1, bool default_locale = false,
323 const Pathname& root = "") : ExternalProgram()
324 {
325 start_program( argv, Environment(), stderr_disp, stderr_fd, default_locale, root.c_str(), true );
326 }
327
328 };
329
330} // namespace zypp
331
332#endif // ZYPP_EXTERNALPROGRAM_H
ExternalDataSource.h
Globals.h
Provides API related macros.
Pathname.h
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:319
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:532
zypp::ExternalProgramWithStderr::_buffer
std::string _buffer
Definition ExternalProgram.h:310
zypp::ExternalProgramWithStderr::ExternalProgramWithStderr
ExternalProgramWithStderr(const Arguments &argv_r, bool defaultLocale_r=false, const Pathname &root_r="")
Definition ExternalProgram.h:273
zypp::ExternalProgramWithStderr::_initStdErr
void _initStdErr()
Close write end of the pipe (childs end).
Definition ExternalProgram.h:306
zypp::ExternalProgramWithStderr::stderrGetline
bool stderrGetline(std::string &retval_r, bool returnDelim_r=false)
Read next complete line from stderr (nonblocking).
Definition ExternalProgram.h:301
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, bool usePty=false)
Definition ExternalProgram.cc:157
zypp::ExternalProgram::Arguments
std::vector< std::string > Arguments
Definition ExternalProgram.h:68
zypp::ExternalProgram::Environment
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
Definition ExternalProgram.h:84
zypp::ExternalProgram::ExternalProgram
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.
Definition ExternalProgram.cc:48
zypp::ExternalProgram::Stderr_Disposition
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
Definition ExternalProgram.h:74
zypp::ExternalProgram::Stderr_To_FileDesc
@ Stderr_To_FileDesc
Definition ExternalProgram.h:78
zypp::ExternalProgram::Discard_Stderr
@ Discard_Stderr
Definition ExternalProgram.h:76
zypp::ExternalProgram::Stderr_To_Stdout
@ Stderr_To_Stdout
Definition ExternalProgram.h:77
zypp::ExternalProgram::Normal_Stderr
@ Normal_Stderr
Definition ExternalProgram.h:75
zypp::ExternalProgram::_backend
std::unique_ptr< zyppng::AbstractSpawnEngine > _backend
Definition ExternalProgram.h:235
zypp::externalprogram::ExternalDataSource
Bidirectional stream to external data.
Definition ExternalDataSource.h:28
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zyppng::AbstractSpawnEngine
Definition abstractspawnengine_p.h:22
zypp::externalprogram
Definition ExternalDataSource.cc:33
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::operator>>
std::istream & operator>>(std::istream &str, PluginFrame &obj)
relates: PluginFrame Construct from stream.
Definition PluginFrame.h:284
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zyppng
Definition MediaNetworkRequestExecutor.h:16
zypp::externalprogram::EarlyPipe
Helper providing pipe FDs for ExternalProgramWithStderr.
Definition ExternalProgram.h:255
zypp::externalprogram::EarlyPipe::~EarlyPipe
~EarlyPipe()
Definition ExternalProgram.cc:524
zypp::externalprogram::EarlyPipe::closeW
void closeW()
Definition ExternalProgram.h:259
zypp::externalprogram::EarlyPipe::_fds
int _fds[2]
Definition ExternalProgram.h:263
zypp::externalprogram::EarlyPipe::EarlyPipe
EarlyPipe()
Definition ExternalProgram.cc:511
zypp::externalprogram::EarlyPipe::_stderr
FILE * _stderr
Definition ExternalProgram.h:262
zypp::externalprogram::EarlyPipe::R
@ R
Definition ExternalProgram.h:256
zypp::externalprogram::EarlyPipe::W
@ W
Definition ExternalProgram.h:256
zypp::externalprogram::EarlyPipe::fStdErr
FILE * fStdErr()
Definition ExternalProgram.h:260