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"
36 #include "mkdir-label.h"
37 #include "process-util.h"
38 #include "selinux-access.h"
39 #include "serialize.h"
42 #include "string-util.h"
45 #include "umask-util.h"
46 #include "user-util.h"
48 #define CONNECTIONS_MAX 4096
50 static void destroy_bus(Manager
*m
, sd_bus
**bus
);
52 int bus_send_pending_reload_message(Manager
*m
) {
57 if (!m
->pending_reload_message
)
60 /* If we cannot get rid of this message we won't dispatch any D-Bus messages, so that we won't end up wanting
61 * to queue another message. */
63 r
= sd_bus_send(NULL
, m
->pending_reload_message
, NULL
);
65 log_warning_errno(r
, "Failed to send queued message, ignoring: %m");
67 m
->pending_reload_message
= sd_bus_message_unref(m
->pending_reload_message
);
72 int bus_forward_agent_released(Manager
*m
, const char *path
) {
78 if (!MANAGER_IS_SYSTEM(m
))
84 /* If we are running a system instance we forward the agent message on the system bus, so that the user
85 * instances get notified about this, too */
87 r
= sd_bus_emit_signal(m
->system_bus
,
88 "/org/freedesktop/systemd1/agent",
89 "org.freedesktop.systemd1.Agent",
93 return log_debug_errno(r
, "Failed to propagate agent release message: %m");
98 static int signal_agent_released(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
99 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
100 Manager
*m
= userdata
;
108 /* only accept org.freedesktop.systemd1.Agent from UID=0 */
109 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
113 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
114 if (r
< 0 || sender_uid
!= 0)
117 /* parse 'cgroup-empty' notification */
118 r
= sd_bus_message_read(message
, "s", &cgroup
);
120 bus_log_parse_error(r
);
124 manager_notify_cgroup_empty(m
, cgroup
);
128 static int signal_disconnected(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
129 Manager
*m
= userdata
;
134 assert_se(bus
= sd_bus_message_get_bus(message
));
136 if (bus
== m
->api_bus
)
138 if (bus
== m
->system_bus
)
141 if (set_remove(m
->private_buses
, bus
)) {
142 log_debug("Got disconnect on private connection.");
143 destroy_bus(m
, &bus
);
149 static int signal_activation_request(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
150 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
151 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
152 Manager
*m
= userdata
;
160 r
= sd_bus_message_read(message
, "s", &name
);
162 bus_log_parse_error(r
);
166 if (manager_unit_inactive_or_pending(m
, SPECIAL_DBUS_SERVICE
) ||
167 manager_unit_inactive_or_pending(m
, SPECIAL_DBUS_SOCKET
)) {
168 r
= sd_bus_error_set(&error
, BUS_ERROR_SHUTTING_DOWN
, "Refusing activation, D-Bus is shutting down.");
172 r
= manager_load_unit(m
, name
, NULL
, &error
, &u
);
176 if (u
->refuse_manual_start
) {
177 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
);
181 r
= manager_add_job(m
, JOB_START
, u
, JOB_REPLACE
, NULL
, &error
, NULL
);
185 /* Successfully queued, that's it for us */
189 if (!sd_bus_error_is_set(&error
))
190 sd_bus_error_set_errno(&error
, r
);
192 log_debug("D-Bus activation failed for %s: %s", name
, bus_error_message(&error
, r
));
194 r
= sd_bus_message_new_signal(sd_bus_message_get_bus(message
), &reply
, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure");
196 bus_log_create_error(r
);
200 r
= sd_bus_message_append(reply
, "sss", name
, error
.name
, error
.message
);
202 bus_log_create_error(r
);
206 r
= sd_bus_send_to(NULL
, reply
, "org.freedesktop.DBus", NULL
);
208 return log_error_errno(r
, "Failed to respond with to bus activation request: %m");
214 static int mac_selinux_filter(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
215 Manager
*m
= userdata
;
216 const char *verb
, *path
;
223 /* Our own method calls are all protected individually with
224 * selinux checks, but the built-in interfaces need to be
227 if (sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Set"))
229 else if (sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", NULL
) ||
230 sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", NULL
) ||
231 sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.ObjectManager", NULL
) ||
232 sd_bus_message_is_method_call(message
, "org.freedesktop.DBus.Peer", NULL
))
237 path
= sd_bus_message_get_path(message
);
239 if (object_path_startswith("/org/freedesktop/systemd1", path
)) {
240 r
= mac_selinux_access_check(message
, verb
, error
);
247 if (streq_ptr(path
, "/org/freedesktop/systemd1/unit/self")) {
248 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
251 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
255 r
= sd_bus_creds_get_pid(creds
, &pid
);
259 u
= manager_get_unit_by_pid(m
, pid
);
261 r
= manager_get_job_from_dbus_path(m
, path
, &j
);
265 manager_load_unit_from_dbus_path(m
, path
, NULL
, &u
);
270 r
= mac_selinux_unit_access_check(u
, message
, verb
, error
);
278 static int find_unit(Manager
*m
, sd_bus
*bus
, const char *path
, Unit
**unit
, sd_bus_error
*error
) {
279 Unit
*u
= NULL
; /* just to appease gcc, initialization is not really necessary */
286 if (streq_ptr(path
, "/org/freedesktop/systemd1/unit/self")) {
287 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
288 sd_bus_message
*message
;
291 message
= sd_bus_get_current_message(bus
);
295 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
299 r
= sd_bus_creds_get_pid(creds
, &pid
);
303 u
= manager_get_unit_by_pid(m
, pid
);
307 r
= manager_load_unit_from_dbus_path(m
, path
, error
, &u
);
317 static int bus_unit_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
318 Manager
*m
= userdata
;
326 return find_unit(m
, bus
, path
, (Unit
**) found
, error
);
329 static int bus_unit_interface_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
330 Manager
*m
= userdata
;
340 r
= find_unit(m
, bus
, path
, &u
, error
);
344 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
351 static int bus_unit_cgroup_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
352 Manager
*m
= userdata
;
362 r
= find_unit(m
, bus
, path
, &u
, error
);
366 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
369 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
376 static int bus_cgroup_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
377 Manager
*m
= userdata
;
388 r
= find_unit(m
, bus
, path
, &u
, error
);
392 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
395 c
= unit_get_cgroup_context(u
);
403 static int bus_exec_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
404 Manager
*m
= userdata
;
415 r
= find_unit(m
, bus
, path
, &u
, error
);
419 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
422 c
= unit_get_exec_context(u
);
430 static int bus_kill_context_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
431 Manager
*m
= userdata
;
442 r
= find_unit(m
, bus
, path
, &u
, error
);
446 if (!streq_ptr(interface
, unit_dbus_interface_from_type(u
->type
)))
449 c
= unit_get_kill_context(u
);
457 static int bus_unit_enumerate(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
458 _cleanup_strv_free_
char **l
= NULL
;
459 Manager
*m
= userdata
;
463 l
= new0(char*, hashmap_size(m
->units
)+1);
467 HASHMAP_FOREACH(u
, m
->units
) {
468 l
[k
] = unit_dbus_path(u
);
475 *nodes
= TAKE_PTR(l
);
480 static const BusObjectImplementation unit_object
= {
481 "/org/freedesktop/systemd1/unit",
482 "org.freedesktop.systemd1.Unit",
483 .fallback_vtables
= BUS_FALLBACK_VTABLES(
484 { bus_unit_vtable
, bus_unit_find
}),
485 .node_enumerator
= bus_unit_enumerate
,
488 static const BusObjectImplementation bus_automount_object
= {
489 "/org/freedesktop/systemd1/unit",
490 "org.freedesktop.systemd1.Automount",
491 .fallback_vtables
= BUS_FALLBACK_VTABLES(
492 { bus_automount_vtable
, bus_unit_interface_find
}),
495 static const BusObjectImplementation bus_device_object
= {
496 "/org/freedesktop/systemd1/unit",
497 "org.freedesktop.systemd1.Device",
498 .fallback_vtables
= BUS_FALLBACK_VTABLES(
499 { bus_device_vtable
, bus_unit_interface_find
}),
502 static const BusObjectImplementation bus_mount_object
= {
503 "/org/freedesktop/systemd1/unit",
504 "org.freedesktop.systemd1.Mount",
505 .fallback_vtables
= BUS_FALLBACK_VTABLES(
506 { bus_mount_vtable
, bus_unit_interface_find
},
507 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
508 { bus_cgroup_vtable
, bus_cgroup_context_find
},
509 { bus_exec_vtable
, bus_exec_context_find
},
510 { bus_kill_vtable
, bus_kill_context_find
}),
513 static const BusObjectImplementation bus_path_object
= {
514 "/org/freedesktop/systemd1/unit",
515 "org.freedesktop.systemd1.Path",
516 .fallback_vtables
= BUS_FALLBACK_VTABLES(
517 { bus_path_vtable
, bus_unit_interface_find
}),
520 static const BusObjectImplementation bus_scope_object
= {
521 "/org/freedesktop/systemd1/unit",
522 "org.freedesktop.systemd1.Scope",
523 .fallback_vtables
= BUS_FALLBACK_VTABLES(
524 { bus_scope_vtable
, bus_unit_interface_find
},
525 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
526 { bus_cgroup_vtable
, bus_cgroup_context_find
},
527 { bus_kill_vtable
, bus_kill_context_find
}),
530 static const BusObjectImplementation bus_service_object
= {
531 "/org/freedesktop/systemd1/unit",
532 "org.freedesktop.systemd1.Service",
533 .fallback_vtables
= BUS_FALLBACK_VTABLES(
534 { bus_service_vtable
, bus_unit_interface_find
},
535 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
536 { bus_cgroup_vtable
, bus_cgroup_context_find
},
537 { bus_exec_vtable
, bus_exec_context_find
},
538 { bus_kill_vtable
, bus_kill_context_find
}),
541 static const BusObjectImplementation bus_slice_object
= {
542 "/org/freedesktop/systemd1/unit",
543 "org.freedesktop.systemd1.Slice",
544 .fallback_vtables
= BUS_FALLBACK_VTABLES(
545 { bus_slice_vtable
, bus_unit_interface_find
},
546 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
547 { bus_cgroup_vtable
, bus_cgroup_context_find
}),
550 static const BusObjectImplementation bus_socket_object
= {
551 "/org/freedesktop/systemd1/unit",
552 "org.freedesktop.systemd1.Socket",
553 .fallback_vtables
= BUS_FALLBACK_VTABLES(
554 { bus_socket_vtable
, bus_unit_interface_find
},
555 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
556 { bus_cgroup_vtable
, bus_cgroup_context_find
},
557 { bus_exec_vtable
, bus_exec_context_find
},
558 { bus_kill_vtable
, bus_kill_context_find
}),
561 static const BusObjectImplementation bus_swap_object
= {
562 "/org/freedesktop/systemd1/unit",
563 "org.freedesktop.systemd1.Swap",
564 .fallback_vtables
= BUS_FALLBACK_VTABLES(
565 { bus_swap_vtable
, bus_unit_interface_find
},
566 { bus_unit_cgroup_vtable
, bus_unit_cgroup_find
},
567 { bus_cgroup_vtable
, bus_cgroup_context_find
},
568 { bus_exec_vtable
, bus_exec_context_find
},
569 { bus_kill_vtable
, bus_kill_context_find
}),
572 static const BusObjectImplementation bus_target_object
= {
573 "/org/freedesktop/systemd1/unit",
574 "org.freedesktop.systemd1.Target",
575 .fallback_vtables
= BUS_FALLBACK_VTABLES(
576 { bus_target_vtable
, bus_unit_interface_find
}),
579 static const BusObjectImplementation bus_timer_object
= {
580 "/org/freedesktop/systemd1/unit",
581 "org.freedesktop.systemd1.Timer",
582 .fallback_vtables
= BUS_FALLBACK_VTABLES(
583 { bus_timer_vtable
, bus_unit_interface_find
}),
586 static const BusObjectImplementation bus_manager_object
= {
587 "/org/freedesktop/systemd1",
588 "org.freedesktop.systemd1.Manager",
589 .vtables
= BUS_VTABLES(bus_manager_vtable
),
590 .children
= BUS_IMPLEMENTATIONS(
593 &bus_automount_object
,
606 static const BusObjectImplementation manager_log_control_object
= {
607 "/org/freedesktop/LogControl1",
608 "org.freedesktop.LogControl1",
609 .vtables
= BUS_VTABLES(bus_manager_log_control_vtable
),
612 int bus_manager_introspect_implementations(FILE *out
, const char *pattern
) {
613 return bus_introspect_implementations(
616 BUS_IMPLEMENTATIONS(&bus_manager_object
,
617 &manager_log_control_object
));
620 static int bus_setup_api_vtables(Manager
*m
, sd_bus
*bus
) {
627 r
= sd_bus_add_filter(bus
, NULL
, mac_selinux_filter
, m
);
629 return log_error_errno(r
, "Failed to add SELinux access filter: %m");
632 r
= bus_add_implementation(bus
, &bus_manager_object
, m
);
636 return bus_add_implementation(bus
, &manager_log_control_object
, m
);
639 static int bus_setup_disconnected_match(Manager
*m
, sd_bus
*bus
) {
645 r
= sd_bus_match_signal_async(
648 "org.freedesktop.DBus.Local",
649 "/org/freedesktop/DBus/Local",
650 "org.freedesktop.DBus.Local",
652 signal_disconnected
, NULL
, m
);
654 return log_error_errno(r
, "Failed to request match for Disconnected message: %m");
659 static int bus_on_connection(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
660 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
661 _cleanup_close_
int nfd
= -1;
662 Manager
*m
= userdata
;
669 nfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
671 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
674 log_warning_errno(errno
, "Failed to accept private connection, ignoring: %m");
678 if (set_size(m
->private_buses
) >= CONNECTIONS_MAX
) {
679 log_warning("Too many concurrent connections, refusing");
683 r
= sd_bus_new(&bus
);
685 log_warning_errno(r
, "Failed to allocate new private connection bus: %m");
689 (void) sd_bus_set_description(bus
, "private-bus-connection");
691 r
= sd_bus_set_fd(bus
, nfd
, nfd
);
693 log_warning_errno(r
, "Failed to set fd on new connection bus: %m");
699 r
= bus_check_peercred(bus
);
701 log_warning_errno(r
, "Incoming private connection from unprivileged client, refusing: %m");
705 assert_se(sd_id128_randomize(&id
) >= 0);
707 r
= sd_bus_set_server(bus
, 1, id
);
709 log_warning_errno(r
, "Failed to enable server support for new connection bus: %m");
713 r
= sd_bus_negotiate_creds(bus
, 1,
714 SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|
715 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
|
716 SD_BUS_CREDS_SELINUX_CONTEXT
);
718 log_warning_errno(r
, "Failed to enable credentials for new connection: %m");
722 r
= sd_bus_set_sender(bus
, "org.freedesktop.systemd1");
724 log_warning_errno(r
, "Failed to set direct connection sender: %m");
728 r
= sd_bus_start(bus
);
730 log_warning_errno(r
, "Failed to start new connection bus: %m");
734 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
736 log_warning_errno(r
, "Failed to attach new connection bus to event loop: %m");
740 r
= bus_setup_disconnected_match(m
, bus
);
744 r
= bus_setup_api_vtables(m
, bus
);
746 log_warning_errno(r
, "Failed to set up API vtables on new connection bus: %m");
750 r
= set_ensure_put(&m
->private_buses
, NULL
, bus
);
756 log_warning_errno(r
, "Failed to add new connection bus to set: %m");
762 log_debug("Accepted new private connection.");
767 static int bus_setup_api(Manager
*m
, sd_bus
*bus
) {
775 /* Let's make sure we have enough credential bits so that we can make security and selinux decisions */
776 r
= sd_bus_negotiate_creds(bus
, 1,
777 SD_BUS_CREDS_PID
|SD_BUS_CREDS_UID
|
778 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
|
779 SD_BUS_CREDS_SELINUX_CONTEXT
);
781 log_warning_errno(r
, "Failed to enable credential passing, ignoring: %m");
783 r
= bus_setup_api_vtables(m
, bus
);
787 HASHMAP_FOREACH_KEY(u
, name
, m
->watch_bus
) {
788 r
= unit_install_bus_match(u
, bus
, name
);
790 log_error_errno(r
, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name
);
793 r
= sd_bus_match_signal_async(
796 "org.freedesktop.DBus",
797 "/org/freedesktop/DBus",
798 "org.freedesktop.systemd1.Activator",
800 signal_activation_request
, NULL
, m
);
802 log_warning_errno(r
, "Failed to subscribe to activation signal: %m");
804 /* Allow replacing of our name, to ease implementation of reexecution, where we keep the old connection open
805 * until after the new connection is set up and the name installed to allow clients to synchronously wait for
806 * reexecution to finish */
807 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.systemd1", SD_BUS_NAME_REPLACE_EXISTING
|SD_BUS_NAME_ALLOW_REPLACEMENT
, NULL
, NULL
);
809 return log_error_errno(r
, "Failed to request name: %m");
811 log_debug("Successfully connected to API bus.");
816 int bus_init_api(Manager
*m
) {
817 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
823 /* The API and system bus is the same if we are running in system mode */
824 if (MANAGER_IS_SYSTEM(m
) && m
->system_bus
)
825 bus
= sd_bus_ref(m
->system_bus
);
827 if (MANAGER_IS_SYSTEM(m
))
828 r
= sd_bus_open_system_with_description(&bus
, "bus-api-system");
830 r
= sd_bus_open_user_with_description(&bus
, "bus-api-user");
832 return log_error_errno(r
, "Failed to connect to API bus: %m");
834 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
836 return log_error_errno(r
, "Failed to attach API bus to event loop: %m");
838 r
= bus_setup_disconnected_match(m
, bus
);
843 r
= bus_setup_api(m
, bus
);
845 return log_error_errno(r
, "Failed to set up API bus: %m");
847 m
->api_bus
= TAKE_PTR(bus
);
852 static int bus_setup_system(Manager
*m
, sd_bus
*bus
) {
858 /* if we are a user instance we get the Released message via the system bus */
859 if (MANAGER_IS_USER(m
)) {
860 r
= sd_bus_match_signal_async(
864 "/org/freedesktop/systemd1/agent",
865 "org.freedesktop.systemd1.Agent",
867 signal_agent_released
, NULL
, m
);
869 log_warning_errno(r
, "Failed to request Released match on system bus: %m");
872 log_debug("Successfully connected to system bus.");
876 int bus_init_system(Manager
*m
) {
877 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
883 /* The API and system bus is the same if we are running in system mode */
884 if (MANAGER_IS_SYSTEM(m
) && m
->api_bus
)
885 bus
= sd_bus_ref(m
->api_bus
);
887 r
= sd_bus_open_system_with_description(&bus
, "bus-system");
889 return log_error_errno(r
, "Failed to connect to system bus: %m");
891 r
= sd_bus_attach_event(bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
893 return log_error_errno(r
, "Failed to attach system bus to event loop: %m");
895 r
= bus_setup_disconnected_match(m
, bus
);
900 r
= bus_setup_system(m
, bus
);
902 return log_error_errno(r
, "Failed to set up system bus: %m");
904 m
->system_bus
= TAKE_PTR(bus
);
909 int bus_init_private(Manager
*m
) {
910 _cleanup_close_
int fd
= -1;
911 union sockaddr_union sa
;
918 if (m
->private_listen_fd
>= 0)
921 if (MANAGER_IS_SYSTEM(m
)) {
923 /* We want the private bus only when running as init */
924 if (getpid_cached() != 1)
927 r
= sockaddr_un_set_path(&sa
.un
, "/run/systemd/private");
929 _cleanup_free_
char *joined
= NULL
;
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
= path_join(e
, "/systemd/private");
941 r
= sockaddr_un_set_path(&sa
.un
, joined
);
944 return log_error_errno(r
, "Can't set path for AF_UNIX socket to bind to: %m");
947 (void) mkdir_parents_label(sa
.un
.sun_path
, 0755);
948 (void) sockaddr_un_unlink(&sa
.un
);
950 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
952 return log_error_errno(errno
, "Failed to allocate private socket: %m");
955 r
= bind(fd
, &sa
.sa
, sa_len
);
957 return log_error_errno(errno
, "Failed to bind private socket: %m");
959 r
= listen(fd
, SOMAXCONN
);
961 return log_error_errno(errno
, "Failed to make private socket listening: %m");
963 /* Generate an inotify event in case somebody waits for this socket to appear using inotify() */
964 (void) touch(sa
.un
.sun_path
);
966 r
= sd_event_add_io(m
->event
, &s
, fd
, EPOLLIN
, bus_on_connection
, m
);
968 return log_error_errno(r
, "Failed to allocate event source: %m");
970 (void) sd_event_source_set_description(s
, "bus-connection");
972 m
->private_listen_fd
= TAKE_FD(fd
);
973 m
->private_listen_event_source
= s
;
975 log_debug("Successfully created private D-Bus server.");
980 static void destroy_bus(Manager
*m
, sd_bus
**bus
) {
990 /* Make sure all bus slots watching names are released. */
991 HASHMAP_FOREACH(u
, m
->watch_bus
) {
992 if (u
->match_bus_slot
&& sd_bus_slot_get_bus(u
->match_bus_slot
) == *bus
)
993 u
->match_bus_slot
= sd_bus_slot_unref(u
->match_bus_slot
);
994 if (u
->get_name_owner_slot
&& sd_bus_slot_get_bus(u
->get_name_owner_slot
) == *bus
)
995 u
->get_name_owner_slot
= sd_bus_slot_unref(u
->get_name_owner_slot
);
998 /* Get rid of tracked clients on this bus */
999 if (m
->subscribed
&& sd_bus_track_get_bus(m
->subscribed
) == *bus
)
1000 m
->subscribed
= sd_bus_track_unref(m
->subscribed
);
1002 HASHMAP_FOREACH(j
, m
->jobs
)
1003 if (j
->bus_track
&& sd_bus_track_get_bus(j
->bus_track
) == *bus
)
1004 j
->bus_track
= sd_bus_track_unref(j
->bus_track
);
1006 HASHMAP_FOREACH(u
, m
->units
) {
1007 if (u
->bus_track
&& sd_bus_track_get_bus(u
->bus_track
) == *bus
)
1008 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
1010 /* Get rid of pending freezer messages on this bus */
1011 if (u
->pending_freezer_message
&& sd_bus_message_get_bus(u
->pending_freezer_message
) == *bus
)
1012 u
->pending_freezer_message
= sd_bus_message_unref(u
->pending_freezer_message
);
1015 /* Get rid of queued message on this bus */
1016 if (m
->pending_reload_message
&& sd_bus_message_get_bus(m
->pending_reload_message
) == *bus
)
1017 m
->pending_reload_message
= sd_bus_message_unref(m
->pending_reload_message
);
1019 /* Possibly flush unwritten data, but only if we are
1020 * unprivileged, since we don't want to sync here */
1021 if (!MANAGER_IS_SYSTEM(m
))
1024 /* And destroy the object */
1025 *bus
= sd_bus_close_unref(*bus
);
1028 void bus_done_api(Manager
*m
) {
1029 destroy_bus(m
, &m
->api_bus
);
1032 void bus_done_system(Manager
*m
) {
1033 destroy_bus(m
, &m
->system_bus
);
1036 void bus_done_private(Manager
*m
) {
1041 while ((b
= set_steal_first(m
->private_buses
)))
1044 m
->private_buses
= set_free(m
->private_buses
);
1046 m
->private_listen_event_source
= sd_event_source_disable_unref(m
->private_listen_event_source
);
1047 m
->private_listen_fd
= safe_close(m
->private_listen_fd
);
1050 void bus_done(Manager
*m
) {
1055 bus_done_private(m
);
1057 assert(!m
->subscribed
);
1059 m
->deserialized_subscribed
= strv_free(m
->deserialized_subscribed
);
1060 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
1063 int bus_fdset_add_all(Manager
*m
, FDSet
*fds
) {
1070 /* When we are about to reexecute we add all D-Bus fds to the
1071 * set to pass over to the newly executed systemd. They won't
1072 * be used there however, except thatt they are closed at the
1073 * very end of deserialization, those making it possible for
1074 * clients to synchronously wait for systemd to reexec by
1075 * simply waiting for disconnection */
1078 fd
= sd_bus_get_fd(m
->api_bus
);
1080 fd
= fdset_put_dup(fds
, fd
);
1086 SET_FOREACH(b
, m
->private_buses
) {
1087 fd
= sd_bus_get_fd(b
);
1089 fd
= fdset_put_dup(fds
, fd
);
1095 /* We don't offer any APIs on the system bus (well, unless it
1096 * is the same as the API bus) hence we don't bother with it
1102 int bus_foreach_bus(
1104 sd_bus_track
*subscribed2
,
1105 int (*send_message
)(sd_bus
*bus
, void *userdata
),
1111 /* Send to all direct buses, unconditionally */
1112 SET_FOREACH(b
, m
->private_buses
) {
1114 /* Don't bother with enqueuing these messages to clients that haven't started yet */
1115 if (sd_bus_is_ready(b
) <= 0)
1118 r
= send_message(b
, userdata
);
1123 /* Send to API bus, but only if somebody is subscribed */
1125 (sd_bus_track_count(m
->subscribed
) > 0 ||
1126 sd_bus_track_count(subscribed2
) > 0)) {
1127 r
= send_message(m
->api_bus
, userdata
);
1135 void bus_track_serialize(sd_bus_track
*t
, FILE *f
, const char *prefix
) {
1141 for (n
= sd_bus_track_first(t
); n
; n
= sd_bus_track_next(t
)) {
1144 c
= sd_bus_track_count_name(t
, n
);
1145 for (j
= 0; j
< c
; j
++)
1146 (void) serialize_item(f
, prefix
, n
);
1150 int bus_track_coldplug(Manager
*m
, sd_bus_track
**t
, bool recursive
, char **l
) {
1156 if (strv_isempty(l
))
1163 r
= sd_bus_track_new(m
->api_bus
, t
, NULL
, NULL
);
1168 r
= sd_bus_track_set_recursive(*t
, recursive
);
1172 return bus_track_add_name_many(*t
, l
);
1175 int bus_verify_manage_units_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-units", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1179 int bus_verify_manage_unit_files_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1180 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.manage-unit-files", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1183 int bus_verify_reload_daemon_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1184 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.reload-daemon", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1187 int bus_verify_set_environment_async(Manager
*m
, sd_bus_message
*call
, sd_bus_error
*error
) {
1188 return bus_verify_polkit_async(call
, CAP_SYS_ADMIN
, "org.freedesktop.systemd1.set-environment", NULL
, false, UID_INVALID
, &m
->polkit_registry
, error
);
1191 uint64_t manager_bus_n_queued_write(Manager
*m
) {
1196 /* Returns the total number of messages queued for writing on all our direct and API buses. */
1198 SET_FOREACH(b
, m
->private_buses
) {
1201 r
= sd_bus_get_n_queued_write(b
, &k
);
1203 log_debug_errno(r
, "Failed to query queued messages for private bus: %m");
1211 r
= sd_bus_get_n_queued_write(m
->api_bus
, &k
);
1213 log_debug_errno(r
, "Failed to query queued messages for API bus: %m");
1221 static void vtable_dump_bus_properties(FILE *f
, const sd_bus_vtable
*table
) {
1222 const sd_bus_vtable
*i
;
1224 for (i
= table
; i
->type
!= _SD_BUS_VTABLE_END
; i
++) {
1225 if (!IN_SET(i
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
) ||
1226 (i
->flags
& (SD_BUS_VTABLE_DEPRECATED
| SD_BUS_VTABLE_HIDDEN
)) != 0)
1229 fprintf(f
, "%s\n", i
->x
.property
.member
);
1233 void dump_bus_properties(FILE *f
) {
1236 vtable_dump_bus_properties(f
, bus_automount_vtable
);
1237 vtable_dump_bus_properties(f
, bus_cgroup_vtable
);
1238 vtable_dump_bus_properties(f
, bus_device_vtable
);
1239 vtable_dump_bus_properties(f
, bus_exec_vtable
);
1240 vtable_dump_bus_properties(f
, bus_job_vtable
);
1241 vtable_dump_bus_properties(f
, bus_kill_vtable
);
1242 vtable_dump_bus_properties(f
, bus_manager_vtable
);
1243 vtable_dump_bus_properties(f
, bus_mount_vtable
);
1244 vtable_dump_bus_properties(f
, bus_path_vtable
);
1245 vtable_dump_bus_properties(f
, bus_scope_vtable
);
1246 vtable_dump_bus_properties(f
, bus_service_vtable
);
1247 vtable_dump_bus_properties(f
, bus_slice_vtable
);
1248 vtable_dump_bus_properties(f
, bus_socket_vtable
);
1249 vtable_dump_bus_properties(f
, bus_swap_vtable
);
1250 vtable_dump_bus_properties(f
, bus_target_vtable
);
1251 vtable_dump_bus_properties(f
, bus_timer_vtable
);
1252 vtable_dump_bus_properties(f
, bus_unit_vtable
);
1253 vtable_dump_bus_properties(f
, bus_unit_cgroup_vtable
);