1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <sys/resource.h>
9 #include <sys/socket.h>
13 #include "sd-daemon.h"
17 #include "alloc-util.h"
18 #include "bus-internal.h"
19 #include "bus-label.h"
21 #include "path-util.h"
22 #include "rlimit-util.h"
23 #include "socket-util.h"
24 #include "stdio-util.h"
25 #include "string-util.h"
27 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
28 sd_event
*e
= userdata
;
33 sd_bus_close(sd_bus_message_get_bus(m
));
39 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
48 /* We unregister the name here and then wait for the
49 * NameOwnerChanged signal for this event to arrive before we
50 * quit. We do this in order to make sure that any queued
51 * requests are still processed before we really exit. */
53 r
= sd_bus_get_unique_name(bus
, &unique
);
58 "sender='org.freedesktop.DBus',"
60 "interface='org.freedesktop.DBus',"
61 "member='NameOwnerChanged',"
62 "path='/org/freedesktop/DBus',"
64 "arg1='", unique
, "',",
67 r
= sd_bus_add_match_async(bus
, NULL
, match
, name_owner_change_callback
, NULL
, e
);
71 r
= sd_bus_release_name_async(bus
, NULL
, name
, NULL
, NULL
);
78 int bus_event_loop_with_idle(
83 check_idle_t check_idle
,
95 r
= sd_event_get_state(e
);
98 if (r
== SD_EVENT_FINISHED
)
102 idle
= check_idle(userdata
);
106 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
110 if (r
== 0 && !exiting
&& idle
) {
111 /* Inform the service manager that we are going down, so that it will queue all
112 * further start requests, instead of assuming we are already running. */
113 sd_notify(false, "STOPPING=1");
115 r
= bus_async_unregister_and_exit(e
, bus
, name
);
124 r
= sd_event_get_exit_code(e
, &code
);
131 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
132 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
133 int r
, has_owner
= 0;
138 r
= sd_bus_call_method(c
,
139 "org.freedesktop.DBus",
140 "/org/freedesktop/dbus",
141 "org.freedesktop.DBus",
150 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
152 return sd_bus_error_set_errno(error
, r
);
157 int bus_check_peercred(sd_bus
*c
) {
163 fd
= sd_bus_get_fd(c
);
167 r
= getpeercred(fd
, &ucred
);
171 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
177 int bus_connect_system_systemd(sd_bus
**_bus
) {
178 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
184 return sd_bus_default_system(_bus
);
186 /* If we are root then let's talk directly to the system
187 * instance, instead of going via the bus */
189 r
= sd_bus_new(&bus
);
193 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
197 r
= sd_bus_start(bus
);
199 return sd_bus_default_system(_bus
);
201 r
= bus_check_peercred(bus
);
205 *_bus
= TAKE_PTR(bus
);
210 int bus_connect_user_systemd(sd_bus
**_bus
) {
211 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
212 _cleanup_free_
char *ee
= NULL
;
218 e
= secure_getenv("XDG_RUNTIME_DIR");
220 return sd_bus_default_user(_bus
);
222 ee
= bus_address_escape(e
);
226 r
= sd_bus_new(&bus
);
230 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
234 r
= sd_bus_start(bus
);
236 return sd_bus_default_user(_bus
);
238 r
= bus_check_peercred(bus
);
242 *_bus
= TAKE_PTR(bus
);
247 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
248 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
251 assert(transport
>= 0);
252 assert(transport
< _BUS_TRANSPORT_MAX
);
255 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
256 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
260 case BUS_TRANSPORT_LOCAL
:
262 r
= sd_bus_default_user(&bus
);
264 if (sd_booted() <= 0) {
265 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
266 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
270 r
= sd_bus_default_system(&bus
);
274 case BUS_TRANSPORT_REMOTE
:
275 r
= sd_bus_open_system_remote(&bus
, host
);
278 case BUS_TRANSPORT_MACHINE
:
279 r
= sd_bus_open_system_machine(&bus
, host
);
283 assert_not_reached("Hmm, unknown transport type.");
288 r
= sd_bus_set_exit_on_disconnect(bus
, true);
292 *ret
= TAKE_PTR(bus
);
297 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
300 assert(transport
>= 0);
301 assert(transport
< _BUS_TRANSPORT_MAX
);
304 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
305 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
309 case BUS_TRANSPORT_LOCAL
:
311 r
= bus_connect_user_systemd(bus
);
313 if (sd_booted() <= 0)
314 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
315 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
316 "System has not been booted with systemd as init system (PID 1). Can't operate.");
317 r
= bus_connect_system_systemd(bus
);
321 case BUS_TRANSPORT_REMOTE
:
322 r
= sd_bus_open_system_remote(bus
, host
);
325 case BUS_TRANSPORT_MACHINE
:
326 r
= sd_bus_open_system_machine(bus
, host
);
330 assert_not_reached("Hmm, unknown transport type.");
336 int bus_property_get_bool(
339 const char *interface
,
340 const char *property
,
341 sd_bus_message
*reply
,
343 sd_bus_error
*error
) {
345 int b
= *(bool*) userdata
;
347 return sd_bus_message_append_basic(reply
, 'b', &b
);
350 int bus_property_set_bool(
353 const char *interface
,
354 const char *property
,
355 sd_bus_message
*value
,
357 sd_bus_error
*error
) {
361 r
= sd_bus_message_read(value
, "b", &b
);
365 *(bool*) userdata
= b
;
369 int bus_property_get_id128(
372 const char *interface
,
373 const char *property
,
374 sd_bus_message
*reply
,
376 sd_bus_error
*error
) {
378 sd_id128_t
*id
= userdata
;
380 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
381 return sd_bus_message_append(reply
, "ay", 0);
383 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
386 #if __SIZEOF_SIZE_T__ != 8
387 int bus_property_get_size(
390 const char *interface
,
391 const char *property
,
392 sd_bus_message
*reply
,
394 sd_bus_error
*error
) {
396 uint64_t sz
= *(size_t*) userdata
;
398 return sd_bus_message_append_basic(reply
, 't', &sz
);
402 #if __SIZEOF_LONG__ != 8
403 int bus_property_get_long(
406 const char *interface
,
407 const char *property
,
408 sd_bus_message
*reply
,
410 sd_bus_error
*error
) {
412 int64_t l
= *(long*) userdata
;
414 return sd_bus_message_append_basic(reply
, 'x', &l
);
417 int bus_property_get_ulong(
420 const char *interface
,
421 const char *property
,
422 sd_bus_message
*reply
,
424 sd_bus_error
*error
) {
426 uint64_t ul
= *(unsigned long*) userdata
;
428 return sd_bus_message_append_basic(reply
, 't', &ul
);
433 * bus_path_encode_unique() - encode unique object path
434 * @b: bus connection or NULL
435 * @prefix: object path prefix
436 * @sender_id: unique-name of client, or NULL
437 * @external_id: external ID to be chosen by client, or NULL
438 * @ret_path: storage for encoded object path pointer
440 * Whenever we provide a bus API that allows clients to create and manage
441 * server-side objects, we need to provide a unique name for these objects. If
442 * we let the server choose the name, we suffer from a race condition: If a
443 * client creates an object asynchronously, it cannot destroy that object until
444 * it received the method reply. It cannot know the name of the new object,
445 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
447 * Therefore, many APIs allow the client to choose the unique name for newly
448 * created objects. There're two problems to solve, though:
449 * 1) Object names are usually defined via dbus object paths, which are
450 * usually globally namespaced. Therefore, multiple clients must be able
451 * to choose unique object names without interference.
452 * 2) If multiple libraries share the same bus connection, they must be
453 * able to choose unique object names without interference.
454 * The first problem is solved easily by prefixing a name with the
455 * unique-bus-name of a connection. The server side must enforce this and
456 * reject any other name. The second problem is solved by providing unique
457 * suffixes from within sd-bus.
459 * This helper allows clients to create unique object-paths. It uses the
460 * template '/prefix/sender_id/external_id' and returns the new path in
461 * @ret_path (must be freed by the caller).
462 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
463 * NULL, this function allocates a unique suffix via @b (by requesting a new
464 * cookie). If both @sender_id and @external_id are given, @b can be passed as
467 * Returns: 0 on success, negative error code on failure.
469 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
470 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
471 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
474 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
475 assert_return(sd_bus_object_path_is_valid(prefix
), -EINVAL
);
476 assert_return(ret_path
, -EINVAL
);
479 r
= sd_bus_get_unique_name(b
, &sender_id
);
485 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
486 external_id
= external_buf
;
489 sender_label
= bus_label_escape(sender_id
);
493 external_label
= bus_label_escape(external_id
);
497 p
= path_join(prefix
, sender_label
, external_label
);
506 * bus_path_decode_unique() - decode unique object path
507 * @path: object path to decode
508 * @prefix: object path prefix
509 * @ret_sender: output parameter for sender-id label
510 * @ret_external: output parameter for external-id label
512 * This does the reverse of bus_path_encode_unique() (see its description for
513 * details). Both trailing labels, sender-id and external-id, are unescaped and
514 * returned in the given output parameters (the caller must free them).
516 * Note that this function returns 0 if the path does not match the template
517 * (see bus_path_encode_unique()), 1 if it matched.
519 * Returns: Negative error code on failure, 0 if the given object path does not
520 * match the template (return parameters are set to NULL), 1 if it was
521 * parsed successfully (return parameters contain allocated labels).
523 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
525 char *sender
, *external
;
527 assert(sd_bus_object_path_is_valid(path
));
528 assert(sd_bus_object_path_is_valid(prefix
));
530 assert(ret_external
);
532 p
= object_path_startswith(path
, prefix
);
535 *ret_external
= NULL
;
542 *ret_external
= NULL
;
546 sender
= bus_label_unescape_n(p
, q
- p
);
547 external
= bus_label_unescape(q
+ 1);
548 if (!sender
|| !external
) {
554 *ret_sender
= sender
;
555 *ret_external
= external
;
559 int bus_property_get_rlimit(
562 const char *interface
,
563 const char *property
,
564 sd_bus_message
*reply
,
566 sd_bus_error
*error
) {
577 is_soft
= endswith(property
, "Soft");
579 rl
= *(struct rlimit
**) userdata
;
581 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
583 struct rlimit buf
= {};
587 /* Chop off "Soft" suffix */
588 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
590 /* Skip over any prefix, such as "Default" */
591 assert_se(p
= strstr(s
, "Limit"));
593 z
= rlimit_from_string(p
+ 5);
596 (void) getrlimit(z
, &buf
);
597 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
600 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
602 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
604 return sd_bus_message_append(reply
, "t", u
);
607 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
613 /* Continues adding after failure, and returns the first failure. */
618 k
= sd_bus_track_add_name(t
, *i
);
626 int bus_open_system_watch_bind_with_description(sd_bus
**ret
, const char *description
) {
627 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
633 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
636 r
= sd_bus_new(&bus
);
641 r
= sd_bus_set_description(bus
, description
);
646 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
648 e
= DEFAULT_SYSTEM_BUS_ADDRESS
;
650 r
= sd_bus_set_address(bus
, e
);
654 r
= sd_bus_set_bus_client(bus
, true);
658 r
= sd_bus_negotiate_creds(bus
, true, SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
662 r
= sd_bus_set_watch_bind(bus
, true);
666 r
= sd_bus_set_connected_signal(bus
, true);
670 r
= sd_bus_start(bus
);
674 *ret
= TAKE_PTR(bus
);
679 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
680 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
686 /* Reply to the specified message with a message containing a dictionary put together from the
689 r
= sd_bus_message_new_method_return(m
, &reply
);
693 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
697 STRV_FOREACH_PAIR(k
, v
, l
) {
698 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
703 r
= sd_bus_message_close_container(reply
);
707 return sd_bus_send(NULL
, reply
, NULL
);
710 static void bus_message_unref_wrapper(void *m
) {
711 sd_bus_message_unref(m
);
714 const struct hash_ops bus_message_hash_ops
= {
715 .hash
= trivial_hash_func
,
716 .compare
= trivial_compare_func
,
717 .free_value
= bus_message_unref_wrapper
,