Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
SDBusIpcService.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2019 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 //
20 // SDBusIpcService.h
21 // IpcService
22 //
23 //
24 
25 #ifndef SDBUSIPCSERVICE_H
26 #define SDBUSIPCSERVICE_H
27 
28 #include "IpcCommon.h"
29 #include "IIpcService.h"
30 
31 #include <set>
32 #include <atomic>
33 #include <string>
34 #include <thread>
35 #include <deque>
36 #include <mutex>
37 #include <condition_variable>
38 #include <functional>
39 #include <queue>
40 
41 
42 typedef struct sd_bus sd_bus;
43 typedef struct sd_bus_slot sd_bus_slot;
44 typedef struct sd_bus_message sd_bus_message;
45 typedef struct sd_event_source sd_event_source;
46 
47 
49 
50 
52  , public std::enable_shared_from_this<SDBusIpcService>
53 {
54 public:
55  enum BusType
56  {
57  SessionBus,
58  SystemBus
59  };
60 
61 public:
62  SDBusIpcService(BusType busType, const std::string& serviceName, int defaultTimeoutMs = -1);
63  SDBusIpcService(const std::string &busAddress, const std::string& serviceName, int defaultTimeoutMs = -1);
64  ~SDBusIpcService() final;
65 
66  bool isValid() const override;
67 
68  std::shared_ptr<AI_IPC::IAsyncReplyGetter> invokeMethod(const AI_IPC::Method &method, const AI_IPC::VariantList &args, int timeoutMs) override;
69  bool invokeMethod(const AI_IPC::Method &method, const AI_IPC::VariantList &args, AI_IPC::VariantList &replyArgs, int timeoutMs) override;
70 
71  bool emitSignal(const AI_IPC::Signal &signal, const AI_IPC::VariantList &args) override;
72 
73  std::string registerMethodHandler(const AI_IPC::Method &method, const AI_IPC::MethodHandler &handler) override;
74  std::string registerSignalHandler(const AI_IPC::Signal &signal, const AI_IPC::SignalHandler &handler) override;
75  bool unregisterHandler(const std::string& regId) override;
76 
77  bool enableMonitor(const std::set<std::string> &matchRules, const AI_IPC::MonitorHandler& handler) override;
78  bool disableMonitor() override;
79 
80  void flush() override;
81 
82  bool start() override;
83  bool stop() override;
84 
85  bool isServiceAvailable(const std::string& serviceName) const override;
86 
87  std::string getBusAddress() const override;
88 
89 private:
90  friend class SDBusAsyncReplySender;
91  void freeMethodReply(uint32_t replyId);
92  bool sendMethodReply(uint32_t replyId,
93  const AI_IPC::VariantList& replyArgs);
94 
95  uid_t getSenderUid(const std::string &senderName);
96 
97 private:
98  bool init(const std::string &serviceName, int defaultTimeoutMs);
99 
100  void eventLoopThread();
101 
102  bool runOnEventLoopThread(std::function<void()> &&fn) const;
103 
104  static int onExecCall(sd_event_source *s, int fd, uint32_t revents, void *userdata);
105 
106  static int onRuleMatch(sd_bus_message *m, void *userData, void *retError);
107  static int onMethodCall(sd_bus_message *reply, void *userData, void *retError);
108  static int onMethodReply(sd_bus_message *reply, void *userData, void *retError);
109 
110 private:
111  uint64_t mDefaultTimeoutUsecs;
112 
113  std::thread mThread;
114  sd_bus *mSDBus;
115 
116  std::atomic<bool> mStarted;
117 
118  bool mValid;
119 
121  {
122  sd_bus_slot *objectSlot;
123  std::string path;
124  std::string interface;
125  std::string name;
126  AI_IPC::MethodHandler callback;
127 
128  RegisteredMethod(sd_bus_slot *slot,
129  const AI_IPC::Method &method,
130  const AI_IPC::MethodHandler &handler)
131  : objectSlot(slot)
132  , path(method.object), interface(method.interface), name(method.name)
133  , callback(handler)
134  { }
135  };
136 
138  {
139  sd_bus_slot *matchSlot;
140  AI_IPC::SignalHandler callback;
141 
142  RegisteredSignal(sd_bus_slot *slot,
143  const AI_IPC::SignalHandler &handler)
144  : matchSlot(slot)
145  , callback(handler)
146  { }
147  };
148 
149  uint64_t mHandlerTag;
150  std::map<std::string, RegisteredMethod> mMethodHandlers;
151  std::map<std::string, RegisteredSignal> mSignalHandlers;
152 
153  struct Executor
154  {
155  uint64_t tag;
156  std::function<void()> func;
157 
158  Executor(uint64_t t, std::function<void()> &&f)
159  : tag(t), func(std::move(f))
160  { }
161  };
162 
163  mutable uint64_t mExecCounter;
164  uint64_t mLastExecTag;
165  int mExecEventFd;
166  mutable std::mutex mExecLock;
167  mutable std::condition_variable mExecCond;
168  mutable std::deque< Executor > mExecQueue;
169 
170  std::map< uint64_t, std::shared_ptr<SDBusAsyncReplyGetter> > mCalls;
171  std::map< uint32_t, sd_bus_message* > mCallReplies;
172  std::queue<uint32_t> mReplyIdentifiers;
173 
174 };
175 
176 #endif // SDBUSIPCSERVICE_H
IPC service that enables us to invoke remote method and emit signals as well as to handle incoming me...
Definition: IIpcService.h:42
Implements the IAsyncReplyGetter getter interface to provide an API to get the results to a method ca...
Definition: SDBusAsyncReplyGetter.h:35
Definition: SDBusAsyncReplySender.h:37
uid_t getSenderUid() const override
Get Id of the user (unix user Id) who invoked the method.
Definition: SDBusAsyncReplySender.cpp:80
Definition: SDBusIpcService.h:53
bool enableMonitor(const std::set< std::string > &matchRules, const AI_IPC::MonitorHandler &handler) override
Definition: SDBusIpcService.cpp:256
bool stop() override
Definition: SDBusIpcService.cpp:321
bool start() override
Definition: SDBusIpcService.cpp:294
std::string getBusAddress() const override
Definition: SDBusIpcService.cpp:392
std::string registerMethodHandler(const AI_IPC::Method &method, const AI_IPC::MethodHandler &handler) override
Definition: SDBusIpcService.cpp:636
bool emitSignal(const AI_IPC::Signal &signal, const AI_IPC::VariantList &args) override
Definition: SDBusIpcService.cpp:582
std::string registerSignalHandler(const AI_IPC::Signal &signal, const AI_IPC::SignalHandler &handler) override
Definition: SDBusIpcService.cpp:707
void flush() override
Definition: SDBusIpcService.cpp:282
bool isServiceAvailable(const std::string &serviceName) const override
Definition: SDBusIpcService.cpp:354
bool disableMonitor() override
Definition: SDBusIpcService.cpp:269
std::shared_ptr< AI_IPC::IAsyncReplyGetter > invokeMethod(const AI_IPC::Method &method, const AI_IPC::VariantList &args, int timeoutMs) override
Definition: SDBusIpcService.cpp:412
bool unregisterHandler(const std::string &regId) override
Definition: SDBusIpcService.cpp:763
bool isValid() const override
Returns true if we initialised ourselves successfully.
Definition: SDBusIpcService.cpp:204
Method identified by a service, object, interface and method name itself.
Definition: IpcCommon.h:90
Method identified by object, interface and signal name itself.
Definition: IpcCommon.h:105
Definition: SDBusIpcService.h:154
Definition: SDBusIpcService.h:121
Definition: SDBusIpcService.h:138