1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/socket.h>
28 #include <dbus/dbus.h>
30 #include <sys/epoll.h>
33 #include "dbus-common.h"
39 int bus_check_peercred(DBusConnection *c) {
46 assert_se(dbus_connection_get_unix_fd(c, &fd));
48 l = sizeof(struct ucred);
49 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
50 log_error("SO_PEERCRED failed: %m");
54 if (l != sizeof(struct ucred)) {
55 log_error("SO_PEERCRED returned wrong size.");
59 if (ucred.uid != 0 && ucred.uid != geteuid())
65 static int sync_auth(DBusConnection *bus, DBusError *error) {
70 /* This complexity should probably move into D-Bus itself:
72 * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
74 begin = tstamp = now(CLOCK_MONOTONIC);
77 if (tstamp > begin + DEFAULT_TIMEOUT_USEC)
80 if (dbus_connection_get_is_authenticated(bus))
83 if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
86 tstamp = now(CLOCK_MONOTONIC);
89 if (!dbus_connection_get_is_connected(bus)) {
90 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
94 if (!dbus_connection_get_is_authenticated(bus)) {
95 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
102 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *_private, DBusError *error) {
103 DBusConnection *bus = NULL;
109 if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
110 /* If we are root, then let's talk directly to the
111 * system instance, instead of going via the bus */
113 bus = dbus_connection_open_private("unix:path=/run/systemd/private", error);
118 if (t == DBUS_BUS_SESSION) {
121 /* If we are supposed to talk to the instance,
122 * try via XDG_RUNTIME_DIR first, then
123 * fallback to normal bus access */
125 e = secure_getenv("XDG_RUNTIME_DIR");
129 if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0)
132 bus = dbus_connection_open_private(p, NULL);
138 bus = dbus_bus_get_private(t, error);
146 dbus_connection_set_exit_on_disconnect(bus, FALSE);
149 if (bus_check_peercred(bus) < 0) {
150 dbus_connection_close(bus);
151 dbus_connection_unref(bus);
153 dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
158 r = sync_auth(bus, error);
160 dbus_connection_close(bus);
161 dbus_connection_unref(bus);
172 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
178 assert(user || host);
181 asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
183 asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
185 asprintf(&p, "unixexec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
188 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
192 bus = dbus_connection_open_private(p, error);
198 dbus_connection_set_exit_on_disconnect(bus, FALSE);
200 if ((r = sync_auth(bus, error)) < 0) {
201 dbus_connection_close(bus);
202 dbus_connection_unref(bus);
206 if (!dbus_bus_register(bus, error)) {
207 dbus_connection_close(bus);
208 dbus_connection_unref(bus);
216 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
222 /* Don't bother with PolicyKit if we are root */
224 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
226 bus = dbus_connection_open_private("unixexec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error);
230 dbus_connection_set_exit_on_disconnect(bus, FALSE);
232 r = sync_auth(bus, error);
234 dbus_connection_close(bus);
235 dbus_connection_unref(bus);
239 if (!dbus_bus_register(bus, error)) {
240 dbus_connection_close(bus);
241 dbus_connection_unref(bus);
249 const char *bus_error_message(const DBusError *error) {
253 /* Sometimes the D-Bus server is a little bit too verbose with
254 * its error messages, so let's override them here */
255 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
256 return "Access denied";
258 return error->message;
261 const char *bus_error_message_or_strerror(const DBusError *error, int err) {
263 if (error && dbus_error_is_set(error))
264 return bus_error_message(error);
266 return strerror(err);
269 DBusHandlerResult bus_default_message_handler(
271 DBusMessage *message,
272 const char *introspection,
273 const char *interfaces,
274 const BusBoundProperties *bound_properties) {
277 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
283 dbus_error_init(&error);
285 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
287 reply = dbus_message_new_method_return(message);
291 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
294 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && bound_properties) {
295 const char *interface, *property;
296 const BusBoundProperties *bp;
297 const BusProperty *p;
299 DBusMessageIter iter, sub;
301 if (!dbus_message_get_args(
304 DBUS_TYPE_STRING, &interface,
305 DBUS_TYPE_STRING, &property,
307 return bus_send_error_reply(c, message, &error, -EINVAL);
309 for (bp = bound_properties; bp->interface; bp++) {
310 if (!streq(bp->interface, interface))
313 for (p = bp->properties; p->property; p++)
314 if (streq(p->property, property))
319 if (!nulstr_contains(interfaces, interface))
320 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
322 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
324 return bus_send_error_reply(c, message, &error, -EINVAL);
327 reply = dbus_message_new_method_return(message);
331 dbus_message_iter_init_append(reply, &iter);
333 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
336 data = (char*)bp->base + p->offset;
338 data = *(void**)data;
340 r = p->append(&sub, property, data);
344 return bus_send_error_reply(c, message, NULL, r);
346 if (!dbus_message_iter_close_container(&iter, &sub))
349 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && bound_properties) {
350 const char *interface;
351 const BusBoundProperties *bp;
352 const BusProperty *p;
353 DBusMessageIter iter, sub, sub2, sub3;
355 if (!dbus_message_get_args(
358 DBUS_TYPE_STRING, &interface,
360 return bus_send_error_reply(c, message, &error, -EINVAL);
362 if (interface[0] && !nulstr_contains(interfaces, interface)) {
363 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
364 return bus_send_error_reply(c, message, &error, -EINVAL);
367 reply = dbus_message_new_method_return(message);
371 dbus_message_iter_init_append(reply, &iter);
373 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
376 for (bp = bound_properties; bp->interface; bp++) {
377 if (interface[0] && !streq(bp->interface, interface))
380 for (p = bp->properties; p->property; p++) {
383 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
384 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
385 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
388 data = (char*)bp->base + p->offset;
390 data = *(void**)data;
391 r = p->append(&sub3, p->property, data);
395 return bus_send_error_reply(c, message, NULL, r);
397 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
398 !dbus_message_iter_close_container(&sub, &sub2))
403 if (!dbus_message_iter_close_container(&iter, &sub))
406 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && bound_properties) {
407 const char *interface, *property;
408 DBusMessageIter iter;
409 const BusBoundProperties *bp;
410 const BusProperty *p;
414 DBusMessage *changed;
416 if (!dbus_message_iter_init(message, &iter) ||
417 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
418 return bus_send_error_reply(c, message, NULL, -EINVAL);
420 dbus_message_iter_get_basic(&iter, &interface);
422 if (!dbus_message_iter_next(&iter) ||
423 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
424 return bus_send_error_reply(c, message, NULL, -EINVAL);
426 dbus_message_iter_get_basic(&iter, &property);
428 if (!dbus_message_iter_next(&iter) ||
429 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
430 dbus_message_iter_has_next(&iter))
431 return bus_send_error_reply(c, message, NULL, -EINVAL);
433 for (bp = bound_properties; bp->interface; bp++) {
434 if (!streq(bp->interface, interface))
437 for (p = bp->properties; p->property; p++)
438 if (streq(p->property, property))
443 if (!nulstr_contains(interfaces, interface))
444 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
446 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
448 return bus_send_error_reply(c, message, &error, -EINVAL);
452 dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
453 return bus_send_error_reply(c, message, &error, -EINVAL);
456 dbus_message_iter_recurse(&iter, &sub);
458 sig = dbus_message_iter_get_signature(&sub);
462 if (!streq(sig, p->signature)) {
464 return bus_send_error_reply(c, message, NULL, -EINVAL);
468 data = (uint8_t*) bp->base + p->offset;
470 data = *(void**)data;
472 r = p->set(&sub, property, data);
476 return bus_send_error_reply(c, message, NULL, r);
478 reply = dbus_message_new_method_return(message);
482 /* Send out a signal about this, but it doesn't really
483 * matter if this fails, so eat all errors */
484 changed = bus_properties_changed_one_new(
485 dbus_message_get_path(message),
489 dbus_connection_send(c, changed, NULL);
490 dbus_message_unref(changed);
495 const char *interface = dbus_message_get_interface(message);
497 if (!interface || !nulstr_contains(interfaces, interface)) {
498 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
499 return bus_send_error_reply(c, message, &error, -EINVAL);
504 if (!bus_maybe_send_reply(c, message, reply))
507 return DBUS_HANDLER_RESULT_HANDLED;
510 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
513 dbus_error_free(&error);
515 return DBUS_HANDLER_RESULT_NEED_MEMORY;
518 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
519 const char *t = data;
527 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
533 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
539 return bus_append_strv_iter(i, t);
542 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
552 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
558 int bus_property_append_tristate_false(DBusMessageIter *i, const char *property, void *data) {
568 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
574 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
579 /* Let's ensure that usec_t is actually 64bit, and hence this
580 * function can be used for usec_t */
581 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
583 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
589 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
594 /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
595 * 32bit, and hence this function can be used for
596 * pid_t/mode_t/uid_t/gid_t */
597 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
598 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
599 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
600 assert_cc(sizeof(uint32_t) == sizeof(uid_t));
601 assert_cc(sizeof(uint32_t) == sizeof(gid_t));
603 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
609 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
614 assert_cc(sizeof(int32_t) == sizeof(int));
616 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
622 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
629 u = (uint64_t) *(size_t*) data;
631 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
637 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
644 u = (uint64_t) *(unsigned long*) data;
646 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
652 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
659 l = (int64_t) *(long*) data;
661 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
667 int bus_property_set_uint64(DBusMessageIter *i, const char *property, void *data) {
673 dbus_message_iter_get_basic(i, t);
677 const char *bus_errno_to_dbus(int error) {
682 return DBUS_ERROR_INVALID_ARGS;
685 return DBUS_ERROR_NO_MEMORY;
689 return DBUS_ERROR_ACCESS_DENIED;
692 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
695 return DBUS_ERROR_FILE_NOT_FOUND;
698 return DBUS_ERROR_FILE_EXISTS;
702 return DBUS_ERROR_TIMEOUT;
705 return DBUS_ERROR_IO_ERROR;
710 return DBUS_ERROR_DISCONNECTED;
713 return DBUS_ERROR_FAILED;
716 dbus_bool_t bus_maybe_send_reply (DBusConnection *c,
717 DBusMessage *message,
720 if (dbus_message_get_no_reply (message))
722 return dbus_connection_send (c, reply, NULL);
725 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
726 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
727 const char *name, *text;
729 if (berror && dbus_error_is_set(berror)) {
731 text = berror->message;
733 name = bus_errno_to_dbus(error);
734 text = strerror(-error);
737 reply = dbus_message_new_error(message, name, text);
741 if (!bus_maybe_send_reply(c, message, reply))
745 dbus_error_free(berror);
747 return DBUS_HANDLER_RESULT_HANDLED;
751 dbus_message_unref(reply);
754 dbus_error_free(berror);
756 return DBUS_HANDLER_RESULT_NEED_MEMORY;
759 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
761 DBusMessageIter iter, sub;
767 m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
771 dbus_message_iter_init_append(m, &iter);
773 /* We won't send any property values, since they might be
774 * large and sometimes not cheap to generated */
776 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
777 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
778 !dbus_message_iter_close_container(&iter, &sub) ||
779 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
782 NULSTR_FOREACH(i, properties)
783 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
786 if (!dbus_message_iter_close_container(&iter, &sub))
793 dbus_message_unref(m);
798 DBusMessage* bus_properties_changed_one_new(const char *path, const char *interface, const char *property) {
800 DBusMessageIter iter, sub;
805 m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
809 dbus_message_iter_init_append(m, &iter);
811 /* We won't send any property values, since they might be
812 * large and sometimes not cheap to generated */
814 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
815 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
816 !dbus_message_iter_close_container(&iter, &sub) ||
817 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
820 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &property))
823 if (!dbus_message_iter_close_container(&iter, &sub))
830 dbus_message_unref(m);
835 uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
841 /* no watch flags for disabled watches */
842 if (!dbus_watch_get_enabled(bus_watch))
845 flags = dbus_watch_get_flags(bus_watch);
847 if (flags & DBUS_WATCH_READABLE)
849 if (flags & DBUS_WATCH_WRITABLE)
852 return events | EPOLLHUP | EPOLLERR;
855 unsigned bus_events_to_flags(uint32_t events) {
858 if (events & EPOLLIN)
859 flags |= DBUS_WATCH_READABLE;
860 if (events & EPOLLOUT)
861 flags |= DBUS_WATCH_WRITABLE;
862 if (events & EPOLLHUP)
863 flags |= DBUS_WATCH_HANGUP;
864 if (events & EPOLLERR)
865 flags |= DBUS_WATCH_ERROR;
870 int bus_parse_strv(DBusMessage *m, char ***_l) {
871 DBusMessageIter iter;
876 if (!dbus_message_iter_init(m, &iter))
879 return bus_parse_strv_iter(&iter, _l);
882 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
884 unsigned n = 0, i = 0;
890 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
891 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
894 dbus_message_iter_recurse(iter, &sub);
896 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
898 dbus_message_iter_next(&sub);
905 dbus_message_iter_recurse(iter, &sub);
907 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
910 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
911 dbus_message_iter_get_basic(&sub, &s);
913 if (!(l[i++] = strdup(s))) {
918 dbus_message_iter_next(&sub);
930 int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
935 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &sub))
939 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, l))
942 if (!dbus_message_iter_close_container(iter, &sub))
948 int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
953 if (dbus_message_iter_get_arg_type(iter) != type)
956 dbus_message_iter_get_basic(iter, data);
958 if (!dbus_message_iter_next(iter) != !next)
964 int generic_print_property(const char *name, DBusMessageIter *iter, bool all) {
968 switch (dbus_message_iter_get_arg_type(iter)) {
970 case DBUS_TYPE_STRING: {
972 dbus_message_iter_get_basic(iter, &s);
974 if (all || !isempty(s))
975 printf("%s=%s\n", name, s);
980 case DBUS_TYPE_BOOLEAN: {
983 dbus_message_iter_get_basic(iter, &b);
984 printf("%s=%s\n", name, yes_no(b));
989 case DBUS_TYPE_UINT64: {
991 dbus_message_iter_get_basic(iter, &u);
993 /* Yes, heuristics! But we can change this check
994 * should it turn out to not be sufficient */
996 if (endswith(name, "Timestamp")) {
997 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
999 t = format_timestamp(timestamp, sizeof(timestamp), u);
1001 printf("%s=%s\n", name, strempty(t));
1003 } else if (strstr(name, "USec")) {
1004 char timespan[FORMAT_TIMESPAN_MAX];
1006 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1008 printf("%s=%llu\n", name, (unsigned long long) u);
1013 case DBUS_TYPE_UINT32: {
1015 dbus_message_iter_get_basic(iter, &u);
1017 if (strstr(name, "UMask") || strstr(name, "Mode"))
1018 printf("%s=%04o\n", name, u);
1020 printf("%s=%u\n", name, (unsigned) u);
1025 case DBUS_TYPE_INT32: {
1027 dbus_message_iter_get_basic(iter, &i);
1029 printf("%s=%i\n", name, (int) i);
1033 case DBUS_TYPE_DOUBLE: {
1035 dbus_message_iter_get_basic(iter, &d);
1037 printf("%s=%g\n", name, d);
1041 case DBUS_TYPE_ARRAY:
1043 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1044 DBusMessageIter sub;
1047 dbus_message_iter_recurse(iter, &sub);
1049 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1050 printf("%s=", name);
1052 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1055 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1056 dbus_message_iter_get_basic(&sub, &s);
1057 printf("%s%s", space ? " " : "", s);
1060 dbus_message_iter_next(&sub);
1068 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1069 DBusMessageIter sub;
1071 dbus_message_iter_recurse(iter, &sub);
1073 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1074 printf("%s=", name);
1076 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1079 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1080 dbus_message_iter_get_basic(&sub, &u);
1083 dbus_message_iter_next(&sub);
1091 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_UINT32) {
1092 DBusMessageIter sub;
1094 dbus_message_iter_recurse(iter, &sub);
1096 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1097 printf("%s=", name);
1099 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1102 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32);
1103 dbus_message_iter_get_basic(&sub, &u);
1106 dbus_message_iter_next(&sub);
1121 static void release_name_pending_cb(DBusPendingCall *pending, void *userdata) {
1123 DBusConnection *bus = userdata;
1125 assert_se(reply = dbus_pending_call_steal_reply(pending));
1126 dbus_message_unref(reply);
1128 dbus_connection_close(bus);
1131 void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) {
1132 _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
1133 DBusPendingCall *pending = NULL;
1137 /* We unregister the name here, but we continue to process
1138 * requests, until we get the response for it, so that all
1139 * requests are guaranteed to be processed. */
1141 m = dbus_message_new_method_call(
1144 DBUS_INTERFACE_DBUS,
1149 if (!dbus_message_append_args(
1156 if (!dbus_connection_send_with_reply(bus, m, &pending, -1))
1159 if (!dbus_pending_call_set_notify(pending, release_name_pending_cb, bus, NULL))
1162 dbus_pending_call_unref(pending);
1170 dbus_pending_call_cancel(pending);
1171 dbus_pending_call_unref(pending);
1175 DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata) {
1176 usec_t *remain_until = userdata;
1180 assert(remain_until);
1182 /* Every time we get a new message we reset out timeout */
1183 *remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
1185 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected"))
1186 dbus_connection_close(bus);
1188 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1191 /* This mimics dbus_bus_get_unix_user() */
1192 pid_t bus_get_unix_process_id(
1193 DBusConnection *connection,
1197 _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL;
1200 m = dbus_message_new_method_call(
1203 DBUS_INTERFACE_DBUS,
1204 "GetConnectionUnixProcessID");
1206 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
1210 if (!dbus_message_append_args(
1212 DBUS_TYPE_STRING, &name,
1213 DBUS_TYPE_INVALID)) {
1214 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
1218 reply = dbus_connection_send_with_reply_and_block(connection, m, -1, error);
1222 if (dbus_set_error_from_message(error, reply))
1225 if (!dbus_message_get_args(
1227 DBUS_TYPE_UINT32, &pid,
1234 bool bus_error_is_no_service(const DBusError *error) {
1237 if (!dbus_error_is_set(error))
1240 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
1243 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
1246 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
1249 int bus_method_call_with_reply(
1250 DBusConnection *bus,
1251 const char *destination,
1253 const char *interface,
1255 DBusMessage **return_reply,
1256 DBusError *return_error,
1257 int first_arg_type, ...) {
1260 _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
1265 dbus_error_init(&error);
1268 m = dbus_message_new_method_call(destination, path, interface, method);
1274 va_start(ap, first_arg_type);
1275 if (!dbus_message_append_args_valist(m, first_arg_type, ap)) {
1282 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1285 log_error("Failed to issue method call: %s", bus_error_message(&error));
1287 if (bus_error_is_no_service(&error))
1289 else if (dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED))
1291 else if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY))
1299 *return_reply = reply;
1301 dbus_message_unref(reply);
1305 *return_error = error;
1307 dbus_error_free(&error);
1312 void bus_message_unrefp(DBusMessage **reply) {
1319 dbus_message_unref(*reply);
1322 const char *bus_message_get_sender_with_fallback(DBusMessage *m) {
1327 s = dbus_message_get_sender(m);
1331 /* When the message came in from a direct connection the
1332 * message will have no sender. We fix that here. */
1334 return ":no-sender";