dev-lang/vala: p.masked bump to 0.45.3
[gentoo.git] / kde-apps / akonadi / files / akonadi-18.12.3-akonadi_control-start-race-condition.patch
1 From c21bb5220a3ae835a5183afd58c186ba21f6c93d Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Daniel=20Vr=C3=A1til?= <dvratil@kde.org>
3 Date: Fri, 28 Jun 2019 17:10:04 +0200
4 Subject: Fix race-condition on akonadi_control start
5
6 Summary:
7 Check that there are no other akonadi_control instances running as the
8 very first thing on startup. Previously this check would happen after
9 AkApplication initialization, which contains some potential race-
10 conditions, like rotating log files.
11
12 The situation when akonadi_control is launched multiple times can occur
13 on session startup, when multiple different components will attempt to
14 launch Akonadi if its not yet running.
15
16 BUG: 392092
17 FIXED-IN: 19.04.3
18
19 Reviewers: #kde_pim, vkrause
20
21 Reviewed By: #kde_pim, vkrause
22
23 Subscribers: kde-pim
24
25 Tags: #kde_pim
26
27 Differential Revision: https://phabricator.kde.org/D22092
28 ---
29  src/akonadicontrol/main.cpp  | 16 ++-------------
30  src/shared/akapplication.cpp |  7 +++----
31  src/shared/akapplication.h   | 49 ++++++++++++++++++++++++++++++++++++++------
32  3 files changed, 48 insertions(+), 24 deletions(-)
33
34 diff --git a/src/akonadicontrol/main.cpp b/src/akonadicontrol/main.cpp
35 index 19bebb8..7dba85b 100644
36 --- a/src/akonadicontrol/main.cpp
37 +++ b/src/akonadicontrol/main.cpp
38 @@ -52,7 +52,7 @@ void crashHandler(int)
39  
40  int main(int argc, char **argv)
41  {
42 -    AkGuiApplication app(argc, argv, AKONADICONTROL_LOG());
43 +    AkUniqueGuiApplication app(argc, argv, Akonadi::DBus::serviceName(Akonadi::DBus::ControlLock), AKONADICONTROL_LOG());
44      app.setDescription(QStringLiteral("Akonadi Control Process\nDo not run this manually, use 'akonadictl' instead to start/stop Akonadi."));
45  
46      KAboutData aboutData(QStringLiteral("akonadi_control"),
47 @@ -64,20 +64,8 @@ int main(int argc, char **argv)
48  
49      app.parseCommandLine();
50  
51 -    // try to acquire the lock first, that means there is no second instance trying to start up at the same time
52 -    // registering the real service name happens in AgentManager::continueStartup(), when everything is in fact up and running
53 -    if (!QDBusConnection::sessionBus().registerService(Akonadi::DBus::serviceName(Akonadi::DBus::ControlLock))) {
54 -        // We couldn't register. Most likely, it's already running.
55 -        const QString lastError = QDBusConnection::sessionBus().lastError().message();
56 -        if (lastError.isEmpty()) {
57 -            qCWarning(AKONADICONTROL_LOG) << "Unable to register service as" << Akonadi::DBus::serviceName(Akonadi::DBus::ControlLock) << "Maybe it's already running?";
58 -        } else {
59 -            qCWarning(AKONADICONTROL_LOG) << "Unable to register service as" << Akonadi::DBus::serviceName(Akonadi::DBus::ControlLock) << "Error was:" << lastError;
60 -        }
61 -        return -1;
62 -    }
63 -
64      // older Akonadi server versions don't use the lock service yet, so check if one is already running before we try to start another one
65 +    // TODO: Remove this legacy check?
66      if (QDBusConnection::sessionBus().interface()->isServiceRegistered(Akonadi::DBus::serviceName(Akonadi::DBus::Control))) {
67          qCWarning(AKONADICONTROL_LOG) << "Another Akonadi control process is already running.";
68          return -1;
69 diff --git a/src/shared/akapplication.cpp b/src/shared/akapplication.cpp
70 index af860e5..b790b8d 100644
71 --- a/src/shared/akapplication.cpp
72 +++ b/src/shared/akapplication.cpp
73 @@ -32,10 +32,9 @@
74  
75  AkApplicationBase *AkApplicationBase::sInstance = nullptr;
76  
77 -AkApplicationBase::AkApplicationBase(int &argc, char **argv, const QLoggingCategory &loggingCategory)
78 +AkApplicationBase::AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory)
79      : QObject(nullptr)
80 -    , mArgc(argc)
81 -    , mArgv(argv)
82 +    , mApp(std::move(app))
83      , mLoggingCategory(loggingCategory)
84  {
85      Q_ASSERT(!sInstance);
86 @@ -59,7 +58,7 @@ AkApplicationBase *AkApplicationBase::instance()
87  
88  void AkApplicationBase::init()
89  {
90 -    akInit(QString::fromLatin1(mArgv[0]));
91 +    akInit(mApp->applicationName());
92      akInitRemoteLog();
93  
94      if (!QDBusConnection::sessionBus().isConnected()) {
95 diff --git a/src/shared/akapplication.h b/src/shared/akapplication.h
96 index aae7a99..433b725 100644
97 --- a/src/shared/akapplication.h
98 +++ b/src/shared/akapplication.h
99 @@ -23,6 +23,10 @@
100  #include <QObject>
101  #include <QCommandLineParser>
102  #include <QLoggingCategory>
103 +#include <QDBusConnection>
104 +#include <QDBusError>
105 +
106 +#include <memory>
107  
108  class QCoreApplication;
109  class QApplication;
110 @@ -55,16 +59,15 @@ public:
111      int exec();
112  
113  protected:
114 -    AkApplicationBase(int &argc, char **argv, const QLoggingCategory &loggingCategory);
115 +    AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory);
116      void init();
117 -    QScopedPointer<QCoreApplication> mApp;
118 +
119 +    std::unique_ptr<QCoreApplication> mApp;
120  
121  private Q_SLOTS:
122      void pollSessionBus() const;
123  
124  private:
125 -    int mArgc;
126 -    char **mArgv;
127      QString mInstanceId;
128      const QLoggingCategory &mLoggingCategory;
129      static AkApplicationBase *sInstance;
130 @@ -77,13 +80,46 @@ class AkApplicationImpl : public AkApplicationBase
131  {
132  public:
133      AkApplicationImpl(int &argc, char **argv, const QLoggingCategory &loggingCategory = *QLoggingCategory::defaultCategory())
134 -        : AkApplicationBase(argc, argv, loggingCategory)
135 +        : AkApplicationBase(std::make_unique<T>(argc, argv), loggingCategory)
136      {
137 -        mApp.reset(new T(argc, argv));
138          init();
139      }
140  };
141  
142 +template<typename T>
143 +class AkUniqueApplicationImpl : public AkApplicationBase
144 +{
145 +public:
146 +    AkUniqueApplicationImpl(int &argc, char **argv, const QString &serviceName, const QLoggingCategory &loggingCategory = *QLoggingCategory::defaultCategory())
147 +        : AkApplicationBase(std::make_unique<T>(argc, argv), loggingCategory)
148 +    {
149 +        registerUniqueServiceOrTerminate(serviceName, loggingCategory);
150 +        init();
151 +    }
152 +
153 +private:
154 +    void registerUniqueServiceOrTerminate(const QString &serviceName, const QLoggingCategory &log)
155 +    {
156 +        auto bus = QDBusConnection::sessionBus();
157 +        if (!bus.isConnected()) {
158 +            qCCritical(log, "Session bus not found. Is DBus running?");
159 +            exit(1);
160 +        }
161 +
162 +        if (!bus.registerService(serviceName)) {
163 +            // We couldn't register. Most likely, it's already running.
164 +            const QString lastError = bus.lastError().message();
165 +            if (lastError.isEmpty()) {
166 +                qCInfo(log, "Service %s already registered, terminating now.", qUtf8Printable(serviceName));
167 +                exit(0); // already running, so it's OK. Terminate now.
168 +            } else {
169 +                qCCritical(log, "Unable to register service as %s due to an error: %s", qUtf8Printable(serviceName), qUtf8Printable(lastError));
170 +                exit(1); // :(
171 +            }
172 +        }
173 +    }
174 +};
175 +
176  /**
177   * Returns the contents of @p name environment variable if it is defined,
178   * or @p defaultValue otherwise.
179 @@ -93,5 +129,6 @@ QString akGetEnv(const char *name, const QString &defaultValue = QString());
180  typedef AkApplicationImpl<QCoreApplication> AkCoreApplication;
181  typedef AkApplicationImpl<QApplication> AkApplication;
182  typedef AkApplicationImpl<QGuiApplication> AkGuiApplication;
183 +typedef AkUniqueApplicationImpl<QGuiApplication> AkUniqueGuiApplication;
184  
185  #endif
186 -- 
187 cgit v1.1