diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-08-14 21:17:03 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-08-14 21:17:03 +0200 |
commit | ea6ba98e19f970dc56fb762085aec35915681718 (patch) | |
tree | c31a61f2a5cfc6c3faceb26615b06d657634dffb | |
parent | e25e2bb20e826d215f08328f22ad0e8d7c9a0bb5 (diff) | |
download | kube-ea6ba98e19f970dc56fb762085aec35915681718.tar.gz kube-ea6ba98e19f970dc56fb762085aec35915681718.zip |
Possibility to enforce a single kube instance through a lockfile
Necessary for the flatpak until we solve the pid issues.
-rw-r--r-- | applications/kube/main.cpp | 27 |
1 files 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 @@ | |||
39 | #include <QDebug> | 39 | #include <QDebug> |
40 | #include <QTimer> | 40 | #include <QTimer> |
41 | #include <QQmlContext> | 41 | #include <QQmlContext> |
42 | #include <QIcon> | ||
43 | #include <QStandardPaths> | 42 | #include <QStandardPaths> |
44 | #include <QResource> | 43 | #include <QLockFile> |
44 | #include <QDir> | ||
45 | #include <sink/store.h> | 45 | #include <sink/store.h> |
46 | 46 | ||
47 | #include "backtrace.h" | 47 | #include "backtrace.h" |
@@ -130,9 +130,30 @@ int main(int argc, char *argv[]) | |||
130 | parser.addOption({{"k", "keyring"}, | 130 | parser.addOption({{"k", "keyring"}, |
131 | QCoreApplication::translate("main", "To automatically unlock the keyring pass in a keyring in the form of {\"accountId\": {\"resourceId\": \"secret\", *}}"), "keyring dictionary"} | 131 | QCoreApplication::translate("main", "To automatically unlock the keyring pass in a keyring in the form of {\"accountId\": {\"resourceId\": \"secret\", *}}"), "keyring dictionary"} |
132 | ); | 132 | ); |
133 | parser.addOption({{"u", "upgrade"}, "", ""}); | 133 | parser.addOption({{"l", "lockfile"}, "Use a lockfile to enforce that only a single instance can be started.", ""}); |
134 | parser.process(app); | 134 | parser.process(app); |
135 | 135 | ||
136 | |||
137 | static QString location = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kube"; | ||
138 | QDir{}.mkpath(location); | ||
139 | QLockFile lockfile(location + QString("/%1.lock").arg(QString("kube"))); | ||
140 | lockfile.setStaleLockTime(500); | ||
141 | if (parser.isSet("lockfile")) { | ||
142 | if (!lockfile.tryLock(0)) { | ||
143 | const auto error = lockfile.error(); | ||
144 | if (error == QLockFile::LockFailedError) { | ||
145 | qint64 pid; | ||
146 | QString hostname, appname; | ||
147 | lockfile.getLockInfo(&pid, &hostname, &appname); | ||
148 | qWarning() << "Failed to acquire exclusive lock. You can only run one instance of Kube."; | ||
149 | qWarning() << "Pid:" << pid << "Host:" << hostname << "App:" << appname; | ||
150 | } else { | ||
151 | qWarning() << "Error while trying to acquire exclusive lock: " << error; | ||
152 | } | ||
153 | return -1; | ||
154 | } | ||
155 | } | ||
156 | |||
136 | if (parser.isSet("keyring")) { | 157 | if (parser.isSet("keyring")) { |
137 | auto keyringDict = parser.value("keyring"); | 158 | auto keyringDict = parser.value("keyring"); |
138 | auto json = QJsonDocument::fromJson(keyringDict.toUtf8()); | 159 | auto json = QJsonDocument::fromJson(keyringDict.toUtf8()); |