kabc
lock.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "lock.h"
00022
00023 #include <krandom.h>
00024 #include <kcomponentdata.h>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <kstandarddirs.h>
00028
00029 #include <QtCore/QFile>
00030 #include <QtCore/QTextStream>
00031
00032 #include <errno.h>
00033 #include <signal.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <unistd.h>
00037
00038 using namespace KABC;
00039
00040 class Lock::Private
00041 {
00042 public:
00043 Private( const QString &identifier )
00044 : mIdentifier( identifier ),
00045 mOrigIdentifier( identifier )
00046 {
00047 mIdentifier.replace( '/', '_' );
00048 #ifdef Q_WS_WIN
00049 mIdentifier.replace( ':', '_' );
00050 #endif
00051 }
00052
00053 QString mIdentifier;
00054 QString mOrigIdentifier;
00055 QString mLockUniqueName;
00056 QString mError;
00057 };
00058
00059 Lock::Lock( const QString &identifier )
00060 : d( new Private( identifier ) )
00061 {
00062 }
00063
00064 Lock::~Lock()
00065 {
00066 unlock();
00067
00068 delete d;
00069 }
00070
00071 QString Lock::locksDir()
00072 {
00073 return KStandardDirs::locateLocal( "data", "kabc/lock/" );
00074 }
00075
00076 bool Lock::readLockFile( const QString &filename, int &pid, QString &app )
00077 {
00078 QFile file( filename );
00079 if ( !file.open( QIODevice::ReadOnly ) ) {
00080 return false;
00081 }
00082
00083 QTextStream t( &file );
00084 t >> pid >> ws >> app;
00085
00086 return true;
00087 }
00088
00089 bool Lock::writeLockFile( const QString &filename )
00090 {
00091 QFile file( filename );
00092 if ( !file.open( QIODevice::WriteOnly ) ) {
00093 return false;
00094 }
00095
00096 QTextStream t( &file );
00097 t << ::getpid() << endl << QString( KGlobal::mainComponent().componentName() );
00098
00099 return true;
00100 }
00101
00102 QString Lock::lockFileName() const
00103 {
00104 return locksDir() + d->mIdentifier + ".lock";
00105 }
00106
00107 bool Lock::lock()
00108 {
00109 QString lockName = lockFileName();
00110 kDebug() << "-- lock name:" << lockName;
00111
00112 if ( QFile::exists( lockName ) ) {
00113 int pid;
00114 QString app;
00115
00116 if ( !readLockFile( lockFileName(), pid, app ) ) {
00117 d->mError = i18n( "Unable to open lock file." );
00118 return false;
00119 }
00120
00121 int retval = ::kill( pid, 0 );
00122 if ( retval == -1 && errno == ESRCH ) {
00123 QFile::remove( lockName );
00124 kWarning() << "Removed stale lock file from process '" << app << "'";
00125 } else {
00126 d->mError = i18n( "The resource '%1' is locked by application '%2'.",
00127 d->mOrigIdentifier, app );
00128 return false;
00129 }
00130 }
00131
00132 QString lockUniqueName;
00133 lockUniqueName = d->mIdentifier + KRandom::randomString( 8 );
00134 d->mLockUniqueName = KStandardDirs::locateLocal( "data", "kabc/lock/" + lockUniqueName );
00135 kDebug() << "-- lock unique name:" << d->mLockUniqueName;
00136
00137
00138 writeLockFile( d->mLockUniqueName );
00139
00140
00141 int result = ::link( QFile::encodeName( d->mLockUniqueName ),
00142 QFile::encodeName( lockName ) );
00143
00144 if ( result == 0 ) {
00145 d->mError.clear();
00146 emit locked();
00147 return true;
00148 }
00149
00150
00151
00152 d->mError = i18n( "Error" );
00153 return false;
00154 }
00155
00156 bool Lock::unlock()
00157 {
00158 int pid;
00159 QString app;
00160 if ( readLockFile( lockFileName(), pid, app ) ) {
00161 if ( pid == getpid() ) {
00162 QFile::remove( lockFileName() );
00163 QFile::remove( d->mLockUniqueName );
00164 emit unlocked();
00165 } else {
00166 d->mError = i18n( "Unlock failed. Lock file is owned by other process: %1 (%2)", app, pid );
00167 kDebug() << d->mError;
00168 return false;
00169 }
00170 }
00171
00172 d->mError.clear();
00173
00174 return true;
00175 }
00176
00177 QString Lock::error() const
00178 {
00179 return d->mError;
00180 }
00181
00182 #include "lock.moc"