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 <kapplication.h>
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <kstandarddirs.h>
00027 #include <ktempfile.h>
00028
00029 #include <qfile.h>
00030
00031 #include <signal.h>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <unistd.h>
00035
00036 using namespace KABC;
00037
00038 Lock::Lock( const QString &identifier )
00039 : mIdentifier( identifier )
00040 {
00041 mIdentifier.replace( "/", "_" );
00042 }
00043
00044 Lock::~Lock()
00045 {
00046 unlock();
00047 }
00048
00049 QString Lock::locksDir()
00050 {
00051 return locateLocal( "data", "kabc/lock/" );
00052 }
00053
00054 bool Lock::readLockFile( const QString &filename, int &pid, QString &app )
00055 {
00056 QFile file( filename );
00057 if ( !file.open( IO_ReadOnly ) ) return false;
00058
00059 QTextStream t( &file );
00060 t >> pid >> app;
00061
00062 return true;
00063 }
00064
00065 bool Lock::writeLockFile( const QString &filename )
00066 {
00067 QFile file( filename );
00068 if ( !file.open( IO_WriteOnly ) ) return false;
00069 QTextStream t( &file );
00070 t << ::getpid() << endl << QString( KGlobal::instance()->instanceName() );
00071
00072 return true;
00073 }
00074
00075 QString Lock::lockFileName() const
00076 {
00077 return locksDir() + mIdentifier + ".lock";
00078 }
00079
00080 bool Lock::lock()
00081 {
00082 kdDebug(5700) << "Lock::lock()" << endl;
00083
00084 QString lockName = lockFileName();
00085 kdDebug(5700) << "-- lock name: " << lockName << endl;
00086
00087 if ( QFile::exists( lockName ) ) {
00088 int pid;
00089 QString app;
00090
00091 if ( !readLockFile( lockFileName(), pid, app ) ) {
00092 mError = i18n("Unable to open lock file.");
00093 return false;
00094 }
00095
00096 int retval = ::kill( pid, 0 );
00097 if ( retval == -1 && errno == ESRCH ) {
00098 QFile::remove( lockName );
00099 kdWarning(5700) << "Removed stale lock file from process '" << app << "'"
00100 << endl;
00101 } else {
00102 mError = i18n("The resource '%1' is locked by application '%2'.")
00103 .arg( mIdentifier ).arg( app );
00104 return false;
00105 }
00106 }
00107
00108 QString lockUniqueName;
00109 lockUniqueName = mIdentifier + kapp->randomString( 8 );
00110 mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
00111 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
00112
00113
00114 writeLockFile( mLockUniqueName );
00115
00116
00117 int result = ::link( QFile::encodeName( mLockUniqueName ),
00118 QFile::encodeName( lockName ) );
00119
00120 if ( result == 0 ) {
00121 mError = "";
00122 emit locked();
00123 return true;
00124 }
00125
00126
00127
00128 mError = i18n("Error");
00129 return false;
00130 }
00131
00132 bool Lock::unlock()
00133 {
00134 int pid;
00135 QString app;
00136 if ( readLockFile( lockFileName(), pid, app ) ) {
00137 if ( pid == getpid() ) {
00138 QFile::remove( lockFileName() );
00139 QFile::remove( mLockUniqueName );
00140 emit unlocked();
00141 } else {
00142 mError = i18n("Unlock failed. Lock file is owned by other process: %1 (%2)")
00143 .arg( app ).arg( QString::number( pid ) );
00144 kdDebug() << "Lock::unlock(): " << mError << endl;
00145 return false;
00146 }
00147 }
00148
00149 mError = "";
00150 return true;
00151 }
00152
00153 QString Lock::error() const
00154 {
00155 return mError;
00156 }
00157
00158 #include "lock.moc"
This file is part of the documentation for kabc Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:45:22 2004 by
doxygen 1.3.6-20040222 written by
Dimitri van Heesch, © 1997-2003