1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-error.h"
12 #include "bus-internal.h"
13 #include "bus-polkit.h"
15 #include "dbus-automount.h"
16 #include "dbus-cgroup.h"
17 #include "dbus-device.h"
18 #include "dbus-execute.h"
20 #include "dbus-kill.h"
21 #include "dbus-manager.h"
22 #include "dbus-mount.h"
23 #include "dbus-path.h"
24 #include "dbus-scope.h"
25 #include "dbus-service.h"
26 #include "dbus-slice.h"
27 #include "dbus-socket.h"
28 #include "dbus-swap.h"
29 #include "dbus-target.h"
30 #include "dbus-timer.h"
31 #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 find_unit(Manager
*m
, sd_bus
*bus
, const char *path
, Unit
**unit
, sd_bus_error
*error
) {
278 Unit
*u
= NULL
; /* just to appease gcc, initialization is not really necessary */
285 if (streq_ptr(path
, "/org/freedesktop/systemd1/unit/self")) {
286 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
287 sd_bus_message
*message
;
290 message
= sd_bus_get_current_message(bus
);
294 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
298 r
= sd_bus_creds_get_pid(creds
, &pid
);
302 u
= manager_get_unit_by_pid(m
, pid
);
306 r
= manager_load_unit_from_dbus_path(m
, path
, error
, &u
);
316 static int bus_unit_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
317 Manager
*m
= userdata
;
325 return find_unit(m
, bus
, path
, (Unit
**) found
, error
);
328 static int bus_unit_interface_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
329 Manager
*m
= userdata
;
339 r
= find_unit(m
, bus
, path
, &u
, error
);
343 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
350 static int bus_unit_cgroup_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
351 Manager
*m
= userdata
;
361 r
= find_unit(m
, bus
, path
, &u
, error
);
365 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
368 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
375 static int bus_cgroup_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
376 Manager
*m
= userdata
;
387 r
= find_unit(m
, bus
, path
, &u
, error
);
391 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
394 c
= unit_get_cgroup_context(u
);
402 static int bus_exec_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
403 Manager
*m
= userdata
;
414 r
= find_unit(m
, bus
, path
, &u
, error
);
418 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
421 c
= unit_get_exec_context(u
);
429 static int bus_kill_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
430 Manager
*m
= userdata
;
441 r
= find_unit(m
, bus
, path
, &u
, error
);
445 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
448 c
= unit_get_kill_context(u
);
456 static int bus_unit_enumerate(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
457 _cleanup_strv_free_
char **l
= NULL
;
458 Manager
*m
= userdata
;
462 l
= new0(char*, hashmap_size(m
->units
)+1);
466 HASHMAP_FOREACH(u
, m
->units
) {
467 l
[k
] = unit_dbus_path(u
);
474 *nodes
= TAKE_PTR(l
);
479 static const BusObjectImplementation unit_object
= {
480 "/org/freedesktop/systemd1/unit",
481 "org.freedesktop.systemd1.Unit",
482 .fallback_vtables
= BUS_FALLBACK_VTABLES(
483 { bus_unit_vtable
, bus_unit_find
}),
484 .node_enumerator
= bus_unit_enumerate
,
487 static const BusObjectImplementation bus_automount_object
= {
488 "/org/freedesktop/systemd1/unit",
489 "org.freedesktop.systemd1.Automount",
490 .fallback_vtables
= BUS_FALLBACK_VTABLES(
491 { bus_automount_vtable
, bus_unit_interface_find
}),
494 static const BusObjectImplementation bus_device_object
= {
495 "/org/freedesktop/systemd1/unit",
496 "org.freedesktop.systemd1.Device",
497 .fallback_vtables
= BUS_FALLBACK_VTABLES(
498 { bus_device_vtable
, bus_unit_interface_find
}),
501 static const BusObjectImplementation bus_mount_object
= {
502 "/org/freedesktop/systemd1/unit",
503 "org.freedesktop.systemd1.Mount",
504 .fallback_vtables
= BUS_FALLBACK_VTABLES(
505 { bus_mount_vtable
, bus_unit_interface_find
},
506 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
507 { bus_cgroup_vtable
, bus_cgroup_context_find
},
508 { bus_exec_vtable
, bus_exec_context_find
},
509 { bus_kill_vtable
, bus_kill_context_find
}),
512 static const BusObjectImplementation bus_path_object
= {
513 "/org/freedesktop/systemd1/unit",
514 "org.freedesktop.systemd1.Path",
515 .fallback_vtables
= BUS_FALLBACK_VTABLES(
516 { bus_path_vtable
, bus_unit_interface_find
}),
519 static const BusObjectImplementation bus_scope_object
= {
520 "/org/freedesktop/systemd1/unit",
521 "org.freedesktop.systemd1.Scope",
522 .fallback_vtables
= BUS_FALLBACK_VTABLES(
523 { bus_scope_vtable
, bus_unit_interface_find
},
524 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
525 { bus_cgroup_vtable
, bus_cgroup_context_find
},
526 { bus_kill_vtable
, bus_kill_context_find
}),
529 static const BusObjectImplementation bus_service_object
= {
530 "/org/freedesktop/systemd1/unit",
531 "org.freedesktop.systemd1.Service",
532 .fallback_vtables
= BUS_FALLBACK_VTABLES(
533 { bus_service_vtable
, bus_unit_interface_find
},
534 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
535 { bus_cgroup_vtable
, bus_cgroup_context_find
},
536 { bus_exec_vtable
, bus_exec_context_find
},
537 { bus_kill_vtable
, bus_kill_context_find
}),
540 static const BusObjectImplementation bus_slice_object
= {
541 "/org/freedesktop/systemd1/unit",
542 "org.freedesktop.systemd1.Slice",
543 .fallback_vtables
= BUS_FALLBACK_VTABLES(
544 { bus_slice_vtable
, bus_unit_interface_find
},
545 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
546 { bus_cgroup_vtable
, bus_cgroup_context_find
}),
549 static const BusObjectImplementation bus_socket_object
= {
550 "/org/freedesktop/systemd1/unit",
551 "org.freedesktop.systemd1.Socket",
552 .fallback_vtables
= BUS_FALLBACK_VTABLES(
553 { bus_socket_vtable
, bus_unit_interface_find
},
554 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
555 { bus_cgroup_vtable
, bus_cgroup_context_find
},
556 { bus_exec_vtable
, bus_exec_context_find
},
557 { bus_kill_vtable
, bus_kill_context_find
}),
560 static const BusObjectImplementation bus_swap_object
= {
561 "/org/freedesktop/systemd1/unit",
562 "org.freedesktop.systemd1.Swap",
563 .fallback_vtables
= BUS_FALLBACK_VTABLES(
564 { bus_swap_vtable
, bus_unit_interface_find
},
565 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
566 { bus_cgroup_vtable
, bus_cgroup_context_find
},
567 { bus_exec_vtable
, bus_exec_context_find
},
568 { bus_kill_vtable
, bus_kill_context_find
}),
571 static const BusObjectImplementation bus_target_object
= {
572 "/org/freedesktop/systemd1/unit",
573 "org.freedesktop.systemd1.Target",
574 .fallback_vtables
= BUS_FALLBACK_VTABLES(
575 { bus_target_vtable
, bus_unit_interface_find
}),
578 static const BusObjectImplementation bus_timer_object
= {
579 "/org/freedesktop/systemd1/unit",
580 "org.freedesktop.systemd1.Timer",
581 .fallback_vtables
= BUS_FALLBACK_VTABLES(
582 { bus_timer_vtable
, bus_unit_interface_find
}),
585 static const BusObjectImplementation bus_manager_object
= {
586 "/org/freedesktop/systemd1",
587 "org.freedesktop.systemd1.Manager",
588 .vtables
= BUS_VTABLES(bus_manager_vtable
),
589 .children
= BUS_IMPLEMENTATIONS(
592 &bus_automount_object
,
605 static const BusObjectImplementation manager_log_control_object
= {
606 "/org/freedesktop/LogControl1",
607 "org.freedesktop.LogControl1",
608 .vtables
= BUS_VTABLES(bus_manager_log_control_vtable
),
611 int bus_manager_introspect_implementations(FILE *out
, const char *pattern
) {
612 return bus_introspect_implementations(
615 BUS_IMPLEMENTATIONS(&bus_manager_object
,
616 &manager_log_control_object
));
619 static int bus_setup_api_vtables(Manager
*m
, sd_bus
*bus
) {
626 r
= sd_bus_add_filter(bus
, NULL
, mac_selinux_filter
, m
);
628 return log_error_errno(r
, "Failed to add SELinux access filter: %m");
631 r
= bus_add_implementation(bus
, &bus_manager_object
, m
);
635 return bus_add_implementation(bus
, &manager_log_control_object
, m
);
638 static int bus_setup_disconnected_match(Manager
*m
, sd_bus
*bus
) {
644 r
= sd_bus_match_signal_async(
647 "org.freedesktop.DBus.Local",
648 "/org/freedesktop/DBus/Local",
649 "org.freedesktop.DBus.Local",
651 signal_disconnected
, NULL
, m
);
653 return log_error_errno(r
, "Failed to request match for Disconnected message: %m");
658 static int bus_on_connection(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
659 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
660 _cleanup_close_
int nfd
= -1;
661 Manager
*m
= userdata
;
668 nfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
670 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
673 log_warning_errno(errno
, "Failed to accept private connection, ignoring: %m");
677 if (set_size(m
->private_buses
) >= CONNECTIONS_MAX
) {
678 log_warning("Too many concurrent connections, refusing");
682 r
= set_ensure_allocated(&m
->private_buses
, NULL
);
688 r
= sd_bus_new(&bus
);
690 log_warning_errno(r
, "Failed to allocate new private connection bus: %m");
694 (void) sd_bus_set_description(bus
, "private-bus-connection");
696 r
= sd_bus_set_fd(bus
, nfd
, nfd
);
698 log_warning_errno(r
, "Failed to set fd on new connection bus: %m");
704 r
= bus_check_peercred(bus
);
706 log_warning_errno(r
, "Incoming private connection from unprivileged client, refusing: %m");
710 assert_se(sd_id128_randomize(&id
) >= 0);
712 r
= sd_bus_set_server(bus
, 1, id
);
714 log_warning_errno(r
, "Failed to enable server support for new connection bus: %m");
718 r
= sd_bus_negotiate_creds(bus
, 1,
719 SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|
720 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
|
721 SD_BUS_CREDS_SELINUX_CONTEXT
);
723 log_warning_errno(r
, "Failed to enable credentials for new connection: %m");
727 r
= sd_bus_set_sender(bus
, "org.freedesktop.systemd1");
729 log_warning_errno(r
, "Failed to set direct connection sender: %m");
733 r
= sd_bus_start(bus
);
735 log_warning_errno(r
, "Failed to start new connection bus: %m");
739 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
741 log_warning_errno(r
, "Failed to attach new connection bus to event loop: %m");
745 r
= bus_setup_disconnected_match(m
, bus
);
749 r
= bus_setup_api_vtables(m
, bus
);
751 log_warning_errno(r
, "Failed to set up API vtables on new connection bus: %m");
755 r
= set_put(m
->private_buses
, bus
);
757 log_warning_errno(r
, "Failed to add new connection bus to set: %m");
763 log_debug("Accepted new private connection.");
768 static int bus_setup_api(Manager
*m
, sd_bus
*bus
) {
776 /* Let's make sure we have enough credential bits so that we can make security and selinux decisions */
777 r
= sd_bus_negotiate_creds(bus
, 1,
778 SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|
779 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
|
780 SD_BUS_CREDS_SELINUX_CONTEXT
);
782 log_warning_errno(r
, "Failed to enable credential passing, ignoring: %m");
784 r
= bus_setup_api_vtables(m
, bus
);
788 HASHMAP_FOREACH_KEY(u
, name
, m
->watch_bus
) {
789 r
= unit_install_bus_match(u
, bus
, name
);
791 log_error_errno(r
, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name
);
794 r
= sd_bus_match_signal_async(
797 "org.freedesktop.DBus",
798 "/org/freedesktop/DBus",
799 "org.freedesktop.systemd1.Activator",
801 signal_activation_request
, NULL
, m
);
803 log_warning_errno(r
, "Failed to subscribe to activation signal: %m");
805 /* Allow replacing of our name, to ease implementation of reexecution, where we keep the old connection open
806 * until after the new connection is set up and the name installed to allow clients to synchronously wait for
807 * reexecution to finish */
808 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.systemd1", SD_BUS_NAME_REPLACE_EXISTING
|SD_BUS_NAME_ALLOW_REPLACEMENT
, NULL
, NULL
);
810 return log_error_errno(r
, "Failed to request name: %m");
812 log_debug("Successfully connected to API bus.");
817 int bus_init_api(Manager
*m
) {
818 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
824 /* The API and system bus is the same if we are running in system mode */
825 if (MANAGER_IS_SYSTEM(m
) && m
->system_bus
)
826 bus
= sd_bus_ref(m
->system_bus
);
828 if (MANAGER_IS_SYSTEM(m
))
829 r
= sd_bus_open_system_with_description(&bus
, "bus-api-system");
831 r
= sd_bus_open_user_with_description(&bus
, "bus-api-user");
833 return log_error_errno(r
, "Failed to connect to API bus: %m");
835 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
837 return log_error_errno(r
, "Failed to attach API bus to event loop: %m");
839 r
= bus_setup_disconnected_match(m
, bus
);
844 r
= bus_setup_api(m
, bus
);
846 return log_error_errno(r
, "Failed to set up API bus: %m");
848 m
->api_bus
= TAKE_PTR(bus
);
853 static int bus_setup_system(Manager
*m
, sd_bus
*bus
) {
859 /* if we are a user instance we get the Released message via the system bus */
860 if (MANAGER_IS_USER(m
)) {
861 r
= sd_bus_match_signal_async(
865 "/org/freedesktop/systemd1/agent",
866 "org.freedesktop.systemd1.Agent",
868 signal_agent_released
, NULL
, m
);
870 log_warning_errno(r
, "Failed to request Released match on system bus: %m");
873 log_debug("Successfully connected to system bus.");
877 int bus_init_system(Manager
*m
) {
878 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
884 /* The API and system bus is the same if we are running in system mode */
885 if (MANAGER_IS_SYSTEM(m
) && m
->api_bus
)
886 bus
= sd_bus_ref(m
->api_bus
);
888 r
= sd_bus_open_system_with_description(&bus
, "bus-system");
890 return log_error_errno(r
, "Failed to connect to system bus: %m");
892 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
894 return log_error_errno(r
, "Failed to attach system bus to event loop: %m");
896 r
= bus_setup_disconnected_match(m
, bus
);
901 r
= bus_setup_system(m
, bus
);
903 return log_error_errno(r
, "Failed to set up system bus: %m");
905 m
->system_bus
= TAKE_PTR(bus
);
910 int bus_init_private(Manager
*m
) {
911 _cleanup_close_
int fd
= -1;
912 union sockaddr_union sa
;
919 if (m
->private_listen_fd
>= 0)
922 if (MANAGER_IS_SYSTEM(m
)) {
924 /* We want the private bus only when running as init */
925 if (getpid_cached() != 1)
928 r
= sockaddr_un_set_path(&sa
.un
, "/run/systemd/private");
930 const char *e
, *joined
;
932 e
= secure_getenv("XDG_RUNTIME_DIR");
934 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
935 "XDG_RUNTIME_DIR is not set, refusing.");
937 joined
= strjoina(e
, "/systemd/private");
938 r
= sockaddr_un_set_path(&sa
.un
, joined
);
941 return log_error_errno(r
, "Can't set path for AF_UNIX socket to bind to: %m");
944 (void) mkdir_parents_label(sa
.un
.sun_path
, 0755);
945 (void) sockaddr_un_unlink(&sa
.un
);
947 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
949 return log_error_errno(errno
, "Failed to allocate private socket: %m");
951 r
= bind(fd
, &sa
.sa
, sa_len
);
953 return log_error_errno(errno
, "Failed to bind private socket: %m");
955 r
= listen(fd
, SOMAXCONN
);
957 return log_error_errno(errno
, "Failed to make private socket listening: %m");
959 /* Generate an inotify event in case somebody waits for this socket to appear using inotify() */
960 (void) touch(sa
.un
.sun_path
);
962 r
= sd_event_add_io(m
->event
, &s
, fd
, EPOLLIN
, bus_on_connection
, m
);
964 return log_error_errno(r
, "Failed to allocate event source: %m");
966 (void) sd_event_source_set_description(s
, "bus-connection");
968 m
->private_listen_fd
= TAKE_FD(fd
);
969 m
->private_listen_event_source
= s
;
971 log_debug("Successfully created private D-Bus server.");
976 static void destroy_bus(Manager
*m
, sd_bus
**bus
) {
986 /* Make sure all bus slots watching names are released. */
987 HASHMAP_FOREACH(u
, m
->watch_bus
) {
988 if (u
->match_bus_slot
&& sd_bus_slot_get_bus(u
->match_bus_slot
) == *bus
)
989 u
->match_bus_slot
= sd_bus_slot_unref(u
->match_bus_slot
);
990 if (u
->get_name_owner_slot
&& sd_bus_slot_get_bus(u
->get_name_owner_slot
) == *bus
)
991 u
->get_name_owner_slot
= sd_bus_slot_unref(u
->get_name_owner_slot
);
994 /* Get rid of tracked clients on this bus */
995 if (m
->subscribed
&& sd_bus_track_get_bus(m
->subscribed
) == *bus
)
996 m
->subscribed
= sd_bus_track_unref(m
->subscribed
);
998 HASHMAP_FOREACH(j
, m
->jobs
)
999 if (j
->bus_track
&& sd_bus_track_get_bus(j
->bus_track
) == *bus
)
1000 j
->bus_track
= sd_bus_track_unref(j
->bus_track
);
1002 HASHMAP_FOREACH(u
, m
->units
) {
1003 if (u
->bus_track
&& sd_bus_track_get_bus(u
->bus_track
) == *bus
)
1004 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
1006 /* Get rid of pending freezer messages on this bus */
1007 if (u
->pending_freezer_message
&& sd_bus_message_get_bus(u
->pending_freezer_message
) == *bus
)
1008 u
->pending_freezer_message
= sd_bus_message_unref(u
->pending_freezer_message
);
1011 /* Get rid of queued message on this bus */
1012 if (m
->pending_reload_message
&& sd_bus_message_get_bus(m
->pending_reload_message
) == *bus
)
1013 m
->pending_reload_message
= sd_bus_message_unref(m
->pending_reload_message
);
1015 /* Possibly flush unwritten data, but only if we are
1016 * unprivileged, since we don't want to sync here */
1017 if (!MANAGER_IS_SYSTEM(m
))
1020 /* And destroy the object */
1021 *bus
= sd_bus_close_unref(*bus
);
1024 void bus_done_api(Manager
*m
) {
1025 destroy_bus(m
, &m
->api_bus
);
1028 void bus_done_system(Manager
*m
) {
1029 destroy_bus(m
, &m
->system_bus
);
1032 void bus_done_private(Manager
*m
) {
1037 while ((b
= set_steal_first(m
->private_buses
)))
1040 m
->private_buses
= set_free(m
->private_buses
);
1042 m
->private_listen_event_source
= sd_event_source_unref(m
->private_listen_event_source
);
1043 m
->private_listen_fd
= safe_close(m
->private_listen_fd
);
1046 void bus_done(Manager
*m
) {
1051 bus_done_private(m
);
1053 assert(!m
->subscribed
);
1055 m
->deserialized_subscribed
= strv_free(m
->deserialized_subscribed
);
1056 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
1059 int bus_fdset_add_all(Manager
*m
, FDSet
*fds
) {
1066 /* When we are about to reexecute we add all D-Bus fds to the
1067 * set to pass over to the newly executed systemd. They won't
1068 * be used there however, except thatt they are closed at the
1069 * very end of deserialization, those making it possible for
1070 * clients to synchronously wait for systemd to reexec by
1071 * simply waiting for disconnection */
1074 fd
= sd_bus_get_fd(m
->api_bus
);
1076 fd
= fdset_put_dup(fds
, fd
);
1082 SET_FOREACH(b
, m
->private_buses
) {
1083 fd
= sd_bus_get_fd(b
);
1085 fd
= fdset_put_dup(fds
, fd
);
1091 /* We don't offer any APIs on the system bus (well, unless it
1092 * is the same as the API bus) hence we don't bother with it
1098 int bus_foreach_bus(
1100 sd_bus_track
*subscribed2
,
1101 int (*send_message
)(sd_bus
*bus
, void *userdata
),
1107 /* Send to all direct buses, unconditionally */
1108 SET_FOREACH(b
, m
->private_buses
) {
1110 /* Don't bother with enqueuing these messages to clients that haven't started yet */
1111 if (sd_bus_is_ready(b
) <= 0)
1114 r
= send_message(b
, userdata
);
1119 /* Send to API bus, but only if somebody is subscribed */
1121 (sd_bus_track_count(m
->subscribed
) > 0 ||
1122 sd_bus_track_count(subscribed2
) > 0)) {
1123 r
= send_message(m
->api_bus
, userdata
);
1131 void bus_track_serialize(sd_bus_track
*t
, FILE *f
, const char *prefix
) {
1137 for (n
= sd_bus_track_first(t
); n
; n
= sd_bus_track_next(t
)) {
1140 c
= sd_bus_track_count_name(t
, n
);
1141 for (j
= 0; j
< c
; j
++)
1142 (void) serialize_item(f
, prefix
, n
);
1146 int bus_track_coldplug(Manager
*m
, sd_bus_track
**t
, bool recursive
, char **l
) {
1152 if (strv_isempty(l
))
1159 r
= sd_bus_track_new(m
->api_bus
, t
, NULL
, NULL
);
1164 r
= sd_bus_track_set_recursive(*t
, recursive
);
1168 return bus_track_add_name_many(*t
, l
);
1171 int bus_verify_manage_units_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1172 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.manage-units", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1175 int bus_verify_manage_unit_files_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1176 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.manage-unit-files", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1179 int bus_verify_reload_daemon_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1180 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.reload-daemon", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1183 int bus_verify_set_environment_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1184 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.set-environment", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1187 uint64_t manager_bus_n_queued_write(Manager
*m
) {
1192 /* Returns the total number of messages queued for writing on all our direct and API buses. */
1194 SET_FOREACH(b
, m
->private_buses
) {
1197 r
= sd_bus_get_n_queued_write(b
, &k
);
1199 log_debug_errno(r
, "Failed to query queued messages for private bus: %m");
1207 r
= sd_bus_get_n_queued_write(m
->api_bus
, &k
);
1209 log_debug_errno(r
, "Failed to query queued messages for API bus: %m");
1217 static void vtable_dump_bus_properties(FILE *f
, const sd_bus_vtable
*table
) {
1218 const sd_bus_vtable
*i
;
1220 for (i
= table
; i
->type
!= _SD_BUS_VTABLE_END
; i
++) {
1221 if (!IN_SET(i
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
) ||
1222 (i
->flags
& (SD_BUS_VTABLE_DEPRECATED
| SD_BUS_VTABLE_HIDDEN
)) != 0)
1225 fprintf(f
, "%s\n", i
->x
.property
.member
);
1229 void dump_bus_properties(FILE *f
) {
1232 vtable_dump_bus_properties(f
, bus_automount_vtable
);
1233 vtable_dump_bus_properties(f
, bus_cgroup_vtable
);
1234 vtable_dump_bus_properties(f
, bus_device_vtable
);
1235 vtable_dump_bus_properties(f
, bus_exec_vtable
);
1236 vtable_dump_bus_properties(f
, bus_job_vtable
);
1237 vtable_dump_bus_properties(f
, bus_kill_vtable
);
1238 vtable_dump_bus_properties(f
, bus_manager_vtable
);
1239 vtable_dump_bus_properties(f
, bus_mount_vtable
);
1240 vtable_dump_bus_properties(f
, bus_path_vtable
);
1241 vtable_dump_bus_properties(f
, bus_scope_vtable
);
1242 vtable_dump_bus_properties(f
, bus_service_vtable
);
1243 vtable_dump_bus_properties(f
, bus_slice_vtable
);
1244 vtable_dump_bus_properties(f
, bus_socket_vtable
);
1245 vtable_dump_bus_properties(f
, bus_swap_vtable
);
1246 vtable_dump_bus_properties(f
, bus_target_vtable
);
1247 vtable_dump_bus_properties(f
, bus_timer_vtable
);
1248 vtable_dump_bus_properties(f
, bus_unit_vtable
);
1249 vtable_dump_bus_properties(f
, bus_unit_cgroup_vtable
);