1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "bpf-firewall.h"
7 #include "bus-common-errors.h"
8 #include "bus-get-properties.h"
9 #include "bus-polkit.h"
10 #include "cgroup-util.h"
11 #include "condition.h"
13 #include "dbus-manager.h"
14 #include "dbus-unit.h"
15 #include "dbus-util.h"
19 #include "locale-util.h"
21 #include "path-util.h"
22 #include "process-util.h"
23 #include "selinux-access.h"
25 #include "signal-util.h"
27 #include "string-table.h"
28 #include "string-util.h"
30 #include "user-util.h"
33 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
34 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
35 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
36 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
37 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
38 static BUS_DEFINE_PROPERTY_GET2(property_get_freezer_state
, "s", Unit
, unit_freezer_state
, freezer_state_to_string
);
39 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
40 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
41 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
42 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
43 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
44 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
45 static BUS_DEFINE_PROPERTY_GET(property_get_can_freeze
, "b", Unit
, unit_can_freeze
);
46 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
47 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
49 static int property_get_can_clean(
52 const char *interface
,
54 sd_bus_message
*reply
,
56 sd_bus_error
*error
) {
65 r
= unit_can_clean(u
, &mask
);
69 r
= sd_bus_message_open_container(reply
, 'a', "s");
73 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
74 if (!FLAGS_SET(mask
, 1U << t
))
77 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
82 if (FLAGS_SET(mask
, EXEC_CLEAN_FDSTORE
)) {
83 r
= sd_bus_message_append(reply
, "s", "fdstore");
88 return sd_bus_message_close_container(reply
);
91 static int property_get_names(
94 const char *interface
,
96 sd_bus_message
*reply
,
98 sd_bus_error
*error
) {
100 Unit
*u
= ASSERT_PTR(userdata
);
107 r
= sd_bus_message_open_container(reply
, 'a', "s");
111 r
= sd_bus_message_append(reply
, "s", u
->id
);
115 SET_FOREACH(t
, u
->aliases
) {
116 r
= sd_bus_message_append(reply
, "s", t
);
121 return sd_bus_message_close_container(reply
);
124 static int property_get_following(
127 const char *interface
,
128 const char *property
,
129 sd_bus_message
*reply
,
131 sd_bus_error
*error
) {
133 Unit
*u
= userdata
, *f
;
139 f
= unit_following(u
);
140 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
143 static int property_get_dependencies(
146 const char *interface
,
147 const char *property
,
148 sd_bus_message
*reply
,
150 sd_bus_error
*error
) {
152 Unit
*u
= userdata
, *other
;
162 d
= unit_dependency_from_string(property
);
165 deps
= unit_get_dependencies(u
, d
);
167 r
= sd_bus_message_open_container(reply
, 'a', "s");
171 HASHMAP_FOREACH_KEY(v
, other
, deps
) {
172 r
= sd_bus_message_append(reply
, "s", other
->id
);
177 return sd_bus_message_close_container(reply
);
180 static int property_get_mounts_for(
183 const char *interface
,
184 const char *property
,
185 sd_bus_message
*reply
,
187 sd_bus_error
*error
) {
189 Hashmap
**h
= ASSERT_PTR(userdata
);
197 r
= sd_bus_message_open_container(reply
, 'a', "s");
201 HASHMAP_FOREACH_KEY(v
, p
, *h
) {
202 r
= sd_bus_message_append(reply
, "s", p
);
207 return sd_bus_message_close_container(reply
);
210 static int property_get_unit_file_preset(
213 const char *interface
,
214 const char *property
,
215 sd_bus_message
*reply
,
217 sd_bus_error
*error
) {
219 Unit
*u
= ASSERT_PTR(userdata
);
225 r
= unit_get_unit_file_preset(u
);
227 return sd_bus_message_append(reply
, "s", preset_action_past_tense_to_string(r
));
230 static int property_get_job(
233 const char *interface
,
234 const char *property
,
235 sd_bus_message
*reply
,
237 sd_bus_error
*error
) {
239 _cleanup_free_
char *p
= NULL
;
240 Job
**j
= ASSERT_PTR(userdata
);
246 return sd_bus_message_append(reply
, "(uo)", 0, "/");
248 p
= job_dbus_path(*j
);
252 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
255 static int property_get_conditions(
258 const char *interface
,
259 const char *property
,
260 sd_bus_message
*reply
,
262 sd_bus_error
*error
) {
264 const char *(*to_string
)(ConditionType type
) = NULL
;
265 Condition
**list
= ASSERT_PTR(userdata
);
271 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
273 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
277 LIST_FOREACH(conditions
, c
, *list
) {
281 c
->result
== CONDITION_UNTESTED
? 0 :
282 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
284 r
= sd_bus_message_append(reply
, "(sbbsi)",
286 c
->trigger
, c
->negate
,
287 c
->parameter
, tristate
);
293 return sd_bus_message_close_container(reply
);
296 static int property_get_load_error(
299 const char *interface
,
300 const char *property
,
301 sd_bus_message
*reply
,
303 sd_bus_error
*error
) {
305 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
306 Unit
*u
= ASSERT_PTR(userdata
);
312 r
= bus_unit_validate_load_state(u
, &e
);
314 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
316 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
319 static int property_get_markers(
322 const char *interface
,
323 const char *property
,
324 sd_bus_message
*reply
,
326 sd_bus_error
*error
) {
328 unsigned *markers
= ASSERT_PTR(userdata
);
334 r
= sd_bus_message_open_container(reply
, 'a', "s");
338 /* Make sure out values fit in the bitfield. */
339 assert_cc(_UNIT_MARKER_MAX
<= sizeof(((Unit
){}).markers
) * 8);
341 for (UnitMarker m
= 0; m
< _UNIT_MARKER_MAX
; m
++)
342 if (FLAGS_SET(*markers
, 1u << m
)) {
343 r
= sd_bus_message_append(reply
, "s", unit_marker_to_string(m
));
348 return sd_bus_message_close_container(reply
);
351 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
352 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
353 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
354 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
355 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
356 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
359 int bus_unit_method_start_generic(
360 sd_bus_message
*message
,
363 bool reload_if_possible
,
364 sd_bus_error
*error
) {
366 BusUnitQueueFlags job_flags
= reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0;
367 const char *smode
, *verb
;
373 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
375 r
= mac_selinux_unit_access_check(
377 job_type_to_access_method(job_type
),
382 r
= sd_bus_message_read(message
, "s", &smode
);
386 mode
= job_mode_from_string(smode
);
388 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
390 if (reload_if_possible
)
391 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
393 verb
= job_type_to_string(job_type
);
395 if (sd_bus_message_is_method_call(message
, NULL
, "StartUnitWithFlags")) {
396 uint64_t input_flags
= 0;
398 r
= sd_bus_message_read(message
, "t", &input_flags
);
401 /* Let clients know that this version doesn't support any flags at the moment. */
402 if (input_flags
!= 0)
403 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
404 "Invalid 'flags' parameter '%" PRIu64
"'",
408 r
= bus_verify_manage_units_async_full(
411 polkit_message_for_job
[job_type
],
417 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
419 return bus_unit_queue_job(message
, u
, job_type
, mode
, job_flags
, error
);
422 static int bus_unit_method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
423 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
426 static int bus_unit_method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
427 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
430 static int bus_unit_method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
431 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
434 static int bus_unit_method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
435 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
438 static int bus_unit_method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
439 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
442 static int bus_unit_method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
443 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
446 static int bus_unit_method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
447 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
450 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
451 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
452 const char *jtype
, *smode
;
453 Unit
*u
= ASSERT_PTR(userdata
);
460 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
464 /* Parse the two magic reload types "reload-or-…" manually */
465 if (streq(jtype
, "reload-or-restart")) {
467 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
468 } else if (streq(jtype
, "reload-or-try-restart")) {
469 type
= JOB_TRY_RESTART
;
470 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
472 /* And the rest generically */
473 type
= job_type_from_string(jtype
);
475 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
478 mode
= job_mode_from_string(smode
);
480 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
482 r
= mac_selinux_unit_access_check(
484 job_type_to_access_method(type
),
489 r
= bus_verify_manage_units_async_full(
492 polkit_message_for_job
[type
],
498 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
500 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
503 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
504 Unit
*u
= ASSERT_PTR(userdata
);
513 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
517 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
521 if (startswith(sd_bus_message_get_member(message
), "QueueSignal")) {
522 r
= sd_bus_message_read(message
, "i", &value
);
533 who
= kill_who_from_string(swho
);
535 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument: %s", swho
);
538 if (!SIGNAL_VALID(signo
))
539 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
541 if (code
== SI_QUEUE
&& !((signo
>= SIGRTMIN
) && (signo
<= SIGRTMAX
)))
542 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
543 "Value parameter only accepted for realtime signals (SIGRTMIN…SIGRTMAX), refusing for signal SIG%s.", signal_to_string(signo
));
545 r
= bus_verify_manage_units_async_full(
548 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
554 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
556 r
= unit_kill(u
, who
, signo
, code
, value
, error
);
560 return sd_bus_reply_method_return(message
, NULL
);
563 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
564 Unit
*u
= ASSERT_PTR(userdata
);
569 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
573 r
= bus_verify_manage_units_async_full(
576 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
582 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
584 unit_reset_failed(u
);
586 return sd_bus_reply_method_return(message
, NULL
);
589 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
590 Unit
*u
= ASSERT_PTR(userdata
);
595 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
599 r
= sd_bus_message_read(message
, "b", &runtime
);
603 r
= bus_verify_manage_units_async_full(
606 N_("Authentication is required to set properties on '$(unit)'."),
612 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
614 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
618 return sd_bus_reply_method_return(message
, NULL
);
621 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
622 Unit
*u
= ASSERT_PTR(userdata
);
627 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
631 r
= bus_verify_manage_units_async_full(
634 /* polkit_message= */ NULL
,
640 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
642 r
= bus_unit_track_add_sender(u
, message
);
646 return sd_bus_reply_method_return(message
, NULL
);
649 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
650 Unit
*u
= ASSERT_PTR(userdata
);
655 r
= bus_unit_track_remove_sender(u
, message
);
657 return sd_bus_error_set(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
661 return sd_bus_reply_method_return(message
, NULL
);
664 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
665 ExecCleanMask mask
= 0;
666 Unit
*u
= ASSERT_PTR(userdata
);
671 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
675 r
= sd_bus_message_enter_container(message
, 'a', "s");
683 r
= sd_bus_message_read(message
, "s", &i
);
689 m
= exec_clean_mask_from_string(i
);
691 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
696 r
= sd_bus_message_exit_container(message
);
700 r
= bus_verify_manage_units_async_full(
703 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
709 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
711 r
= unit_clean(u
, mask
);
712 if (r
== -EOPNOTSUPP
)
713 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support cleaning.", u
->id
);
715 return sd_bus_error_set(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
717 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
721 return sd_bus_reply_method_return(message
, NULL
);
724 static int bus_unit_method_freezer_generic(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
, FreezerAction action
) {
726 int (*method
)(Unit
*);
727 Unit
*u
= ASSERT_PTR(userdata
);
728 bool reply_no_delay
= false;
732 assert(IN_SET(action
, FREEZER_FREEZE
, FREEZER_THAW
));
734 if (action
== FREEZER_FREEZE
) {
736 method
= unit_freeze
;
742 r
= mac_selinux_unit_access_check(u
, message
, perm
, error
);
746 r
= bus_verify_manage_units_async_full(
749 N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
755 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
758 if (r
== -EOPNOTSUPP
)
759 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support freezing.", u
->id
);
761 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit has a pending job.");
763 return sd_bus_error_set(error
, BUS_ERROR_UNIT_INACTIVE
, "Unit is inactive.");
765 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Previously requested freezer operation for unit '%s' is still in progress.", u
->id
);
769 reply_no_delay
= true;
771 if (u
->pending_freezer_invocation
) {
772 bus_unit_send_pending_freezer_message(u
, true);
773 assert(!u
->pending_freezer_invocation
);
776 u
->pending_freezer_invocation
= sd_bus_message_ref(message
);
778 if (reply_no_delay
) {
779 r
= bus_unit_send_pending_freezer_message(u
, false);
787 int bus_unit_method_thaw(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
788 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_THAW
);
791 int bus_unit_method_freeze(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
792 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_FREEZE
);
795 static int property_get_refs(
798 const char *interface
,
799 const char *property
,
800 sd_bus_message
*reply
,
802 sd_bus_error
*error
) {
810 r
= sd_bus_message_open_container(reply
, 'a', "s");
814 for (const char *i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
817 c
= sd_bus_track_count_name(u
->bus_track
, i
);
821 /* Add the item multiple times if the ref count for each is above 1 */
822 for (int k
= 0; k
< c
; k
++) {
823 r
= sd_bus_message_append(reply
, "s", i
);
829 return sd_bus_message_close_container(reply
);
832 const sd_bus_vtable bus_unit_vtable
[] = {
833 SD_BUS_VTABLE_START(0),
835 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
836 SD_BUS_PROPERTY("Names", "as", property_get_names
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
837 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
838 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
839 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
840 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
841 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
842 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
843 SD_BUS_PROPERTY("Upholds", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
844 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
845 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
846 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
847 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
848 SD_BUS_PROPERTY("UpheldBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
849 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
850 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
851 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
852 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
853 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
854 SD_BUS_PROPERTY("OnSuccess", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
855 SD_BUS_PROPERTY("OnSuccessOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
856 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
857 SD_BUS_PROPERTY("OnFailureOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
858 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
859 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
860 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
861 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
862 SD_BUS_PROPERTY("PropagatesStopTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
863 SD_BUS_PROPERTY("StopPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
864 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
865 SD_BUS_PROPERTY("SliceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
866 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_mounts_for
, offsetof(Unit
, mounts_for
[UNIT_MOUNT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
867 SD_BUS_PROPERTY("WantsMountsFor", "as", property_get_mounts_for
, offsetof(Unit
, mounts_for
[UNIT_MOUNT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
868 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
869 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
870 SD_BUS_PROPERTY("AccessSELinuxContext", "s", NULL
, offsetof(Unit
, access_selinux_context
), SD_BUS_VTABLE_PROPERTY_CONST
),
871 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
872 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
873 SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
874 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
875 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
876 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
877 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
878 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
879 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
880 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
881 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
882 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
883 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
884 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
885 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
886 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
887 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
888 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
889 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
890 SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
891 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
892 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
893 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
894 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
895 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
896 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
897 SD_BUS_PROPERTY("SurviveFinalKillSignal", "b", bus_property_get_bool
, offsetof(Unit
, survive_final_kill_signal
), SD_BUS_VTABLE_PROPERTY_CONST
),
898 SD_BUS_PROPERTY("OnSuccesJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_success_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
), /* deprecated */
899 SD_BUS_PROPERTY("OnSuccessJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_success_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
900 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
901 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
902 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, 0),
903 SD_BUS_PROPERTY("Markers", "as", property_get_markers
, offsetof(Unit
, markers
), 0),
904 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
905 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
906 SD_BUS_PROPERTY("JobTimeoutAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
907 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
908 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
909 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
910 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
911 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
912 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
913 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
914 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
915 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
916 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
917 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
918 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
919 SD_BUS_PROPERTY("StartLimitAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
920 SD_BUS_PROPERTY("FailureAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
921 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
922 SD_BUS_PROPERTY("SuccessAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
923 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
924 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
925 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
926 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
927 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
928 SD_BUS_PROPERTY("ActivationDetails", "a(ss)", bus_property_get_activation_details
, offsetof(Unit
, activation_details
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
930 SD_BUS_METHOD_WITH_ARGS("Start",
931 SD_BUS_ARGS("s", mode
),
932 SD_BUS_RESULT("o", job
),
933 bus_unit_method_start
,
934 SD_BUS_VTABLE_UNPRIVILEGED
),
935 SD_BUS_METHOD_WITH_ARGS("Stop",
936 SD_BUS_ARGS("s", mode
),
937 SD_BUS_RESULT("o", job
),
938 bus_unit_method_stop
,
939 SD_BUS_VTABLE_UNPRIVILEGED
),
940 SD_BUS_METHOD_WITH_ARGS("Reload",
941 SD_BUS_ARGS("s", mode
),
942 SD_BUS_RESULT("o", job
),
943 bus_unit_method_reload
,
944 SD_BUS_VTABLE_UNPRIVILEGED
),
945 SD_BUS_METHOD_WITH_ARGS("Restart",
946 SD_BUS_ARGS("s", mode
),
947 SD_BUS_RESULT("o", job
),
948 bus_unit_method_restart
,
949 SD_BUS_VTABLE_UNPRIVILEGED
),
950 SD_BUS_METHOD_WITH_ARGS("TryRestart",
951 SD_BUS_ARGS("s", mode
),
952 SD_BUS_RESULT("o", job
),
953 bus_unit_method_try_restart
,
954 SD_BUS_VTABLE_UNPRIVILEGED
),
955 SD_BUS_METHOD_WITH_ARGS("ReloadOrRestart",
956 SD_BUS_ARGS("s", mode
),
957 SD_BUS_RESULT("o", job
),
958 bus_unit_method_reload_or_restart
,
959 SD_BUS_VTABLE_UNPRIVILEGED
),
960 SD_BUS_METHOD_WITH_ARGS("ReloadOrTryRestart",
961 SD_BUS_ARGS("s", mode
),
962 SD_BUS_RESULT("o", job
),
963 bus_unit_method_reload_or_try_restart
,
964 SD_BUS_VTABLE_UNPRIVILEGED
),
965 SD_BUS_METHOD_WITH_ARGS("EnqueueJob",
966 SD_BUS_ARGS("s", job_type
, "s", job_mode
),
967 SD_BUS_RESULT("u", job_id
, "o", job_path
, "s", unit_id
, "o", unit_path
, "s", job_type
, "a(uosos)", affected_jobs
),
968 bus_unit_method_enqueue_job
,
969 SD_BUS_VTABLE_UNPRIVILEGED
),
970 SD_BUS_METHOD_WITH_ARGS("Kill",
971 SD_BUS_ARGS("s", whom
, "i", signal
),
973 bus_unit_method_kill
,
974 SD_BUS_VTABLE_UNPRIVILEGED
),
975 SD_BUS_METHOD_WITH_ARGS("QueueSignal",
976 SD_BUS_ARGS("s", whom
, "i", signal
, "i", value
),
978 bus_unit_method_kill
,
979 SD_BUS_VTABLE_UNPRIVILEGED
),
980 SD_BUS_METHOD("ResetFailed",
983 bus_unit_method_reset_failed
,
984 SD_BUS_VTABLE_UNPRIVILEGED
),
985 SD_BUS_METHOD_WITH_ARGS("SetProperties",
986 SD_BUS_ARGS("b", runtime
, "a(sv)", properties
),
988 bus_unit_method_set_properties
,
989 SD_BUS_VTABLE_UNPRIVILEGED
),
994 SD_BUS_VTABLE_UNPRIVILEGED
),
995 SD_BUS_METHOD("Unref",
998 bus_unit_method_unref
,
999 SD_BUS_VTABLE_UNPRIVILEGED
),
1000 SD_BUS_METHOD_WITH_ARGS("Clean",
1001 SD_BUS_ARGS("as", mask
),
1003 bus_unit_method_clean
,
1004 SD_BUS_VTABLE_UNPRIVILEGED
),
1005 SD_BUS_METHOD("Freeze",
1008 bus_unit_method_freeze
,
1009 SD_BUS_VTABLE_UNPRIVILEGED
),
1010 SD_BUS_METHOD("Thaw",
1013 bus_unit_method_thaw
,
1014 SD_BUS_VTABLE_UNPRIVILEGED
),
1016 /* For dependency types we don't support anymore always return an empty array */
1017 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1018 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1019 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1020 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1021 /* Obsolete alias names */
1022 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1023 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1028 static int property_get_slice(
1031 const char *interface
,
1032 const char *property
,
1033 sd_bus_message
*reply
,
1035 sd_bus_error
*error
) {
1037 Unit
*u
= ASSERT_PTR(userdata
);
1042 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
1045 static int property_get_current_memory(
1048 const char *interface
,
1049 const char *property
,
1050 sd_bus_message
*reply
,
1052 sd_bus_error
*error
) {
1054 uint64_t sz
= UINT64_MAX
;
1055 Unit
*u
= ASSERT_PTR(userdata
);
1061 r
= unit_get_memory_current(u
, &sz
);
1062 if (r
< 0 && r
!= -ENODATA
)
1063 log_unit_warning_errno(u
, r
, "Failed to get current memory usage from cgroup: %m");
1065 return sd_bus_message_append(reply
, "t", sz
);
1068 static int property_get_available_memory(
1071 const char *interface
,
1072 const char *property
,
1073 sd_bus_message
*reply
,
1075 sd_bus_error
*error
) {
1077 uint64_t sz
= UINT64_MAX
;
1078 Unit
*u
= ASSERT_PTR(userdata
);
1084 r
= unit_get_memory_available(u
, &sz
);
1085 if (r
< 0 && r
!= -ENODATA
)
1086 log_unit_warning_errno(u
, r
, "Failed to get total available memory from cgroup: %m");
1088 return sd_bus_message_append(reply
, "t", sz
);
1091 static int property_get_memory_accounting(
1094 const char *interface
,
1095 const char *property
,
1096 sd_bus_message
*reply
,
1098 sd_bus_error
*error
) {
1100 Unit
*u
= ASSERT_PTR(userdata
);
1101 CGroupMemoryAccountingMetric metric
;
1102 uint64_t sz
= UINT64_MAX
;
1107 assert_se((metric
= cgroup_memory_accounting_metric_from_string(property
)) >= 0);
1108 (void) unit_get_memory_accounting(u
, metric
, &sz
);
1109 return sd_bus_message_append(reply
, "t", sz
);
1112 static int property_get_current_tasks(
1115 const char *interface
,
1116 const char *property
,
1117 sd_bus_message
*reply
,
1119 sd_bus_error
*error
) {
1121 uint64_t cn
= UINT64_MAX
;
1122 Unit
*u
= ASSERT_PTR(userdata
);
1128 r
= unit_get_tasks_current(u
, &cn
);
1129 if (r
< 0 && r
!= -ENODATA
)
1130 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
1132 return sd_bus_message_append(reply
, "t", cn
);
1135 static int property_get_cpu_usage(
1138 const char *interface
,
1139 const char *property
,
1140 sd_bus_message
*reply
,
1142 sd_bus_error
*error
) {
1144 nsec_t ns
= NSEC_INFINITY
;
1145 Unit
*u
= ASSERT_PTR(userdata
);
1151 r
= unit_get_cpu_usage(u
, &ns
);
1152 if (r
< 0 && r
!= -ENODATA
)
1153 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
1155 return sd_bus_message_append(reply
, "t", ns
);
1158 static int property_get_cpuset_cpus(
1161 const char *interface
,
1162 const char *property
,
1163 sd_bus_message
*reply
,
1165 sd_bus_error
*error
) {
1167 Unit
*u
= ASSERT_PTR(userdata
);
1168 _cleanup_(cpu_set_reset
) CPUSet cpus
= {};
1169 _cleanup_free_
uint8_t *array
= NULL
;
1175 (void) unit_get_cpuset(u
, &cpus
, "cpuset.cpus.effective");
1176 (void) cpu_set_to_dbus(&cpus
, &array
, &allocated
);
1177 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1180 static int property_get_cpuset_mems(
1183 const char *interface
,
1184 const char *property
,
1185 sd_bus_message
*reply
,
1187 sd_bus_error
*error
) {
1189 Unit
*u
= ASSERT_PTR(userdata
);
1190 _cleanup_(cpu_set_reset
) CPUSet mems
= {};
1191 _cleanup_free_
uint8_t *array
= NULL
;
1197 (void) unit_get_cpuset(u
, &mems
, "cpuset.mems.effective");
1198 (void) cpu_set_to_dbus(&mems
, &array
, &allocated
);
1199 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1202 static int property_get_cgroup(
1205 const char *interface
,
1206 const char *property
,
1207 sd_bus_message
*reply
,
1209 sd_bus_error
*error
) {
1211 Unit
*u
= ASSERT_PTR(userdata
);
1212 const char *t
= NULL
;
1217 /* Three cases: a) u->cgroup_path is NULL, in which case the
1218 * unit has no control group, which we report as the empty
1219 * string. b) u->cgroup_path is the empty string, which
1220 * indicates the root cgroup, which we report as "/". c) all
1221 * other cases we report as-is. */
1224 t
= empty_to_root(u
->cgroup_path
);
1226 return sd_bus_message_append(reply
, "s", t
);
1229 static int append_process(sd_bus_message
*reply
, const char *p
, PidRef
*pid
, Set
*pids
) {
1230 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
1234 assert(pidref_is_set(pid
));
1236 r
= set_put(pids
, PID_TO_PTR(pid
->pid
));
1237 if (IN_SET(r
, 0, -EEXIST
))
1243 r
= cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1252 (void) pidref_get_cmdline(
1255 PROCESS_CMDLINE_COMM_FALLBACK
| PROCESS_CMDLINE_QUOTE
,
1258 return sd_bus_message_append(reply
,
1261 (uint32_t) pid
->pid
,
1265 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1266 _cleanup_closedir_
DIR *d
= NULL
;
1267 _cleanup_fclose_
FILE *f
= NULL
;
1273 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1280 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1282 /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
1283 * From https://docs.kernel.org/admin-guide/cgroup-v2.html#threads, “cgroup.procs” in a
1284 * threaded domain cgroup contains the PIDs of all processes in the subtree and is not
1285 * readable in the subtree proper. */
1287 r
= cg_read_pidref(f
, &pidref
);
1288 if (IN_SET(r
, 0, -EOPNOTSUPP
))
1293 r
= pidref_is_kernel_thread(&pidref
);
1294 if (r
== -ESRCH
) /* gone by now */
1297 log_debug_errno(r
, "Failed to determine if " PID_FMT
" is a kernel thread, assuming not: %m", pidref
.pid
);
1301 r
= append_process(reply
, p
, &pidref
, pids
);
1306 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1313 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1315 r
= cg_read_subgroup(d
, &g
);
1321 j
= path_join(empty_to_root(p
), g
);
1325 r
= append_cgroup(reply
, j
, pids
);
1333 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1334 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1335 _cleanup_set_free_ Set
*pids
= NULL
;
1341 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1345 pids
= set_new(NULL
);
1349 r
= sd_bus_message_new_method_return(message
, &reply
);
1353 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1357 if (u
->cgroup_path
) {
1358 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1363 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1364 PidRef
*pid
= unit_main_pid(u
);
1365 if (pidref_is_set(pid
)) {
1366 r
= append_process(reply
, NULL
, pid
, pids
);
1371 pid
= unit_control_pid(u
);
1372 if (pidref_is_set(pid
)) {
1373 r
= append_process(reply
, NULL
, pid
, pids
);
1378 r
= sd_bus_message_close_container(reply
);
1382 return sd_bus_send(NULL
, reply
, NULL
);
1385 static int property_get_ip_counter(
1388 const char *interface
,
1389 const char *property
,
1390 sd_bus_message
*reply
,
1392 sd_bus_error
*error
) {
1394 uint64_t value
= UINT64_MAX
;
1395 Unit
*u
= ASSERT_PTR(userdata
);
1396 CGroupIPAccountingMetric metric
;
1402 assert_se((metric
= cgroup_ip_accounting_metric_from_string(property
)) >= 0);
1403 (void) unit_get_ip_accounting(u
, metric
, &value
);
1404 return sd_bus_message_append(reply
, "t", value
);
1407 static int property_get_io_counter(
1410 const char *interface
,
1411 const char *property
,
1412 sd_bus_message
*reply
,
1414 sd_bus_error
*error
) {
1416 uint64_t value
= UINT64_MAX
;
1417 Unit
*u
= ASSERT_PTR(userdata
);
1424 assert_se((metric
= cgroup_io_accounting_metric_from_string(property
)) >= 0);
1425 (void) unit_get_io_accounting(u
, metric
, /* allow_cache= */ false, &value
);
1426 return sd_bus_message_append(reply
, "t", value
);
1429 static int property_get_effective_limit(
1432 const char *interface
,
1433 const char *property
,
1434 sd_bus_message
*reply
,
1436 sd_bus_error
*error
) {
1438 uint64_t value
= CGROUP_LIMIT_MAX
;
1439 Unit
*u
= ASSERT_PTR(userdata
);
1446 assert_se((type
= cgroup_limit_type_from_string(property
)) >= 0);
1447 (void) unit_get_effective_limit(u
, type
, &value
);
1448 return sd_bus_message_append(reply
, "t", value
);
1451 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1452 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1453 _cleanup_set_free_ Set
*pids
= NULL
;
1460 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1461 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1462 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1463 * subcgroup path has no effect! */
1465 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1469 r
= sd_bus_message_read(message
, "s", &path
);
1473 path
= empty_to_null(path
);
1475 if (!path_is_absolute(path
))
1476 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1478 if (!path_is_normalized(path
))
1479 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1482 if (!unit_cgroup_delegate(u
))
1483 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1485 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1486 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1488 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1492 r
= sd_bus_message_enter_container(message
, 'a', "u");
1496 _cleanup_(pidref_freep
) PidRef
*pidref
= NULL
;
1497 uid_t process_uid
, sender_uid
;
1501 r
= sd_bus_message_read(message
, "u", &upid
);
1508 r
= sd_bus_creds_get_pid(creds
, &pid
);
1514 r
= pidref_new_from_pid(pid
, &pidref
);
1518 /* Filter out duplicates */
1519 if (set_contains(pids
, pidref
))
1522 /* Check if this process is suitable for attaching to this unit */
1523 r
= unit_pid_attachable(u
, pidref
, error
);
1527 /* Let's query the sender's UID, so that we can make our security decisions */
1528 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1532 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1533 * then the process' UID and the target unit's UID have to match the sender's UID */
1534 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1535 r
= pidref_get_uid(pidref
, &process_uid
);
1537 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1539 if (process_uid
!= sender_uid
)
1540 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1541 if (process_uid
!= u
->ref_uid
)
1542 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1545 r
= set_ensure_consume(&pids
, &pidref_hash_ops_free
, TAKE_PTR(pidref
));
1550 r
= sd_bus_message_exit_container(message
);
1554 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1556 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1558 return sd_bus_reply_method_return(message
, NULL
);
1561 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1562 SD_BUS_VTABLE_START(0),
1563 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1564 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1565 SD_BUS_PROPERTY("ControlGroupId", "t", NULL
, offsetof(Unit
, cgroup_id
), 0),
1566 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1567 SD_BUS_PROPERTY("MemoryPeak", "t", property_get_memory_accounting
, 0, 0),
1568 SD_BUS_PROPERTY("MemorySwapCurrent", "t", property_get_memory_accounting
, 0, 0),
1569 SD_BUS_PROPERTY("MemorySwapPeak", "t", property_get_memory_accounting
, 0, 0),
1570 SD_BUS_PROPERTY("MemoryZSwapCurrent", "t", property_get_memory_accounting
, 0, 0),
1571 SD_BUS_PROPERTY("MemoryAvailable", "t", property_get_available_memory
, 0, 0),
1572 SD_BUS_PROPERTY("EffectiveMemoryMax", "t", property_get_effective_limit
, 0, 0),
1573 SD_BUS_PROPERTY("EffectiveMemoryHigh", "t", property_get_effective_limit
, 0, 0),
1574 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1575 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus
, 0, 0),
1576 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems
, 0, 0),
1577 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1578 SD_BUS_PROPERTY("EffectiveTasksMax", "t", property_get_effective_limit
, 0, 0),
1579 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1580 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1581 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1582 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1583 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1584 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1585 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1586 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1588 SD_BUS_METHOD_WITH_ARGS("GetProcesses",
1590 SD_BUS_ARGS("a(sus)", processes
),
1591 bus_unit_method_get_processes
,
1592 SD_BUS_VTABLE_UNPRIVILEGED
),
1594 SD_BUS_METHOD_WITH_ARGS("AttachProcesses",
1595 SD_BUS_ARGS("s", subcgroup
, "au", pids
),
1597 bus_unit_method_attach_processes
,
1598 SD_BUS_VTABLE_UNPRIVILEGED
),
1603 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1604 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1605 _cleanup_free_
char *p
= NULL
;
1606 Unit
*u
= ASSERT_PTR(userdata
);
1611 p
= unit_dbus_path(u
);
1615 r
= sd_bus_message_new_signal(
1618 "/org/freedesktop/systemd1",
1619 "org.freedesktop.systemd1.Manager",
1624 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1628 return sd_bus_send(bus
, m
, NULL
);
1631 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1632 _cleanup_free_
char *p
= NULL
;
1633 Unit
*u
= ASSERT_PTR(userdata
);
1638 p
= unit_dbus_path(u
);
1642 /* Send a properties changed signal. First for the specific
1643 * type, then for the generic unit. The clients may rely on
1644 * this order to get atomic behavior if needed. */
1646 r
= sd_bus_emit_properties_changed_strv(
1648 unit_dbus_interface_from_type(u
->type
),
1653 return sd_bus_emit_properties_changed_strv(
1655 "org.freedesktop.systemd1.Unit",
1659 void bus_unit_send_change_signal(Unit
*u
) {
1663 if (u
->in_dbus_queue
) {
1664 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1665 u
->in_dbus_queue
= false;
1667 /* The unit might be good to be GC once its pending signals have been sent */
1668 unit_add_to_gc_queue(u
);
1674 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1676 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1678 u
->sent_dbus_new_signal
= true;
1681 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1683 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1684 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1685 * so that clients can follow the full state transition */
1687 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1690 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1691 * the unit appears in the new state right-away (except if the
1692 * caller explicitly asked us to send it anyway) */
1695 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1696 * when we are reloading. */
1699 bus_unit_send_change_signal(u
);
1702 int bus_unit_send_pending_freezer_message(Unit
*u
, bool cancelled
) {
1703 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1708 if (!u
->pending_freezer_invocation
)
1712 r
= sd_bus_message_new_method_error(
1713 u
->pending_freezer_invocation
,
1715 &SD_BUS_ERROR_MAKE_CONST(
1716 BUS_ERROR_FREEZE_CANCELLED
, "Freeze operation aborted"));
1718 r
= sd_bus_message_new_method_return(u
->pending_freezer_invocation
, &reply
);
1722 r
= sd_bus_send(NULL
, reply
, NULL
);
1724 log_warning_errno(r
, "Failed to send queued message, ignoring: %m");
1726 u
->pending_freezer_invocation
= sd_bus_message_unref(u
->pending_freezer_invocation
);
1731 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1732 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1733 _cleanup_free_
char *p
= NULL
;
1734 Unit
*u
= ASSERT_PTR(userdata
);
1739 p
= unit_dbus_path(u
);
1743 r
= sd_bus_message_new_signal(
1746 "/org/freedesktop/systemd1",
1747 "org.freedesktop.systemd1.Manager",
1752 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1756 return sd_bus_send(bus
, m
, NULL
);
1759 void bus_unit_send_removed_signal(Unit
*u
) {
1763 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1764 bus_unit_send_change_signal(u
);
1769 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1771 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1774 int bus_unit_queue_job_one(
1775 sd_bus_message
*message
,
1779 BusUnitQueueFlags flags
,
1780 sd_bus_message
*reply
,
1781 sd_bus_error
*error
) {
1783 _cleanup_set_free_ Set
*affected
= NULL
;
1784 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1788 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1789 if (type
== JOB_RESTART
)
1790 type
= JOB_RELOAD_OR_START
;
1791 else if (type
== JOB_TRY_RESTART
)
1792 type
= JOB_TRY_RELOAD
;
1795 if (type
== JOB_STOP
&&
1796 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1797 unit_active_state(u
) == UNIT_INACTIVE
)
1798 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1800 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1801 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1802 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1803 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1804 return sd_bus_error_setf(error
,
1805 BUS_ERROR_ONLY_BY_DEPENDENCY
,
1806 "Operation refused, unit %s may be requested by dependency only (it is configured to refuse manual start/stop).",
1809 /* dbus-broker issues StartUnit for activation requests, and Type=dbus services automatically
1810 * gain dependency on dbus.socket. Therefore, if dbus has a pending stop job, the new start
1811 * job that pulls in dbus again would cause job type conflict. Let's avoid that by rejecting
1812 * job enqueuing early.
1814 * Note that unlike signal_activation_request(), we can't use unit_inactive_or_pending()
1815 * here. StartUnit is a more generic interface, and thus users are allowed to use e.g. systemctl
1816 * to start Type=dbus services even when dbus is inactive. */
1817 if (type
== JOB_START
&& u
->type
== UNIT_SERVICE
&& SERVICE(u
)->type
== SERVICE_DBUS
)
1818 FOREACH_STRING(dbus_unit
, SPECIAL_DBUS_SOCKET
, SPECIAL_DBUS_SERVICE
) {
1821 dbus
= manager_get_unit(u
->manager
, dbus_unit
);
1822 if (dbus
&& unit_stop_pending(dbus
))
1823 return sd_bus_error_setf(error
,
1824 BUS_ERROR_SHUTTING_DOWN
,
1825 "Operation for unit %s refused, D-Bus is shutting down.",
1829 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1830 affected
= set_new(NULL
);
1835 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1839 r
= bus_job_track_sender(j
, message
);
1843 /* Before we send the method reply, force out the announcement JobNew for this job */
1844 bus_job_send_pending_change_signal(j
, true);
1846 job_path
= job_dbus_path(j
);
1850 /* The classic response is just a job object path */
1851 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1852 return sd_bus_message_append(reply
, "o", job_path
);
1854 /* In verbose mode respond with the anchor job plus everything that has been affected */
1856 unit_path
= unit_dbus_path(j
->unit
);
1860 r
= sd_bus_message_append(reply
, "uosos",
1862 j
->unit
->id
, unit_path
,
1863 job_type_to_string(j
->type
));
1867 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1871 SET_FOREACH(a
, affected
) {
1875 /* Free paths from previous iteration */
1876 job_path
= mfree(job_path
);
1877 unit_path
= mfree(unit_path
);
1879 job_path
= job_dbus_path(a
);
1883 unit_path
= unit_dbus_path(a
->unit
);
1887 r
= sd_bus_message_append(reply
, "(uosos)",
1889 a
->unit
->id
, unit_path
,
1890 job_type_to_string(a
->type
));
1895 return sd_bus_message_close_container(reply
);
1898 int bus_unit_queue_job(
1899 sd_bus_message
*message
,
1903 BusUnitQueueFlags flags
,
1904 sd_bus_error
*error
) {
1906 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1911 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1912 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1914 r
= mac_selinux_unit_access_check(
1916 job_type_to_access_method(type
),
1921 r
= sd_bus_message_new_method_return(message
, &reply
);
1925 r
= bus_unit_queue_job_one(message
, u
, type
, mode
, flags
, reply
, error
);
1929 return sd_bus_send(NULL
, reply
, NULL
);
1932 static int bus_unit_set_live_property(
1935 sd_bus_message
*message
,
1936 UnitWriteFlags flags
,
1937 sd_bus_error
*error
) {
1945 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for
1946 * transient units that are being created). */
1948 if (streq(name
, "Description")) {
1951 r
= sd_bus_message_read(message
, "s", &d
);
1955 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1956 r
= unit_set_description(u
, d
);
1960 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1966 /* A setting that only applies to active units. We don't actually write this to /run, this state is
1967 * managed internally. "+foo" sets flag foo, "-foo" unsets flag foo, just "foo" resets flags to
1968 * foo. The last type cannot be mixed with "+" or "-". */
1970 if (streq(name
, "Markers")) {
1971 unsigned settings
= 0, mask
= 0;
1972 bool some_plus_minus
= false, some_absolute
= false;
1974 r
= sd_bus_message_enter_container(message
, 'a', "s");
1982 r
= sd_bus_message_read(message
, "s", &word
);
1988 if (IN_SET(word
[0], '+', '-')) {
1991 some_plus_minus
= true;
1994 some_absolute
= true;
1997 UnitMarker m
= unit_marker_from_string(word
);
1999 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
,
2000 "Unknown marker \"%s\".", word
);
2002 SET_FLAG(settings
, 1u << m
, b
);
2003 SET_FLAG(mask
, 1u << m
, true);
2006 r
= sd_bus_message_exit_container(message
);
2010 if (some_plus_minus
&& some_absolute
)
2011 return sd_bus_error_set(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Bad marker syntax.");
2013 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2015 u
->markers
= settings
;
2017 u
->markers
= settings
| (u
->markers
& ~mask
);
2026 static int bus_set_transient_emergency_action(
2030 sd_bus_message
*message
,
2031 UnitWriteFlags flags
,
2032 sd_bus_error
*error
) {
2040 r
= sd_bus_message_read(message
, "s", &s
);
2044 r
= parse_emergency_action(s
, u
->manager
->runtime_scope
, &v
);
2046 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
2047 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
2048 : "Invalid %s setting: %s",
2051 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2053 unit_write_settingf(u
, flags
, name
,
2060 static int bus_set_transient_exit_status(
2064 sd_bus_message
*message
,
2065 UnitWriteFlags flags
,
2066 sd_bus_error
*error
) {
2073 r
= sd_bus_message_read(message
, "i", &k
);
2078 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
2080 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2081 *p
= k
< 0 ? -1 : k
;
2084 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2086 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
2092 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
2093 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
2095 static int bus_set_transient_conditions(
2100 sd_bus_message
*message
,
2101 UnitWriteFlags flags
,
2102 sd_bus_error
*error
) {
2104 const char *type_name
, *param
;
2105 int trigger
, negate
, r
;
2110 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
2114 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
2117 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
2119 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
2122 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
2124 if (condition_takes_path(t
) && !path_is_absolute(param
))
2125 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
2127 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2130 c
= condition_new(t
, param
, trigger
, negate
);
2134 LIST_PREPEND(conditions
, *list
, c
);
2136 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
2137 "%s=%s%s%s", type_name
,
2138 trigger
? "|" : "", negate
? "!" : "", param
);
2146 r
= sd_bus_message_exit_container(message
);
2150 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
2151 *list
= condition_free_list(*list
);
2152 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
2158 static int bus_unit_set_transient_property(
2161 sd_bus_message
*message
,
2162 UnitWriteFlags flags
,
2163 sd_bus_error
*error
) {
2172 /* Handles settings when transient units are created. This settings cannot be altered anymore after
2173 * the unit has been created. */
2175 if (streq(name
, "SourcePath"))
2176 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
2178 if (streq(name
, "StopWhenUnneeded"))
2179 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
2181 if (streq(name
, "RefuseManualStart"))
2182 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
2184 if (streq(name
, "RefuseManualStop"))
2185 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
2187 if (streq(name
, "AllowIsolate"))
2188 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
2190 if (streq(name
, "DefaultDependencies"))
2191 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
2193 if (streq(name
, "SurviveFinalKillSignal"))
2194 return bus_set_transient_bool(u
, name
, &u
->survive_final_kill_signal
, message
, flags
, error
);
2196 if (streq(name
, "OnSuccessJobMode"))
2197 return bus_set_transient_job_mode(u
, name
, &u
->on_success_job_mode
, message
, flags
, error
);
2199 if (streq(name
, "OnFailureJobMode"))
2200 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
2202 if (streq(name
, "IgnoreOnIsolate"))
2203 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
2205 if (streq(name
, "JobTimeoutUSec")) {
2206 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
2207 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
2208 u
->job_running_timeout
= u
->job_timeout
;
2211 if (streq(name
, "JobRunningTimeoutUSec")) {
2212 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
2213 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
2214 u
->job_running_timeout_set
= true;
2219 if (streq(name
, "JobTimeoutAction"))
2220 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
2222 if (streq(name
, "JobTimeoutRebootArgument"))
2223 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
2225 if (streq(name
, "StartLimitIntervalUSec"))
2226 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
2228 if (streq(name
, "StartLimitBurst"))
2229 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
2231 if (streq(name
, "StartLimitAction"))
2232 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
2234 if (streq(name
, "FailureAction"))
2235 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
2237 if (streq(name
, "SuccessAction"))
2238 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
2240 if (streq(name
, "FailureActionExitStatus"))
2241 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
2243 if (streq(name
, "SuccessActionExitStatus"))
2244 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
2246 if (streq(name
, "RebootArgument"))
2247 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
2249 if (streq(name
, "CollectMode"))
2250 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
2252 if (streq(name
, "Conditions"))
2253 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
2255 if (streq(name
, "Asserts"))
2256 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
2258 if (streq(name
, "Documentation")) {
2259 _cleanup_strv_free_
char **l
= NULL
;
2261 r
= sd_bus_message_read_strv(message
, &l
);
2266 if (!documentation_url_is_valid(*p
))
2267 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
2269 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2270 if (strv_isempty(l
)) {
2271 u
->documentation
= strv_free(u
->documentation
);
2272 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2274 r
= strv_extend_strv(&u
->documentation
, l
, /* filter_duplicates= */ false);
2279 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2285 } else if (streq(name
, "Slice")) {
2289 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
2290 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
2291 if (u
->type
== UNIT_SLICE
)
2292 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
2293 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
2294 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
2296 r
= sd_bus_message_read(message
, "s", &s
);
2300 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
2301 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
2303 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2304 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2305 * instead of manager_load_unit() on purpose, here. */
2306 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
2310 if (slice
->type
!= UNIT_SLICE
)
2311 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
2313 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2314 r
= unit_set_slice(u
, slice
);
2318 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
2323 } else if (STR_IN_SET(name
, "RequiresMountsFor", "WantsMountsFor")) {
2324 _cleanup_strv_free_
char **l
= NULL
;
2326 r
= sd_bus_message_read_strv(message
, &l
);
2330 STRV_FOREACH(p
, l
) {
2333 if (!path_is_absolute(*p
))
2334 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
2336 if (!path_is_valid(*p
))
2337 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
2339 if (!path_is_normalized(*p
))
2340 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
2342 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2343 r
= unit_add_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
, unit_mount_dependency_type_from_string(name
));
2345 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add requested mount \"%s\": %m", *p
);
2347 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2354 if (streq(name
, "RequiresOverridable"))
2355 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
2356 else if (streq(name
, "RequisiteOverridable"))
2357 d
= UNIT_REQUISITE
; /* same here */
2359 d
= unit_dependency_from_string(name
);
2376 UNIT_PROPAGATES_RELOAD_TO
,
2377 UNIT_RELOAD_PROPAGATED_FROM
,
2378 UNIT_PROPAGATES_STOP_TO
,
2379 UNIT_STOP_PROPAGATED_FROM
,
2380 UNIT_JOINS_NAMESPACE_OF
))
2381 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d
));
2383 r
= sd_bus_message_enter_container(message
, 'a', "s");
2387 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
2388 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
2389 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
2391 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2392 _cleanup_free_
char *label
= NULL
;
2394 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
2398 label
= strjoin(name
, "-", other
);
2402 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
2409 r
= sd_bus_message_exit_container(message
);
2415 } else if (streq(name
, "AddRef")) {
2419 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2420 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2421 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2422 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2423 * dependency type, hence let's not confuse things with that.
2425 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2426 * the transient unit is complete, so that setting this property multiple times in the same transient
2427 * unit creation call doesn't count as individual references. */
2429 r
= sd_bus_message_read(message
, "b", &b
);
2433 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2434 u
->bus_track_add
= b
;
2442 int bus_unit_set_properties(
2444 sd_bus_message
*message
,
2445 UnitWriteFlags flags
,
2447 sd_bus_error
*error
) {
2449 bool for_real
= false;
2456 /* We iterate through the array twice. First run just checks if all passed data is valid, second run
2457 * actually applies it. This implements transaction-like behaviour without actually providing full
2460 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2468 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2472 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2475 /* Reached EOF. Let's try again, and this time for realz... */
2476 r
= sd_bus_message_rewind(message
, false);
2484 r
= sd_bus_message_read(message
, "s", &name
);
2488 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2492 /* If not for real, then mask out the two target flags */
2493 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2495 if (UNIT_VTABLE(u
)->bus_set_property
)
2496 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2499 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2500 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2502 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2507 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
,
2508 "Cannot set property %s, or unknown property.", name
);
2510 r
= sd_bus_message_exit_container(message
);
2514 r
= sd_bus_message_exit_container(message
);
2521 r
= sd_bus_message_exit_container(message
);
2525 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2526 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2531 /* Pretty much any of the calls above can fail if the message is not formed properly
2532 * or if it has unexpected contents. Fill in a more informative error message here. */
2533 if (sd_bus_error_is_set(error
))
2535 return sd_bus_error_set_errnof(error
, r
,
2536 r
== -ENXIO
? "Failed to set unit properties: Unexpected message contents"
2537 : "Failed to set unit properties: %m");
2540 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2543 /* Generates a pretty error if a unit isn't properly loaded. */
2545 switch (u
->load_state
) {
2550 case UNIT_NOT_FOUND
:
2551 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2553 case UNIT_BAD_SETTING
:
2554 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2556 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2557 return sd_bus_error_set_errnof(error
, u
->load_error
,
2558 "Unit %s failed to load properly, please adjust/correct and reload service manager: %m", u
->id
);
2561 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2566 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2570 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2571 Unit
*u
= ASSERT_PTR(userdata
);
2575 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2577 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2579 unit_add_to_cgroup_empty_queue(u
);
2581 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2582 unit_add_to_gc_queue(u
);
2587 static int bus_unit_allocate_bus_track(Unit
*u
) {
2595 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2599 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2601 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2608 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2613 r
= bus_unit_allocate_bus_track(u
);
2617 return sd_bus_track_add_name(u
->bus_track
, name
);
2620 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2625 r
= bus_unit_allocate_bus_track(u
);
2629 return sd_bus_track_add_sender(u
->bus_track
, m
);
2632 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2635 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet,
2636 * return an error */
2640 return sd_bus_track_remove_sender(u
->bus_track
, m
);