libzypp 17.37.17
ZYppImpl.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include <iostream>
14#include <zypp/TmpPath.h>
15#include <zypp/base/Logger.h>
16#include <zypp/base/String.h>
17#include <zypp/base/Env.h>
18
21#include <zypp/ZYpp.h>
23#include <zypp/ZConfig.h>
24#include <zypp/sat/Pool.h>
25#include <zypp/PoolItem.h>
26
27#include <zypp/ZYppCallbacks.h> // JobReport::instance
28
29using std::endl;
30
31#include <glib.h>
34
35namespace {
36
37 // Helper pipe to safely signal libzypp's poll code
38 // to stop polling and throw a user abort exception.
39 // Due to the pain that is unix signal handling we need
40 // to use atomics and be very careful about what we are calling.
41 //
42 // Not cleaned up, this happens when the application that is using libzypp
43 // exits.
44 volatile sig_atomic_t shutdownPipeRead{-1};
45 volatile sig_atomic_t shutdownPipeWrite{-1};
46
47 bool makeShutdownPipe() {
48 int pipeFds[]{ -1, -1 };
49 #ifdef HAVE_PIPE2
50 if ( ::pipe2( pipeFds, O_CLOEXEC ) != 0 )
51 return false;
52 #else
53 if ( ::pipe( pipeFds ) != 0 )
54 return false;
55 ::fcntl( pipeFds[0], F_SETFD, O_CLOEXEC );
56 ::fcntl( pipeFds[1], F_SETFD, O_CLOEXEC );
57 #endif
58 shutdownPipeRead = pipeFds[0];
59 shutdownPipeWrite = pipeFds[1];
60 return true;
61 }
62
63 const bool ensureShutdownPipe() {
64 static auto pipesInitialized = makeShutdownPipe();
65 return pipesInitialized;
66 }
67
68 const int shutdownPipeReadFd() {
69 if ( !ensureShutdownPipe() )
70 return -1;
71 return static_cast<int>(shutdownPipeRead);
72 }
73
74 const int shutdownPipeWriteFd() {
75 return static_cast<int>(shutdownPipeWrite);
76 }
77
78}
79
81namespace zypp
82{
83
85 namespace media
86 {
88 {
90 if ( condition_r && ! (_guard = globalguard.lock()) )
91 {
92 // aquire a new one....
94 globalguard = _guard;
95 }
96 }
97 } // namespace media
99
101 {
102 static callback::SendReport<JobReport> _report;
103 return _report;
104 }
105
106
108 namespace zypp_detail
109 {
110
112 //
113 // METHOD NAME : ZYppImpl::ZYppImpl
114 // METHOD TYPE : Constructor
115 //
117 : _target( nullptr )
118 , _resolver( new Resolver( ResPool::instance()) )
119 {
120 // trigger creation of the shutdown pipe
121 if ( !ensureShutdownPipe() )
122 WAR << "Failed to create shutdown pipe" << std::endl;
123
125 MIL << "Initializing keyring..." << std::endl;
126 _keyring = new KeyRing(tmpPath());
127 _keyring->allowPreload( true );
128 }
129
131 //
132 // METHOD NAME : ZYppImpl::~ZYppImpl
133 // METHOD TYPE : Destructor
134 //
137
138 //------------------------------------------------------------------------
139 // add/remove resolvables
140
149
151 {
152 _disk_usage.reset(new DiskUsageCounter());
153 _disk_usage->setMountPoints(mp);
154 }
155
157 {
158 if (_disk_usage)
159 return _disk_usage->getMountPoints();
160 else
162 }
163
164 //------------------------------------------------------------------------
165 // target
166
168 {
169 if (! _target)
170 ZYPP_THROW(Exception("Target not initialized."));
171 return _target;
172 }
173
174 void ZYppImpl::changeTargetTo( const Target_Ptr& newtarget_r )
175 {
176 if ( _target && newtarget_r ) // bsc#1203760: Make sure the old target is deleted before a new one is created!
177 INT << "2 active targets at the same time must not happen!" << endl;
178 _target = newtarget_r;
180 resolver()->setDefaultSolverFlags( /*all_r*/false ); // just changed defaults
181 }
182
183 void ZYppImpl::initializeTarget( const Pathname & root, bool doRebuild_r )
184 {
185 MIL << "initTarget( " << root << (doRebuild_r?", rebuilddb":"") << ")" << endl;
186 if (_target) {
187 if (_target->root() == root) {
188 MIL << "Repeated call to initializeTarget()" << endl;
189 return;
190 }
191 _target->unload();
192 _target = nullptr; // bsc#1203760: Make sure the old target is deleted before a new one is created!
193 }
194 changeTargetTo( new Target( root, doRebuild_r ) );
195 _target->buildCache();
196 }
197
199 {
200 if (_target)
201 _target->unload();
202
203 changeTargetTo( nullptr );
204 }
205
206 //------------------------------------------------------------------------
207 // commit
208
212 {
213 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
214 {
215 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
216 }
217
218 MIL << "Attempt to commit (" << policy_r << ")" << endl;
219 if (! _target)
220 ZYPP_THROW( Exception("Target not initialized.") );
221
222
223 env::ScopedSet ea { "ZYPP_IS_RUNNING", str::numstring(getpid()).c_str() };
225 if ( _target->chrooted() )
226 eb = env::ScopedSet( "SYSTEMD_OFFLINE", "1" ); // bsc#1118758 - indicate no systemd if chrooted install
228 {
229 std::string val;
230 if ( const char * env = getenv("GLIBC_TUNABLES"); env ) { // colon-separated list of k=v pairs
231 val = env;
232 val += ":";
233 }
234 val += "glibc.cpu.hwcap_mask=0"; // bsc#1246912 - make ld.so ignore the subarch packages
235 ec = env::ScopedSet( "GLIBC_TUNABLES", val.c_str() );
236 }
237
238 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
239
240 if (! policy_r.dryRun() )
241 {
242 if ( policy_r.syncPoolAfterCommit() )
243 {
244 // reload new status from target
245 DBG << "reloading " << sat::Pool::instance().systemRepoAlias() << " repo to pool" << endl;
246 _target->load();
247 }
248 else
249 {
250 DBG << "unloading " << sat::Pool::instance().systemRepoAlias() << " repo from pool" << endl;
251 _target->unload();
252 }
253 }
254
255 MIL << "Commit (" << policy_r << ") returned: "
256 << res << endl;
257 return res;
258 }
259
260 void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
261 {
262 if (! _target)
263 ZYPP_THROW( Exception("Target not initialized.") );
264 _target->_pimpl->installSrcPackage( srcPackage_r );
265 }
266
267 ManagedFile ZYppImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
268 {
269 if (! _target)
270 ZYPP_THROW( Exception("Target not initialized.") );
271 return _target->_pimpl->provideSrcPackage( srcPackage_r );
272 }
273
274 //------------------------------------------------------------------------
275 // target store path
276
278 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
279
280 void ZYppImpl::setHomePath( const Pathname & path )
281 { _home_path = path; }
282
284 { return zypp::myTmpDir(); }
285
287 {
288 // ONLY signal safe code here, that means no logging or anything else that is more than using atomics
289 // or writing a fd
290 int sigFd = shutdownPipeWriteFd();
291 if ( sigFd == -1 ) {
292 // we have no fd to set this
293 return;
294 }
295 zyppng::eintrSafeCall( write, sigFd, "1", 1 );
296 }
297
299 {
300 // ONLY signal safe code here, that means no logging or anything else that is more than using atomics
301 // or writing a fd
302 int sigFd = shutdownPipeWriteFd();
303 if ( sigFd == -1 ) {
304 // we have no fd so nothing to clear
305 return;
306 }
307
308 char buf = 0;
309 while( zyppng::eintrSafeCall( read, sigFd, &buf, 1 ) > 0 )
310 continue;
311 }
312
313 /******************************************************************
314 **
315 ** FUNCTION NAME : operator<<
316 ** FUNCTION TYPE : std::ostream &
317 */
318 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
319 {
320 return str << "ZYppImpl";
321 }
322
323 int zypp_poll( std::vector<GPollFD> &fds, int timeout)
324 {
325 // request a shutdown fd we can use
326 const auto shutdownFd = shutdownPipeReadFd();
327 if (shutdownFd == -1) {
328 ZYPP_THROW( zypp::Exception("Failed to get shutdown pipe") );
329 }
330
331 fds.push_back( GPollFD {
332 .fd = shutdownFd,
333 .events = G_IO_IN,
334 .revents = 0
335 });
336
337 // make sure to remove our fd again
338 OnScopeExit removeShutdownFd( [&](){ fds.pop_back(); } );
339
340 int r = zyppng::eintrSafeCall( g_poll, fds.data(), fds.size(), timeout );
341 if ( r > 0 ) {
342 // at least one fd triggered, if its our's we throw
343 if ( fds.back().revents )
344 ZYPP_THROW( UserRequestException( UserRequestException::ABORT, "Shutdown signal received during poll." ) );
345 }
346 return r;
347 }
348
350 } // namespace zypp_detail
352
354} // namespace zypp
struct _GPollFD GPollFD
Definition ZYppImpl.h:26
Compute disk space occupied by packages across partitions/directories.
static MountPointSet detectMountPoints(const std::string &rootdir="/")
Get mountpoints of system below rootdir If we happen to detect snapshotting btrfs partitions,...
std::set< MountPoint > MountPointSet
Base class for Exception.
Definition Exception.h:153
Gpg key handling.
Definition KeyRing.h:170
Global ResObject pool.
Definition ResPool.h:62
Dependency resolver interface.
Definition Resolver.h:45
Base for exceptions caused by explicit user request.
void notifyTargetChanged()
internal
Definition ZConfig.cc:974
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:940
std::ostream & about(std::ostream &str) const
Print some detail about the current libzypp version.
Definition ZConfig.cc:1367
Options and policies for ZYpp::commit.
ZYppCommitPolicy & syncPoolAfterCommit(bool yesNo_r)
Kepp pool in sync with the Target databases after commit (default: true)
ZYppCommitPolicy & dryRun(bool yesNo_r)
Set dry run (default: false).
Result returned from ZYpp::commit.
static Pool instance()
Singleton ctor.
Definition Pool.h:55
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition Pool.cc:46
DiskUsageCounter::MountPointSet getPartitions() const
Definition ZYppImpl.cc:156
ResPool pool() const
Definition ZYppImpl.h:52
void setPartitions(const DiskUsageCounter::MountPointSet &mp)
Definition ZYppImpl.cc:150
Target_Ptr target() const
Definition ZYppImpl.cc:167
void setHomePath(const Pathname &path)
set the home, if you need to change it
Definition ZYppImpl.cc:280
Resolver_Ptr resolver() const
Definition ZYppImpl.h:63
Pathname tmpPath() const
Get the path where zypp related plugins store tmp data.
Definition ZYppImpl.cc:283
ZYppCommitResult commit(const ZYppCommitPolicy &policy_r)
Commit changes and transactions.
Definition ZYppImpl.cc:211
static void setShutdownSignal()
Enable the shutdown signal for zypp_poll calls.
Definition ZYppImpl.cc:286
void installSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition ZYppImpl.cc:260
shared_ptr< DiskUsageCounter > _disk_usage
defined mount points, used for disk usage counting
Definition ZYppImpl.h:138
ManagedFile provideSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition ZYppImpl.cc:267
void changeTargetTo(const Target_Ptr &newtarget_r)
Hook for actions to trigger if the Target changes (initialize/finish)
Definition ZYppImpl.cc:174
Pathname homePath() const
Get the path where zypp related plugins store persistent data and caches.
Definition ZYppImpl.cc:277
DiskUsageCounter::MountPointSet diskUsage()
Definition ZYppImpl.cc:141
static void clearShutdownSignal()
Disable the shutdown signal for zypp_poll calls.
Definition ZYppImpl.cc:298
void initializeTarget(const Pathname &root, bool doRebuild_r)
Definition ZYppImpl.cc:183
String related utilities and Regular expression matching.
Namespace intended to collect all environment variables we use.
Definition Env.h:25
std::string numstring(char n, int w=0)
Definition String.h:290
std::ostream & operator<<(std::ostream &str, const ZYppImpl &obj)
Definition ZYppImpl.cc:318
int zypp_poll(std::vector< GPollFD > &fds, int timeout)
Small wrapper around g_poll that additionally listens to the shutdown FD returned by ZYpp::shutdownSi...
Definition ZYppImpl.cc:323
Easy-to use interface to the ZYPP dependency resolver.
Pathname myTmpDir()
Global access to the zypp.TMPDIR (created on demand, deleted when libzypp is unloaded)
Definition TmpPath.cc:340
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
Definition ManagedFile.h:27
AutoDispose< void > OnScopeExit
auto eintrSafeCall(Fun &&function, Args &&... args)
static callback::SendReport< JobReport > & instance()
Singleton sender instance.
Definition ZYppImpl.cc:100
Temporarily connect a ReceiveReport then restore the previous one.
Definition Callback.h:285
Temporarily set/unset an environment variable.
Definition Env.h:45
ScopedDisableMediaChangeReport(bool condition_r=true)
Disbale MediaChangeReport if condition_r is true.
Definition ZYppImpl.cc:87
shared_ptr< callback::TempConnect< media::MediaChangeReport > > _guard
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define DBG
Definition Logger.h:99
#define MIL
Definition Logger.h:100
#define WAR
Definition Logger.h:101
#define INT
Definition Logger.h:104