]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/dbus-common.c
unit: use safe downcasts, remove pointless casts
[thirdparty/systemd.git] / src / dbus-common.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
9a1ac7b9
LP
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <assert.h>
23#include <sys/socket.h>
24#include <errno.h>
25#include <unistd.h>
a8f11321
LP
26#include <stdio.h>
27#include <stdlib.h>
9a1ac7b9 28#include <dbus/dbus.h>
bfebab7f 29#include <string.h>
3df5bf61 30#include <sys/epoll.h>
9a1ac7b9
LP
31
32#include "log.h"
33#include "dbus-common.h"
b9978121 34#include "util.h"
f6a6225e 35#include "def.h"
bfebab7f 36#include "strv.h"
9a1ac7b9
LP
37
38int bus_check_peercred(DBusConnection *c) {
39 int fd;
40 struct ucred ucred;
41 socklen_t l;
42
43 assert(c);
44
45 assert_se(dbus_connection_get_unix_fd(c, &fd));
46
47 l = sizeof(struct ucred);
48 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
49 log_error("SO_PEERCRED failed: %m");
50 return -errno;
51 }
52
53 if (l != sizeof(struct ucred)) {
54 log_error("SO_PEERCRED returned wrong size.");
55 return -E2BIG;
56 }
57
be81bfc4 58 if (ucred.uid != 0 && ucred.uid != geteuid())
9a1ac7b9
LP
59 return -EPERM;
60
61 return 1;
62}
63
a8f11321
LP
64static int sync_auth(DBusConnection *bus, DBusError *error) {
65 usec_t begin, tstamp;
9a1ac7b9 66
a8f11321 67 assert(bus);
b9978121 68
a8f11321
LP
69 /* This complexity should probably move into D-Bus itself:
70 *
71 * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
9a1ac7b9 72
a8f11321
LP
73 begin = tstamp = now(CLOCK_MONOTONIC);
74 for (;;) {
9a1ac7b9 75
f6a6225e 76 if (tstamp > begin + DEFAULT_TIMEOUT_USEC)
a8f11321 77 break;
9a1ac7b9 78
a8f11321
LP
79 if (dbus_connection_get_is_authenticated(bus))
80 break;
f4579ce7 81
f6a6225e 82 if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
a8f11321 83 break;
b9978121 84
a8f11321
LP
85 tstamp = now(CLOCK_MONOTONIC);
86 }
b9978121 87
a8f11321
LP
88 if (!dbus_connection_get_is_connected(bus)) {
89 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
90 return -ECONNREFUSED;
91 }
b9978121 92
a8f11321
LP
93 if (!dbus_connection_get_is_authenticated(bus)) {
94 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
95 return -EACCES;
96 }
b9978121 97
a8f11321
LP
98 return 0;
99}
b9978121 100
be81bfc4
LP
101int bus_connect(DBusBusType t, DBusConnection **_bus, bool *_private, DBusError *error) {
102 DBusConnection *bus = NULL;
a8f11321 103 int r;
be81bfc4 104 bool private = true;
b9978121 105
a8f11321 106 assert(_bus);
b9978121 107
a8f11321 108 if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
be81bfc4
LP
109 /* If we are root, then let's talk directly to the
110 * system instance, instead of going via the bus */
111
112 bus = dbus_connection_open_private("unix:path=/run/systemd/private", error);
113 if (!bus)
114 return -EIO;
115
116 } else {
117 if (t == DBUS_BUS_SESSION) {
118 const char *e;
91b22f21 119
be81bfc4
LP
120 /* If we are supposed to talk to the instance,
121 * try via XDG_RUNTIME_DIR first, then
122 * fallback to normal bus access */
123
124 e = getenv("XDG_RUNTIME_DIR");
125 if (e) {
126 char *p;
127
128 if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0)
129 return -ENOMEM;
130
131 bus = dbus_connection_open_private(p, NULL);
132 free(p);
133 }
134 }
91b22f21 135
be81bfc4
LP
136 if (!bus) {
137 bus = dbus_bus_get_private(t, error);
138 if (!bus)
91b22f21 139 return -EIO;
be81bfc4
LP
140
141 private = false;
91b22f21 142 }
be81bfc4 143 }
a8f11321 144
be81bfc4 145 dbus_connection_set_exit_on_disconnect(bus, FALSE);
a8f11321 146
be81bfc4 147 if (private) {
a8f11321 148 if (bus_check_peercred(bus) < 0) {
b9978121
LP
149 dbus_connection_close(bus);
150 dbus_connection_unref(bus);
151
a8f11321 152 dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
b9978121
LP
153 return -EACCES;
154 }
9a1ac7b9
LP
155 }
156
be81bfc4
LP
157 r = sync_auth(bus, error);
158 if (r < 0) {
a8f11321
LP
159 dbus_connection_close(bus);
160 dbus_connection_unref(bus);
161 return r;
162 }
163
be81bfc4
LP
164 if (_private)
165 *_private = private;
166
a8f11321
LP
167 *_bus = bus;
168 return 0;
169}
170
171int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
172 DBusConnection *bus;
173 char *p = NULL;
174 int r;
175
176 assert(_bus);
177 assert(user || host);
178
179 if (user && host)
180 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
181 else if (user)
182 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
183 else if (host)
184 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
185
186 if (!p) {
187 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
188 return -ENOMEM;
189 }
190
191 bus = dbus_connection_open_private(p, error);
192 free(p);
193
194 if (!bus)
195 return -EIO;
196
197 dbus_connection_set_exit_on_disconnect(bus, FALSE);
198
199 if ((r = sync_auth(bus, error)) < 0) {
200 dbus_connection_close(bus);
201 dbus_connection_unref(bus);
202 return r;
203 }
204
205 if (!dbus_bus_register(bus, error)) {
206 dbus_connection_close(bus);
207 dbus_connection_unref(bus);
208 return r;
209 }
210
211 *_bus = bus;
212 return 0;
213}
214
215int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
216 DBusConnection *bus;
217 int r;
218
219 assert(_bus);
220
221 /* Don't bother with PolicyKit if we are root */
222 if (geteuid() == 0)
223 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
224
225 if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
226 return -EIO;
227
9a1ac7b9
LP
228 dbus_connection_set_exit_on_disconnect(bus, FALSE);
229
a8f11321
LP
230 if ((r = sync_auth(bus, error)) < 0) {
231 dbus_connection_close(bus);
232 dbus_connection_unref(bus);
233 return r;
234 }
235
236 if (!dbus_bus_register(bus, error)) {
237 dbus_connection_close(bus);
238 dbus_connection_unref(bus);
239 return r;
240 }
241
9a1ac7b9
LP
242 *_bus = bus;
243 return 0;
244}
4cf5d675
LP
245
246const char *bus_error_message(const DBusError *error) {
247 assert(error);
248
249 /* Sometimes the D-Bus server is a little bit too verbose with
250 * its error messages, so let's override them here */
251 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
252 return "Access denied";
253
254 return error->message;
255}
bfebab7f
LP
256
257DBusHandlerResult bus_default_message_handler(
258 DBusConnection *c,
259 DBusMessage *message,
260 const char *introspection,
261 const char *interfaces,
262 const BusProperty *properties) {
263
264 DBusError error;
265 DBusMessage *reply = NULL;
266 int r;
267
268 assert(c);
269 assert(message);
270
271 dbus_error_init(&error);
272
273 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
274
275 if (!(reply = dbus_message_new_method_return(message)))
276 goto oom;
277
278 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
279 goto oom;
280
281 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
282 const char *interface, *property;
283 const BusProperty *p;
284
285 if (!dbus_message_get_args(
286 message,
287 &error,
288 DBUS_TYPE_STRING, &interface,
289 DBUS_TYPE_STRING, &property,
290 DBUS_TYPE_INVALID))
291 return bus_send_error_reply(c, message, &error, -EINVAL);
292
293 for (p = properties; p->property; p++)
294 if (streq(p->interface, interface) && streq(p->property, property))
295 break;
296
297 if (p->property) {
298 DBusMessageIter iter, sub;
299
300 if (!(reply = dbus_message_new_method_return(message)))
301 goto oom;
302
303 dbus_message_iter_init_append(reply, &iter);
304
305 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
306 goto oom;
307
308 if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
309
310 if (r == -ENOMEM)
311 goto oom;
312
313 dbus_message_unref(reply);
314 return bus_send_error_reply(c, message, NULL, r);
315 }
316
317 if (!dbus_message_iter_close_container(&iter, &sub))
318 goto oom;
319 } else {
320 if (!nulstr_contains(interfaces, interface))
321 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
322 else
323 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
324
325 return bus_send_error_reply(c, message, &error, -EINVAL);
326 }
327
328 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
329 const char *interface;
330 const BusProperty *p;
331 DBusMessageIter iter, sub, sub2, sub3;
332
333 if (!dbus_message_get_args(
334 message,
335 &error,
336 DBUS_TYPE_STRING, &interface,
337 DBUS_TYPE_INVALID))
338 return bus_send_error_reply(c, message, &error, -EINVAL);
339
340 if (interface[0] && !nulstr_contains(interfaces, interface)) {
341 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
342 return bus_send_error_reply(c, message, &error, -EINVAL);
343 }
344
345 if (!(reply = dbus_message_new_method_return(message)))
346 goto oom;
347
348 dbus_message_iter_init_append(reply, &iter);
349
350 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
351 goto oom;
352
353 for (p = properties; p->property; p++) {
354 if (interface[0] && !streq(p->interface, interface))
355 continue;
356
357 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
358 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
359 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
360 goto oom;
361
362 if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
363
364 if (r == -ENOMEM)
365 goto oom;
366
367 dbus_message_unref(reply);
368 return bus_send_error_reply(c, message, NULL, r);
369 }
370
371 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
372 !dbus_message_iter_close_container(&sub, &sub2))
373 goto oom;
374 }
375
376 if (!dbus_message_iter_close_container(&iter, &sub))
377 goto oom;
378
379 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) {
380 const char *interface, *property;
381 DBusMessageIter iter;
382 const BusProperty *p;
383
384 if (!dbus_message_iter_init(message, &iter) ||
385 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
386 return bus_send_error_reply(c, message, NULL, -EINVAL);
387
388 dbus_message_iter_get_basic(&iter, &interface);
389
390 if (!dbus_message_iter_next(&iter) ||
391 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
392 return bus_send_error_reply(c, message, NULL, -EINVAL);
393
394 dbus_message_iter_get_basic(&iter, &property);
395
396 if (!dbus_message_iter_next(&iter) ||
397 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
398 dbus_message_iter_has_next(&iter))
399 return bus_send_error_reply(c, message, NULL, -EINVAL);
400
401 for (p = properties; p->property; p++)
402 if (streq(p->interface, interface) && streq(p->property, property))
403 break;
404
405 if (p->set) {
406 DBusMessageIter sub;
407 char *sig;
408
409 dbus_message_iter_recurse(&iter, &sub);
410
411 if (!(sig = dbus_message_iter_get_signature(&sub)))
412 goto oom;
413
414 if (!streq(sig, p->signature)) {
415 dbus_free(sig);
416 return bus_send_error_reply(c, message, NULL, -EINVAL);
417 }
418
419 dbus_free(sig);
420
421 if ((r = p->set(&sub, property)) < 0) {
422 if (r == -ENOMEM)
423 goto oom;
424 return bus_send_error_reply(c, message, NULL, r);
425 }
426
427 if (!(reply = dbus_message_new_method_return(message)))
428 goto oom;
429 } else {
430 if (p->property)
431 dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
432 else if (!nulstr_contains(interfaces, interface))
433 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
434 else
435 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
436
437 return bus_send_error_reply(c, message, &error, -EINVAL);
438 }
439
cda2b84a
MS
440 } else {
441 const char *interface = dbus_message_get_interface(message);
442
443 if (!interface || !nulstr_contains(interfaces, interface)) {
444 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
445 return bus_send_error_reply(c, message, &error, -EINVAL);
446 }
bfebab7f
LP
447 }
448
449 if (reply) {
450 if (!dbus_connection_send(c, reply, NULL))
451 goto oom;
452
453 dbus_message_unref(reply);
454 return DBUS_HANDLER_RESULT_HANDLED;
455 }
456
457 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
458
459oom:
460 if (reply)
461 dbus_message_unref(reply);
462
463 dbus_error_free(&error);
464
465 return DBUS_HANDLER_RESULT_NEED_MEMORY;
466}
467
468int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
469 const char *t = data;
470
471 assert(i);
472 assert(property);
473
474 if (!t)
475 t = "";
476
477 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
478 return -ENOMEM;
479
480 return 0;
481}
482
483int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
bfebab7f
LP
484 char **t = data;
485
486 assert(i);
487 assert(property);
488
98a28fef 489 return bus_append_strv_iter(i, t);
bfebab7f
LP
490}
491
492int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
493 bool *b = data;
494 dbus_bool_t db;
495
496 assert(i);
497 assert(property);
498 assert(b);
499
500 db = *b;
501
502 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
503 return -ENOMEM;
504
505 return 0;
506}
507
508int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
509 assert(i);
510 assert(property);
511 assert(data);
512
bd253d1b 513 /* Let's ensure that usec_t is actually 64bit, and hence this
bfebab7f
LP
514 * function can be used for usec_t */
515 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
516
517 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
518 return -ENOMEM;
519
520 return 0;
521}
522
523int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
524 assert(i);
525 assert(property);
526 assert(data);
527
bd253d1b
LP
528 /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
529 * 32bit, and hence this function can be used for
530 * pid_t/mode_t/uid_t/gid_t */
bfebab7f
LP
531 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
532 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
533 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
bd253d1b
LP
534 assert_cc(sizeof(uint32_t) == sizeof(uid_t));
535 assert_cc(sizeof(uint32_t) == sizeof(gid_t));
bfebab7f
LP
536
537 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
538 return -ENOMEM;
539
540 return 0;
541}
542
543int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
544 assert(i);
545 assert(property);
546 assert(data);
547
548 assert_cc(sizeof(int32_t) == sizeof(int));
549
550 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
551 return -ENOMEM;
552
553 return 0;
554}
555
556int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
557 uint64_t u;
558
559 assert(i);
560 assert(property);
561 assert(data);
562
563 u = (uint64_t) *(size_t*) data;
564
565 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
566 return -ENOMEM;
567
568 return 0;
569}
570
571int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
572 uint64_t u;
573
574 assert(i);
575 assert(property);
576 assert(data);
577
578 u = (uint64_t) *(unsigned long*) data;
579
580 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
581 return -ENOMEM;
916abb21
LP
582
583 return 0;
584}
585
586int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
572481b9 587 int64_t l;
916abb21
LP
588
589 assert(i);
590 assert(property);
591 assert(data);
592
572481b9 593 l = (int64_t) *(long*) data;
916abb21 594
572481b9 595 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
916abb21 596 return -ENOMEM;
bfebab7f
LP
597
598 return 0;
599}
600
601const char *bus_errno_to_dbus(int error) {
602
603 switch(error) {
604
605 case -EINVAL:
606 return DBUS_ERROR_INVALID_ARGS;
607
608 case -ENOMEM:
609 return DBUS_ERROR_NO_MEMORY;
610
611 case -EPERM:
612 case -EACCES:
613 return DBUS_ERROR_ACCESS_DENIED;
614
615 case -ESRCH:
616 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
617
618 case -ENOENT:
619 return DBUS_ERROR_FILE_NOT_FOUND;
620
621 case -EEXIST:
622 return DBUS_ERROR_FILE_EXISTS;
623
624 case -ETIMEDOUT:
625 case -ETIME:
626 return DBUS_ERROR_TIMEOUT;
627
628 case -EIO:
629 return DBUS_ERROR_IO_ERROR;
630
631 case -ENETRESET:
632 case -ECONNABORTED:
633 case -ECONNRESET:
634 return DBUS_ERROR_DISCONNECTED;
635 }
636
637 return DBUS_ERROR_FAILED;
638}
639
640DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
641 DBusMessage *reply = NULL;
642 const char *name, *text;
643
644 if (berror && dbus_error_is_set(berror)) {
645 name = berror->name;
646 text = berror->message;
647 } else {
648 name = bus_errno_to_dbus(error);
649 text = strerror(-error);
650 }
651
652 if (!(reply = dbus_message_new_error(message, name, text)))
653 goto oom;
654
655 if (!dbus_connection_send(c, reply, NULL))
656 goto oom;
657
658 dbus_message_unref(reply);
659
660 if (berror)
661 dbus_error_free(berror);
662
663 return DBUS_HANDLER_RESULT_HANDLED;
664
665oom:
666 if (reply)
667 dbus_message_unref(reply);
668
669 if (berror)
670 dbus_error_free(berror);
671
672 return DBUS_HANDLER_RESULT_NEED_MEMORY;
673}
674
675DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
676 DBusMessage *m;
677 DBusMessageIter iter, sub;
678 const char *i;
679
680 assert(interface);
681 assert(properties);
682
683 if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
684 goto oom;
685
686 dbus_message_iter_init_append(m, &iter);
687
688 /* We won't send any property values, since they might be
689 * large and sometimes not cheap to generated */
690
691 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
692 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
693 !dbus_message_iter_close_container(&iter, &sub) ||
694 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
695 goto oom;
696
697 NULSTR_FOREACH(i, properties)
698 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
699 goto oom;
700
701 if (!dbus_message_iter_close_container(&iter, &sub))
702 goto oom;
703
704 return m;
705
706oom:
707 if (m)
708 dbus_message_unref(m);
709
710 return NULL;
711}
3df5bf61
LP
712
713uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
714 unsigned flags;
715 uint32_t events = 0;
716
717 assert(bus_watch);
718
719 /* no watch flags for disabled watches */
720 if (!dbus_watch_get_enabled(bus_watch))
721 return 0;
722
723 flags = dbus_watch_get_flags(bus_watch);
724
725 if (flags & DBUS_WATCH_READABLE)
726 events |= EPOLLIN;
727 if (flags & DBUS_WATCH_WRITABLE)
728 events |= EPOLLOUT;
729
730 return events | EPOLLHUP | EPOLLERR;
731}
732
733unsigned bus_events_to_flags(uint32_t events) {
734 unsigned flags = 0;
735
736 if (events & EPOLLIN)
737 flags |= DBUS_WATCH_READABLE;
738 if (events & EPOLLOUT)
739 flags |= DBUS_WATCH_WRITABLE;
740 if (events & EPOLLHUP)
741 flags |= DBUS_WATCH_HANGUP;
742 if (events & EPOLLERR)
743 flags |= DBUS_WATCH_ERROR;
744
745 return flags;
746}
8d0e38a2
LP
747
748int bus_parse_strv(DBusMessage *m, char ***_l) {
749 DBusMessageIter iter;
750
751 assert(m);
752 assert(_l);
753
754 if (!dbus_message_iter_init(m, &iter))
755 return -EINVAL;
756
757 return bus_parse_strv_iter(&iter, _l);
758}
759
760int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
761 DBusMessageIter sub;
762 unsigned n = 0, i = 0;
763 char **l;
764
765 assert(iter);
766 assert(_l);
767
768 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
769 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
770 return -EINVAL;
771
772 dbus_message_iter_recurse(iter, &sub);
773
774 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
775 n++;
776 dbus_message_iter_next(&sub);
777 }
778
779 if (!(l = new(char*, n+1)))
780 return -ENOMEM;
781
782 dbus_message_iter_recurse(iter, &sub);
783
784 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
785 const char *s;
786
787 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
788 dbus_message_iter_get_basic(&sub, &s);
789
790 if (!(l[i++] = strdup(s))) {
791 strv_free(l);
792 return -ENOMEM;
793 }
794
795 dbus_message_iter_next(&sub);
796 }
797
798 assert(i == n);
799 l[i] = NULL;
800
801 if (_l)
802 *_l = l;
803
804 return 0;
805}
98a28fef
LP
806
807int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
808 DBusMessageIter sub;
809
810 assert(iter);
811
812 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &sub))
813 return -ENOMEM;
814
815 STRV_FOREACH(l, l)
816 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, l))
817 return -ENOMEM;
818
819 if (!dbus_message_iter_close_container(iter, &sub))
820 return -ENOMEM;
821
822 return 0;
823}
abca4822
LP
824
825int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
826
827 assert(iter);
828 assert(data);
829
830 if (dbus_message_iter_get_arg_type(iter) != type)
831 return -EIO;
832
833 dbus_message_iter_get_basic(iter, data);
834
835 if (!dbus_message_iter_next(iter) != !next)
836 return -EIO;
837
838 return 0;
839}
a4c279f8
LP
840
841int generic_print_property(const char *name, DBusMessageIter *iter, bool all) {
842 assert(name);
843 assert(iter);
844
845 switch (dbus_message_iter_get_arg_type(iter)) {
846
847 case DBUS_TYPE_STRING: {
848 const char *s;
849 dbus_message_iter_get_basic(iter, &s);
850
851 if (all || !isempty(s))
852 printf("%s=%s\n", name, s);
853
854 return 1;
855 }
856
857 case DBUS_TYPE_BOOLEAN: {
858 dbus_bool_t b;
859
860 dbus_message_iter_get_basic(iter, &b);
861 printf("%s=%s\n", name, yes_no(b));
862
863 return 1;
864 }
865
866 case DBUS_TYPE_UINT64: {
867 uint64_t u;
868 dbus_message_iter_get_basic(iter, &u);
869
870 /* Yes, heuristics! But we can change this check
871 * should it turn out to not be sufficient */
872
873 if (endswith(name, "Timestamp")) {
874 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
875
876 t = format_timestamp(timestamp, sizeof(timestamp), u);
877 if (t || all)
878 printf("%s=%s\n", name, strempty(t));
879
880 } else if (strstr(name, "USec")) {
881 char timespan[FORMAT_TIMESPAN_MAX];
882
883 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
884 } else
885 printf("%s=%llu\n", name, (unsigned long long) u);
886
887 return 1;
888 }
889
890 case DBUS_TYPE_UINT32: {
891 uint32_t u;
892 dbus_message_iter_get_basic(iter, &u);
893
894 if (strstr(name, "UMask") || strstr(name, "Mode"))
895 printf("%s=%04o\n", name, u);
896 else
897 printf("%s=%u\n", name, (unsigned) u);
898
899 return 1;
900 }
901
902 case DBUS_TYPE_INT32: {
903 int32_t i;
904 dbus_message_iter_get_basic(iter, &i);
905
906 printf("%s=%i\n", name, (int) i);
907 return 1;
908 }
909
910 case DBUS_TYPE_DOUBLE: {
911 double d;
912 dbus_message_iter_get_basic(iter, &d);
913
914 printf("%s=%g\n", name, d);
915 return 1;
916 }
917
918 case DBUS_TYPE_ARRAY:
919
920 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
921 DBusMessageIter sub;
922 bool space = false;
923
924 dbus_message_iter_recurse(iter, &sub);
925 if (all ||
926 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
927 printf("%s=", name);
928
929 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
930 const char *s;
931
932 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
933 dbus_message_iter_get_basic(&sub, &s);
934 printf("%s%s", space ? " " : "", s);
935
936 space = true;
937 dbus_message_iter_next(&sub);
938 }
939
940 puts("");
941 }
942
943 return 1;
944
945 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
946 DBusMessageIter sub;
947
948 dbus_message_iter_recurse(iter, &sub);
949 if (all ||
950 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
951 printf("%s=", name);
952
953 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
954 uint8_t u;
955
956 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
957 dbus_message_iter_get_basic(&sub, &u);
958 printf("%02x", u);
959
960 dbus_message_iter_next(&sub);
961 }
962
963 puts("");
964 }
965
966 return 1;
967 }
968
969 break;
970 }
971
972 return 0;
973}
ad740100
LP
974
975static void release_name_pending_cb(DBusPendingCall *pending, void *userdata) {
976 DBusMessage *reply;
977 DBusConnection *bus = userdata;
978
979 assert_se(reply = dbus_pending_call_steal_reply(pending));
980 dbus_message_unref(reply);
981
982 dbus_connection_close(bus);
983}
984
985void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) {
986 DBusMessage *m = NULL;
987 DBusPendingCall *pending = NULL;
988
989 assert(bus);
990
991 /* We unregister the name here, but we continue to process
992 * requests, until we get the response for it, so that all
993 * requests are guaranteed to be processed. */
994
995 m = dbus_message_new_method_call(
996 DBUS_SERVICE_DBUS,
997 DBUS_PATH_DBUS,
998 DBUS_INTERFACE_DBUS,
999 "ReleaseName");
1000 if (!m)
1001 goto oom;
1002
1003 if (!dbus_message_append_args(
1004 m,
1005 DBUS_TYPE_STRING,
1006 &name,
1007 DBUS_TYPE_INVALID))
1008 goto oom;
1009
1010 if (!dbus_connection_send_with_reply(bus, m, &pending, -1))
1011 goto oom;
1012
1013 if (!dbus_pending_call_set_notify(pending, release_name_pending_cb, bus, NULL))
1014 goto oom;
1015
1016 dbus_message_unref(m);
1017 dbus_pending_call_unref(pending);
1018
1019 return;
1020
1021oom:
1022 log_error("Out of memory");
1023
1024 if (pending) {
1025 dbus_pending_call_cancel(pending);
1026 dbus_pending_call_unref(pending);
1027 }
1028
1029 if (m)
1030 dbus_message_unref(m);
1031}
1032
1033DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata) {
1034 usec_t *remain_until = userdata;
1035
1036 assert(bus);
1037 assert(m);
1038 assert(remain_until);
1039
1040 /* Everytime we get a new message we reset out timeout */
1041 *remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
1042
1043 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected"))
1044 dbus_connection_close(bus);
1045
1046 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1047}