32#include <boost/interprocess/sync/file_lock.hpp>
33#include <boost/interprocess/sync/scoped_lock.hpp>
34#include <boost/interprocess/sync/sharable_lock.hpp>
39using boost::interprocess::file_lock;
40using boost::interprocess::scoped_lock;
41using boost::interprocess::sharable_lock;
76 {
return getenv(
"ZYPP_LOCKFILE_ROOT") ?
getenv(
"ZYPP_LOCKFILE_ROOT") :
"/"; }
80 namespace zypp_readonly_hack
88 MIL <<
"ZYPP_READONLY promised." << endl;
110 _lockerPid(0), _cleanLock(
false) {
131 MIL <<
"Cleaned lock file. (" << getpid() <<
")" << std::endl;
137 {
return _lockerPid; }
140 {
return _lockerName; }
143 {
return _zyppLockFilePath; }
174 if ( _zyppLockFile !=
NULL )
178 _zyppLockFile =
fopen( _zyppLockFilePath.
c_str(),
"a+" );
179 if ( _zyppLockFile ==
NULL )
181 _zyppLockFileLock = _zyppLockFilePath.
c_str();
182 MIL <<
"Open lockfile " << _zyppLockFilePath << endl;
188 if ( _zyppLockFile ==
NULL )
197 _zyppLockFileLock = file_lock();
199 _zyppLockFile =
NULL;
200 MIL <<
"Close lockfile " << _zyppLockFilePath << endl;
209 MIL <<
"Checking " << status << endl;
211 if ( ! status.
isDir() )
213 DBG <<
"No such process." << endl;
223 DBG <<
"Is running: " << _lockerName << endl;
227 DBG <<
"In zombie state." << endl;
237 MIL <<
"read: Lockfile " << _zyppLockFilePath <<
" has pid " <<
readpid <<
" (our pid: " << getpid() <<
") "<< std::endl;
246 fprintf(_zyppLockFile,
"%ld\n", (
long)getpid() );
249 MIL <<
"write: Lockfile " << _zyppLockFilePath <<
" got pid " << getpid() << std::endl;
257 _lockerPid = readLockFile();
258 if ( _lockerPid == 0 ) {
261 }
else if ( _lockerPid == getpid() ) {
266 if ( isProcessRunning( _lockerPid ) ) {
267 WAR << _lockerPid <<
" is running and has a ZYpp lock. Sorry." << std::endl;
270 MIL << _lockerPid <<
" is dead. Ignoring the existing lock file." << std::endl;
286 return safeCheckIsLocked ();
300 if ( !safeCheckIsLocked() ) {
315 ZYppGlobalLock & globalLock()
317 if ( !_theGlobalLock )
319 return *_theGlobalLock;
334 MIL <<
"ZYpp is on..." << endl;
340 MIL <<
"ZYpp is off..." << endl;
378 const auto &
makeLockedError = []( pid_t pid,
const std::string &lockerName ){
379 const std::string &
t =
str::form(
_(
"System management is locked by the application with pid %d (%s).\n"
380 "Close this application before trying again."), pid, lockerName.c_str() );
389 MIL <<
"Running as user. Skip creating " <<
globalLock().zyppLockFilePath() << std::endl;
393 MIL <<
"ZYPP_READONLY active." << endl;
406 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Waiting for the zypp lock until " <<
giveup << endl;
409 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Waiting for the zypp lock..." << endl;
418 WAR <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Another day has passed waiting for the zypp lock..." << endl;
430 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Gave up waiting for the zypp lock." << endl;
433 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Finally got the zypp lock." << endl;
478 return str <<
"ZYppFactory";
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
void reset()
Reset to default Ctor values.
Store and operate on date (time_t).
static const ValueType day
static Date now()
Return the current time.
Base class for Exception.
ZYppFactoryException(std::string msg_r, pid_t lockerPid_r, std::string lockerName_r)
~ZYppFactoryException() override
ZYpp factory class (Singleton)
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)
shared_ptr< Impl > Impl_Ptr
::boost::shared_ptr< ZYpp > Ptr
ZYpp(const Impl_Ptr &impl_r)
Factory ctor.
static LogControl instance()
Singleton access.
Wrapper class for stat/lstat.
const char * c_str() const
String representation.
const std::string & asString() const
String representation.
Signal handler logging a stack trace.
static void backtraceHandler(int sig)
::sighandler_t lastSigHandler
String related utilities and Regular expression matching.
Namespace intended to collect all environment variables we use.
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 & dumpBacktrace(std::ostream &stream_r)
Dump current stack trace to a stream.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
void repoVariablesReset()
Exchange LineWriter for the lifetime of this object.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.