libzypp 17.37.10

abstractspawnengine.cc
Go to the documentation of this file.
1#include "private/abstractspawnengine_p.h"
2#include <zypp-core/base/LogControl.h>
3#include <zypp-core/base/Gettext.h>
4#include <zypp-core/base/String.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7
8#include "private/forkspawnengine_p.h"
9
10#undef ZYPP_BASE_LOGGER_LOGGROUP
11#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::exec"
12
13 namespace zyppng {
14
15
16#if ZYPP_HAS_GLIBSPAWNENGINE
17 namespace {
18
19 enum class SpawnEngine {
20 GSPAWN,
21 PFORK
22 };
23
24 SpawnEngine initEngineFromEnv () {
25 const std::string fBackend ( zypp::str::asString( ::getenv("ZYPP_FORK_BACKEND") ) );
26 if ( fBackend.empty() || fBackend == "auto" || fBackend == "pfork" ) {
27 DBG << "Starting processes via posix fork" << std::endl;
28 return SpawnEngine::PFORK;
29 } else if ( fBackend == "gspawn" ) {
30 DBG << "Starting processes via glib spawn" << std::endl;
31 return SpawnEngine::GSPAWN;
32 }
33
34 DBG << "Falling back to starting process via posix fork" << std::endl;
35 return SpawnEngine::PFORK;
36 }
37
38 std::unique_ptr<zyppng::AbstractSpawnEngine> engineFromEnv () {
39 static const SpawnEngine eng = initEngineFromEnv();
40 switch ( eng ) {
41 case SpawnEngine::GSPAWN:
42 return std::make_unique<zyppng::GlibSpawnEngine>();
43 case SpawnEngine::PFORK:
44 default:
45 return std::make_unique<zyppng::ForkSpawnEngine>();
46 }
47 }
48 }
49#else
50
51 std::unique_ptr<zyppng::AbstractSpawnEngine> engineFromEnv () {
52 return std::make_unique<zyppng::ForkSpawnEngine>();
53 }
54
55#endif
56
57 AbstractSpawnEngine::AbstractSpawnEngine()
58 {
59 }
60
61 AbstractSpawnEngine::~AbstractSpawnEngine()
62 { }
63
64 std::unique_ptr<AbstractSpawnEngine> AbstractSpawnEngine::createDefaultEngine()
65 {
66 return engineFromEnv();
67 }
68
69 bool AbstractSpawnEngine::switchPgid() const
70 {
71 return _switchPgid;
72 }
73
74 void AbstractSpawnEngine::setSwitchPgid(bool switchPgid)
75 {
76 _switchPgid = switchPgid;
77 }
78
79 zypp::Pathname AbstractSpawnEngine::workingDirectory() const
80 {
81 return _workingDirectory;
82 }
83
84 void AbstractSpawnEngine::setWorkingDirectory(const zypp::Pathname &workingDirectory)
85 {
86 _workingDirectory = workingDirectory;
87 }
88
89 const std::vector<int> &AbstractSpawnEngine::fdsToMap() const
90 {
91 return _mapFds;
92 }
93
94 void AbstractSpawnEngine::addFd(int fd)
95 {
96 _mapFds.push_back( fd );
97 }
98
99 void AbstractSpawnEngine::notifyExited(int status)
100 {
101 _exitStatus = checkStatus( status );
102 _pid = -1;
103 }
104
105 bool AbstractSpawnEngine::dieWithParent() const
106 {
107 return _dieWithParent;
108 }
109
110 void AbstractSpawnEngine::setDieWithParent( bool dieWithParent )
111 {
112 _dieWithParent = dieWithParent;
113 }
114
115 int AbstractSpawnEngine::exitStatus() const
116 {
117 return _exitStatus;
118 }
119
120 void AbstractSpawnEngine::setExitStatus(const int state)
121 {
122 _exitStatus = state;
123 }
124
125 const std::string &AbstractSpawnEngine::executedCommand() const
126 {
127 return _executedCommand;
128 }
129
130 const std::string &AbstractSpawnEngine::execError() const
131 {
132 return _execError;
133 }
134
135 void AbstractSpawnEngine::setExecError(const std::string &str)
136 {
137 _execError = str;
138 }
139
140 zypp::Pathname AbstractSpawnEngine::chroot() const
141 {
142 return _chroot;
143 }
144
145 void AbstractSpawnEngine::setChroot( const zypp::Pathname &chroot )
146 {
147 _chroot = chroot;
148 }
149
150 bool AbstractSpawnEngine::useDefaultLocale() const
151 {
152 return _useDefaultLocale;
153 }
154
155 void AbstractSpawnEngine::setUseDefaultLocale( bool defaultLocale )
156 {
157 _useDefaultLocale = defaultLocale;
158 }
159
160 AbstractSpawnEngine::Environment AbstractSpawnEngine::environment() const
161 {
162 return _environment;
163 }
164
165 void AbstractSpawnEngine::setEnvironment( const Environment &environment )
166 {
167 _environment = environment;
168 }
169
170 pid_t AbstractSpawnEngine::pid( )
171 {
172 return _pid;
173 }
174
175 int AbstractSpawnEngine::checkStatus( int status )
176 {
177 if (WIFEXITED (status))
178 {
179 status = WEXITSTATUS (status);
180 if(status)
181 {
182 WAR << "Pid " << _pid << " exited with status " << status << std::endl;
183 _execError = zypp::str::form( _("Command exited with status %d."), status );
184 }
185 else
186 {
187 // if 'launch' is logged, completion should be logged,
188 // even if successfull.
189 DBG << "Pid " << _pid << " successfully completed" << std::endl;
190 _execError.clear(); // empty if running or successfully completed
191 }
192 }
193 else if (WIFSIGNALED (status))
194 {
195 status = WTERMSIG (status);
196 std::string sigdetail { strsignal(status) };
197 if ( WCOREDUMP(status) ) {
198 sigdetail += "; Core dumped";
199 }
200 if ( status == SIGKILL ) {
201 sigdetail += "; Out of memory?";
202 }
203 WAR << "Pid " << _pid << " was killed by signal " << status << " (" << sigdetail << ")" << std::endl;
204 _execError = zypp::str::form( _("Command was killed by signal %d (%s)."), status, sigdetail.c_str() );
205 status+=128;
206 }
207 else {
208 ERR << "Pid " << _pid << " exited with unknown error" << std::endl;
209 _execError = _("Command exited with unknown error.");
210 }
211 return status;
212 }
213
214} // namespace zyppng
abstractspawnengine_p.h
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zyppng::AbstractSpawnEngine::pid
pid_t pid()
Definition abstractspawnengine.cc:170
zyppng::AbstractSpawnEngine::_chroot
zypp::Pathname _chroot
Path to chroot into.
Definition abstractspawnengine_p.h:104
zyppng::AbstractSpawnEngine::dieWithParent
bool dieWithParent() const
Definition abstractspawnengine.cc:105
zyppng::AbstractSpawnEngine::setSwitchPgid
void setSwitchPgid(bool switchPgid)
Definition abstractspawnengine.cc:74
zyppng::AbstractSpawnEngine::executedCommand
const std::string & executedCommand() const
Definition abstractspawnengine.cc:125
zyppng::AbstractSpawnEngine::AbstractSpawnEngine
AbstractSpawnEngine()
Definition abstractspawnengine.cc:57
zyppng::AbstractSpawnEngine::environment
Environment environment() const
Definition abstractspawnengine.cc:160
zyppng::AbstractSpawnEngine::fdsToMap
const std::vector< int > & fdsToMap() const
Definition abstractspawnengine.cc:89
zyppng::AbstractSpawnEngine::workingDirectory
zypp::Pathname workingDirectory() const
Definition abstractspawnengine.cc:79
zyppng::AbstractSpawnEngine::setExecError
void setExecError(const std::string &str)
Definition abstractspawnengine.cc:135
zyppng::AbstractSpawnEngine::switchPgid
bool switchPgid() const
Definition abstractspawnengine.cc:69
zyppng::AbstractSpawnEngine::_workingDirectory
zypp::Pathname _workingDirectory
Working directory.
Definition abstractspawnengine_p.h:106
zyppng::AbstractSpawnEngine::_execError
std::string _execError
Remember execution errors like failed fork/exec.
Definition abstractspawnengine_p.h:96
zyppng::AbstractSpawnEngine::_executedCommand
std::string _executedCommand
Store the command we're executing.
Definition abstractspawnengine_p.h:98
zyppng::AbstractSpawnEngine::_exitStatus
int _exitStatus
Definition abstractspawnengine_p.h:94
zyppng::AbstractSpawnEngine::_dieWithParent
bool _dieWithParent
Should the process die with the parent process.
Definition abstractspawnengine_p.h:89
zyppng::AbstractSpawnEngine::_switchPgid
bool _switchPgid
Definition abstractspawnengine_p.h:91
zyppng::AbstractSpawnEngine::~AbstractSpawnEngine
virtual ~AbstractSpawnEngine()
Definition abstractspawnengine.cc:61
zyppng::AbstractSpawnEngine::Environment
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
Definition abstractspawnengine_p.h:28
zyppng::AbstractSpawnEngine::_useDefaultLocale
bool _useDefaultLocale
Definition abstractspawnengine_p.h:87
zyppng::AbstractSpawnEngine::_mapFds
std::vector< int > _mapFds
Additional file descriptors we want to map to the new process.
Definition abstractspawnengine_p.h:108
zyppng::AbstractSpawnEngine::setExitStatus
void setExitStatus(const int state)
Definition abstractspawnengine.cc:120
zyppng::AbstractSpawnEngine::setChroot
void setChroot(const zypp::Pathname &chroot)
Definition abstractspawnengine.cc:145
zyppng::AbstractSpawnEngine::notifyExited
virtual void notifyExited(int status)
Definition abstractspawnengine.cc:99
zyppng::AbstractSpawnEngine::addFd
void addFd(int fd)
Definition abstractspawnengine.cc:94
zyppng::AbstractSpawnEngine::_environment
Environment _environment
Environment variables to set in the new process.
Definition abstractspawnengine_p.h:102
zyppng::AbstractSpawnEngine::exitStatus
int exitStatus() const
Definition abstractspawnengine.cc:115
zyppng::AbstractSpawnEngine::_pid
pid_t _pid
Definition abstractspawnengine_p.h:93
zyppng::AbstractSpawnEngine::useDefaultLocale
bool useDefaultLocale() const
Definition abstractspawnengine.cc:150
zyppng::AbstractSpawnEngine::setWorkingDirectory
void setWorkingDirectory(const zypp::Pathname &workingDirectory)
Definition abstractspawnengine.cc:84
zyppng::AbstractSpawnEngine::checkStatus
int checkStatus(int status)
Definition abstractspawnengine.cc:175
zyppng::AbstractSpawnEngine::chroot
zypp::Pathname chroot() const
Definition abstractspawnengine.cc:140
zyppng::AbstractSpawnEngine::setUseDefaultLocale
void setUseDefaultLocale(bool defaultLocale)
Definition abstractspawnengine.cc:155
zyppng::AbstractSpawnEngine::createDefaultEngine
static std::unique_ptr< zyppng::AbstractSpawnEngine > createDefaultEngine()
Definition abstractspawnengine.cc:64
zyppng::AbstractSpawnEngine::setEnvironment
void setEnvironment(const Environment &environment)
Definition abstractspawnengine.cc:165
zyppng::AbstractSpawnEngine::execError
const std::string & execError() const
Definition abstractspawnengine.cc:130
zyppng::AbstractSpawnEngine::setDieWithParent
void setDieWithParent(bool dieWithParent)
Definition abstractspawnengine.cc:110
forkspawnengine_p.h
str
String related utilities and Regular expression matching.
zypp::str::asString
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
Definition String.h:140
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::engineFromEnv
std::unique_ptr< zyppng::AbstractSpawnEngine > engineFromEnv()
Definition abstractspawnengine.cc:51
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
LogControl.h
DBG
#define DBG
Definition Logger.h:99
ERR
#define ERR
Definition Logger.h:102
WAR
#define WAR
Definition Logger.h:101
String.h