29 #include <boost/interprocess/sync/file_lock.hpp>
30 #include <boost/interprocess/sync/scoped_lock.hpp>
31 #include <boost/interprocess/sync/sharable_lock.hpp>
33 using boost::interprocess::file_lock;
34 using boost::interprocess::scoped_lock;
35 using boost::interprocess::sharable_lock;
47 {
return getenv(
"ZYPP_LOCKFILE_ROOT") ? getenv(
"ZYPP_LOCKFILE_ROOT") :
"/"; }
51 namespace zypp_readonly_hack
59 MIL <<
"ZYPP_READONLY promised." << endl;
100 MIL <<
"Cleanned lock file. (" << getpid() <<
")" << std::endl;
177 PathInfo status( procdir );
178 MIL <<
"Checking " << status << endl;
180 if ( ! status.isDir() )
182 DBG <<
"No such process." << endl;
186 static char buffer[513];
187 buffer[0] = buffer[512] = 0;
189 if ( std::ifstream( (procdir/
"cmdline").c_str() ).read( buffer, 512 ).gcount() > 0 )
196 DBG <<
"In zombie state." << endl;
206 MIL <<
"read: Lockfile " <<
_zyppLockFilePath <<
" has pid " << readpid <<
" (our pid: " << getpid() <<
") "<< std::endl;
207 return (pid_t)readpid;
228 if ( geteuid() != 0 )
253 WAR <<
_lockerPid <<
" is running and has a ZYpp lock. Sorry." << std::endl;
258 MIL <<
_lockerPid <<
" is dead. Taking the lock file." << std::endl;
264 INT <<
"Oops! We should not be here!" << std::endl;
272 static ZYppGlobalLock & globalLock()
274 static ZYppGlobalLock lock;
277 bool _haveZYpp =
false;
288 , _lockerPid( lockerPid_r )
289 , _lockerName( lockerName_r )
337 if ( geteuid() != 0 )
339 MIL <<
"Running as user. Skip creating " << globalLock().zyppLockFilePath() << std::endl;
343 MIL <<
"ZYPP_READONLY active." << endl;
345 else if ( globalLock().zyppLocked() )
348 const long LOCK_TIMEOUT = str::strtonum<long>( getenv(
"ZYPP_LOCK_TIMEOUT" ) );
349 if ( LOCK_TIMEOUT > 0 )
351 MIL <<
"Waiting whether pid " << globalLock().lockerPid() <<
" ends within $LOCK_TIMEOUT=" << LOCK_TIMEOUT <<
" sec." << endl;
353 Pathname procdir(
"/proc"/
str::numstring(globalLock().lockerPid()) );
354 for (
long i = 0; i < LOCK_TIMEOUT; i += delay )
356 if ( PathInfo( procdir ).isDir() )
360 MIL <<
"Retry after " << i <<
" sec." << endl;
361 failed = globalLock().zyppLocked();
365 MIL <<
"Waiting whether pid " << globalLock().lockerPid() <<
" ends within " << (LOCK_TIMEOUT-i) <<
" sec." << endl;
366 procdir = Pathname(
"/proc"/
str::numstring(globalLock().lockerPid()) );
370 MIL <<
"Finally got the lock!" << endl;
379 std::string t =
str::form(
_(
"System management is locked by the application with pid %d (%s).\n"
380 "Close this application before trying again."),
381 globalLock().lockerPid(),
382 globalLock().lockerName().c_str()
399 {
return _haveZYpp; }
408 return str <<
"ZYppFactory";