1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-error.h"
12 #include "bus-internal.h"
14 #include "dbus-automount.h"
15 #include "dbus-cgroup.h"
16 #include "dbus-device.h"
17 #include "dbus-execute.h"
19 #include "dbus-kill.h"
20 #include "dbus-manager.h"
21 #include "dbus-mount.h"
22 #include "dbus-path.h"
23 #include "dbus-scope.h"
24 #include "dbus-service.h"
25 #include "dbus-slice.h"
26 #include "dbus-socket.h"
27 #include "dbus-swap.h"
28 #include "dbus-target.h"
29 #include "dbus-timer.h"
30 #include "dbus-unit.h"
37 #include "process-util.h"
38 #include "selinux-access.h"
39 #include "serialize.h"
42 #include "string-util.h"
45 #include "user-util.h"
47 #define CONNECTIONS_MAX 4096
49 static void destroy_bus(Manager
*m
, sd_bus
**bus
);
51 int bus_send_pending_reload_message(Manager
*m
) {
56 if (!m
->pending_reload_message
)
59 /* If we cannot get rid of this message we won't dispatch any D-Bus messages, so that we won't end up wanting
60 * to queue another message. */
62 r
= sd_bus_send(NULL
, m
->pending_reload_message
, NULL
);
64 log_warning_errno(r
, "Failed to send queued message, ignoring: %m");
66 m
->pending_reload_message
= sd_bus_message_unref(m
->pending_reload_message
);
71 int bus_forward_agent_released(Manager
*m
, const char *path
) {
77 if (!MANAGER_IS_SYSTEM(m
))
83 /* If we are running a system instance we forward the agent message on the system bus, so that the user
84 * instances get notified about this, too */
86 r
= sd_bus_emit_signal(m
->system_bus
,
87 "/org/freedesktop/systemd1/agent",
88 "org.freedesktop.systemd1.Agent",
92 return log_debug_errno(r
, "Failed to propagate agent release message: %m");
97 static int signal_agent_released(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
98 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
99 Manager
*m
= userdata
;
107 /* only accept org.freedesktop.systemd1.Agent from UID=0 */
108 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
112 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
113 if (r
< 0 || sender_uid
!= 0)
116 /* parse 'cgroup-empty' notification */
117 r
= sd_bus_message_read(message
, "s", &cgroup
);
119 bus_log_parse_error(r
);
123 manager_notify_cgroup_empty(m
, cgroup
);
127 static int signal_disconnected(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
128 Manager
*m
= userdata
;
133 assert_se(bus
= sd_bus_message_get_bus(message
));
135 if (bus
== m
->api_bus
)
137 if (bus
== m
->system_bus
)
140 if (set_remove(m
->private_buses
, bus
)) {
141 log_debug("Got disconnect on private connection.");
142 destroy_bus(m
, &bus
);
148 static int signal_activation_request(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
149 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
150 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
151 Manager
*m
= userdata
;
159 r
= sd_bus_message_read(message
, "s", &name
);
161 bus_log_parse_error(r
);
165 if (manager_unit_inactive_or_pending(m
, SPECIAL_DBUS_SERVICE
) ||
166 manager_unit_inactive_or_pending(m
, SPECIAL_DBUS_SOCKET
)) {
167 r
= sd_bus_error_setf(&error
, BUS_ERROR_SHUTTING_DOWN
, "Refusing activation, D-Bus is shutting down.");
171 r
= manager_load_unit(m
, name
, NULL
, &error
, &u
);
175 if (u
->refuse_manual_start
) {
176 r
= sd_bus_error_setf(&error
, BUS_ERROR_ONLY_BY_DEPENDENCY
, "Operation refused, %s may be requested by dependency only (it is configured to refuse manual start/stop).", u
->id
);
180 r
= manager_add_job(m
, JOB_START
, u
, JOB_REPLACE
, NULL
, &error
, NULL
);
184 /* Successfully queued, that's it for us */
188 if (!sd_bus_error_is_set(&error
))
189 sd_bus_error_set_errno(&error
, r
);
191 log_debug("D-Bus activation failed for %s: %s", name
, bus_error_message(&error
, r
));
193 r
= sd_bus_message_new_signal(sd_bus_message_get_bus(message
), &reply
, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure");
195 bus_log_create_error(r
);
199 r
= sd_bus_message_append(reply
, "sss", name
, error
.name
, error
.message
);
201 bus_log_create_error(r
);
205 r
= sd_bus_send_to(NULL
, reply
, "org.freedesktop.DBus", NULL
);
207 return log_error_errno(r
, "Failed to respond with to bus activation request: %m");
213 static int mac_selinux_filter(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
214 Manager
*m
= userdata
;
215 const char *verb
, *path
;
222 /* Our own method calls are all protected individually with
223 * selinux checks, but the built-in interfaces need to be
226 if (sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Set"))
228 else if (sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", NULL
) ||
229 sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", NULL
) ||
230 sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.ObjectManager", NULL
) ||
231 sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Peer", NULL
))
236 path
= sd_bus_message_get_path(message
);
238 if (object_path_startswith("/org/freedesktop/systemd1", path
)) {
239 r
= mac_selinux_access_check(message
, verb
, error
);
246 if (streq_ptr(path
, "/org/freedesktop/systemd1/unit/self")) {
247 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
250 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
254 r
= sd_bus_creds_get_pid(creds
, &pid
);
258 u
= manager_get_unit_by_pid(m
, pid
);
260 r
= manager_get_job_from_dbus_path(m
, path
, &j
);
264 manager_load_unit_from_dbus_path(m
, path
, NULL
, &u
);
269 r
= mac_selinux_unit_access_check(u
, message
, verb
, error
);
277 static int bus_job_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
278 Manager
*m
= userdata
;
288 r
= manager_get_job_from_dbus_path(m
, path
, &j
);
296 static int find_unit(Manager
*m
, sd_bus
*bus
, const char *path
, Unit
**unit
, sd_bus_error
*error
) {
297 Unit
*u
= NULL
; /* just to appease gcc, initialization is not really necessary */
304 if (streq_ptr(path
, "/org/freedesktop/systemd1/unit/self")) {
305 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
306 sd_bus_message
*message
;
309 message
= sd_bus_get_current_message(bus
);
313 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
317 r
= sd_bus_creds_get_pid(creds
, &pid
);
321 u
= manager_get_unit_by_pid(m
, pid
);
325 r
= manager_load_unit_from_dbus_path(m
, path
, error
, &u
);
335 static int bus_unit_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
336 Manager
*m
= userdata
;
344 return find_unit(m
, bus
, path
, (Unit
**) found
, error
);
347 static int bus_unit_interface_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
348 Manager
*m
= userdata
;
358 r
= find_unit(m
, bus
, path
, &u
, error
);
362 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
369 static int bus_unit_cgroup_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
370 Manager
*m
= userdata
;
380 r
= find_unit(m
, bus
, path
, &u
, error
);
384 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
387 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
394 static int bus_cgroup_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
395 Manager
*m
= userdata
;
406 r
= find_unit(m
, bus
, path
, &u
, error
);
410 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
413 c
= unit_get_cgroup_context(u
);
421 static int bus_exec_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
422 Manager
*m
= userdata
;
433 r
= find_unit(m
, bus
, path
, &u
, error
);
437 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
440 c
= unit_get_exec_context(u
);
448 static int bus_kill_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
449 Manager
*m
= userdata
;
460 r
= find_unit(m
, bus
, path
, &u
, error
);
464 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
467 c
= unit_get_kill_context(u
);
475 static int bus_job_enumerate(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
476 _cleanup_strv_free_
char **l
= NULL
;
477 Manager
*m
= userdata
;
482 l
= new0(char*, hashmap_size(m
->jobs
)+1);
486 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
487 l
[k
] = job_dbus_path(j
);
494 assert(hashmap_size(m
->jobs
) == k
);
496 *nodes
= TAKE_PTR(l
);
501 static int bus_unit_enumerate(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
502 _cleanup_strv_free_
char **l
= NULL
;
503 Manager
*m
= userdata
;
508 l
= new0(char*, hashmap_size(m
->units
)+1);
512 HASHMAP_FOREACH(u
, m
->units
, i
) {
513 l
[k
] = unit_dbus_path(u
);
520 *nodes
= TAKE_PTR(l
);
525 static int bus_setup_api_vtables(Manager
*m
, sd_bus
*bus
) {
533 r
= sd_bus_add_filter(bus
, NULL
, mac_selinux_filter
, m
);
535 return log_error_errno(r
, "Failed to add SELinux access filter: %m");
538 r
= sd_bus_add_object_vtable(bus
, NULL
, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", bus_manager_vtable
, m
);
540 return log_error_errno(r
, "Failed to register Manager vtable: %m");
542 r
= sd_bus_add_fallback_vtable(bus
, NULL
, "/org/freedesktop/systemd1/job", "org.freedesktop.systemd1.Job", bus_job_vtable
, bus_job_find
, m
);
544 return log_error_errno(r
, "Failed to register Job vtable: %m");
546 r
= sd_bus_add_node_enumerator(bus
, NULL
, "/org/freedesktop/systemd1/job", bus_job_enumerate
, m
);
548 return log_error_errno(r
, "Failed to add job enumerator: %m");
550 r
= sd_bus_add_fallback_vtable(bus
, NULL
, "/org/freedesktop/systemd1/unit", "org.freedesktop.systemd1.Unit", bus_unit_vtable
, bus_unit_find
, m
);
552 return log_error_errno(r
, "Failed to register Unit vtable: %m");
554 r
= sd_bus_add_node_enumerator(bus
, NULL
, "/org/freedesktop/systemd1/unit", bus_unit_enumerate
, m
);
556 return log_error_errno(r
, "Failed to add job enumerator: %m");
558 for (t
= 0; t
< _UNIT_TYPE_MAX
; t
++) {
559 const char *interface
;
561 assert_se(interface
= unit_dbus_interface_from_type(t
));
563 r
= sd_bus_add_fallback_vtable(bus
, NULL
, "/org/freedesktop/systemd1/unit", interface
, unit_vtable
[t
]->bus_vtable
, bus_unit_interface_find
, m
);
565 return log_error_errno(r
, "Failed to register type specific vtable for %s: %m", interface
);
567 if (unit_vtable
[t
]->cgroup_context_offset
> 0) {
568 r
= sd_bus_add_fallback_vtable(bus
, NULL
, "/org/freedesktop/systemd1/unit", interface
, bus_unit_cgroup_vtable
, bus_unit_cgroup_find
, m
);
570 return log_error_errno(r
, "Failed to register control group unit vtable for %s: %m", interface
);
572 r
= sd_bus_add_fallback_vtable(bus
, NULL
, "/org/freedesktop/systemd1/unit", interface
, bus_cgroup_vtable
, bus_cgroup_context_find
, m
);
574 return log_error_errno(r
, "Failed to register control group vtable for %s: %m", interface
);
577 if (unit_vtable
[t
]->exec_context_offset
> 0) {
578 r
= sd_bus_add_fallback_vtable(bus
, NULL
, "/org/freedesktop/systemd1/unit", interface
, bus_exec_vtable
, bus_exec_context_find
, m
);
580 return log_error_errno(r
, "Failed to register execute vtable for %s: %m", interface
);
583 if (unit_vtable
[t
]->kill_context_offset
> 0) {
584 r
= sd_bus_add_fallback_vtable(bus
, NULL
, "/org/freedesktop/systemd1/unit", interface
, bus_kill_vtable
, bus_kill_context_find
, m
);
586 return log_error_errno(r
, "Failed to register kill vtable for %s: %m", interface
);
593 static int bus_setup_disconnected_match(Manager
*m
, sd_bus
*bus
) {
599 r
= sd_bus_match_signal_async(
602 "org.freedesktop.DBus.Local",
603 "/org/freedesktop/DBus/Local",
604 "org.freedesktop.DBus.Local",
606 signal_disconnected
, NULL
, m
);
608 return log_error_errno(r
, "Failed to request match for Disconnected message: %m");
613 static int bus_on_connection(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
614 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
615 _cleanup_close_
int nfd
= -1;
616 Manager
*m
= userdata
;
623 nfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
625 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
628 log_warning_errno(errno
, "Failed to accept private connection, ignoring: %m");
632 if (set_size(m
->private_buses
) >= CONNECTIONS_MAX
) {
633 log_warning("Too many concurrent connections, refusing");
637 r
= set_ensure_allocated(&m
->private_buses
, NULL
);
643 r
= sd_bus_new(&bus
);
645 log_warning_errno(r
, "Failed to allocate new private connection bus: %m");
649 (void) sd_bus_set_description(bus
, "private-bus-connection");
651 r
= sd_bus_set_fd(bus
, nfd
, nfd
);
653 log_warning_errno(r
, "Failed to set fd on new connection bus: %m");
659 r
= bus_check_peercred(bus
);
661 log_warning_errno(r
, "Incoming private connection from unprivileged client, refusing: %m");
665 assert_se(sd_id128_randomize(&id
) >= 0);
667 r
= sd_bus_set_server(bus
, 1, id
);
669 log_warning_errno(r
, "Failed to enable server support for new connection bus: %m");
673 r
= sd_bus_negotiate_creds(bus
, 1,
674 SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|
675 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
|
676 SD_BUS_CREDS_SELINUX_CONTEXT
);
678 log_warning_errno(r
, "Failed to enable credentials for new connection: %m");
682 r
= sd_bus_set_sender(bus
, "org.freedesktop.systemd1");
684 log_warning_errno(r
, "Failed to set direct connection sender: %m");
688 r
= sd_bus_start(bus
);
690 log_warning_errno(r
, "Failed to start new connection bus: %m");
694 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
696 log_warning_errno(r
, "Failed to attach new connection bus to event loop: %m");
700 r
= bus_setup_disconnected_match(m
, bus
);
704 r
= bus_setup_api_vtables(m
, bus
);
706 log_warning_errno(r
, "Failed to set up API vtables on new connection bus: %m");
710 r
= set_put(m
->private_buses
, bus
);
712 log_warning_errno(r
, "Failed to add new connection bus to set: %m");
718 log_debug("Accepted new private connection.");
723 static int manager_dispatch_sync_bus_names(sd_event_source
*es
, void *userdata
) {
724 _cleanup_strv_free_
char **names
= NULL
;
725 Manager
*m
= userdata
;
733 assert(m
->sync_bus_names_event_source
== es
);
735 /* First things first, destroy the defer event so that we aren't triggered again */
736 m
->sync_bus_names_event_source
= sd_event_source_unref(m
->sync_bus_names_event_source
);
738 /* Let's see if there's anything to do still? */
741 if (hashmap_isempty(m
->watch_bus
))
744 /* OK, let's sync up the names. Let's see which names are currently on the bus. */
745 r
= sd_bus_list_names(m
->api_bus
, &names
, NULL
);
747 return log_error_errno(r
, "Failed to get initial list of names: %m");
749 /* We have to synchronize the current bus names with the
750 * list of active services. To do this, walk the list of
751 * all units with bus names. */
752 HASHMAP_FOREACH_KEY(u
, name
, m
->watch_bus
, i
) {
753 Service
*s
= SERVICE(u
);
757 if (!streq_ptr(s
->bus_name
, name
)) {
758 log_unit_warning(u
, "Bus name has changed from %s → %s, ignoring.", s
->bus_name
, name
);
762 /* Check if a service's bus name is in the list of currently
764 if (strv_contains(names
, name
)) {
765 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
768 /* If it is, determine its current owner */
769 r
= sd_bus_get_name_creds(m
->api_bus
, name
, SD_BUS_CREDS_UNIQUE_NAME
, &creds
);
771 log_full_errno(r
== -ENXIO
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get bus name owner %s: %m", name
);
775 r
= sd_bus_creds_get_unique_name(creds
, &unique
);
777 log_full_errno(r
== -ENXIO
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get unique name for %s: %m", name
);
781 /* Now, let's compare that to the previous bus owner, and
782 * if it's still the same, all is fine, so just don't
783 * bother the service. Otherwise, the name has apparently
784 * changed, so synthesize a name owner changed signal. */
786 if (!streq_ptr(unique
, s
->bus_name_owner
))
787 UNIT_VTABLE(u
)->bus_name_owner_change(u
, name
, s
->bus_name_owner
, unique
);
789 /* So, the name we're watching is not on the bus.
790 * This either means it simply hasn't appeared yet,
791 * or it was lost during the daemon reload.
792 * Check if the service has a stored name owner,
793 * and synthesize a name loss signal in this case. */
795 if (s
->bus_name_owner
)
796 UNIT_VTABLE(u
)->bus_name_owner_change(u
, name
, s
->bus_name_owner
, NULL
);
803 int manager_enqueue_sync_bus_names(Manager
*m
) {
808 /* Enqueues a request to synchronize the bus names in a later event loop iteration. The callers generally don't
809 * want us to invoke ->bus_name_owner_change() unit calls from their stack frames as this might result in event
810 * dispatching on its own creating loops, hence we simply create a defer event for the event loop and exit. */
812 if (m
->sync_bus_names_event_source
)
815 r
= sd_event_add_defer(m
->event
, &m
->sync_bus_names_event_source
, manager_dispatch_sync_bus_names
, m
);
817 return log_error_errno(r
, "Failed to create bus name synchronization event: %m");
819 r
= sd_event_source_set_priority(m
->sync_bus_names_event_source
, SD_EVENT_PRIORITY_IDLE
);
821 return log_error_errno(r
, "Failed to set event priority: %m");
823 r
= sd_event_source_set_enabled(m
->sync_bus_names_event_source
, SD_EVENT_ONESHOT
);
825 return log_error_errno(r
, "Failed to set even to oneshot: %m");
827 (void) sd_event_source_set_description(m
->sync_bus_names_event_source
, "manager-sync-bus-names");
831 static int bus_setup_api(Manager
*m
, sd_bus
*bus
) {
840 /* Let's make sure we have enough credential bits so that we can make security and selinux decisions */
841 r
= sd_bus_negotiate_creds(bus
, 1,
842 SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|
843 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
|
844 SD_BUS_CREDS_SELINUX_CONTEXT
);
846 log_warning_errno(r
, "Failed to enable credential passing, ignoring: %m");
848 r
= bus_setup_api_vtables(m
, bus
);
852 HASHMAP_FOREACH_KEY(u
, name
, m
->watch_bus
, i
) {
853 r
= unit_install_bus_match(u
, bus
, name
);
855 log_error_errno(r
, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name
);
858 r
= sd_bus_match_signal_async(
861 "org.freedesktop.DBus",
862 "/org/freedesktop/DBus",
863 "org.freedesktop.systemd1.Activator",
865 signal_activation_request
, NULL
, m
);
867 log_warning_errno(r
, "Failed to subscribe to activation signal: %m");
869 /* Allow replacing of our name, to ease implementation of reexecution, where we keep the old connection open
870 * until after the new connection is set up and the name installed to allow clients to synchronously wait for
871 * reexecution to finish */
872 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.systemd1", SD_BUS_NAME_REPLACE_EXISTING
|SD_BUS_NAME_ALLOW_REPLACEMENT
, NULL
, NULL
);
874 return log_error_errno(r
, "Failed to request name: %m");
876 log_debug("Successfully connected to API bus.");
881 int bus_init_api(Manager
*m
) {
882 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
888 /* The API and system bus is the same if we are running in system mode */
889 if (MANAGER_IS_SYSTEM(m
) && m
->system_bus
)
890 bus
= sd_bus_ref(m
->system_bus
);
892 if (MANAGER_IS_SYSTEM(m
))
893 r
= sd_bus_open_system_with_description(&bus
, "bus-api-system");
895 r
= sd_bus_open_user_with_description(&bus
, "bus-api-user");
897 return log_error_errno(r
, "Failed to connect to API bus: %m");
899 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
901 return log_error_errno(r
, "Failed to attach API bus to event loop: %m");
903 r
= bus_setup_disconnected_match(m
, bus
);
908 r
= bus_setup_api(m
, bus
);
910 return log_error_errno(r
, "Failed to set up API bus: %m");
912 m
->api_bus
= TAKE_PTR(bus
);
914 r
= manager_enqueue_sync_bus_names(m
);
921 static int bus_setup_system(Manager
*m
, sd_bus
*bus
) {
927 /* if we are a user instance we get the Released message via the system bus */
928 if (MANAGER_IS_USER(m
)) {
929 r
= sd_bus_match_signal_async(
933 "/org/freedesktop/systemd1/agent",
934 "org.freedesktop.systemd1.Agent",
936 signal_agent_released
, NULL
, m
);
938 log_warning_errno(r
, "Failed to request Released match on system bus: %m");
941 log_debug("Successfully connected to system bus.");
945 int bus_init_system(Manager
*m
) {
946 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
952 /* The API and system bus is the same if we are running in system mode */
953 if (MANAGER_IS_SYSTEM(m
) && m
->api_bus
)
954 bus
= sd_bus_ref(m
->api_bus
);
956 r
= sd_bus_open_system_with_description(&bus
, "bus-system");
958 return log_error_errno(r
, "Failed to connect to system bus: %m");
960 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
962 return log_error_errno(r
, "Failed to attach system bus to event loop: %m");
964 r
= bus_setup_disconnected_match(m
, bus
);
969 r
= bus_setup_system(m
, bus
);
971 return log_error_errno(r
, "Failed to set up system bus: %m");
973 m
->system_bus
= TAKE_PTR(bus
);
978 int bus_init_private(Manager
*m
) {
979 _cleanup_close_
int fd
= -1;
980 union sockaddr_union sa
= {};
986 if (m
->private_listen_fd
>= 0)
989 if (MANAGER_IS_SYSTEM(m
)) {
991 /* We want the private bus only when running as init */
992 if (getpid_cached() != 1)
995 salen
= sockaddr_un_set_path(&sa
.un
, "/run/systemd/private");
997 const char *e
, *joined
;
999 e
= secure_getenv("XDG_RUNTIME_DIR");
1001 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
1002 "XDG_RUNTIME_DIR is not set, refusing.");
1004 joined
= strjoina(e
, "/systemd/private");
1005 salen
= sockaddr_un_set_path(&sa
.un
, joined
);
1008 return log_error_errno(salen
, "Can't set path for AF_UNIX socket to bind to: %m");
1010 (void) mkdir_parents_label(sa
.un
.sun_path
, 0755);
1011 (void) sockaddr_un_unlink(&sa
.un
);
1013 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
1015 return log_error_errno(errno
, "Failed to allocate private socket: %m");
1017 r
= bind(fd
, &sa
.sa
, salen
);
1019 return log_error_errno(errno
, "Failed to bind private socket: %m");
1021 r
= listen(fd
, SOMAXCONN
);
1023 return log_error_errno(errno
, "Failed to make private socket listening: %m");
1025 /* Generate an inotify event in case somebody waits for this socket to appear using inotify() */
1026 (void) touch(sa
.un
.sun_path
);
1028 r
= sd_event_add_io(m
->event
, &s
, fd
, EPOLLIN
, bus_on_connection
, m
);
1030 return log_error_errno(r
, "Failed to allocate event source: %m");
1032 (void) sd_event_source_set_description(s
, "bus-connection");
1034 m
->private_listen_fd
= TAKE_FD(fd
);
1035 m
->private_listen_event_source
= s
;
1037 log_debug("Successfully created private D-Bus server.");
1042 static void destroy_bus(Manager
*m
, sd_bus
**bus
) {
1053 /* Make sure all bus slots watching names are released. */
1054 HASHMAP_FOREACH(u
, m
->watch_bus
, i
) {
1055 if (!u
->match_bus_slot
)
1058 if (sd_bus_slot_get_bus(u
->match_bus_slot
) != *bus
)
1061 u
->match_bus_slot
= sd_bus_slot_unref(u
->match_bus_slot
);
1064 /* Get rid of tracked clients on this bus */
1065 if (m
->subscribed
&& sd_bus_track_get_bus(m
->subscribed
) == *bus
)
1066 m
->subscribed
= sd_bus_track_unref(m
->subscribed
);
1068 HASHMAP_FOREACH(j
, m
->jobs
, i
)
1069 if (j
->bus_track
&& sd_bus_track_get_bus(j
->bus_track
) == *bus
)
1070 j
->bus_track
= sd_bus_track_unref(j
->bus_track
);
1072 HASHMAP_FOREACH(u
, m
->units
, i
)
1073 if (u
->bus_track
&& sd_bus_track_get_bus(u
->bus_track
) == *bus
)
1074 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
1076 /* Get rid of queued message on this bus */
1077 if (m
->pending_reload_message
&& sd_bus_message_get_bus(m
->pending_reload_message
) == *bus
)
1078 m
->pending_reload_message
= sd_bus_message_unref(m
->pending_reload_message
);
1080 /* Possibly flush unwritten data, but only if we are
1081 * unprivileged, since we don't want to sync here */
1082 if (!MANAGER_IS_SYSTEM(m
))
1085 /* And destroy the object */
1086 *bus
= sd_bus_close_unref(*bus
);
1089 void bus_done_api(Manager
*m
) {
1090 destroy_bus(m
, &m
->api_bus
);
1093 void bus_done_system(Manager
*m
) {
1094 destroy_bus(m
, &m
->system_bus
);
1097 void bus_done_private(Manager
*m
) {
1102 while ((b
= set_steal_first(m
->private_buses
)))
1105 m
->private_buses
= set_free(m
->private_buses
);
1107 m
->private_listen_event_source
= sd_event_source_unref(m
->private_listen_event_source
);
1108 m
->private_listen_fd
= safe_close(m
->private_listen_fd
);
1111 void bus_done(Manager
*m
) {
1116 bus_done_private(m
);
1118 assert(!m
->subscribed
);
1120 m
->deserialized_subscribed
= strv_free(m
->deserialized_subscribed
);
1121 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
1124 int bus_fdset_add_all(Manager
*m
, FDSet
*fds
) {
1132 /* When we are about to reexecute we add all D-Bus fds to the
1133 * set to pass over to the newly executed systemd. They won't
1134 * be used there however, except thatt they are closed at the
1135 * very end of deserialization, those making it possible for
1136 * clients to synchronously wait for systemd to reexec by
1137 * simply waiting for disconnection */
1140 fd
= sd_bus_get_fd(m
->api_bus
);
1142 fd
= fdset_put_dup(fds
, fd
);
1148 SET_FOREACH(b
, m
->private_buses
, i
) {
1149 fd
= sd_bus_get_fd(b
);
1151 fd
= fdset_put_dup(fds
, fd
);
1157 /* We don't offer any APIs on the system bus (well, unless it
1158 * is the same as the API bus) hence we don't bother with it
1164 int bus_foreach_bus(
1166 sd_bus_track
*subscribed2
,
1167 int (*send_message
)(sd_bus
*bus
, void *userdata
),
1174 /* Send to all direct buses, unconditionally */
1175 SET_FOREACH(b
, m
->private_buses
, i
) {
1177 /* Don't bother with enqueing these messages to clients that haven't started yet */
1178 if (sd_bus_is_ready(b
) <= 0)
1181 r
= send_message(b
, userdata
);
1186 /* Send to API bus, but only if somebody is subscribed */
1188 (sd_bus_track_count(m
->subscribed
) > 0 ||
1189 sd_bus_track_count(subscribed2
) > 0)) {
1190 r
= send_message(m
->api_bus
, userdata
);
1198 void bus_track_serialize(sd_bus_track
*t
, FILE *f
, const char *prefix
) {
1204 for (n
= sd_bus_track_first(t
); n
; n
= sd_bus_track_next(t
)) {
1207 c
= sd_bus_track_count_name(t
, n
);
1208 for (j
= 0; j
< c
; j
++)
1209 (void) serialize_item(f
, prefix
, n
);
1213 int bus_track_coldplug(Manager
*m
, sd_bus_track
**t
, bool recursive
, char **l
) {
1219 if (strv_isempty(l
))
1226 r
= sd_bus_track_new(m
->api_bus
, t
, NULL
, NULL
);
1231 r
= sd_bus_track_set_recursive(*t
, recursive
);
1235 return bus_track_add_name_many(*t
, l
);
1238 int bus_verify_manage_units_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1239 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.manage-units", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1242 int bus_verify_manage_unit_files_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1243 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.manage-unit-files", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1246 int bus_verify_reload_daemon_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1247 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.reload-daemon", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1250 int bus_verify_set_environment_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1251 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.set-environment", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1254 uint64_t manager_bus_n_queued_write(Manager
*m
) {
1260 /* Returns the total number of messages queued for writing on all our direct and API busses. */
1262 SET_FOREACH(b
, m
->private_buses
, i
) {
1265 r
= sd_bus_get_n_queued_write(b
, &k
);
1267 log_debug_errno(r
, "Failed to query queued messages for private bus: %m");
1275 r
= sd_bus_get_n_queued_write(m
->api_bus
, &k
);
1277 log_debug_errno(r
, "Failed to query queued messages for API bus: %m");
1285 static void vtable_dump_bus_properties(FILE *f
, const sd_bus_vtable
*table
) {
1286 const sd_bus_vtable
*i
;
1288 for (i
= table
; i
->type
!= _SD_BUS_VTABLE_END
; i
++) {
1289 if (!IN_SET(i
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
) ||
1290 (i
->flags
& (SD_BUS_VTABLE_DEPRECATED
| SD_BUS_VTABLE_HIDDEN
)) != 0)
1293 fprintf(f
, "%s\n", i
->x
.property
.member
);
1297 void dump_bus_properties(FILE *f
) {
1300 vtable_dump_bus_properties(f
, bus_automount_vtable
);
1301 vtable_dump_bus_properties(f
, bus_cgroup_vtable
);
1302 vtable_dump_bus_properties(f
, bus_device_vtable
);
1303 vtable_dump_bus_properties(f
, bus_exec_vtable
);
1304 vtable_dump_bus_properties(f
, bus_job_vtable
);
1305 vtable_dump_bus_properties(f
, bus_kill_vtable
);
1306 vtable_dump_bus_properties(f
, bus_manager_vtable
);
1307 vtable_dump_bus_properties(f
, bus_mount_vtable
);
1308 vtable_dump_bus_properties(f
, bus_path_vtable
);
1309 vtable_dump_bus_properties(f
, bus_scope_vtable
);
1310 vtable_dump_bus_properties(f
, bus_service_vtable
);
1311 vtable_dump_bus_properties(f
, bus_slice_vtable
);
1312 vtable_dump_bus_properties(f
, bus_socket_vtable
);
1313 vtable_dump_bus_properties(f
, bus_swap_vtable
);
1314 vtable_dump_bus_properties(f
, bus_target_vtable
);
1315 vtable_dump_bus_properties(f
, bus_timer_vtable
);
1316 vtable_dump_bus_properties(f
, bus_unit_vtable
);
1317 vtable_dump_bus_properties(f
, bus_unit_cgroup_vtable
);