• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

kabc

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 <krandom.h>
24 #include <kcomponentdata.h>
25 #include <kdebug.h>
26 #include <kglobal.h>
27 #include <klocalizedstring.h>
28 #include <kstandarddirs.h>
29 
30 #include <QtCore/QFile>
31 #include <QtCore/QTextStream>
32 
33 #include <errno.h>
34 #include <signal.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 
39 using namespace KABC;
40 
41 class Lock::Private
42 {
43  public:
44  Private( const QString &identifier )
45  : mIdentifier( identifier ),
46  mOrigIdentifier( identifier )
47  {
48  mIdentifier.replace( QLatin1Char( '/' ), QLatin1Char( '_' ) );
49 #ifdef Q_WS_WIN
50  mIdentifier.replace( QLatin1Char( ':' ), QLatin1Char( '_' ) );
51 #endif
52  }
53 
54  QString mIdentifier;
55  QString mOrigIdentifier;
56  QString mLockUniqueName;
57  QString mError;
58 };
59 
60 Lock::Lock( const QString &identifier )
61  : d( new Private( identifier ) )
62 {
63 }
64 
65 Lock::~Lock()
66 {
67  unlock();
68 
69  delete d;
70 }
71 
72 QString Lock::locksDir()
73 {
74  return KStandardDirs::locateLocal( "data", QLatin1String( "kabc/lock/" ) );
75 }
76 
77 bool Lock::readLockFile( const QString &filename, int &pid, QString &app )
78 {
79  QFile file( filename );
80  if ( !file.open( QIODevice::ReadOnly ) ) {
81  return false;
82  }
83 
84  QTextStream t( &file );
85  t >> pid >> ws >> app;
86 
87  return true;
88 }
89 
90 bool Lock::writeLockFile( const QString &filename )
91 {
92  QFile file( filename );
93  if ( !file.open( QIODevice::WriteOnly ) ) {
94  return false;
95  }
96 
97  QTextStream t( &file );
98  t << ::getpid() << endl << QString( KGlobal::mainComponent().componentName() );
99 
100  return true;
101 }
102 
103 QString Lock::lockFileName() const
104 {
105  return locksDir() + d->mIdentifier + QLatin1String( ".lock" );
106 }
107 
108 bool Lock::lock()
109 {
110  QString lockName = lockFileName();
111  kDebug() << "-- lock name:" << lockName;
112 
113  if ( QFile::exists( lockName ) ) { // check if it is a stale lock file
114  int pid;
115  QString app;
116 
117  if ( !readLockFile( lockFileName(), pid, app ) ) {
118  d->mError = i18n( "Unable to open lock file." );
119  return false;
120  }
121 
122  int retval = ::kill( pid, 0 );
123  if ( retval == -1 && errno == ESRCH ) { // process doesn't exists anymore
124  QFile::remove( lockName );
125  kWarning() << "Removed stale lock file from process '" << app << "'";
126  } else {
127  d->mError = i18n( "The resource '%1' is locked by application '%2'.",
128  d->mOrigIdentifier, app );
129  return false;
130  }
131  }
132 
133  QString lockUniqueName;
134  lockUniqueName = d->mIdentifier + KRandom::randomString( 8 );
135  d->mLockUniqueName = KStandardDirs::locateLocal(
136  "data", QLatin1String( "kabc/lock/" ) + lockUniqueName );
137  kDebug() << "-- lock unique name:" << d->mLockUniqueName;
138 
139  // Create unique file
140  writeLockFile( d->mLockUniqueName );
141 
142  // Create lock file
143  int result = ::link( QFile::encodeName( d->mLockUniqueName ),
144  QFile::encodeName( lockName ) );
145 
146  if ( result == 0 ) {
147  d->mError.clear();
148  emit locked();
149  return true;
150  }
151 
152  // TODO: check stat
153 
154  d->mError = i18n( "Error" );
155  return false;
156 }
157 
158 bool Lock::unlock()
159 {
160  int pid;
161  QString app;
162  if ( readLockFile( lockFileName(), pid, app ) ) {
163  if ( pid == getpid() ) {
164  QFile::remove( lockFileName() );
165  QFile::remove( d->mLockUniqueName );
166  emit unlocked();
167  } else {
168  d->mError = i18n( "Unlock failed. Lock file is owned by other process: %1 (%2)", app, pid );
169  kDebug() << d->mError;
170  return false;
171  }
172  }
173 
174  d->mError.clear();
175 
176  return true;
177 }
178 
179 QString Lock::error() const
180 {
181  return d->mError;
182 }
183 
KABC::Lock::lockFileName
QString lockFileName() const
Returns the path of the lock file.
Definition: lock.cpp:103
KABC::Lock::writeLockFile
static bool writeLockFile(const QString &filename)
Writes the process ID and the application name to a lock file.
Definition: lock.cpp:90
KABC::Lock::unlocked
void unlocked()
Emitted after the lock has been unlocked.
KABC::Lock::readLockFile
static bool readLockFile(const QString &filename, int &pid, QString &app)
Reads the process ID and the application name from a lock file.
Definition: lock.cpp:77
KABC::Lock::locksDir
static QString locksDir()
Returns the path of the directory where locks are created.
Definition: lock.cpp:72
KABC::Lock::Lock
Lock(const QString &identifier)
Constructor.
Definition: lock.cpp:60
KABC::Lock::unlock
virtual bool unlock()
Unlock resource.
Definition: lock.cpp:158
KABC::Lock::lock
virtual bool lock()
Lock resource.
Definition: lock.cpp:108
KABC::Lock::locked
void locked()
Emitted after the lock has been locked.
KABC::Lock::error
virtual QString error() const
Returns the lastest error message.
Definition: lock.cpp:179
KABC::Lock::~Lock
~Lock()
Destruct lock object.
Definition: lock.cpp:65
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:01:05 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal