33#include <boost/interprocess/sync/file_lock.hpp>
34#include <boost/interprocess/sync/scoped_lock.hpp>
35#include <boost/interprocess/sync/sharable_lock.hpp>
40using boost::interprocess::file_lock;
41using boost::interprocess::scoped_lock;
42using boost::interprocess::sharable_lock;
69 template class SigBacktraceHandler<SIGSEGV>;
70 template class SigBacktraceHandler<SIGABRT>;
77 {
return getenv(
"ZYPP_LOCKFILE_ROOT") ? getenv(
"ZYPP_LOCKFILE_ROOT") :
"/"; }
81 namespace zypp_readonly_hack
84 static bool active = getenv(
"ZYPP_READONLY_HACK");
89 MIL <<
"ZYPP_READONLY promised." << endl;
132 MIL <<
"Cleaned lock file. (" << getpid() <<
")" << std::endl;
210 MIL <<
"Checking " << status << endl;
212 if ( ! status.
isDir() )
214 DBG <<
"No such process." << endl;
218 static char buffer[513];
219 buffer[0] = buffer[512] = 0;
221 if ( std::ifstream( (procdir/
"cmdline").c_str() ).read( buffer, 512 ).gcount() > 0 )
228 DBG <<
"In zombie state." << endl;
238 MIL <<
"read: Lockfile " <<
_zyppLockFilePath <<
" has pid " << readpid <<
" (our pid: " << getpid() <<
") "<< std::endl;
239 return (pid_t)readpid;
268 WAR <<
_lockerPid <<
" is running and has a ZYpp lock. Sorry." << std::endl;
271 MIL <<
_lockerPid <<
" is dead. Ignoring the existing lock file." << std::endl;
281 if ( geteuid() != 0 )
295 if ( geteuid() != 0 )
316 ZYppGlobalLock & globalLock()
318 if ( !_theGlobalLock )
320 return *_theGlobalLock;
335 MIL <<
"ZYpp is on..." << endl;
340 _theGlobalLock.reset();
341 MIL <<
"ZYpp is off..." << endl;
379 const auto &makeLockedError = []( pid_t pid,
const std::string &lockerName ){
380 const std::string &t =
str::form(
_(
"System management is locked by the application with pid %d (%s).\n"
381 "Close this application before trying again."), pid, lockerName.c_str() );
385 ZYpp::Ptr _instance = _theZYppInstance.lock();
388 if ( geteuid() != 0 )
390 MIL <<
"Running as user. Skip creating " << globalLock().zyppLockFilePath() << std::endl;
394 MIL <<
"ZYPP_READONLY active." << endl;
396 else if ( globalLock().zyppLocked() )
401 if ( LOCK_TIMEOUT != 0 )
405 if ( LOCK_TIMEOUT > 0 ) {
406 giveup = logwait+LOCK_TIMEOUT;
407 MIL <<
"$ZYPP_LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec. Waiting for the zypp lock until " << giveup << endl;
410 MIL <<
"$ZYPP_LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec. Waiting for the zypp lock..." << endl;
419 WAR <<
"$ZYPP_LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec. Another day has passed waiting for the zypp lock..." << endl;
426 failed = globalLock().zyppLocked();
428 }
while ( failed && ( not giveup ||
Date::now() <= giveup ) );
431 MIL <<
"$ZYPP_LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec. Gave up waiting for the zypp lock." << endl;
434 MIL <<
"$ZYPP_LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec. Finally got the zypp lock." << endl;
438 ZYPP_THROW( makeLockedError( globalLock().lockerPid(), globalLock().lockerName() ));
445 _theGlobalLock.reset();
453 if ( !_theImplInstance )
455 _instance.reset(
new ZYpp( _theImplInstance ) );
456 _theZYppInstance = _instance;
465 {
return !_theZYppInstance.expired(); }
479 return str <<
"ZYppFactory";
Store and operate on date (time_t).
static const ValueType day
static Date now()
Return the current time.
Base class for Exception.
static ZConfig & instance()
Singleton ctor.
long lockTimeout() const
The number of seconds to wait for the zypp lock to become available.
ZYppFactoryException(std::string msg_r, pid_t lockerPid_r, std::string lockerName_r)
~ZYppFactoryException() override
static ZYppFactory instance()
Singleton ctor.
bool haveZYpp() const
Whether the ZYpp instance is already created.
ZYpp::Ptr getZYpp() const
ZYppFactory()
Default ctor.
static zypp::Pathname lockfileDir()
const std::string & lockerName() const
ZYppGlobalLock & operator=(const ZYppGlobalLock &)=delete
void _closeLockFile()
Use accessLockFile.
shared_ptr< void > ScopedGuard
file_lock _zyppLockFileLock
void _openLockFile()
Use accessLockFile.
bool zyppLocked()
Try to aquire a lock.
ScopedGuard accessLockFile()
Exception safe access to the lockfile.
Pathname _zyppLockFilePath
const Pathname & zyppLockFilePath() const
bool isProcessRunning(pid_t pid_r)
ZYppGlobalLock & operator=(ZYppGlobalLock &&)=delete
ZYppGlobalLock(const ZYppGlobalLock &)=delete
ZYppGlobalLock(ZYppGlobalLock &&)=delete
ZYppGlobalLock(Pathname &&lFilePath)
zypp_detail::ZYppImpl Impl
RW_pointer< Impl > _pimpl
Pointer to implementation.
shared_ptr< Impl > Impl_Ptr
ZYpp(const Impl_Ptr &impl_r)
Factory ctor.
static LogControl instance()
Singleton access.
void emergencyShutdown()
will cause the log thread to exit and flush all sockets
Wrapper class for stat/lstat.
Signal handler logging a stack trace.
static void backtraceHandler(int sig)
::sighandler_t lastSigHandler
String related utilities and Regular expression matching.
Pathname ZYPP_LOCKFILE_ROOT()
Hack to circumvent the currently poor –root support.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
std::string numstring(char n, int w=0)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
void IWantIt() ZYPP_DEPRECATED
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
std::ostream & dumpBacktrace(std::ostream &stream_r)
Dump current stack trace to a stream.
void repoVariablesReset()
Exchange LineWriter for the lifetime of this object.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.