1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "bus-common-errors.h"
5 #include "bus-internal.h"
7 #include "dbus-cgroup.h"
9 #include "dbus-scope.h"
10 #include "dbus-unit.h"
11 #include "dbus-util.h"
14 #include "selinux-access.h"
17 int bus_scope_method_abandon(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
24 r
= mac_selinux_unit_access_check(UNIT(s
), message
, "stop", error
);
28 r
= bus_verify_manage_units_async(UNIT(s
)->manager
, message
, error
);
32 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
36 return sd_bus_error_setf(error
, BUS_ERROR_SCOPE_NOT_RUNNING
, "Scope %s is not running, cannot abandon.", UNIT(s
)->id
);
40 return sd_bus_reply_method_return(message
, NULL
);
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result
, scope_result
, ScopeResult
);
45 const sd_bus_vtable bus_scope_vtable
[] = {
46 SD_BUS_VTABLE_START(0),
47 SD_BUS_PROPERTY("Controller", "s", NULL
, offsetof(Scope
, controller
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
48 SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec
, offsetof(Scope
, timeout_stop_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
49 SD_BUS_PROPERTY("Result", "s", property_get_result
, offsetof(Scope
, result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
50 SD_BUS_PROPERTY("RuntimeMaxUSec", "t", bus_property_get_usec
, offsetof(Scope
, runtime_max_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
51 SD_BUS_SIGNAL("RequestStop", NULL
, 0),
52 SD_BUS_METHOD("Abandon", NULL
, NULL
, bus_scope_method_abandon
, SD_BUS_VTABLE_UNPRIVILEGED
),
56 static int bus_scope_set_transient_property(
59 sd_bus_message
*message
,
61 sd_bus_error
*error
) {
70 flags
|= UNIT_PRIVATE
;
72 if (streq(name
, "TimeoutStopUSec"))
73 return bus_set_transient_usec(u
, name
, &s
->timeout_stop_usec
, message
, flags
, error
);
75 if (streq(name
, "RuntimeMaxUSec"))
76 return bus_set_transient_usec(u
, name
, &s
->runtime_max_usec
, message
, flags
, error
);
78 if (streq(name
, "PIDs")) {
79 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
82 r
= sd_bus_message_enter_container(message
, 'a', "u");
90 r
= sd_bus_message_read(message
, "u", &upid
);
98 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
103 r
= sd_bus_creds_get_pid(creds
, &pid
);
109 r
= unit_pid_attachable(u
, pid
, error
);
113 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
114 r
= unit_watch_pid(u
, pid
, false);
115 if (r
< 0 && r
!= -EEXIST
)
122 r
= sd_bus_message_exit_container(message
);
131 } else if (streq(name
, "Controller")) {
132 const char *controller
;
134 /* We can't support direct connections with this, as direct connections know no service or unique name
135 * concept, but the Controller field stores exactly that. */
136 if (sd_bus_message_get_bus(message
) != u
->manager
->api_bus
)
137 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Sorry, Controller= logic only supported via the bus.");
139 r
= sd_bus_message_read(message
, "s", &controller
);
143 if (!isempty(controller
) && !service_name_is_valid(controller
))
144 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Controller '%s' is not a valid bus name.", controller
);
146 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
147 r
= free_and_strdup(&s
->controller
, empty_to_null(controller
));
158 int bus_scope_set_property(
161 sd_bus_message
*message
,
162 UnitWriteFlags flags
,
163 sd_bus_error
*error
) {
172 r
= bus_cgroup_set_property(u
, &s
->cgroup_context
, name
, message
, flags
, error
);
176 if (u
->load_state
== UNIT_STUB
) {
177 /* While we are created we still accept PIDs */
179 r
= bus_scope_set_transient_property(s
, name
, message
, flags
, error
);
183 r
= bus_kill_context_set_transient_property(u
, &s
->kill_context
, name
, message
, flags
, error
);
191 int bus_scope_commit_properties(Unit
*u
) {
194 unit_invalidate_cgroup_members_masks(u
);
195 unit_realize_cgroup(u
);
200 int bus_scope_send_request_stop(Scope
*s
) {
201 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
202 _cleanup_free_
char *p
= NULL
;
210 p
= unit_dbus_path(UNIT(s
));
214 r
= sd_bus_message_new_signal(
215 UNIT(s
)->manager
->api_bus
,
218 "org.freedesktop.systemd1.Scope",
223 return sd_bus_send_to(UNIT(s
)->manager
->api_bus
, m
, s
->controller
, NULL
);
226 static int on_controller_gone(sd_bus_track
*track
, void *userdata
) {
232 log_unit_debug(UNIT(s
), "Controller %s disappeared from bus.", s
->controller
);
233 unit_add_to_dbus_queue(UNIT(s
));
234 s
->controller
= mfree(s
->controller
);
237 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
242 int bus_scope_track_controller(Scope
*s
) {
247 if (!s
->controller
|| s
->controller_track
)
250 r
= sd_bus_track_new(UNIT(s
)->manager
->api_bus
, &s
->controller_track
, on_controller_gone
, s
);
254 r
= sd_bus_track_add_name(s
->controller_track
, s
->controller
);
256 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);