diff options
Diffstat (limited to 'src/corelib/io/qlockfile.cpp')
-rw-r--r-- | src/corelib/io/qlockfile.cpp | 71 |
1 files changed, 61 insertions, 10 deletions
diff --git a/src/corelib/io/qlockfile.cpp b/src/corelib/io/qlockfile.cpp index 48317d07e0..129cf01b63 100644 --- a/src/corelib/io/qlockfile.cpp +++ b/src/corelib/io/qlockfile.cpp @@ -2,6 +2,7 @@ ** ** Copyright (C) 2013 David Faure <faure+bluesystems@kde.org> ** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -42,12 +43,34 @@ #include "qlockfile_p.h" #include <QtCore/qthread.h> +#include <QtCore/qcoreapplication.h> #include <QtCore/qdeadlinetimer.h> #include <QtCore/qdatetime.h> #include <QtCore/qfileinfo.h> QT_BEGIN_NAMESPACE +namespace { +struct LockFileInfo +{ + qint64 pid; + QString appname; + QString hostname; +}; +} + +static bool getLockInfo_helper(const QString &fileName, LockFileInfo *info); + +static QString machineName() +{ +#ifdef Q_OS_WIN + // we don't use QSysInfo because it tries to do name resolution + return qEnvironmentVariable("COMPUTERNAME"); +#else + return QSysInfo::machineHostName(); +#endif +} + /*! \class QLockFile \inmodule QtCore @@ -291,10 +314,27 @@ bool QLockFile::tryLock(int timeout) bool QLockFile::getLockInfo(qint64 *pid, QString *hostname, QString *appname) const { Q_D(const QLockFile); - return d->getLockInfo(pid, hostname, appname); + LockFileInfo info; + if (!getLockInfo_helper(d->fileName, &info)) + return false; + if (pid) + *pid = info.pid; + if (hostname) + *hostname = info.hostname; + if (appname) + *appname = info.appname; + return true; } -bool QLockFilePrivate::getLockInfo(qint64 *pid, QString *hostname, QString *appname) const +QByteArray QLockFilePrivate::lockFileContents() const +{ + // Use operator% from the fast builder to avoid multiple memory allocations. + return QByteArray::number(QCoreApplication::applicationPid()) % '\n' + % processNameByPid(QCoreApplication::applicationPid()).toUtf8() % '\n' + % machineName().toUtf8() % '\n'; +} + +static bool getLockInfo_helper(const QString &fileName, LockFileInfo *info) { QFile reader(fileName); if (!reader.open(QIODevice::ReadOnly)) @@ -309,14 +349,25 @@ bool QLockFilePrivate::getLockInfo(qint64 *pid, QString *hostname, QString *appn QByteArray hostNameLine = reader.readLine(); hostNameLine.chop(1); - qint64 thePid = pidLine.toLongLong(); - if (pid) - *pid = thePid; - if (appname) - *appname = QString::fromUtf8(appNameLine); - if (hostname) - *hostname = QString::fromUtf8(hostNameLine); - return thePid > 0; + bool ok; + info->appname = QString::fromUtf8(appNameLine); + info->hostname = QString::fromUtf8(hostNameLine); + info->pid = pidLine.toLongLong(&ok); + return ok && info->pid > 0; +} + +bool QLockFilePrivate::isApparentlyStale() const +{ + LockFileInfo info; + if (getLockInfo_helper(fileName, &info)) { + if (info.hostname.isEmpty() || info.hostname == machineName()) { + if (!isProcessRunning(info.pid, info.appname)) + return true; + } + } + + const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTimeUtc()); + return staleLockTime > 0 && qAbs(age) > staleLockTime; } /*! |