kdelibs/kdecore/io/klockfile_unix.cpp

166 lines
5 KiB
C++
Raw Normal View History

/* This file is part of the KDE libraries
Copyright (c) 2004 Waldo Bastian <bastian@kde.org>
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
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.
2014-11-13 01:04:59 +02:00
*/
#include "klockfile.h"
2015-08-27 20:35:05 +03:00
#include "kdebug.h"
2014-11-13 01:04:59 +02:00
#include "kde_file.h"
#include <QHostInfo>
#include <QCoreApplication>
#include <QThread>
2014-11-13 01:04:59 +02:00
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
2014-11-13 01:04:59 +02:00
#define KLOCKFILE_TIMEOUT 150
#define KLOCKFILE_SLEEPTIME 150
2014-11-13 01:04:59 +02:00
class KLockFilePrivate
2014-11-13 01:04:59 +02:00
{
public:
KLockFilePrivate();
2014-11-13 01:04:59 +02:00
KLockFile::LockResult tryLock();
2014-11-13 01:04:59 +02:00
QByteArray m_lockfile;
int m_lockfd;
qint64 m_pid;
QByteArray m_hostname;
2014-11-13 01:04:59 +02:00
};
KLockFilePrivate::KLockFilePrivate()
: m_lockfd(-1),
m_pid(-1)
2014-11-13 01:04:59 +02:00
{
m_pid = static_cast<qint64>(::getpid());
m_hostname = QHostInfo::localHostName().toUtf8();
2014-11-13 01:04:59 +02:00
}
KLockFile::LockResult KLockFilePrivate::tryLock()
2014-11-13 01:04:59 +02:00
{
if (m_lockfd != -1) {
return KLockFile::LockOK;
}
2014-11-13 01:04:59 +02:00
m_lockfd = KDE_open(m_lockfile.constData(), O_WRONLY | O_CREAT | O_EXCL, 0644);
if (m_lockfd == -1) {
const int savederrno = errno;
if (savederrno == EEXIST) {
QFile infofile(QFile::decodeName(m_lockfile));
if (Q_UNLIKELY(!infofile.open(QFile::ReadOnly))) {
kWarning() << infofile.errorString();
return KLockFile::LockFail;
}
const QList<QByteArray> lockinfo = infofile.readAll().split('\t');
if (Q_UNLIKELY(lockinfo.size() != 2)) {
kWarning() << "Invalid lock information";
return KLockFile::LockFail;
}
const qint64 lockpid = lockinfo.at(0).toLongLong();
const QByteArray lockhost = lockinfo.at(1);
kDebug() << "Lock" << m_lockfile << "held by" << lockpid << "on" << lockhost;
2014-11-13 01:04:59 +02:00
if (lockhost == m_hostname && ::kill(lockpid, 0) == -1 && errno == ESRCH) {
kWarning() << "Stale lock" << m_lockfile << "held by" << lockpid << "on" << lockhost;
return KLockFile::LockStale;
}
2014-11-13 01:04:59 +02:00
return KLockFile::LockFail;
}
2014-11-13 01:04:59 +02:00
kWarning() << "Could not create lock file" << qt_error_string(savederrno);
return KLockFile::LockError;
}
2014-11-13 01:04:59 +02:00
QByteArray infodata = QByteArray::number(m_pid);
infodata.append('\t');
infodata.append(m_hostname);
if (Q_UNLIKELY(QT_WRITE(m_lockfd, infodata.constData(), infodata.size()) != infodata.size())) {
const int savederrno = errno;
kWarning() << "Could not write lock information" << qt_error_string(savederrno);
return KLockFile::LockError;
}
2014-11-13 01:04:59 +02:00
return KLockFile::LockOK;
2014-11-13 01:04:59 +02:00
}
KLockFile::KLockFile(const QString &file)
: d(new KLockFilePrivate())
2014-11-13 01:04:59 +02:00
{
d->m_lockfile = QFile::encodeName(file);
d->m_lockfile.append(".klockfile");
2014-11-13 01:04:59 +02:00
}
KLockFile::~KLockFile()
2014-11-13 01:04:59 +02:00
{
unlock();
delete d;
2014-11-13 01:04:59 +02:00
}
KLockFile::LockResult KLockFile::lock(LockFlags options)
{
tryagain:
KLockFile::LockResult result = d->tryLock();
if (result == KLockFile::LockStale && options & KLockFile::ForceFlag) {
kWarning() << "Deleting stale lock file" << d->m_lockfile;
if (Q_UNLIKELY(::unlink(d->m_lockfile.constData()) == -1)) {
const int savederrno = errno;
kWarning() << "Could not remove lock file" << qt_error_string(savederrno);
2014-11-13 01:04:59 +02:00
}
result = d->tryLock();
}
if (!(options & KLockFile::NoBlockFlag) && result == KLockFile::LockFail) {
kDebug() << "Retrying to lock after" << (KLOCKFILE_TIMEOUT + KLOCKFILE_SLEEPTIME);
QCoreApplication::processEvents(QEventLoop::AllEvents, KLOCKFILE_TIMEOUT);
QThread::msleep(KLOCKFILE_SLEEPTIME);
goto tryagain;
}
return result;
2014-11-13 01:04:59 +02:00
}
bool KLockFile::isLocked() const
{
return (d->m_lockfd != -1);
2014-11-13 01:04:59 +02:00
}
void KLockFile::unlock()
{
if (d->m_lockfd != -1) {
QT_CLOSE(d->m_lockfd);
if (Q_UNLIKELY(::unlink(d->m_lockfile.constData()) == -1)) {
const int savederrno = errno;
kWarning() << "Could not remove lock file" << qt_error_string(savederrno);
}
d->m_lockfd = -1;
}
2014-11-13 01:04:59 +02:00
}
bool KLockFile::getLockInfo(qint64 &pid, QString &hostname)
2014-11-13 01:04:59 +02:00
{
if (d->m_lockfd == -1) {
return false;
}
pid = d->m_pid;
hostname = QString::fromUtf8(d->m_hostname.constData(), d->m_hostname.size());
return true;
2014-11-13 01:04:59 +02:00
}