From ea6ba98e19f970dc56fb762085aec35915681718 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 14 Aug 2018 21:17:03 +0200 Subject: Possibility to enforce a single kube instance through a lockfile Necessary for the flatpak until we solve the pid issues. --- applications/kube/main.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/applications/kube/main.cpp b/applications/kube/main.cpp index 20b033cb..ef9be5ab 100644 --- a/applications/kube/main.cpp +++ b/applications/kube/main.cpp @@ -39,9 +39,9 @@ #include #include #include -#include #include -#include +#include +#include #include #include "backtrace.h" @@ -130,9 +130,30 @@ int main(int argc, char *argv[]) parser.addOption({{"k", "keyring"}, QCoreApplication::translate("main", "To automatically unlock the keyring pass in a keyring in the form of {\"accountId\": {\"resourceId\": \"secret\", *}}"), "keyring dictionary"} ); - parser.addOption({{"u", "upgrade"}, "", ""}); + parser.addOption({{"l", "lockfile"}, "Use a lockfile to enforce that only a single instance can be started.", ""}); parser.process(app); + + static QString location = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kube"; + QDir{}.mkpath(location); + QLockFile lockfile(location + QString("/%1.lock").arg(QString("kube"))); + lockfile.setStaleLockTime(500); + if (parser.isSet("lockfile")) { + if (!lockfile.tryLock(0)) { + const auto error = lockfile.error(); + if (error == QLockFile::LockFailedError) { + qint64 pid; + QString hostname, appname; + lockfile.getLockInfo(&pid, &hostname, &appname); + qWarning() << "Failed to acquire exclusive lock. You can only run one instance of Kube."; + qWarning() << "Pid:" << pid << "Host:" << hostname << "App:" << appname; + } else { + qWarning() << "Error while trying to acquire exclusive lock: " << error; + } + return -1; + } + } + if (parser.isSet("keyring")) { auto keyringDict = parser.value("keyring"); auto json = QJsonDocument::fromJson(keyringDict.toUtf8()); -- cgit v1.2.3