20#include <zypp/base/Logger.h>
21#include <zypp/base/LogControl.h>
22#include <zypp/base/Gettext.h>
23#include <zypp/base/IOStream.h>
26#include <zypp/base/LogControl.h>
27#include <zypp/PathInfo.h>
32#include <boost/interprocess/sync/file_lock.hpp>
33#include <boost/interprocess/sync/scoped_lock.hpp>
34#include <boost/interprocess/sync/sharable_lock.hpp>
38using boost::interprocess::file_lock;
39using boost::interprocess::scoped_lock;
40using boost::interprocess::sharable_lock;
75 {
return getenv(
"ZYPP_LOCKFILE_ROOT") ?
getenv(
"ZYPP_LOCKFILE_ROOT") :
"/"; }
79 namespace zypp_readonly_hack
87 MIL <<
"ZYPP_READONLY promised." << endl;
109 , _zyppLockFile(
NULL )
111 , _cleanLock(
false )
128 MIL <<
"Cleaned lock file. (" << getpid() <<
")" << std::endl;
134 {
return _lockerPid; }
137 {
return _lockerName; }
140 {
return _zyppLockFilePath; }
171 if ( _zyppLockFile !=
NULL )
175 _zyppLockFile =
fopen( _zyppLockFilePath.
c_str(),
"a+" );
176 if ( _zyppLockFile ==
NULL )
178 _zyppLockFileLock = _zyppLockFilePath.
c_str();
179 MIL <<
"Open lockfile " << _zyppLockFilePath << endl;
185 if ( _zyppLockFile ==
NULL )
194 _zyppLockFileLock = file_lock();
196 _zyppLockFile =
NULL;
197 MIL <<
"Close lockfile " << _zyppLockFilePath << endl;
206 MIL <<
"Checking " << status << endl;
208 if ( ! status.
isDir() )
210 DBG <<
"No such process." << endl;
220 DBG <<
"Is running: " << _lockerName << endl;
224 DBG <<
"In zombie state." << endl;
234 MIL <<
"read: Lockfile " << _zyppLockFilePath <<
" has pid " <<
readpid <<
" (our pid: " << getpid() <<
") "<< std::endl;
243 fprintf(_zyppLockFile,
"%ld\n", (
long)getpid() );
246 MIL <<
"write: Lockfile " << _zyppLockFilePath <<
" got pid " << getpid() << std::endl;
254 _lockerPid = readLockFile();
255 if ( _lockerPid == 0 ) {
258 }
else if ( _lockerPid == getpid() ) {
263 if ( isProcessRunning( _lockerPid ) ) {
264 WAR << _lockerPid <<
" is running and has a ZYpp lock. Sorry." << std::endl;
267 MIL << _lockerPid <<
" is dead. Ignoring the existing lock file." << std::endl;
283 return safeCheckIsLocked ();
297 if ( !safeCheckIsLocked() ) {
312 ZYppGlobalLock & globalLock()
314 if ( !_theGlobalLock )
316 return *_theGlobalLock;
331 MIL <<
"ZYpp is on..." << endl;
337 MIL <<
"ZYpp is off..." << endl;
375 const auto &
makeLockedError = []( pid_t pid,
const std::string &lockerName ){
376 const std::string &
t =
str::form(
_(
"System management is locked by the application with pid %d (%s).\n"
377 "Close this application before trying again."), pid, lockerName.c_str() );
386 MIL <<
"Running as user. Skip creating " <<
globalLock().zyppLockFilePath() << std::endl;
390 MIL <<
"ZYPP_READONLY active." << endl;
403 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Waiting for the zypp lock until " <<
giveup << endl;
406 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Waiting for the zypp lock..." << endl;
415 WAR <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Another day has passed waiting for the zypp lock..." << endl;
427 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Gave up waiting for the zypp lock." << endl;
430 MIL <<
"$ZYPP_LOCK_TIMEOUT=" <<
LOCK_TIMEOUT <<
" sec. Finally got the zypp lock." << endl;
475 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(const std::string &msg_r, pid_t lockerPid_r, const std::string &lockerName_r)
virtual ~ZYppFactoryException()
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
void _closeLockFile()
Use accessLockFile.
file_lock _zyppLockFileLock
void _openLockFile()
Use accessLockFile.
shared_ptr< void > ScopedGuard
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(Pathname &&lFilePath)
shared_ptr< Impl > Impl_Ptr
ZYpp(const Impl_Ptr &impl_r)
Factory ctor.
::boost::shared_ptr< ZYpp > Ptr
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.
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.