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 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.
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.
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/>.
23 #include <sys/socket.h>
28 #include <dbus/dbus.h>
30 #include <sys/epoll.h>
33 #include "dbus-common.h"
38 int bus_check_peercred(DBusConnection
*c
) {
45 assert_se(dbus_connection_get_unix_fd(c
, &fd
));
47 l
= sizeof(struct ucred
);
48 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0) {
49 log_error("SO_PEERCRED failed: %m");
53 if (l
!= sizeof(struct ucred
)) {
54 log_error("SO_PEERCRED returned wrong size.");
58 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
64 static int sync_auth(DBusConnection
*bus
, DBusError
*error
) {
69 /* This complexity should probably move into D-Bus itself:
71 * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
73 begin
= tstamp
= now(CLOCK_MONOTONIC
);
76 if (tstamp
> begin
+ DEFAULT_TIMEOUT_USEC
)
79 if (dbus_connection_get_is_authenticated(bus
))
82 if (!dbus_connection_read_write_dispatch(bus
, ((begin
+ DEFAULT_TIMEOUT_USEC
- tstamp
) + USEC_PER_MSEC
- 1) / USEC_PER_MSEC
))
85 tstamp
= now(CLOCK_MONOTONIC
);
88 if (!dbus_connection_get_is_connected(bus
)) {
89 dbus_set_error_const(error
, DBUS_ERROR_NO_SERVER
, "Connection terminated during authentication.");
93 if (!dbus_connection_get_is_authenticated(bus
)) {
94 dbus_set_error_const(error
, DBUS_ERROR_TIMEOUT
, "Failed to authenticate in time.");
101 int bus_connect(DBusBusType t
, DBusConnection
**_bus
, bool *_private
, DBusError
*error
) {
102 DBusConnection
*bus
= NULL
;
108 if (geteuid() == 0 && t
== DBUS_BUS_SYSTEM
) {
109 /* If we are root, then let's talk directly to the
110 * system instance, instead of going via the bus */
112 bus
= dbus_connection_open_private("unix:path=/run/systemd/private", error
);
117 if (t
== DBUS_BUS_SESSION
) {
120 /* If we are supposed to talk to the instance,
121 * try via XDG_RUNTIME_DIR first, then
122 * fallback to normal bus access */
124 e
= getenv("XDG_RUNTIME_DIR");
128 if (asprintf(&p
, "unix:path=%s/systemd/private", e
) < 0)
131 bus
= dbus_connection_open_private(p
, NULL
);
137 bus
= dbus_bus_get_private(t
, error
);
145 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
148 if (bus_check_peercred(bus
) < 0) {
149 dbus_connection_close(bus
);
150 dbus_connection_unref(bus
);
152 dbus_set_error_const(error
, DBUS_ERROR_ACCESS_DENIED
, "Failed to verify owner of bus.");
157 r
= sync_auth(bus
, error
);
159 dbus_connection_close(bus
);
160 dbus_connection_unref(bus
);
171 int bus_connect_system_ssh(const char *user
, const char *host
, DBusConnection
**_bus
, DBusError
*error
) {
177 assert(user
|| host
);
180 asprintf(&p
, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user
, host
);
182 asprintf(&p
, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user
);
184 asprintf(&p
, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host
);
187 dbus_set_error_const(error
, DBUS_ERROR_NO_MEMORY
, NULL
);
191 bus
= dbus_connection_open_private(p
, error
);
197 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
199 if ((r
= sync_auth(bus
, error
)) < 0) {
200 dbus_connection_close(bus
);
201 dbus_connection_unref(bus
);
205 if (!dbus_bus_register(bus
, error
)) {
206 dbus_connection_close(bus
);
207 dbus_connection_unref(bus
);
215 int bus_connect_system_polkit(DBusConnection
**_bus
, DBusError
*error
) {
221 /* Don't bother with PolicyKit if we are root */
223 return bus_connect(DBUS_BUS_SYSTEM
, _bus
, NULL
, error
);
225 if (!(bus
= dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH
, error
)))
228 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
230 if ((r
= sync_auth(bus
, error
)) < 0) {
231 dbus_connection_close(bus
);
232 dbus_connection_unref(bus
);
236 if (!dbus_bus_register(bus
, error
)) {
237 dbus_connection_close(bus
);
238 dbus_connection_unref(bus
);
246 const char *bus_error_message(const DBusError
*error
) {
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";
254 return error
->message
;
257 DBusHandlerResult
bus_default_message_handler(
259 DBusMessage
*message
,
260 const char *introspection
,
261 const char *interfaces
,
262 const BusBoundProperties
*bound_properties
) {
265 DBusMessage
*reply
= NULL
;
271 dbus_error_init(&error
);
273 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection
) {
275 if (!(reply
= dbus_message_new_method_return(message
)))
278 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
))
281 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Get") && bound_properties
) {
282 const char *interface
, *property
;
283 const BusBoundProperties
*bp
;
284 const BusProperty
*p
;
286 DBusMessageIter iter
, sub
;
288 if (!dbus_message_get_args(
291 DBUS_TYPE_STRING
, &interface
,
292 DBUS_TYPE_STRING
, &property
,
294 return bus_send_error_reply(c
, message
, &error
, -EINVAL
);
296 for (bp
= bound_properties
; bp
->interface
; bp
++) {
297 if (!streq(bp
->interface
, interface
))
300 for (p
= bp
->properties
; p
->property
; p
++)
301 if (streq(p
->property
, property
))
306 if (!nulstr_contains(interfaces
, interface
))
307 dbus_set_error_const(&error
, DBUS_ERROR_UNKNOWN_INTERFACE
, "Unknown interface");
309 dbus_set_error_const(&error
, DBUS_ERROR_UNKNOWN_PROPERTY
, "Unknown property");
311 return bus_send_error_reply(c
, message
, &error
, -EINVAL
);
314 reply
= dbus_message_new_method_return(message
);
318 dbus_message_iter_init_append(reply
, &iter
);
320 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_VARIANT
, p
->signature
, &sub
))
323 data
= (char*)bp
->base
+ p
->offset
;
325 data
= *(void**)data
;
326 r
= p
->append(&sub
, property
, data
);
331 dbus_message_unref(reply
);
332 return bus_send_error_reply(c
, message
, NULL
, r
);
335 if (!dbus_message_iter_close_container(&iter
, &sub
))
338 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "GetAll") && bound_properties
) {
339 const char *interface
;
340 const BusBoundProperties
*bp
;
341 const BusProperty
*p
;
342 DBusMessageIter iter
, sub
, sub2
, sub3
;
344 if (!dbus_message_get_args(
347 DBUS_TYPE_STRING
, &interface
,
349 return bus_send_error_reply(c
, message
, &error
, -EINVAL
);
351 if (interface
[0] && !nulstr_contains(interfaces
, interface
)) {
352 dbus_set_error_const(&error
, DBUS_ERROR_UNKNOWN_INTERFACE
, "Unknown interface");
353 return bus_send_error_reply(c
, message
, &error
, -EINVAL
);
356 if (!(reply
= dbus_message_new_method_return(message
)))
359 dbus_message_iter_init_append(reply
, &iter
);
361 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "{sv}", &sub
))
364 for (bp
= bound_properties
; bp
->interface
; bp
++) {
365 if (interface
[0] && !streq(bp
->interface
, interface
))
368 for (p
= bp
->properties
; p
->property
; p
++) {
371 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_DICT_ENTRY
, NULL
, &sub2
) ||
372 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &p
->property
) ||
373 !dbus_message_iter_open_container(&sub2
, DBUS_TYPE_VARIANT
, p
->signature
, &sub3
))
376 data
= (char*)bp
->base
+ p
->offset
;
378 data
= *(void**)data
;
379 r
= p
->append(&sub3
, p
->property
, data
);
384 dbus_message_unref(reply
);
385 return bus_send_error_reply(c
, message
, NULL
, r
);
388 if (!dbus_message_iter_close_container(&sub2
, &sub3
) ||
389 !dbus_message_iter_close_container(&sub
, &sub2
))
394 if (!dbus_message_iter_close_container(&iter
, &sub
))
397 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Set") && bound_properties
) {
398 const char *interface
, *property
;
399 DBusMessageIter iter
;
400 const BusBoundProperties
*bp
;
401 const BusProperty
*p
;
405 if (!dbus_message_iter_init(message
, &iter
) ||
406 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
407 return bus_send_error_reply(c
, message
, NULL
, -EINVAL
);
409 dbus_message_iter_get_basic(&iter
, &interface
);
411 if (!dbus_message_iter_next(&iter
) ||
412 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
413 return bus_send_error_reply(c
, message
, NULL
, -EINVAL
);
415 dbus_message_iter_get_basic(&iter
, &property
);
417 if (!dbus_message_iter_next(&iter
) ||
418 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
||
419 dbus_message_iter_has_next(&iter
))
420 return bus_send_error_reply(c
, message
, NULL
, -EINVAL
);
422 for (bp
= bound_properties
; bp
->interface
; bp
++) {
423 if (!streq(bp
->interface
, interface
))
426 for (p
= bp
->properties
; p
->property
; p
++)
427 if (streq(p
->property
, property
))
432 if (!nulstr_contains(interfaces
, interface
))
433 dbus_set_error_const(&error
, DBUS_ERROR_UNKNOWN_INTERFACE
, "Unknown interface");
435 dbus_set_error_const(&error
, DBUS_ERROR_UNKNOWN_PROPERTY
, "Unknown property");
437 return bus_send_error_reply(c
, message
, &error
, -EINVAL
);
441 dbus_set_error_const(&error
, DBUS_ERROR_PROPERTY_READ_ONLY
, "Property read-only");
442 return bus_send_error_reply(c
, message
, &error
, -EINVAL
);
445 dbus_message_iter_recurse(&iter
, &sub
);
447 sig
= dbus_message_iter_get_signature(&sub
);
451 if (!streq(sig
, p
->signature
)) {
453 return bus_send_error_reply(c
, message
, NULL
, -EINVAL
);
458 r
= p
->set(&sub
, property
);
462 return bus_send_error_reply(c
, message
, NULL
, r
);
465 reply
= dbus_message_new_method_return(message
);
469 const char *interface
= dbus_message_get_interface(message
);
471 if (!interface
|| !nulstr_contains(interfaces
, interface
)) {
472 dbus_set_error_const(&error
, DBUS_ERROR_UNKNOWN_INTERFACE
, "Unknown interface");
473 return bus_send_error_reply(c
, message
, &error
, -EINVAL
);
478 if (!dbus_connection_send(c
, reply
, NULL
))
481 dbus_message_unref(reply
);
482 return DBUS_HANDLER_RESULT_HANDLED
;
485 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
489 dbus_message_unref(reply
);
491 dbus_error_free(&error
);
493 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
496 int bus_property_append_string(DBusMessageIter
*i
, const char *property
, void *data
) {
497 const char *t
= data
;
505 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
511 int bus_property_append_strv(DBusMessageIter
*i
, const char *property
, void *data
) {
517 return bus_append_strv_iter(i
, t
);
520 int bus_property_append_bool(DBusMessageIter
*i
, const char *property
, void *data
) {
530 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &db
))
536 int bus_property_append_tristate_false(DBusMessageIter
*i
, const char *property
, void *data
) {
546 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &db
))
552 int bus_property_append_uint64(DBusMessageIter
*i
, const char *property
, void *data
) {
557 /* Let's ensure that usec_t is actually 64bit, and hence this
558 * function can be used for usec_t */
559 assert_cc(sizeof(uint64_t) == sizeof(usec_t
));
561 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, data
))
567 int bus_property_append_uint32(DBusMessageIter
*i
, const char *property
, void *data
) {
572 /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
573 * 32bit, and hence this function can be used for
574 * pid_t/mode_t/uid_t/gid_t */
575 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
576 assert_cc(sizeof(uint32_t) == sizeof(mode_t
));
577 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
578 assert_cc(sizeof(uint32_t) == sizeof(uid_t
));
579 assert_cc(sizeof(uint32_t) == sizeof(gid_t
));
581 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, data
))
587 int bus_property_append_int32(DBusMessageIter
*i
, const char *property
, void *data
) {
592 assert_cc(sizeof(int32_t) == sizeof(int));
594 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_INT32
, data
))
600 int bus_property_append_size(DBusMessageIter
*i
, const char *property
, void *data
) {
607 u
= (uint64_t) *(size_t*) data
;
609 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, &u
))
615 int bus_property_append_ul(DBusMessageIter
*i
, const char *property
, void *data
) {
622 u
= (uint64_t) *(unsigned long*) data
;
624 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, &u
))
630 int bus_property_append_long(DBusMessageIter
*i
, const char *property
, void *data
) {
637 l
= (int64_t) *(long*) data
;
639 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_INT64
, &l
))
645 const char *bus_errno_to_dbus(int error
) {
650 return DBUS_ERROR_INVALID_ARGS
;
653 return DBUS_ERROR_NO_MEMORY
;
657 return DBUS_ERROR_ACCESS_DENIED
;
660 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN
;
663 return DBUS_ERROR_FILE_NOT_FOUND
;
666 return DBUS_ERROR_FILE_EXISTS
;
670 return DBUS_ERROR_TIMEOUT
;
673 return DBUS_ERROR_IO_ERROR
;
678 return DBUS_ERROR_DISCONNECTED
;
681 return DBUS_ERROR_FAILED
;
684 DBusHandlerResult
bus_send_error_reply(DBusConnection
*c
, DBusMessage
*message
, DBusError
*berror
, int error
) {
685 DBusMessage
*reply
= NULL
;
686 const char *name
, *text
;
688 if (berror
&& dbus_error_is_set(berror
)) {
690 text
= berror
->message
;
692 name
= bus_errno_to_dbus(error
);
693 text
= strerror(-error
);
696 if (!(reply
= dbus_message_new_error(message
, name
, text
)))
699 if (!dbus_connection_send(c
, reply
, NULL
))
702 dbus_message_unref(reply
);
705 dbus_error_free(berror
);
707 return DBUS_HANDLER_RESULT_HANDLED
;
711 dbus_message_unref(reply
);
714 dbus_error_free(berror
);
716 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
719 DBusMessage
* bus_properties_changed_new(const char *path
, const char *interface
, const char *properties
) {
721 DBusMessageIter iter
, sub
;
727 if (!(m
= dbus_message_new_signal(path
, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
730 dbus_message_iter_init_append(m
, &iter
);
732 /* We won't send any property values, since they might be
733 * large and sometimes not cheap to generated */
735 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &interface
) ||
736 !dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "{sv}", &sub
) ||
737 !dbus_message_iter_close_container(&iter
, &sub
) ||
738 !dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
))
741 NULSTR_FOREACH(i
, properties
)
742 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &i
))
745 if (!dbus_message_iter_close_container(&iter
, &sub
))
752 dbus_message_unref(m
);
757 uint32_t bus_flags_to_events(DBusWatch
*bus_watch
) {
763 /* no watch flags for disabled watches */
764 if (!dbus_watch_get_enabled(bus_watch
))
767 flags
= dbus_watch_get_flags(bus_watch
);
769 if (flags
& DBUS_WATCH_READABLE
)
771 if (flags
& DBUS_WATCH_WRITABLE
)
774 return events
| EPOLLHUP
| EPOLLERR
;
777 unsigned bus_events_to_flags(uint32_t events
) {
780 if (events
& EPOLLIN
)
781 flags
|= DBUS_WATCH_READABLE
;
782 if (events
& EPOLLOUT
)
783 flags
|= DBUS_WATCH_WRITABLE
;
784 if (events
& EPOLLHUP
)
785 flags
|= DBUS_WATCH_HANGUP
;
786 if (events
& EPOLLERR
)
787 flags
|= DBUS_WATCH_ERROR
;
792 int bus_parse_strv(DBusMessage
*m
, char ***_l
) {
793 DBusMessageIter iter
;
798 if (!dbus_message_iter_init(m
, &iter
))
801 return bus_parse_strv_iter(&iter
, _l
);
804 int bus_parse_strv_iter(DBusMessageIter
*iter
, char ***_l
) {
806 unsigned n
= 0, i
= 0;
812 if (dbus_message_iter_get_arg_type(iter
) != DBUS_TYPE_ARRAY
||
813 dbus_message_iter_get_element_type(iter
) != DBUS_TYPE_STRING
)
816 dbus_message_iter_recurse(iter
, &sub
);
818 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
820 dbus_message_iter_next(&sub
);
823 if (!(l
= new(char*, n
+1)))
826 dbus_message_iter_recurse(iter
, &sub
);
828 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
831 assert_se(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
832 dbus_message_iter_get_basic(&sub
, &s
);
834 if (!(l
[i
++] = strdup(s
))) {
839 dbus_message_iter_next(&sub
);
851 int bus_append_strv_iter(DBusMessageIter
*iter
, char **l
) {
856 if (!dbus_message_iter_open_container(iter
, DBUS_TYPE_ARRAY
, "s", &sub
))
860 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, l
))
863 if (!dbus_message_iter_close_container(iter
, &sub
))
869 int bus_iter_get_basic_and_next(DBusMessageIter
*iter
, int type
, void *data
, bool next
) {
874 if (dbus_message_iter_get_arg_type(iter
) != type
)
877 dbus_message_iter_get_basic(iter
, data
);
879 if (!dbus_message_iter_next(iter
) != !next
)
885 int generic_print_property(const char *name
, DBusMessageIter
*iter
, bool all
) {
889 switch (dbus_message_iter_get_arg_type(iter
)) {
891 case DBUS_TYPE_STRING
: {
893 dbus_message_iter_get_basic(iter
, &s
);
895 if (all
|| !isempty(s
))
896 printf("%s=%s\n", name
, s
);
901 case DBUS_TYPE_BOOLEAN
: {
904 dbus_message_iter_get_basic(iter
, &b
);
905 printf("%s=%s\n", name
, yes_no(b
));
910 case DBUS_TYPE_UINT64
: {
912 dbus_message_iter_get_basic(iter
, &u
);
914 /* Yes, heuristics! But we can change this check
915 * should it turn out to not be sufficient */
917 if (endswith(name
, "Timestamp")) {
918 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
920 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
922 printf("%s=%s\n", name
, strempty(t
));
924 } else if (strstr(name
, "USec")) {
925 char timespan
[FORMAT_TIMESPAN_MAX
];
927 printf("%s=%s\n", name
, format_timespan(timespan
, sizeof(timespan
), u
));
929 printf("%s=%llu\n", name
, (unsigned long long) u
);
934 case DBUS_TYPE_UINT32
: {
936 dbus_message_iter_get_basic(iter
, &u
);
938 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
939 printf("%s=%04o\n", name
, u
);
941 printf("%s=%u\n", name
, (unsigned) u
);
946 case DBUS_TYPE_INT32
: {
948 dbus_message_iter_get_basic(iter
, &i
);
950 printf("%s=%i\n", name
, (int) i
);
954 case DBUS_TYPE_DOUBLE
: {
956 dbus_message_iter_get_basic(iter
, &d
);
958 printf("%s=%g\n", name
, d
);
962 case DBUS_TYPE_ARRAY
:
964 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
968 dbus_message_iter_recurse(iter
, &sub
);
970 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
973 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
976 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
977 dbus_message_iter_get_basic(&sub
, &s
);
978 printf("%s%s", space
? " " : "", s
);
981 dbus_message_iter_next(&sub
);
989 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_BYTE
) {
992 dbus_message_iter_recurse(iter
, &sub
);
994 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
997 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1000 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_BYTE
);
1001 dbus_message_iter_get_basic(&sub
, &u
);
1004 dbus_message_iter_next(&sub
);
1019 static void release_name_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
1021 DBusConnection
*bus
= userdata
;
1023 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
1024 dbus_message_unref(reply
);
1026 dbus_connection_close(bus
);
1029 void bus_async_unregister_and_exit(DBusConnection
*bus
, const char *name
) {
1030 DBusMessage
*m
= NULL
;
1031 DBusPendingCall
*pending
= NULL
;
1035 /* We unregister the name here, but we continue to process
1036 * requests, until we get the response for it, so that all
1037 * requests are guaranteed to be processed. */
1039 m
= dbus_message_new_method_call(
1042 DBUS_INTERFACE_DBUS
,
1047 if (!dbus_message_append_args(
1054 if (!dbus_connection_send_with_reply(bus
, m
, &pending
, -1))
1057 if (!dbus_pending_call_set_notify(pending
, release_name_pending_cb
, bus
, NULL
))
1060 dbus_message_unref(m
);
1061 dbus_pending_call_unref(pending
);
1066 log_error("Out of memory");
1069 dbus_pending_call_cancel(pending
);
1070 dbus_pending_call_unref(pending
);
1074 dbus_message_unref(m
);
1077 DBusHandlerResult
bus_exit_idle_filter(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
1078 usec_t
*remain_until
= userdata
;
1082 assert(remain_until
);
1084 /* Everytime we get a new message we reset out timeout */
1085 *remain_until
= now(CLOCK_MONOTONIC
) + DEFAULT_EXIT_USEC
;
1087 if (dbus_message_is_signal(m
, DBUS_INTERFACE_LOCAL
, "Disconnected"))
1088 dbus_connection_close(bus
);
1090 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;