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;
67 template class SigBacktraceHandler<SIGSEGV>;
68 template class SigBacktraceHandler<SIGABRT>;
75 {
return getenv(
"ZYPP_LOCKFILE_ROOT") ? getenv(
"ZYPP_LOCKFILE_ROOT") :
"/"; }
79 namespace zypp_readonly_hack
82 static bool active = getenv(
"ZYPP_READONLY_HACK");
87 MIL <<
"ZYPP_READONLY promised." << endl;
108 : _zyppLockFilePath(
std::move(lFilePath) )
109 , _zyppLockFile( NULL )
111 , _cleanLock( false )
123 scoped_lock<file_lock> flock( _zyppLockFileLock );
126 ftruncate( fileno(_zyppLockFile), 0 );
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 )
188 clearerr( _zyppLockFile );
189 fflush( _zyppLockFile );
194 _zyppLockFileLock = file_lock();
195 fclose( _zyppLockFile );
196 _zyppLockFile = NULL;
197 MIL <<
"Close lockfile " << _zyppLockFilePath << endl;
206 MIL <<
"Checking " << status << endl;
208 if ( ! status.
isDir() )
210 DBG <<
"No such process." << endl;
214 static char buffer[513];
215 buffer[0] = buffer[512] = 0;
217 if ( std::ifstream( (procdir/
"cmdline").c_str() ).read( buffer, 512 ).gcount() > 0 )
219 _lockerName = buffer;
220 DBG <<
"Is running: " << _lockerName << endl;
224 DBG <<
"In zombie state." << endl;
230 clearerr( _zyppLockFile );
231 fseek( _zyppLockFile, 0, SEEK_SET );
233 fscanf( _zyppLockFile,
"%ld", &readpid );
234 MIL <<
"read: Lockfile " << _zyppLockFilePath <<
" has pid " << readpid <<
" (our pid: " << getpid() <<
") "<< std::endl;
235 return (pid_t)readpid;
240 clearerr( _zyppLockFile );
241 fseek( _zyppLockFile, 0, SEEK_SET );
242 ftruncate( fileno(_zyppLockFile), 0 );
243 fprintf(_zyppLockFile,
"%ld\n", (
long)getpid() );
244 fflush( _zyppLockFile );
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;
277 if ( geteuid() != 0 )
282 scoped_lock<file_lock> flock( _zyppLockFileLock );
283 return safeCheckIsLocked ();
291 if ( geteuid() != 0 )
296 scoped_lock<file_lock> flock( _zyppLockFileLock );
297 if ( !safeCheckIsLocked() ) {
312 ZYppGlobalLock & globalLock()
314 if ( !_theGlobalLock )
316 return *_theGlobalLock;
331 MIL <<
"ZYpp is on..." << endl;
336 _theGlobalLock.reset();
337 MIL <<
"ZYpp is off..." << endl;
348 , _lockerPid( lockerPid_r )
349 , _lockerName( lockerName_r )
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() );
381 ZYpp::Ptr _instance = _theZYppInstance.lock();
384 if ( geteuid() != 0 )
386 MIL <<
"Running as user. Skip creating " << globalLock().zyppLockFilePath() << std::endl;
390 MIL <<
"ZYPP_READONLY active." << endl;
392 else if ( globalLock().zyppLocked() )
396 const long LOCK_TIMEOUT = str::strtonum<long>( getenv(
"ZYPP_LOCK_TIMEOUT" ) );
397 if ( LOCK_TIMEOUT != 0 )
401 if ( LOCK_TIMEOUT > 0 ) {
402 giveup = logwait+LOCK_TIMEOUT;
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;
422 failed = globalLock().zyppLocked();
424 }
while ( failed && ( not giveup ||
Date::now() <= giveup ) );
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;
434 ZYPP_THROW( makeLockedError( globalLock().lockerPid(), globalLock().lockerName() ));
441 _theGlobalLock.reset();
449 if ( !_theImplInstance )
451 _instance.reset(
new ZYpp( _theImplInstance ) );
452 _theZYppInstance = _instance;
461 {
return !_theZYppInstance.expired(); }
475 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.
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
void emergencyShutdown()
will cause the log thread to exit and flush all sockets
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.