Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
DbusConnection.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 2020 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 * DbusConnection.h
21 *
22 */
23#ifndef AI_IPC_DBUSCONNECTION_H
24#define AI_IPC_DBUSCONNECTION_H
25
26#include "DbusEventDispatcher.h"
27#include <ConditionVariable.h>
28#include <Mutex.h>
29#include <SpinLock.h>
30
31#include <map>
32#include <mutex>
33#include <atomic>
34#include <string>
35#include <cstdint>
36#include <memory>
37#include <functional>
38#include <condition_variable>
39
40#include <sys/types.h>
41
42#include <dbus/dbus.h>
43
44
45namespace AI_IPC
46{
47
48
49// -----------------------------------------------------------------------------
61{
62public:
64
66
67public:
68 bool connect(DBusBusType busType, const std::string& serviceName = std::string());
69
70 bool connect(const std::string& address, const std::string& serviceName = std::string());
71
72 void disconnect();
73
74public:
75 void registerMessageHandler(const std::function<DBusHandlerResult(DBusMessage*)>& handler);
76
77public:
78 bool sendMessageNoReply(DBusMessage *msg);
79
80 uint64_t sendMessageWithReply(DBusMessage *msg, int timeout);
81
82 DBusMessage* getReply(uint64_t token);
83
84 bool cancelReply(uint64_t token);
85
86
87 bool nameHasOwner(const std::string& name);
88
89 uid_t getUnixUser(const std::string& name);
90
91
92 bool addMatch(const std::string& rule);
93
94 bool removeMatch(const std::string& rule);
95
96 std::string getAddress();
97
98 bool flushConnection();
99
100
101private:
102 DBusConnection *mDbusConnection;
103
104 DbusEventDispatcher mEventDispacher;
105
106 bool completeConnect(DBusConnection* conn, const std::string& serviceName);
107
108private:
109 static DBusHandlerResult handleDbusMessageCb(DBusConnection *connection,
110 DBusMessage *message,
111 void *userData);
112
113 typedef std::function<DBusHandlerResult(DBusMessage*)> MessageHandler;
114 MessageHandler mHandler;
115
116 AICommon::Spinlock mHandlerLock;
117
118private:
119 bool reserveServiceName(DBusConnection *dbusConnection,
120 const std::string& name) const;
121
122 std::string mServiceName;
123
124private:
125 static void pendingCallNotifyFcn(DBusPendingCall *pending, void *userData);
126
127 static void pendingCallFreeFcn(void *userData);
128
129private:
130 typedef struct _ReplyContext
131 {
132 uint64_t token;
133 DbusConnection* conn;
134 } ReplyContext;
135
136 std::atomic<uint64_t> mTokenCounter;
137
138 AICommon::Mutex mRepliesLock;
139 AICommon::ConditionVariable mRepliesCondVar;
140 std::map<uint64_t, DBusMessage*> mReplies;
141};
142
143
144} // namespace AI_IPC
145
146#endif // !defined(AI_IPC_DBUSCONNECTION_H)
Definition ConditionVariable.h:67
Definition Mutex.h:67
Definition SpinLock.h:42
Wraps a dbus connection pointer and runs the dispatch loop for it.
Definition DbusConnection.h:61
uint64_t sendMessageWithReply(DBusMessage *msg, int timeout)
Sends a dbus message out the connection.
Definition DbusConnection.cpp:491
static DBusHandlerResult handleDbusMessageCb(DBusConnection *connection, DBusMessage *message, void *userData)
Callback from the libdbus in the context of the event / dispatcher thread.
Definition DbusConnection.cpp:341
void disconnect()
Disconnect from the bus.
Definition DbusConnection.cpp:258
static void pendingCallFreeFcn(void *userData)
Callback from libdus when a pending call notifier is being destroyed and we should clean up the conte...
Definition DbusConnection.cpp:459
uid_t getUnixUser(const std::string &name)
Returns the unix user id of the named client.
Definition DbusConnection.cpp:739
DBusMessage * getReply(uint64_t token)
Gets the reply for the given request.
Definition DbusConnection.cpp:611
bool sendMessageNoReply(DBusMessage *msg)
Sends a message on the connection without expecting a reply.
Definition DbusConnection.cpp:566
static void pendingCallNotifyFcn(DBusPendingCall *pending, void *userData)
Callback from libdus when either a reply is received for a pending call or the timeout expires.
Definition DbusConnection.cpp:404
bool reserveServiceName(DBusConnection *dbusConnection, const std::string &name) const
Attempts to reserve a service name on dbus.
Definition DbusConnection.cpp:69
bool nameHasOwner(const std::string &name)
Returns true if the supplied name exists on the bus.
Definition DbusConnection.cpp:697
bool addMatch(const std::string &rule)
Adds a match rule for the connection.
Definition DbusConnection.cpp:786
bool cancelReply(uint64_t token)
Cancels waiting for the reply.
Definition DbusConnection.cpp:663
bool completeConnect(DBusConnection *conn, const std::string &serviceName)
Completes the initialisation of the dbus connection.
Definition DbusConnection.cpp:189
bool removeMatch(const std::string &rule)
Removes a match rule from the connection.
Definition DbusConnection.cpp:834
void registerMessageHandler(const std::function< DBusHandlerResult(DBusMessage *)> &handler)
Registers a handler to be called when any message (method call or signal) is received.
Definition DbusConnection.cpp:376
bool connect(DBusBusType busType, const std::string &serviceName=std::string())
Attempts to connect to one of the known buses and optionally reserve the given service name.
Definition DbusConnection.cpp:131
bool flushConnection()
Flushes the dbus connection.
Definition DbusConnection.cpp:880
Event dispatcher loop, runs the thread that polls on the dbus fds.
Definition DbusEventDispatcher.h:60
Definition DbusConnection.h:131