You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.1 KiB
163 lines
4.1 KiB
15 years ago
|
/*
|
||
|
This file is part of libkabc.
|
||
|
Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include "lock.h"
|
||
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
#include <kstandarddirs.h>
|
||
12 years ago
|
#include <tdetempfile.h>
|
||
15 years ago
|
|
||
14 years ago
|
#include <tqfile.h>
|
||
15 years ago
|
|
||
|
#include <signal.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
using namespace KABC;
|
||
|
|
||
14 years ago
|
Lock::Lock( const TQString &identifier )
|
||
15 years ago
|
: mIdentifier( identifier )
|
||
|
{
|
||
14 years ago
|
mIdentifier.replace( "/", "_" );
|
||
15 years ago
|
}
|
||
|
|
||
|
Lock::~Lock()
|
||
|
{
|
||
|
unlock();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Lock::locksDir()
|
||
15 years ago
|
{
|
||
12 years ago
|
return locateLocal( "data", "tdeabc/lock/" );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
bool Lock::readLockFile( const TQString &filename, int &pid, TQString &app )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQFile file( filename );
|
||
15 years ago
|
if ( !file.open( IO_ReadOnly ) ) return false;
|
||
|
|
||
14 years ago
|
TQTextStream t( &file );
|
||
15 years ago
|
pid = t.readLine().toInt();
|
||
|
app = t.readLine();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
bool Lock::writeLockFile( const TQString &filename )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQFile file( filename );
|
||
15 years ago
|
if ( !file.open( IO_WriteOnly ) ) return false;
|
||
14 years ago
|
TQTextStream t( &file );
|
||
12 years ago
|
t << ::getpid() << endl << TQString( TDEGlobal::instance()->instanceName() );
|
||
15 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Lock::lockFileName() const
|
||
15 years ago
|
{
|
||
|
return locksDir() + mIdentifier + ".lock";
|
||
|
}
|
||
|
|
||
|
bool Lock::lock()
|
||
|
{
|
||
|
kdDebug(5700) << "Lock::lock()" << endl;
|
||
|
|
||
14 years ago
|
TQString lockName = lockFileName();
|
||
15 years ago
|
kdDebug(5700) << "-- lock name: " << lockName << endl;
|
||
|
|
||
14 years ago
|
if ( TQFile::exists( lockName ) ) { // check if it is a stale lock file
|
||
15 years ago
|
int pid;
|
||
14 years ago
|
TQString app;
|
||
15 years ago
|
|
||
|
if ( !readLockFile( lockFileName(), pid, app ) ) {
|
||
|
mError = i18n("Unable to open lock file.");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
int retval = ::kill( pid, 0 );
|
||
|
if ( retval == -1 && errno == ESRCH ) { // process doesn't exists anymore
|
||
14 years ago
|
TQFile::remove( lockName );
|
||
15 years ago
|
kdWarning(5700) << "Removed stale lock file from process '" << app << "'"
|
||
|
<< endl;
|
||
|
} else {
|
||
14 years ago
|
TQString identifier( mIdentifier );
|
||
14 years ago
|
identifier.replace( '_', '/' );
|
||
15 years ago
|
|
||
|
mError = i18n("The address book '%1' is locked by application '%2'.\nIf you believe this is incorrect, just remove the lock file from '%3'")
|
||
12 years ago
|
.arg( identifier ).arg( app ).arg( locateLocal( "data", "tdeabc/lock/*.lock" ) );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString lockUniqueName;
|
||
15 years ago
|
lockUniqueName = mIdentifier + kapp->randomString( 8 );
|
||
12 years ago
|
mLockUniqueName = locateLocal( "data", "tdeabc/lock/" + lockUniqueName );
|
||
15 years ago
|
kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
|
||
|
|
||
|
// Create unique file
|
||
|
writeLockFile( mLockUniqueName );
|
||
|
|
||
|
// Create lock file
|
||
14 years ago
|
int result = ::link( TQFile::encodeName( mLockUniqueName ),
|
||
|
TQFile::encodeName( lockName ) );
|
||
15 years ago
|
|
||
|
if ( result == 0 ) {
|
||
|
mError = "";
|
||
|
emit locked();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// TODO: check stat
|
||
|
|
||
|
mError = i18n("Error");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool Lock::unlock()
|
||
|
{
|
||
|
int pid;
|
||
14 years ago
|
TQString app;
|
||
15 years ago
|
if ( readLockFile( lockFileName(), pid, app ) ) {
|
||
|
if ( pid == getpid() ) {
|
||
14 years ago
|
TQFile::remove( lockFileName() );
|
||
|
TQFile::remove( mLockUniqueName );
|
||
15 years ago
|
emit unlocked();
|
||
|
} else {
|
||
|
mError = i18n("Unlock failed. Lock file is owned by other process: %1 (%2)")
|
||
14 years ago
|
.arg( app ).arg( TQString::number( pid ) );
|
||
15 years ago
|
kdDebug() << "Lock::unlock(): " << mError << endl;
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mError = "";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Lock::error() const
|
||
15 years ago
|
{
|
||
|
return mError;
|
||
|
}
|
||
|
|
||
|
#include "lock.moc"
|