1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/*
* virgdbus.c: helper for using GDBus
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include "virerror.h"
#include "virlog.h"
#include "virgdbus.h"
#include "virthread.h"
#define VIR_FROM_THIS VIR_FROM_DBUS
VIR_LOG_INIT("util.dbus");
static bool sharedBus = true;
static GDBusConnection *systemBus;
static GDBusConnection *sessionBus;
static virOnceControl systemOnce = VIR_ONCE_CONTROL_INITIALIZER;
static virOnceControl sessionOnce = VIR_ONCE_CONTROL_INITIALIZER;
static GError *systemError;
static GError *sessionError;
void
virGDBusSetSharedBus(bool shared)
{
sharedBus = shared;
}
static GDBusConnection *
virGDBusBusInit(GBusType type, GError **error)
{
g_autofree char *address = NULL;
if (sharedBus) {
return g_bus_get_sync(type, NULL, error);
} else {
GDBusConnectionFlags dbusFlags =
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION;
address = g_dbus_address_get_for_bus_sync(type, NULL, error);
if (*error)
return NULL;
return g_dbus_connection_new_for_address_sync(address,
dbusFlags,
NULL,
NULL,
error);
}
}
static void
virGDBusSystemBusInit(void)
{
systemBus = virGDBusBusInit(G_BUS_TYPE_SYSTEM, &systemError);
}
static GDBusConnection *
virGDBusGetSystemBusInternal(void)
{
if (virOnce(&systemOnce, virGDBusSystemBusInit) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to run one time GDBus initializer"));
return NULL;
}
return systemBus;
}
GDBusConnection *
virGDBusGetSystemBus(void)
{
GDBusConnection *bus = virGDBusGetSystemBusInternal();
if (!bus) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to get system bus connection: %1$s"),
systemError->message);
return NULL;
}
return bus;
}
static void
virGDBusSessionBusInit(void)
{
sessionBus = virGDBusBusInit(G_BUS_TYPE_SESSION, &sessionError);
}
GDBusConnection *
virGDBusGetSessionBus(void)
{
if (virOnce(&sessionOnce, virGDBusSessionBusInit) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to run one time GDBus initializer"));
return NULL;
}
if (!sessionBus) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to get session bus connection: %1$s"),
sessionError->message);
return NULL;
}
return sessionBus;
}
/**
* virGDBusHasSystemBus:
*
* Check if DBus system bus is running. This does not imply that we have
* a connection. DBus might be running and refusing connections due to its
* client limit. The latter must be treated as a fatal error.
*
* Return false if dbus is not available, true if probably available.
*/
bool
virGDBusHasSystemBus(void)
{
g_autofree char *name = NULL;
if (virGDBusGetSystemBusInternal())
return true;
if (!g_dbus_error_is_remote_error(systemError))
return false;
name = g_dbus_error_get_remote_error(systemError);
if (name &&
(STREQ(name, "org.freedesktop.DBus.Error.FileNotFound") ||
STREQ(name, "org.freedesktop.DBus.Error.NoServer"))) {
VIR_DEBUG("System bus not available: %s", NULLSTR(systemError->message));
return false;
}
return true;
}
void
virGDBusCloseSystemBus(void)
{
if (!systemBus || sharedBus)
return;
g_dbus_connection_flush_sync(systemBus, NULL, NULL);
g_dbus_connection_close_sync(systemBus, NULL, NULL);
g_clear_pointer(&systemBus, g_object_unref);
}
#define VIR_DBUS_METHOD_CALL_TIMEOUT_MILIS 30 * 1000
/**
* virGDBusCallMethod:
* @conn: a DBus connection
* @reply: pointer to receive reply message, or NULL
* @replyType: pointer to GVariantType to validate reply data, or NULL
* @error: libvirt error pointer or NULL
* @busName: bus identifier of the target service
* @objectPath: object path of the target service
* @ifaceName: the interface of the object
* @method: the name of the method in the interface
* @data: pointer to data passed to DBus method
*
* If @error is NULL then a libvirt error will be raised when a DBus error
* is received and the return value will be -1. If @error is non-NULL then
* any DBus error will be saved into that object and the return value will
* be 0.
*
* Returns 0 on success, or -1 upon error.
*/
int
virGDBusCallMethod(GDBusConnection *conn,
GVariant **reply,
const GVariantType *replyType,
virErrorPtr error,
const char *busName,
const char *objectPath,
const char *ifaceName,
const char *method,
GVariant *data)
{
g_autoptr(GVariant) ret = NULL;
g_autoptr(GError) gerror = NULL;
if (error)
memset(error, 0, sizeof(*error));
if (data)
g_variant_ref_sink(data);
ret = g_dbus_connection_call_sync(conn,
busName,
objectPath,
ifaceName,
method,
data,
replyType,
G_DBUS_CALL_FLAGS_NONE,
VIR_DBUS_METHOD_CALL_TIMEOUT_MILIS,
NULL,
&gerror);
if (!ret) {
if (error && g_dbus_error_is_remote_error(gerror)) {
error->level = VIR_ERR_ERROR;
error->code = VIR_ERR_DBUS_SERVICE;
error->domain = VIR_FROM_DBUS;
error->str1 = g_dbus_error_get_remote_error(gerror);
error->message = g_strdup(gerror->message);
} else {
virReportError(VIR_ERR_DBUS_SERVICE, "%s", gerror->message);
return -1;
}
}
if (reply)
*reply = g_steal_pointer(&ret);
return 0;
}
#ifdef G_OS_UNIX
int
virGDBusCallMethodWithFD(GDBusConnection *conn,
GVariant **reply,
const GVariantType *replyType,
GUnixFDList **replyFD,
virErrorPtr error,
const char *busName,
const char *objectPath,
const char *ifaceName,
const char *method,
GVariant *data,
GUnixFDList *dataFD)
{
g_autoptr(GVariant) ret = NULL;
g_autoptr(GError) gerror = NULL;
if (error)
memset(error, 0, sizeof(*error));
if (data)
g_variant_ref_sink(data);
ret = g_dbus_connection_call_with_unix_fd_list_sync(conn,
busName,
objectPath,
ifaceName,
method,
data,
replyType,
G_DBUS_CALL_FLAGS_NONE,
VIR_DBUS_METHOD_CALL_TIMEOUT_MILIS,
dataFD,
replyFD,
NULL,
&gerror);
if (!ret) {
if (error && g_dbus_error_is_remote_error(gerror)) {
error->level = VIR_ERR_ERROR;
error->code = VIR_ERR_DBUS_SERVICE;
error->domain = VIR_FROM_DBUS;
error->str1 = g_dbus_error_get_remote_error(gerror);
error->message = g_strdup(gerror->message);
if (!error->str1 || !error->message)
return -1;
} else {
virReportError(VIR_ERR_DBUS_SERVICE, "%s", gerror->message);
return -1;
}
}
if (reply)
*reply = g_steal_pointer(&ret);
return 0;
}
#else
int
virGDBusCallMethodWithFD(GDBusConnection *conn G_GNUC_UNUSED,
GVariant **reply G_GNUC_UNUSED,
const GVariantType *replyType G_GNUC_UNUSED,
GUnixFDList **replyFD G_GNUC_UNUSED,
virErrorPtr error G_GNUC_UNUSED,
const char *busName G_GNUC_UNUSED,
const char *objectPath G_GNUC_UNUSED,
const char *ifaceName G_GNUC_UNUSED,
const char *method G_GNUC_UNUSED,
GVariant *data G_GNUC_UNUSED,
GUnixFDList *dataFD G_GNUC_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
_("Unix file descriptors not supported on this platform"));
return -1;
}
#endif
static int
virGDBusIsServiceInList(const char *listMethod,
const char *name)
{
GDBusConnection *conn;
g_autoptr(GVariant) reply = NULL;
g_autoptr(GVariantIter) iter = NULL;
char *str;
int rc;
if (!virGDBusHasSystemBus())
return -2;
conn = virGDBusGetSystemBus();
if (!conn)
return -1;
rc = virGDBusCallMethod(conn,
&reply,
G_VARIANT_TYPE("(as)"),
NULL,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
listMethod,
NULL);
if (rc < 0)
return -1;
g_variant_get(reply, "(as)", &iter);
while (g_variant_iter_loop(iter, "s", &str)) {
if (STREQ(str, name)) {
g_free(str);
return 0;
}
}
return -2;
}
/**
* virGDBusIsServiceEnabled:
* @name: service name
*
* Returns 0 if service is available, -1 on fatal error, or -2 if service is not available
*/
int
virGDBusIsServiceEnabled(const char *name)
{
int ret = virGDBusIsServiceInList("ListActivatableNames", name);
VIR_DEBUG("Service %s is %s", name, ret ? "unavailable" : "available");
return ret;
}
/**
* virGDBusIsServiceRegistered:
* @name: service name
*
* Returns 0 if service is registered, -1 on fatal error, or -2 if service is not registered
*/
int
virGDBusIsServiceRegistered(const char *name)
{
int ret = virGDBusIsServiceInList("ListNames", name);
VIR_DEBUG("Service %s is %s", name, ret ? "not registered" : "registered");
return ret;
}
bool
virGDBusErrorIsUnknownMethod(virErrorPtr err)
{
return err->domain == VIR_FROM_DBUS &&
err->code == VIR_ERR_DBUS_SERVICE &&
err->level == VIR_ERR_ERROR &&
STREQ_NULLABLE("org.freedesktop.DBus.Error.UnknownMethod",
err->str1);
}
bool
virGDBusMessageIsSignal(GDBusMessage *message,
const char *iface,
const char *signal)
{
GDBusMessageType type = g_dbus_message_get_message_type(message);
if (type == G_DBUS_MESSAGE_TYPE_SIGNAL) {
const char *interface = g_dbus_message_get_interface(message);
const char *member = g_dbus_message_get_member(message);
return STREQ(interface, iface) && STREQ(member, signal);
}
return false;
}
|