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> 38 using boost::interprocess::file_lock;
39 using boost::interprocess::scoped_lock;
40 using boost::interprocess::sharable_lock;
52 void sigsegvHandler(
int sig );
53 ::sighandler_t lastSigsegvHandler = ::signal( SIGSEGV, sigsegvHandler );
56 void sigsegvHandler(
int sig )
60 ::signal( SIGSEGV, lastSigsegvHandler );
68 {
return getenv(
"ZYPP_LOCKFILE_ROOT") ? getenv(
"ZYPP_LOCKFILE_ROOT") :
"/"; }
72 namespace zypp_readonly_hack
75 static bool active = getenv(
"ZYPP_READONLY_HACK");
80 MIL <<
"ZYPP_READONLY promised." << endl;
121 MIL <<
"Cleaned lock file. (" << getpid() <<
")" << std::endl;
199 MIL <<
"Checking " << status << endl;
201 if ( ! status.
isDir() )
203 DBG <<
"No such process." << endl;
207 static char buffer[513];
208 buffer[0] = buffer[512] = 0;
210 if ( std::ifstream( (procdir/
"cmdline").c_str() ).read( buffer, 512 ).gcount() > 0 )
217 DBG <<
"In zombie state." << endl;
227 MIL <<
"read: Lockfile " <<
_zyppLockFilePath <<
" has pid " << readpid <<
" (our pid: " << getpid() <<
") "<< std::endl;
228 return (pid_t)readpid;
249 if ( geteuid() != 0 )
274 WAR <<
_lockerPid <<
" is running and has a ZYpp lock. Sorry." << std::endl;
279 MIL <<
_lockerPid <<
" is dead. Taking the lock file." << std::endl;
285 INT <<
"Oops! We should not be here!" << std::endl;
297 ZYppGlobalLock & globalLock()
299 if ( !_theGlobalLock )
300 _theGlobalLock.reset(
new ZYppGlobalLock );
301 return *_theGlobalLock;
316 MIL <<
"ZYpp is on..." << endl;
321 _theGlobalLock.reset();
322 MIL <<
"ZYpp is off..." << endl;
333 , _lockerPid( lockerPid_r )
334 , _lockerName( lockerName_r )
359 ZYpp::Ptr _instance = _theZYppInstance.lock();
362 if ( geteuid() != 0 )
364 MIL <<
"Running as user. Skip creating " << globalLock().zyppLockFilePath() << std::endl;
368 MIL <<
"ZYPP_READONLY active." << endl;
370 else if ( globalLock().zyppLocked() )
373 const long LOCK_TIMEOUT = str::strtonum<long>( getenv(
"ZYPP_LOCK_TIMEOUT" ) );
374 if ( LOCK_TIMEOUT > 0 )
376 MIL <<
"Waiting whether pid " << globalLock().lockerPid() <<
" ends within $LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec." << endl;
379 for (
long i = 0; i < LOCK_TIMEOUT; i += delay )
385 MIL <<
"Retry after " << i <<
" sec." << endl;
386 failed = globalLock().zyppLocked();
390 MIL <<
"Waiting whether pid " << globalLock().lockerPid() <<
" ends within " << (LOCK_TIMEOUT-i) <<
" sec." << endl;
395 MIL <<
"Finally got the lock!" << endl;
403 std::string t =
str::form(
_(
"System management is locked by the application with pid %d (%s).\n" 404 "Close this application before trying again."),
405 globalLock().lockerPid(),
406 globalLock().lockerName().c_str()
413 if ( !_theImplInstance )
415 _instance.reset(
new ZYpp( _theImplInstance ) );
416 _theZYppInstance = _instance;
425 {
return !_theZYppInstance.expired(); }
434 return str <<
"ZYppFactory";
ScopedGuard accessLockFile()
Exception safe access to the lockfile.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
std::ostream & dumpBacktrace(std::ostream &stream_r)
Dump current stack trace to a stream.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
bool haveZYpp() const
Whether the ZYpp instance is already created.
void IWantIt() ZYPP_DEPRECATED
virtual ~ZYppFactoryException()
const char * c_str() const
String representation.
const std::string & lockerName() const
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Pathname ZYPP_LOCKFILE_ROOT()
Hack to circumvent the currently poor –root support.
shared_ptr< Impl > Impl_Ptr
static ZYppFactory instance()
Singleton ctor.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
ZYpp(const Impl_Ptr &impl_r)
Factory ctor.
bool isProcessRunning(pid_t pid_r)
static LogControl instance()
Singleton access.
Pathname _zyppLockFilePath
const std::string & asString() const
String representation.
shared_ptr< void > ScopedGuard
Pathname dirname() const
Return all but the last component od this path.
::boost::shared_ptr< ZYpp > Ptr
ZYpp::Ptr getZYpp() const
ZYppFactoryException(const std::string &msg_r, pid_t lockerPid_r, const std::string &lockerName_r)
ZYpp factory class (Singleton)
ZYppFactory()
Default ctor.
const Pathname & zyppLockFilePath() const
std::string numstring(char n, int w=0)
void _closeLockFile()
Use accessLockFile.
Base class for Exception.
Wrapper class for ::stat/::lstat.
bool zyppLocked()
Try to aquire a lock.
void _openLockFile()
Use accessLockFile.
Easy-to use interface to the ZYPP dependency resolver.
void emergencyShutdown()
will cause the log thread to exit and flush all sockets
void repoVariablesReset()
file_lock _zyppLockFileLock