1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ZYppFactory.cc
20 #include "zypp/base/Logger.h"
21 #include "zypp/base/Gettext.h"
22 #include "zypp/base/IOStream.h"
23 #include "zypp/base/Functional.h"
24 #include "zypp/base/Backtrace.h"
25 #include "zypp/PathInfo.h"
27 #include "zypp/ZYppFactory.h"
28 #include "zypp/zypp_detail/ZYppImpl.h"
29 #include "zypp/zypp_detail/ZYppReadOnlyHack.h"
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;
41 namespace zyppintern { void repoVariablesReset(); } // upon re-acquiring the lock...
43 ///////////////////////////////////////////////////////////////////
45 { /////////////////////////////////////////////////////////////////
49 void sigsegvHandler( int sig );
50 ::sighandler_t lastSigsegvHandler = ::signal( SIGSEGV, sigsegvHandler );
52 /** SIGSEGV handler to log stack trace */
53 void sigsegvHandler( int sig )
55 INT << "Error: signal " << sig << endl << dumpBacktrace << endl;
56 ::signal( SIGSEGV, lastSigsegvHandler );
62 /** Hack to circumvent the currently poor --root support. */
63 inline Pathname ZYPP_LOCKFILE_ROOT()
64 { return getenv("ZYPP_LOCKFILE_ROOT") ? getenv("ZYPP_LOCKFILE_ROOT") : "/"; }
67 ///////////////////////////////////////////////////////////////////
68 namespace zypp_readonly_hack
69 { /////////////////////////////////////////////////////////////////
71 static bool active = getenv("ZYPP_READONLY_HACK");
76 MIL << "ZYPP_READONLY promised." << endl;
84 /////////////////////////////////////////////////////////////////
85 } // namespace zypp_readonly_hack
86 ///////////////////////////////////////////////////////////////////
88 ///////////////////////////////////////////////////////////////////
89 /// \class ZYppGlobalLock
90 /// \brief Our broken global lock
92 ///////////////////////////////////////////////////////////////////
97 : _zyppLockFilePath( env::ZYPP_LOCKFILE_ROOT() / "/var/run/zypp.pid" )
98 , _zyppLockFile( NULL )
100 , _cleanLock( false )
102 filesystem::assert_dir(_zyppLockFilePath.dirname() );
109 // Exception safe access to the lockfile.
110 ScopedGuard closeOnReturn( accessLockFile() );
112 scoped_lock<file_lock> flock( _zyppLockFileLock ); // aquire write lock
113 // Truncate the file rather than deleting it. Other processes may
114 // still use it to synchronsize.
115 ftruncate( fileno(_zyppLockFile), 0 );
117 MIL << "Cleanned lock file. (" << getpid() << ")" << std::endl;
119 catch(...) {} // let no exception escape.
122 pid_t lockerPid() const
123 { return _lockerPid; }
125 const std::string & lockerName() const
126 { return _lockerName; }
128 const Pathname & zyppLockFilePath() const
129 { return _zyppLockFilePath; }
133 Pathname _zyppLockFilePath;
134 file_lock _zyppLockFileLock;
135 FILE * _zyppLockFile;
138 std::string _lockerName;
142 typedef shared_ptr<void> ScopedGuard;
144 /** Exception safe access to the lockfile.
146 * // Exception safe access to the lockfile.
147 * ScopedGuard closeOnReturn( accessLockFile() );
150 ScopedGuard accessLockFile()
153 return ScopedGuard( static_cast<void*>(0),
154 bind( mem_fun_ref( &ZYppGlobalLock::_closeLockFile ), ref(*this) ) );
157 /** Use \ref accessLockFile. */
160 if ( _zyppLockFile != NULL )
163 // open pid file rw so we are sure it exist when creating the flock
164 _zyppLockFile = fopen( _zyppLockFilePath.c_str(), "a+" );
165 if ( _zyppLockFile == NULL )
166 ZYPP_THROW( Exception( "Cant open " + _zyppLockFilePath.asString() ) );
167 _zyppLockFileLock = _zyppLockFilePath.c_str();
168 MIL << "Open lockfile " << _zyppLockFilePath << endl;
171 /** Use \ref accessLockFile. */
172 void _closeLockFile()
174 if ( _zyppLockFile == NULL )
177 clearerr( _zyppLockFile );
178 fflush( _zyppLockFile );
179 // http://www.boost.org/doc/libs/1_50_0/doc/html/interprocess/synchronization_mechanisms.html
180 // If you are using a std::fstream/native file handle to write to the file
181 // while using file locks on that file, don't close the file before releasing
182 // all the locks of the file.
183 _zyppLockFileLock = file_lock();
184 fclose( _zyppLockFile );
185 _zyppLockFile = NULL;
186 MIL << "Close lockfile " << _zyppLockFilePath << endl;
190 bool isProcessRunning( pid_t pid_r )
192 // it is another program, not me, see if it is still running
193 Pathname procdir( "/proc"/str::numstring(pid_r) );
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;
205 // man proc(5): /proc/[pid]/cmdline is empty if zombie.
206 if ( std::ifstream( (procdir/"cmdline").c_str() ).read( buffer, 512 ).gcount() > 0 )
208 _lockerName = buffer;
209 DBG << "Is running: " << _lockerName << endl;
213 DBG << "In zombie state." << endl;
219 clearerr( _zyppLockFile );
220 fseek( _zyppLockFile, 0, SEEK_SET );
222 fscanf( _zyppLockFile, "%ld", &readpid );
223 MIL << "read: Lockfile " << _zyppLockFilePath << " has pid " << readpid << " (our pid: " << getpid() << ") "<< std::endl;
224 return (pid_t)readpid;
229 clearerr( _zyppLockFile );
230 fseek( _zyppLockFile, 0, SEEK_SET );
231 ftruncate( fileno(_zyppLockFile), 0 );
232 fprintf(_zyppLockFile, "%ld\n", (long)getpid() );
233 fflush( _zyppLockFile );
234 _cleanLock = true; // cleanup on exit
235 MIL << "write: Lockfile " << _zyppLockFilePath << " got pid " << getpid() << std::endl;
240 /** Try to aquire a lock.
241 * \return \c true if zypp is already locked by another process.
245 if ( geteuid() != 0 )
246 return false; // no lock as non-root
248 // Exception safe access to the lockfile.
249 ScopedGuard closeOnReturn( accessLockFile() );
251 scoped_lock<file_lock> flock( _zyppLockFileLock ); // aquire write lock
253 _lockerPid = readLockFile();
254 if ( _lockerPid == 0 )
256 // no or empty lock file
260 else if ( _lockerPid == getpid() )
267 // a foreign pid in lock
268 if ( isProcessRunning( _lockerPid ) )
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;
287 ///////////////////////////////////////////////////////////////////
290 static weak_ptr<ZYpp> _theZYppInstance;
291 static scoped_ptr<ZYppGlobalLock> _theGlobalLock; // on/off in sync with _theZYppInstance
293 ZYppGlobalLock & globalLock()
295 if ( !_theGlobalLock )
296 _theGlobalLock.reset( new ZYppGlobalLock );
297 return *_theGlobalLock;
300 ///////////////////////////////////////////////////////////////////
302 ///////////////////////////////////////////////////////////////////
306 ///////////////////////////////////////////////////////////////////
308 ZYpp::ZYpp( const Impl_Ptr & impl_r )
311 ::zyppintern::repoVariablesReset(); // upon re-acquiring the lock...
312 MIL << "ZYpp is on..." << endl;
317 _theGlobalLock.reset();
318 MIL << "ZYpp is off..." << endl;
321 ///////////////////////////////////////////////////////////////////
323 // CLASS NAME : ZYppFactoryException
325 ///////////////////////////////////////////////////////////////////
327 ZYppFactoryException::ZYppFactoryException( const std::string & msg_r, pid_t lockerPid_r, const std::string & lockerName_r )
329 , _lockerPid( lockerPid_r )
330 , _lockerName( lockerName_r )
333 ZYppFactoryException::~ZYppFactoryException() throw ()
336 ///////////////////////////////////////////////////////////////////
338 // CLASS NAME : ZYppFactory
340 ///////////////////////////////////////////////////////////////////
342 ZYppFactory ZYppFactory::instance()
343 { return ZYppFactory(); }
345 ZYppFactory::ZYppFactory()
348 ZYppFactory::~ZYppFactory()
351 ///////////////////////////////////////////////////////////////////
353 ZYpp::Ptr ZYppFactory::getZYpp() const
355 ZYpp::Ptr _instance = _theZYppInstance.lock();
358 if ( geteuid() != 0 )
360 MIL << "Running as user. Skip creating " << globalLock().zyppLockFilePath() << std::endl;
362 else if ( zypp_readonly_hack::active )
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() ) // wait for /proc/pid to disapear
381 MIL << "Retry after " << i << " sec." << endl;
382 failed = globalLock().zyppLocked();
385 // another proc locked faster. maybe it ends fast as well....
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()
404 ZYPP_THROW(ZYppFactoryException(t, globalLock().lockerPid(), globalLock().lockerName() ));
408 static ZYpp::Impl_Ptr _theImplInstance; // for now created once
409 if ( !_theImplInstance )
410 _theImplInstance.reset( new ZYpp::Impl );
411 _instance.reset( new ZYpp( _theImplInstance ) );
412 _theZYppInstance = _instance;
418 ///////////////////////////////////////////////////////////////////
420 bool ZYppFactory::haveZYpp() const
421 { return !_theZYppInstance.expired(); }
423 /******************************************************************
425 ** FUNCTION NAME : operator<<
426 ** FUNCTION TYPE : std::ostream &
428 std::ostream & operator<<( std::ostream & str, const ZYppFactory & obj )
430 return str << "ZYppFactory";
433 /////////////////////////////////////////////////////////////////
435 ///////////////////////////////////////////////////////////////////