2022-10-08 03:28:20 +03:00
|
|
|
/* 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"
|
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
#include <QHostInfo>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QThread>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-16 20:39:36 +03:00
|
|
|
#define KLOCKFILE_TIMEOUT 150
|
|
|
|
#define KLOCKFILE_SLEEPTIME 150
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
class KLockFilePrivate
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
|
|
|
public:
|
2022-10-08 03:28:20 +03:00
|
|
|
KLockFilePrivate();
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
KLockFile::LockResult tryLock();
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
QByteArray m_lockfile;
|
|
|
|
int m_lockfd;
|
|
|
|
qint64 m_pid;
|
|
|
|
QByteArray m_hostname;
|
2014-11-13 01:04:59 +02:00
|
|
|
};
|
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
KLockFilePrivate::KLockFilePrivate()
|
|
|
|
: m_lockfd(-1),
|
|
|
|
m_pid(-1)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-10-08 03:28:20 +03:00
|
|
|
m_pid = static_cast<qint64>(::getpid());
|
|
|
|
m_hostname = QHostInfo::localHostName().toUtf8();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
KLockFile::LockResult KLockFilePrivate::tryLock()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-10-08 03:28:20 +03:00
|
|
|
if (m_lockfd != -1) {
|
|
|
|
return KLockFile::LockOK;
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 03:28:20 +03: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));
|
2022-10-08 03:41:29 +03:00
|
|
|
if (Q_UNLIKELY(!infofile.open(QFile::ReadOnly))) {
|
2022-10-08 03:28:20 +03:00
|
|
|
kWarning() << infofile.errorString();
|
2022-10-08 14:12:41 +03:00
|
|
|
return KLockFile::LockFail;
|
2022-10-08 03:28:20 +03:00
|
|
|
}
|
|
|
|
const QList<QByteArray> lockinfo = infofile.readAll().split('\t');
|
2022-10-08 03:41:29 +03:00
|
|
|
if (Q_UNLIKELY(lockinfo.size() != 2)) {
|
2022-10-08 03:28:20 +03:00
|
|
|
kWarning() << "Invalid lock information";
|
2022-10-08 14:12:41 +03:00
|
|
|
return KLockFile::LockFail;
|
2022-10-08 03:28:20 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-10-08 03:28:20 +03: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
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
return KLockFile::LockFail;
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
kWarning() << "Could not create lock file" << qt_error_string(savederrno);
|
|
|
|
return KLockFile::LockError;
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
QByteArray infodata = QByteArray::number(m_pid);
|
|
|
|
infodata.append('\t');
|
|
|
|
infodata.append(m_hostname);
|
2022-10-08 03:41:29 +03:00
|
|
|
if (Q_UNLIKELY(QT_WRITE(m_lockfd, infodata.constData(), infodata.size()) != infodata.size())) {
|
2022-10-08 03:28:20 +03:00
|
|
|
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
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
return KLockFile::LockOK;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
KLockFile::KLockFile(const QString &file)
|
|
|
|
: d(new KLockFilePrivate())
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-10-08 17:34:42 +03:00
|
|
|
d->m_lockfile = QFile::encodeName(file);
|
|
|
|
d->m_lockfile.append(".klockfile");
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
KLockFile::~KLockFile()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-10-08 03:28:20 +03:00
|
|
|
unlock();
|
|
|
|
delete d;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KLockFile::LockResult KLockFile::lock(LockFlags options)
|
|
|
|
{
|
2022-10-08 03:28:20 +03:00
|
|
|
tryagain:
|
|
|
|
KLockFile::LockResult result = d->tryLock();
|
|
|
|
if (result == KLockFile::LockStale && options & KLockFile::ForceFlag) {
|
|
|
|
kWarning() << "Deleting stale lock file" << d->m_lockfile;
|
2022-10-08 17:34:42 +03:00
|
|
|
if (Q_UNLIKELY(::unlink(d->m_lockfile.constData()) == -1)) {
|
2022-10-08 03:28:20 +03:00
|
|
|
const int savederrno = errno;
|
|
|
|
kWarning() << "Could not remove lock file" << qt_error_string(savederrno);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
2022-10-08 03:28:20 +03:00
|
|
|
result = d->tryLock();
|
2021-06-22 19:09:23 +03:00
|
|
|
}
|
2022-10-08 14:12:41 +03:00
|
|
|
if (!(options & KLockFile::NoBlockFlag) && result == KLockFile::LockFail) {
|
2022-10-16 20:39:36 +03:00
|
|
|
kDebug() << "Retrying to lock after" << (KLOCKFILE_TIMEOUT + KLOCKFILE_SLEEPTIME);
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, KLOCKFILE_TIMEOUT);
|
2022-10-08 03:28:20 +03:00
|
|
|
QThread::msleep(KLOCKFILE_SLEEPTIME);
|
|
|
|
goto tryagain;
|
2021-06-22 19:09:23 +03:00
|
|
|
}
|
|
|
|
return result;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KLockFile::isLocked() const
|
|
|
|
{
|
2022-10-08 03:28:20 +03:00
|
|
|
return (d->m_lockfd != -1);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KLockFile::unlock()
|
|
|
|
{
|
2022-10-08 03:28:20 +03:00
|
|
|
if (d->m_lockfd != -1) {
|
|
|
|
QT_CLOSE(d->m_lockfd);
|
2022-10-08 17:34:42 +03:00
|
|
|
if (Q_UNLIKELY(::unlink(d->m_lockfile.constData()) == -1)) {
|
2022-10-08 03:28:20 +03:00
|
|
|
const int savederrno = errno;
|
|
|
|
kWarning() << "Could not remove lock file" << qt_error_string(savederrno);
|
2021-06-22 19:09:23 +03:00
|
|
|
}
|
2022-10-08 03:28:20 +03:00
|
|
|
d->m_lockfd = -1;
|
2021-06-22 19:09:23 +03:00
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
bool KLockFile::getLockInfo(qint64 &pid, QString &hostname)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-10-08 03:28:20 +03:00
|
|
|
if (d->m_lockfd == -1) {
|
2021-06-22 19:09:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pid = d->m_pid;
|
2022-10-08 03:28:20 +03:00
|
|
|
hostname = QString::fromUtf8(d->m_hostname.constData(), d->m_hostname.size());
|
2021-06-22 19:09:23 +03:00
|
|
|
return true;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|