31 #include <boost/interprocess/sync/file_lock.hpp>
32 #include <boost/interprocess/sync/scoped_lock.hpp>
33 #include <boost/interprocess/sync/sharable_lock.hpp>
35 using boost::interprocess::file_lock;
36 using boost::interprocess::scoped_lock;
37 using boost::interprocess::sharable_lock;
49 void sigsegvHandler(
int sig );
50 ::sighandler_t lastSigsegvHandler = ::signal( SIGSEGV, sigsegvHandler );
53 void sigsegvHandler(
int sig )
56 ::signal( SIGSEGV, lastSigsegvHandler );
64 {
return getenv(
"ZYPP_LOCKFILE_ROOT") ? getenv(
"ZYPP_LOCKFILE_ROOT") :
"/"; }
68 namespace zypp_readonly_hack
71 static bool active = getenv(
"ZYPP_READONLY_HACK");
76 MIL <<
"ZYPP_READONLY promised." << endl;
117 MIL <<
"Cleanned lock file. (" << getpid() <<
")" << std::endl;
194 PathInfo status( procdir );
195 MIL <<
"Checking " << status << endl;
197 if ( ! status.isDir() )
199 DBG <<
"No such process." << endl;
203 static char buffer[513];
204 buffer[0] = buffer[512] = 0;
206 if ( std::ifstream( (procdir/
"cmdline").c_str() ).read( buffer, 512 ).gcount() > 0 )
213 DBG <<
"In zombie state." << endl;
223 MIL <<
"read: Lockfile " <<
_zyppLockFilePath <<
" has pid " << readpid <<
" (our pid: " << getpid() <<
") "<< std::endl;
224 return (pid_t)readpid;
245 if ( geteuid() != 0 )
270 WAR <<
_lockerPid <<
" is running and has a ZYpp lock. Sorry." << std::endl;
275 MIL <<
_lockerPid <<
" is dead. Taking the lock file." << std::endl;
281 INT <<
"Oops! We should not be here!" << std::endl;
290 static weak_ptr<ZYpp> _theZYppInstance;
291 static scoped_ptr<ZYppGlobalLock> _theGlobalLock;
293 ZYppGlobalLock & globalLock()
295 if ( !_theGlobalLock )
296 _theGlobalLock.reset(
new ZYppGlobalLock );
297 return *_theGlobalLock;
312 MIL <<
"ZYpp is on..." << endl;
317 _theGlobalLock.reset();
318 MIL <<
"ZYpp is off..." << endl;
329 , _lockerPid( lockerPid_r )
330 , _lockerName( lockerName_r )
355 ZYpp::Ptr _instance = _theZYppInstance.lock();
358 if ( geteuid() != 0 )
360 MIL <<
"Running as user. Skip creating " << globalLock().zyppLockFilePath() << std::endl;
364 MIL <<
"ZYPP_READONLY active." << endl;
366 else if ( globalLock().zyppLocked() )
369 const long LOCK_TIMEOUT = str::strtonum<long>( getenv(
"ZYPP_LOCK_TIMEOUT" ) );
370 if ( LOCK_TIMEOUT > 0 )
372 MIL <<
"Waiting whether pid " << globalLock().lockerPid() <<
" ends within $LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec." << endl;
374 Pathname procdir(
"/proc"/
str::numstring(globalLock().lockerPid()) );
375 for (
long i = 0; i < LOCK_TIMEOUT; i += delay )
377 if ( PathInfo( procdir ).isDir() )
381 MIL <<
"Retry after " << i <<
" sec." << endl;
382 failed = globalLock().zyppLocked();
386 MIL <<
"Waiting whether pid " << globalLock().lockerPid() <<
" ends within " << (LOCK_TIMEOUT-i) <<
" sec." << endl;
387 procdir = Pathname(
"/proc"/
str::numstring(globalLock().lockerPid()) );
391 MIL <<
"Finally got the lock!" << endl;
399 std::string t =
str::form(
_(
"System management is locked by the application with pid %d (%s).\n"
400 "Close this application before trying again."),
401 globalLock().lockerPid(),
402 globalLock().lockerName().c_str()
409 if ( !_theImplInstance )
411 _instance.reset(
new ZYpp( _theImplInstance ) );
412 _theZYppInstance = _instance;
421 {
return !_theZYppInstance.expired(); }
430 return str <<
"ZYppFactory";
ScopedGuard accessLockFile()
Exception safe access to the lockfile.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
bool haveZYpp() const
Whether the ZYpp instance is already created.
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.
void IWantIt() ZYPP_DEPRECATED
virtual ~ZYppFactoryException()
Pathname ZYPP_LOCKFILE_ROOT()
Hack to circumvent the currently poor –root support.
ZYpp::Ptr getZYpp() const
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)
Pathname _zyppLockFilePath
std::ostream & operator<<(std::ostream &str, const Exception &obj)
shared_ptr< void > ScopedGuard
::boost::shared_ptr< ZYpp > Ptr
ZYppFactoryException(const std::string &msg_r, pid_t lockerPid_r, const std::string &lockerName_r)
ZYpp factory class (Singleton)
const Pathname & zyppLockFilePath() const
ZYppFactory()
Default ctor.
std::string numstring(char n, int w=0)
void _closeLockFile()
Use accessLockFile.
Base class for Exception.
bool zyppLocked()
Try to aquire a lock.
const std::string & lockerName() const
void _openLockFile()
Use accessLockFile.
void repoVariablesReset()
file_lock _zyppLockFileLock