• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kabc
 

kabc

  • kabc
lock.cpp
1 /*
2  This file is part of libkabc.
3  Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "lock.h"
22 
23 #include <kapplication.h>
24 #include <kdebug.h>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
27 #include <ktempfile.h>
28 
29 #include <tqfile.h>
30 
31 #include <signal.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 
36 using namespace KABC;
37 
38 Lock::Lock( const TQString &identifier )
39  : mIdentifier( identifier )
40 {
41  mIdentifier.replace( "/", "_" );
42 }
43 
44 Lock::~Lock()
45 {
46  unlock();
47 }
48 
49 TQString Lock::locksDir()
50 {
51  return locateLocal( "data", "kabc/lock/" );
52 }
53 
54 bool Lock::readLockFile( const TQString &filename, int &pid, TQString &app )
55 {
56  TQFile file( filename );
57  if ( !file.open( IO_ReadOnly ) ) return false;
58 
59  TQTextStream t( &file );
60  pid = t.readLine().toInt();
61  app = t.readLine();
62 
63  return true;
64 }
65 
66 bool Lock::writeLockFile( const TQString &filename )
67 {
68  TQFile file( filename );
69  if ( !file.open( IO_WriteOnly ) ) return false;
70  TQTextStream t( &file );
71  t << ::getpid() << endl << TQString( KGlobal::instance()->instanceName() );
72 
73  return true;
74 }
75 
76 TQString Lock::lockFileName() const
77 {
78  return locksDir() + mIdentifier + ".lock";
79 }
80 
81 bool Lock::lock()
82 {
83  kdDebug(5700) << "Lock::lock()" << endl;
84 
85  TQString lockName = lockFileName();
86  kdDebug(5700) << "-- lock name: " << lockName << endl;
87 
88  if ( TQFile::exists( lockName ) ) { // check if it is a stale lock file
89  int pid;
90  TQString app;
91 
92  if ( !readLockFile( lockFileName(), pid, app ) ) {
93  mError = i18n("Unable to open lock file.");
94  return false;
95  }
96 
97  int retval = ::kill( pid, 0 );
98  if ( retval == -1 && errno == ESRCH ) { // process doesn't exists anymore
99  TQFile::remove( lockName );
100  kdWarning(5700) << "Removed stale lock file from process '" << app << "'"
101  << endl;
102  } else {
103  TQString identifier( mIdentifier );
104  identifier.replace( '_', '/' );
105 
106  mError = i18n("The address book '%1' is locked by application '%2'.\nIf you believe this is incorrect, just remove the lock file from '%3'")
107  .arg( identifier ).arg( app ).arg( locateLocal( "data", "kabc/lock/*.lock" ) );
108  return false;
109  }
110  }
111 
112  TQString lockUniqueName;
113  lockUniqueName = mIdentifier + kapp->randomString( 8 );
114  mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
115  kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
116 
117  // Create unique file
118  writeLockFile( mLockUniqueName );
119 
120  // Create lock file
121  int result = ::link( TQFile::encodeName( mLockUniqueName ),
122  TQFile::encodeName( lockName ) );
123 
124  if ( result == 0 ) {
125  mError = "";
126  emit locked();
127  return true;
128  }
129 
130  // TODO: check stat
131 
132  mError = i18n("Error");
133  return false;
134 }
135 
136 bool Lock::unlock()
137 {
138  int pid;
139  TQString app;
140  if ( readLockFile( lockFileName(), pid, app ) ) {
141  if ( pid == getpid() ) {
142  TQFile::remove( lockFileName() );
143  TQFile::remove( mLockUniqueName );
144  emit unlocked();
145  } else {
146  mError = i18n("Unlock failed. Lock file is owned by other process: %1 (%2)")
147  .arg( app ).arg( TQString::number( pid ) );
148  kdDebug() << "Lock::unlock(): " << mError << endl;
149  return false;
150  }
151  }
152 
153  mError = "";
154  return true;
155 }
156 
157 TQString Lock::error() const
158 {
159  return mError;
160 }
161 
162 #include "lock.moc"

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kabc

Skip menu "kabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kabc by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |