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

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

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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