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"
24 #include "signal-util.h"
26 #include "string-table.h"
27 #include "string-util.h"
29 #include "user-util.h"
32 static bool unit_can_start_refuse_manual(Unit
*u
) {
33 return unit_can_start(u
) && !u
->refuse_manual_start
;
36 static bool unit_can_stop_refuse_manual(Unit
*u
) {
37 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
40 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
41 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
46 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
47 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
48 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
49 static BUS_DEFINE_PROPERTY_GET2(property_get_freezer_state
, "s", Unit
, unit_freezer_state
, freezer_state_to_string
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
51 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
53 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
54 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
55 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
56 static BUS_DEFINE_PROPERTY_GET(property_get_can_freeze
, "b", Unit
, unit_can_freeze
);
57 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
58 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
60 static int property_get_can_clean(
63 const char *interface
,
65 sd_bus_message
*reply
,
67 sd_bus_error
*error
) {
76 r
= unit_can_clean(u
, &mask
);
80 r
= sd_bus_message_open_container(reply
, 'a', "s");
84 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
85 if (!FLAGS_SET(mask
, 1U << t
))
88 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
93 return sd_bus_message_close_container(reply
);
96 static int property_get_names(
99 const char *interface
,
100 const char *property
,
101 sd_bus_message
*reply
,
103 sd_bus_error
*error
) {
113 r
= sd_bus_message_open_container(reply
, 'a', "s");
117 r
= sd_bus_message_append(reply
, "s", u
->id
);
121 SET_FOREACH(t
, u
->aliases
) {
122 r
= sd_bus_message_append(reply
, "s", t
);
127 return sd_bus_message_close_container(reply
);
130 static int property_get_following(
133 const char *interface
,
134 const char *property
,
135 sd_bus_message
*reply
,
137 sd_bus_error
*error
) {
139 Unit
*u
= userdata
, *f
;
145 f
= unit_following(u
);
146 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
149 static int property_get_dependencies(
152 const char *interface
,
153 const char *property
,
154 sd_bus_message
*reply
,
156 sd_bus_error
*error
) {
158 Unit
*u
= userdata
, *other
;
168 d
= unit_dependency_from_string(property
);
171 deps
= unit_get_dependencies(u
, d
);
173 r
= sd_bus_message_open_container(reply
, 'a', "s");
177 HASHMAP_FOREACH_KEY(v
, other
, deps
) {
178 r
= sd_bus_message_append(reply
, "s", other
->id
);
183 return sd_bus_message_close_container(reply
);
186 static int property_get_requires_mounts_for(
189 const char *interface
,
190 const char *property
,
191 sd_bus_message
*reply
,
193 sd_bus_error
*error
) {
195 Hashmap
**h
= userdata
;
204 r
= sd_bus_message_open_container(reply
, 'a', "s");
208 HASHMAP_FOREACH_KEY(v
, p
, *h
) {
209 r
= sd_bus_message_append(reply
, "s", p
);
214 return sd_bus_message_close_container(reply
);
217 static int property_get_unit_file_preset(
220 const char *interface
,
221 const char *property
,
222 sd_bus_message
*reply
,
224 sd_bus_error
*error
) {
233 r
= unit_get_unit_file_preset(u
);
235 return sd_bus_message_append(reply
, "s",
237 r
> 0 ? "enabled" : "disabled");
240 static int property_get_job(
243 const char *interface
,
244 const char *property
,
245 sd_bus_message
*reply
,
247 sd_bus_error
*error
) {
249 _cleanup_free_
char *p
= NULL
;
257 return sd_bus_message_append(reply
, "(uo)", 0, "/");
259 p
= job_dbus_path(*j
);
263 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
266 static int property_get_conditions(
269 const char *interface
,
270 const char *property
,
271 sd_bus_message
*reply
,
273 sd_bus_error
*error
) {
275 const char *(*to_string
)(ConditionType type
) = NULL
;
276 Condition
**list
= userdata
;
283 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
285 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
289 LIST_FOREACH(conditions
, c
, *list
) {
293 c
->result
== CONDITION_UNTESTED
? 0 :
294 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
296 r
= sd_bus_message_append(reply
, "(sbbsi)",
298 c
->trigger
, c
->negate
,
299 c
->parameter
, tristate
);
305 return sd_bus_message_close_container(reply
);
308 static int property_get_load_error(
311 const char *interface
,
312 const char *property
,
313 sd_bus_message
*reply
,
315 sd_bus_error
*error
) {
317 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
325 r
= bus_unit_validate_load_state(u
, &e
);
327 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
329 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
332 static int property_get_markers(
335 const char *interface
,
336 const char *property
,
337 sd_bus_message
*reply
,
339 sd_bus_error
*error
) {
341 unsigned *markers
= userdata
;
348 r
= sd_bus_message_open_container(reply
, 'a', "s");
352 /* Make sure out values fit in the bitfield. */
353 assert_cc(_UNIT_MARKER_MAX
<= sizeof(((Unit
){}).markers
) * 8);
355 for (UnitMarker m
= 0; m
< _UNIT_MARKER_MAX
; m
++)
356 if (FLAGS_SET(*markers
, 1u << m
)) {
357 r
= sd_bus_message_append(reply
, "s", unit_marker_to_string(m
));
362 return sd_bus_message_close_container(reply
);
365 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
366 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
367 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
368 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
369 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
370 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
373 int bus_unit_method_start_generic(
374 sd_bus_message
*message
,
377 bool reload_if_possible
,
378 sd_bus_error
*error
) {
380 BusUnitQueueFlags job_flags
= reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0;
381 const char *smode
, *verb
;
387 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
389 r
= mac_selinux_unit_access_check(
391 job_type_to_access_method(job_type
),
396 r
= sd_bus_message_read(message
, "s", &smode
);
400 mode
= job_mode_from_string(smode
);
402 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
404 if (reload_if_possible
)
405 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
407 verb
= job_type_to_string(job_type
);
409 if (sd_bus_message_is_method_call(message
, NULL
, "StartUnitWithFlags")) {
410 uint64_t input_flags
= 0;
412 r
= sd_bus_message_read(message
, "t", &input_flags
);
415 /* Let clients know that this version doesn't support any flags at the moment. */
416 if (input_flags
!= 0)
417 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
418 "Invalid 'flags' parameter '%" PRIu64
"'",
422 r
= bus_verify_manage_units_async_full(
426 polkit_message_for_job
[job_type
],
433 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
435 return bus_unit_queue_job(message
, u
, job_type
, mode
, job_flags
, error
);
438 static int bus_unit_method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
439 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
442 static int bus_unit_method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
443 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
446 static int bus_unit_method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
447 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
450 static int bus_unit_method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
451 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
454 static int bus_unit_method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
455 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
458 static int bus_unit_method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
459 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
462 static int bus_unit_method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
463 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
466 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
467 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
468 const char *jtype
, *smode
;
477 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
481 /* Parse the two magic reload types "reload-or-…" manually */
482 if (streq(jtype
, "reload-or-restart")) {
484 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
485 } else if (streq(jtype
, "reload-or-try-restart")) {
486 type
= JOB_TRY_RESTART
;
487 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
489 /* And the rest generically */
490 type
= job_type_from_string(jtype
);
492 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
495 mode
= job_mode_from_string(smode
);
497 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
499 r
= mac_selinux_unit_access_check(
501 job_type_to_access_method(type
),
506 r
= bus_verify_manage_units_async_full(
510 polkit_message_for_job
[type
],
517 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
519 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
522 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
532 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
536 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
543 who
= kill_who_from_string(swho
);
545 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
548 if (!SIGNAL_VALID(signo
))
549 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
551 r
= bus_verify_manage_units_async_full(
555 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
562 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
564 r
= unit_kill(u
, who
, signo
, error
);
568 return sd_bus_reply_method_return(message
, NULL
);
571 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
578 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
582 r
= bus_verify_manage_units_async_full(
586 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
593 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
595 unit_reset_failed(u
);
597 return sd_bus_reply_method_return(message
, NULL
);
600 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
607 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
611 r
= sd_bus_message_read(message
, "b", &runtime
);
615 r
= bus_verify_manage_units_async_full(
619 N_("Authentication is required to set properties on '$(unit)'."),
626 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
628 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
632 return sd_bus_reply_method_return(message
, NULL
);
635 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
642 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
646 r
= bus_verify_manage_units_async_full(
657 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
659 r
= bus_unit_track_add_sender(u
, message
);
663 return sd_bus_reply_method_return(message
, NULL
);
666 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
673 r
= bus_unit_track_remove_sender(u
, message
);
675 return sd_bus_error_set(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
679 return sd_bus_reply_method_return(message
, NULL
);
682 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
683 ExecCleanMask mask
= 0;
690 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
694 r
= sd_bus_message_enter_container(message
, 'a', "s");
701 r
= sd_bus_message_read(message
, "s", &i
);
708 mask
|= EXEC_CLEAN_ALL
;
712 t
= exec_resource_type_from_string(i
);
714 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
720 r
= sd_bus_message_exit_container(message
);
724 r
= bus_verify_manage_units_async_full(
728 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
735 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
737 r
= unit_clean(u
, mask
);
738 if (r
== -EOPNOTSUPP
)
739 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not supporting cleaning.", u
->id
);
741 return sd_bus_error_set(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
743 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
747 return sd_bus_reply_method_return(message
, NULL
);
750 static int bus_unit_method_freezer_generic(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
, FreezerAction action
) {
752 int (*method
)(Unit
*);
754 bool reply_no_delay
= false;
759 assert(IN_SET(action
, FREEZER_FREEZE
, FREEZER_THAW
));
761 if (action
== FREEZER_FREEZE
) {
763 method
= unit_freeze
;
769 r
= mac_selinux_unit_access_check(u
, message
, perm
, error
);
773 r
= bus_verify_manage_units_async_full(
777 N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
784 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
787 if (r
== -EOPNOTSUPP
)
788 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support freezing.", u
->id
);
790 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit has a pending job.");
792 return sd_bus_error_set(error
, BUS_ERROR_UNIT_INACTIVE
, "Unit is inactive.");
794 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Previously requested freezer operation for unit '%s' is still in progress.", u
->id
);
798 reply_no_delay
= true;
800 assert(!u
->pending_freezer_message
);
802 r
= sd_bus_message_new_method_return(message
, &u
->pending_freezer_message
);
806 if (reply_no_delay
) {
807 r
= bus_unit_send_pending_freezer_message(u
);
815 int bus_unit_method_thaw(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
816 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_THAW
);
819 int bus_unit_method_freeze(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
820 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_FREEZE
);
823 static int property_get_refs(
826 const char *interface
,
827 const char *property
,
828 sd_bus_message
*reply
,
830 sd_bus_error
*error
) {
838 r
= sd_bus_message_open_container(reply
, 'a', "s");
842 for (const char *i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
845 c
= sd_bus_track_count_name(u
->bus_track
, i
);
849 /* Add the item multiple times if the ref count for each is above 1 */
850 for (int k
= 0; k
< c
; k
++) {
851 r
= sd_bus_message_append(reply
, "s", i
);
857 return sd_bus_message_close_container(reply
);
860 const sd_bus_vtable bus_unit_vtable
[] = {
861 SD_BUS_VTABLE_START(0),
863 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
864 SD_BUS_PROPERTY("Names", "as", property_get_names
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
865 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
866 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
867 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
868 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
869 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
870 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
871 SD_BUS_PROPERTY("Upholds", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
872 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
873 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
874 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
875 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
876 SD_BUS_PROPERTY("UpheldBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
877 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
878 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
879 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
880 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
881 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
882 SD_BUS_PROPERTY("OnSuccess", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
883 SD_BUS_PROPERTY("OnSuccessOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
884 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
885 SD_BUS_PROPERTY("OnFailureOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
886 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
887 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
888 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
889 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
890 SD_BUS_PROPERTY("PropagatesStopTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
891 SD_BUS_PROPERTY("StopPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
892 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
893 SD_BUS_PROPERTY("SliceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
894 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
895 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
896 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
897 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
898 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
899 SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
900 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
901 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
902 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
903 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
904 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
905 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
906 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
907 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
908 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
909 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
910 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
911 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
912 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
913 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
914 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
915 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
916 SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
917 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
918 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
919 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
920 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
921 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
922 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
923 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 */
924 SD_BUS_PROPERTY("OnSuccessJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_success_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
925 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
926 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
927 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, 0),
928 SD_BUS_PROPERTY("Markers", "as", property_get_markers
, offsetof(Unit
, markers
), 0),
929 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
930 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
931 SD_BUS_PROPERTY("JobTimeoutAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
932 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
933 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
934 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
935 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
936 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
937 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
938 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
939 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
940 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
941 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
942 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
943 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
944 SD_BUS_PROPERTY("StartLimitAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
945 SD_BUS_PROPERTY("FailureAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
946 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
947 SD_BUS_PROPERTY("SuccessAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
948 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
949 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
950 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
951 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
952 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
954 SD_BUS_METHOD_WITH_NAMES("Start",
959 bus_unit_method_start
,
960 SD_BUS_VTABLE_UNPRIVILEGED
),
961 SD_BUS_METHOD_WITH_NAMES("Stop",
966 bus_unit_method_stop
,
967 SD_BUS_VTABLE_UNPRIVILEGED
),
968 SD_BUS_METHOD_WITH_NAMES("Reload",
973 bus_unit_method_reload
,
974 SD_BUS_VTABLE_UNPRIVILEGED
),
975 SD_BUS_METHOD_WITH_NAMES("Restart",
980 bus_unit_method_restart
,
981 SD_BUS_VTABLE_UNPRIVILEGED
),
982 SD_BUS_METHOD_WITH_NAMES("TryRestart",
987 bus_unit_method_try_restart
,
988 SD_BUS_VTABLE_UNPRIVILEGED
),
989 SD_BUS_METHOD_WITH_NAMES("ReloadOrRestart",
994 bus_unit_method_reload_or_restart
,
995 SD_BUS_VTABLE_UNPRIVILEGED
),
996 SD_BUS_METHOD_WITH_NAMES("ReloadOrTryRestart",
1001 bus_unit_method_reload_or_try_restart
,
1002 SD_BUS_VTABLE_UNPRIVILEGED
),
1003 SD_BUS_METHOD_WITH_NAMES("EnqueueJob",
1005 SD_BUS_PARAM(job_type
)
1006 SD_BUS_PARAM(job_mode
),
1008 SD_BUS_PARAM(job_id
)
1009 SD_BUS_PARAM(job_path
)
1010 SD_BUS_PARAM(unit_id
)
1011 SD_BUS_PARAM(unit_path
)
1012 SD_BUS_PARAM(job_type
)
1013 SD_BUS_PARAM(affected_jobs
),
1014 bus_unit_method_enqueue_job
,
1015 SD_BUS_VTABLE_UNPRIVILEGED
),
1016 SD_BUS_METHOD_WITH_NAMES("Kill",
1019 SD_BUS_PARAM(signal
),
1021 bus_unit_method_kill
,
1022 SD_BUS_VTABLE_UNPRIVILEGED
),
1023 SD_BUS_METHOD("ResetFailed",
1026 bus_unit_method_reset_failed
,
1027 SD_BUS_VTABLE_UNPRIVILEGED
),
1028 SD_BUS_METHOD_WITH_NAMES("SetProperties",
1030 SD_BUS_PARAM(runtime
)
1031 SD_BUS_PARAM(properties
),
1033 bus_unit_method_set_properties
,
1034 SD_BUS_VTABLE_UNPRIVILEGED
),
1035 SD_BUS_METHOD("Ref",
1038 bus_unit_method_ref
,
1039 SD_BUS_VTABLE_UNPRIVILEGED
),
1040 SD_BUS_METHOD("Unref",
1043 bus_unit_method_unref
,
1044 SD_BUS_VTABLE_UNPRIVILEGED
),
1045 SD_BUS_METHOD_WITH_NAMES("Clean",
1049 bus_unit_method_clean
,
1050 SD_BUS_VTABLE_UNPRIVILEGED
),
1051 SD_BUS_METHOD("Freeze",
1054 bus_unit_method_freeze
,
1055 SD_BUS_VTABLE_UNPRIVILEGED
),
1056 SD_BUS_METHOD("Thaw",
1059 bus_unit_method_thaw
,
1060 SD_BUS_VTABLE_UNPRIVILEGED
),
1062 /* For dependency types we don't support anymore always return an empty array */
1063 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1064 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1065 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1066 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1067 /* Obsolete alias names */
1068 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1069 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1074 static int property_get_slice(
1077 const char *interface
,
1078 const char *property
,
1079 sd_bus_message
*reply
,
1081 sd_bus_error
*error
) {
1089 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
1092 static int property_get_current_memory(
1095 const char *interface
,
1096 const char *property
,
1097 sd_bus_message
*reply
,
1099 sd_bus_error
*error
) {
1101 uint64_t sz
= UINT64_MAX
;
1109 r
= unit_get_memory_current(u
, &sz
);
1110 if (r
< 0 && r
!= -ENODATA
)
1111 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
1113 return sd_bus_message_append(reply
, "t", sz
);
1116 static int property_get_available_memory(
1119 const char *interface
,
1120 const char *property
,
1121 sd_bus_message
*reply
,
1123 sd_bus_error
*error
) {
1125 uint64_t sz
= UINT64_MAX
;
1133 r
= unit_get_memory_available(u
, &sz
);
1134 if (r
< 0 && r
!= -ENODATA
)
1135 log_unit_warning_errno(u
, r
, "Failed to get total available memory from cgroup: %m");
1137 return sd_bus_message_append(reply
, "t", sz
);
1140 static int property_get_current_tasks(
1143 const char *interface
,
1144 const char *property
,
1145 sd_bus_message
*reply
,
1147 sd_bus_error
*error
) {
1149 uint64_t cn
= UINT64_MAX
;
1157 r
= unit_get_tasks_current(u
, &cn
);
1158 if (r
< 0 && r
!= -ENODATA
)
1159 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
1161 return sd_bus_message_append(reply
, "t", cn
);
1164 static int property_get_cpu_usage(
1167 const char *interface
,
1168 const char *property
,
1169 sd_bus_message
*reply
,
1171 sd_bus_error
*error
) {
1173 nsec_t ns
= NSEC_INFINITY
;
1181 r
= unit_get_cpu_usage(u
, &ns
);
1182 if (r
< 0 && r
!= -ENODATA
)
1183 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
1185 return sd_bus_message_append(reply
, "t", ns
);
1188 static int property_get_cpuset_cpus(
1191 const char *interface
,
1192 const char *property
,
1193 sd_bus_message
*reply
,
1195 sd_bus_error
*error
) {
1198 _cleanup_(cpu_set_reset
) CPUSet cpus
= {};
1199 _cleanup_free_
uint8_t *array
= NULL
;
1206 (void) unit_get_cpuset(u
, &cpus
, "cpuset.cpus.effective");
1207 (void) cpu_set_to_dbus(&cpus
, &array
, &allocated
);
1208 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1211 static int property_get_cpuset_mems(
1214 const char *interface
,
1215 const char *property
,
1216 sd_bus_message
*reply
,
1218 sd_bus_error
*error
) {
1221 _cleanup_(cpu_set_reset
) CPUSet mems
= {};
1222 _cleanup_free_
uint8_t *array
= NULL
;
1229 (void) unit_get_cpuset(u
, &mems
, "cpuset.mems.effective");
1230 (void) cpu_set_to_dbus(&mems
, &array
, &allocated
);
1231 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1234 static int property_get_cgroup(
1237 const char *interface
,
1238 const char *property
,
1239 sd_bus_message
*reply
,
1241 sd_bus_error
*error
) {
1244 const char *t
= NULL
;
1250 /* Three cases: a) u->cgroup_path is NULL, in which case the
1251 * unit has no control group, which we report as the empty
1252 * string. b) u->cgroup_path is the empty string, which
1253 * indicates the root cgroup, which we report as "/". c) all
1254 * other cases we report as-is. */
1257 t
= empty_to_root(u
->cgroup_path
);
1259 return sd_bus_message_append(reply
, "s", t
);
1262 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
1263 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
1269 r
= set_put(pids
, PID_TO_PTR(pid
));
1270 if (IN_SET(r
, 0, -EEXIST
))
1276 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1285 (void) get_process_cmdline(pid
, SIZE_MAX
,
1286 PROCESS_CMDLINE_COMM_FALLBACK
| PROCESS_CMDLINE_QUOTE
,
1289 return sd_bus_message_append(reply
,
1296 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1297 _cleanup_closedir_
DIR *d
= NULL
;
1298 _cleanup_fclose_
FILE *f
= NULL
;
1304 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1313 /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
1314 * From https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html#threads,
1315 * “cgroup.procs” in a threaded domain cgroup contains the PIDs of all processes in
1316 * the subtree and is not readable in the subtree proper. */
1317 r
= cg_read_pid(f
, &pid
);
1318 if (IN_SET(r
, 0, -EOPNOTSUPP
))
1323 if (is_kernel_thread(pid
) > 0)
1326 r
= append_process(reply
, p
, pid
, pids
);
1331 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1338 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1340 r
= cg_read_subgroup(d
, &g
);
1346 j
= path_join(empty_to_root(p
), g
);
1350 r
= append_cgroup(reply
, j
, pids
);
1358 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1359 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1360 _cleanup_set_free_ Set
*pids
= NULL
;
1367 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1371 pids
= set_new(NULL
);
1375 r
= sd_bus_message_new_method_return(message
, &reply
);
1379 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1383 if (u
->cgroup_path
) {
1384 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1389 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1390 pid
= unit_main_pid(u
);
1392 r
= append_process(reply
, NULL
, pid
, pids
);
1397 pid
= unit_control_pid(u
);
1399 r
= append_process(reply
, NULL
, pid
, pids
);
1404 r
= sd_bus_message_close_container(reply
);
1408 return sd_bus_send(NULL
, reply
, NULL
);
1411 static int property_get_ip_counter(
1414 const char *interface
,
1415 const char *property
,
1416 sd_bus_message
*reply
,
1418 sd_bus_error
*error
) {
1420 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1421 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1422 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1423 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1424 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1427 uint64_t value
= UINT64_MAX
;
1436 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1437 (void) unit_get_ip_accounting(u
, metric
, &value
);
1438 return sd_bus_message_append(reply
, "t", value
);
1441 static int property_get_io_counter(
1444 const char *interface
,
1445 const char *property
,
1446 sd_bus_message
*reply
,
1448 sd_bus_error
*error
) {
1450 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1451 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1452 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1453 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1454 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1457 uint64_t value
= UINT64_MAX
;
1466 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1467 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1468 return sd_bus_message_append(reply
, "t", value
);
1471 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1473 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1474 _cleanup_set_free_ Set
*pids
= NULL
;
1481 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1482 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1483 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1484 * subcgroup path has no effect! */
1486 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1490 r
= sd_bus_message_read(message
, "s", &path
);
1494 path
= empty_to_null(path
);
1496 if (!path_is_absolute(path
))
1497 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1499 if (!path_is_normalized(path
))
1500 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1503 if (!unit_cgroup_delegate(u
))
1504 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1506 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1507 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1509 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1513 r
= sd_bus_message_enter_container(message
, 'a', "u");
1517 uid_t process_uid
, sender_uid
;
1521 r
= sd_bus_message_read(message
, "u", &upid
);
1528 r
= sd_bus_creds_get_pid(creds
, &pid
);
1534 /* Filter out duplicates */
1535 if (set_contains(pids
, PID_TO_PTR(pid
)))
1538 /* Check if this process is suitable for attaching to this unit */
1539 r
= unit_pid_attachable(u
, pid
, error
);
1543 /* Let's query the sender's UID, so that we can make our security decisions */
1544 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1548 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1549 * then the process' UID and the target unit's UID have to match the sender's UID */
1550 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1551 r
= get_process_uid(pid
, &process_uid
);
1553 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1555 if (process_uid
!= sender_uid
)
1556 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1557 if (process_uid
!= u
->ref_uid
)
1558 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1562 pids
= set_new(NULL
);
1567 r
= set_put(pids
, PID_TO_PTR(pid
));
1572 r
= sd_bus_message_exit_container(message
);
1576 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1578 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1580 return sd_bus_reply_method_return(message
, NULL
);
1583 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1584 SD_BUS_VTABLE_START(0),
1585 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1586 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1587 SD_BUS_PROPERTY("ControlGroupId", "t", NULL
, offsetof(Unit
, cgroup_id
), 0),
1588 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1589 SD_BUS_PROPERTY("MemoryAvailable", "t", property_get_available_memory
, 0, 0),
1590 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1591 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus
, 0, 0),
1592 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems
, 0, 0),
1593 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1594 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1595 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1596 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1597 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1598 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1599 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1600 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1601 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1603 SD_BUS_METHOD_WITH_NAMES("GetProcesses",
1606 SD_BUS_PARAM(processes
),
1607 bus_unit_method_get_processes
,
1608 SD_BUS_VTABLE_UNPRIVILEGED
),
1610 SD_BUS_METHOD_WITH_NAMES("AttachProcesses",
1612 SD_BUS_PARAM(subcgroup
)
1615 bus_unit_method_attach_processes
,
1616 SD_BUS_VTABLE_UNPRIVILEGED
),
1621 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1622 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1623 _cleanup_free_
char *p
= NULL
;
1630 p
= unit_dbus_path(u
);
1634 r
= sd_bus_message_new_signal(
1637 "/org/freedesktop/systemd1",
1638 "org.freedesktop.systemd1.Manager",
1643 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1647 return sd_bus_send(bus
, m
, NULL
);
1650 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1651 _cleanup_free_
char *p
= NULL
;
1658 p
= unit_dbus_path(u
);
1662 /* Send a properties changed signal. First for the specific
1663 * type, then for the generic unit. The clients may rely on
1664 * this order to get atomic behavior if needed. */
1666 r
= sd_bus_emit_properties_changed_strv(
1668 unit_dbus_interface_from_type(u
->type
),
1673 return sd_bus_emit_properties_changed_strv(
1675 "org.freedesktop.systemd1.Unit",
1679 void bus_unit_send_change_signal(Unit
*u
) {
1683 if (u
->in_dbus_queue
) {
1684 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1685 u
->in_dbus_queue
= false;
1691 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1693 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1695 u
->sent_dbus_new_signal
= true;
1698 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1700 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1701 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1702 * so that clients can follow the full state transition */
1704 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1707 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1708 * the unit appears in the new state right-away (except if the
1709 * caller explicitly asked us to send it anyway) */
1712 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1713 * when we are reloading. */
1716 bus_unit_send_change_signal(u
);
1719 int bus_unit_send_pending_freezer_message(Unit
*u
) {
1724 if (!u
->pending_freezer_message
)
1727 r
= sd_bus_send(NULL
, u
->pending_freezer_message
, NULL
);
1729 log_warning_errno(r
, "Failed to send queued message, ignoring: %m");
1731 u
->pending_freezer_message
= sd_bus_message_unref(u
->pending_freezer_message
);
1736 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1737 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1738 _cleanup_free_
char *p
= NULL
;
1745 p
= unit_dbus_path(u
);
1749 r
= sd_bus_message_new_signal(
1752 "/org/freedesktop/systemd1",
1753 "org.freedesktop.systemd1.Manager",
1758 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1762 return sd_bus_send(bus
, m
, NULL
);
1765 void bus_unit_send_removed_signal(Unit
*u
) {
1769 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1770 bus_unit_send_change_signal(u
);
1775 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1777 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1780 int bus_unit_queue_job_one(
1781 sd_bus_message
*message
,
1785 BusUnitQueueFlags flags
,
1786 sd_bus_message
*reply
,
1787 sd_bus_error
*error
) {
1789 _cleanup_set_free_ Set
*affected
= NULL
;
1790 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1794 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1795 affected
= set_new(NULL
);
1800 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1804 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RETURN_SKIP_ON_CONDITION_FAIL
))
1805 j
->return_skip_on_cond_failure
= true;
1807 r
= bus_job_track_sender(j
, message
);
1811 /* Before we send the method reply, force out the announcement JobNew for this job */
1812 bus_job_send_pending_change_signal(j
, true);
1814 job_path
= job_dbus_path(j
);
1818 /* The classic response is just a job object path */
1819 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1820 return sd_bus_message_append(reply
, "o", job_path
);
1822 /* In verbose mode respond with the anchor job plus everything that has been affected */
1824 unit_path
= unit_dbus_path(j
->unit
);
1828 r
= sd_bus_message_append(reply
, "uosos",
1830 j
->unit
->id
, unit_path
,
1831 job_type_to_string(j
->type
));
1835 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1839 SET_FOREACH(a
, affected
) {
1843 /* Free paths from previous iteration */
1844 job_path
= mfree(job_path
);
1845 unit_path
= mfree(unit_path
);
1847 job_path
= job_dbus_path(a
);
1851 unit_path
= unit_dbus_path(a
->unit
);
1855 r
= sd_bus_message_append(reply
, "(uosos)",
1857 a
->unit
->id
, unit_path
,
1858 job_type_to_string(a
->type
));
1863 return sd_bus_message_close_container(reply
);
1866 int bus_unit_queue_job(
1867 sd_bus_message
*message
,
1871 BusUnitQueueFlags flags
,
1872 sd_bus_error
*error
) {
1874 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1879 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1880 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1882 r
= mac_selinux_unit_access_check(
1884 job_type_to_access_method(type
),
1889 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1890 if (type
== JOB_RESTART
)
1891 type
= JOB_RELOAD_OR_START
;
1892 else if (type
== JOB_TRY_RESTART
)
1893 type
= JOB_TRY_RELOAD
;
1896 if (type
== JOB_STOP
&&
1897 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1898 unit_active_state(u
) == UNIT_INACTIVE
)
1899 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1901 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1902 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1903 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1904 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1905 return sd_bus_error_setf(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
, "Operation refused, unit %s may be requested by dependency only (it is configured to refuse manual start/stop).", u
->id
);
1907 r
= sd_bus_message_new_method_return(message
, &reply
);
1911 r
= bus_unit_queue_job_one(message
, u
, type
, mode
, flags
, reply
, error
);
1915 return sd_bus_send(NULL
, reply
, NULL
);
1918 static int bus_unit_set_live_property(
1921 sd_bus_message
*message
,
1922 UnitWriteFlags flags
,
1923 sd_bus_error
*error
) {
1931 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for
1932 * transient units that are being created). */
1934 if (streq(name
, "Description")) {
1937 r
= sd_bus_message_read(message
, "s", &d
);
1941 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1942 r
= unit_set_description(u
, d
);
1946 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1952 /* A setting that only applies to active units. We don't actually write this to /run, this state is
1953 * managed internally. "+foo" sets flag foo, "-foo" unsets flag foo, just "foo" resets flags to
1954 * foo. The last type cannot be mixed with "+" or "-". */
1956 if (streq(name
, "Markers")) {
1957 unsigned settings
= 0, mask
= 0;
1958 bool some_plus_minus
= false, some_absolute
= false;
1960 r
= sd_bus_message_enter_container(message
, 'a', "s");
1968 r
= sd_bus_message_read(message
, "s", &word
);
1974 if (IN_SET(word
[0], '+', '-')) {
1977 some_plus_minus
= true;
1980 some_absolute
= true;
1983 UnitMarker m
= unit_marker_from_string(word
);
1985 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
,
1986 "Unknown marker \"%s\".", word
);
1988 SET_FLAG(settings
, 1u << m
, b
);
1989 SET_FLAG(mask
, 1u << m
, true);
1992 r
= sd_bus_message_exit_container(message
);
1996 if (some_plus_minus
&& some_absolute
)
1997 return sd_bus_error_set(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Bad marker syntax.");
1999 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2001 u
->markers
= settings
;
2003 u
->markers
= settings
| (u
->markers
& ~mask
);
2012 static int bus_set_transient_emergency_action(
2016 sd_bus_message
*message
,
2017 UnitWriteFlags flags
,
2018 sd_bus_error
*error
) {
2027 r
= sd_bus_message_read(message
, "s", &s
);
2031 system
= MANAGER_IS_SYSTEM(u
->manager
);
2032 r
= parse_emergency_action(s
, system
, &v
);
2034 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
2035 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
2036 : "Invalid %s setting: %s",
2039 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2041 unit_write_settingf(u
, flags
, name
,
2048 static int bus_set_transient_exit_status(
2052 sd_bus_message
*message
,
2053 UnitWriteFlags flags
,
2054 sd_bus_error
*error
) {
2061 r
= sd_bus_message_read(message
, "i", &k
);
2066 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
2068 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2069 *p
= k
< 0 ? -1 : k
;
2072 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2074 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
2080 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
2081 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
2083 static int bus_set_transient_conditions(
2088 sd_bus_message
*message
,
2089 UnitWriteFlags flags
,
2090 sd_bus_error
*error
) {
2092 const char *type_name
, *param
;
2093 int trigger
, negate
, r
;
2098 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
2102 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
2105 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
2107 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
2110 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
2112 if (condition_takes_path(t
) && !path_is_absolute(param
))
2113 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
2115 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2118 c
= condition_new(t
, param
, trigger
, negate
);
2122 LIST_PREPEND(conditions
, *list
, c
);
2124 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
2125 "%s=%s%s%s", type_name
,
2126 trigger
? "|" : "", negate
? "!" : "", param
);
2134 r
= sd_bus_message_exit_container(message
);
2138 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
2139 *list
= condition_free_list(*list
);
2140 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
2146 static int bus_unit_set_transient_property(
2149 sd_bus_message
*message
,
2150 UnitWriteFlags flags
,
2151 sd_bus_error
*error
) {
2160 /* Handles settings when transient units are created. This settings cannot be altered anymore after
2161 * the unit has been created. */
2163 if (streq(name
, "SourcePath"))
2164 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
2166 if (streq(name
, "StopWhenUnneeded"))
2167 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
2169 if (streq(name
, "RefuseManualStart"))
2170 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
2172 if (streq(name
, "RefuseManualStop"))
2173 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
2175 if (streq(name
, "AllowIsolate"))
2176 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
2178 if (streq(name
, "DefaultDependencies"))
2179 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
2181 if (streq(name
, "OnSuccessJobMode"))
2182 return bus_set_transient_job_mode(u
, name
, &u
->on_success_job_mode
, message
, flags
, error
);
2184 if (streq(name
, "OnFailureJobMode"))
2185 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
2187 if (streq(name
, "IgnoreOnIsolate"))
2188 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
2190 if (streq(name
, "JobTimeoutUSec")) {
2191 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
2192 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
2193 u
->job_running_timeout
= u
->job_timeout
;
2196 if (streq(name
, "JobRunningTimeoutUSec")) {
2197 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
2198 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
2199 u
->job_running_timeout_set
= true;
2204 if (streq(name
, "JobTimeoutAction"))
2205 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
2207 if (streq(name
, "JobTimeoutRebootArgument"))
2208 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
2210 if (streq(name
, "StartLimitIntervalUSec"))
2211 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
2213 if (streq(name
, "StartLimitBurst"))
2214 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
2216 if (streq(name
, "StartLimitAction"))
2217 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
2219 if (streq(name
, "FailureAction"))
2220 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
2222 if (streq(name
, "SuccessAction"))
2223 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
2225 if (streq(name
, "FailureActionExitStatus"))
2226 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
2228 if (streq(name
, "SuccessActionExitStatus"))
2229 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
2231 if (streq(name
, "RebootArgument"))
2232 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
2234 if (streq(name
, "CollectMode"))
2235 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
2237 if (streq(name
, "Conditions"))
2238 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
2240 if (streq(name
, "Asserts"))
2241 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
2243 if (streq(name
, "Documentation")) {
2244 _cleanup_strv_free_
char **l
= NULL
;
2246 r
= sd_bus_message_read_strv(message
, &l
);
2251 if (!documentation_url_is_valid(*p
))
2252 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
2254 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2255 if (strv_isempty(l
)) {
2256 u
->documentation
= strv_free(u
->documentation
);
2257 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2259 strv_extend_strv(&u
->documentation
, l
, false);
2262 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2268 } else if (streq(name
, "Slice")) {
2272 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
2273 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
2274 if (u
->type
== UNIT_SLICE
)
2275 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
2276 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
2277 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
2279 r
= sd_bus_message_read(message
, "s", &s
);
2283 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
2284 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
2286 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2287 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2288 * instead of manager_load_unit() on purpose, here. */
2289 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
2293 if (slice
->type
!= UNIT_SLICE
)
2294 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
2296 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2297 r
= unit_set_slice(u
, slice
);
2301 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
2306 } else if (streq(name
, "RequiresMountsFor")) {
2307 _cleanup_strv_free_
char **l
= NULL
;
2309 r
= sd_bus_message_read_strv(message
, &l
);
2313 STRV_FOREACH(p
, l
) {
2316 if (!path_is_absolute(*p
))
2317 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
2319 if (!path_is_valid(*p
))
2320 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
2322 if (!path_is_normalized(*p
))
2323 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
2325 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2326 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
2328 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
2330 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2337 if (streq(name
, "RequiresOverridable"))
2338 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
2339 else if (streq(name
, "RequisiteOverridable"))
2340 d
= UNIT_REQUISITE
; /* same here */
2342 d
= unit_dependency_from_string(name
);
2359 UNIT_PROPAGATES_RELOAD_TO
,
2360 UNIT_RELOAD_PROPAGATED_FROM
,
2361 UNIT_PROPAGATES_STOP_TO
,
2362 UNIT_STOP_PROPAGATED_FROM
,
2363 UNIT_JOINS_NAMESPACE_OF
))
2364 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d
));
2366 r
= sd_bus_message_enter_container(message
, 'a', "s");
2370 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
2371 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
2372 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
2374 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2375 _cleanup_free_
char *label
= NULL
;
2377 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
2381 label
= strjoin(name
, "-", other
);
2385 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
2392 r
= sd_bus_message_exit_container(message
);
2398 } else if (streq(name
, "AddRef")) {
2402 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2403 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2404 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2405 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2406 * dependency type, hence let's not confuse things with that.
2408 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2409 * the transient unit is complete, so that setting this property multiple times in the same transient
2410 * unit creation call doesn't count as individual references. */
2412 r
= sd_bus_message_read(message
, "b", &b
);
2416 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2417 u
->bus_track_add
= b
;
2425 int bus_unit_set_properties(
2427 sd_bus_message
*message
,
2428 UnitWriteFlags flags
,
2430 sd_bus_error
*error
) {
2432 bool for_real
= false;
2439 /* We iterate through the array twice. First run we just check
2440 * if all passed data is valid, second run actually applies
2441 * it. This is to implement transaction-like behaviour without
2442 * actually providing full transactions. */
2444 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2452 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2456 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2459 /* Reached EOF. Let's try again, and this time for realz... */
2460 r
= sd_bus_message_rewind(message
, false);
2468 r
= sd_bus_message_read(message
, "s", &name
);
2472 if (!UNIT_VTABLE(u
)->bus_set_property
)
2473 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
,
2474 "Objects of this type do not support setting properties.");
2476 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2480 /* If not for real, then mask out the two target flags */
2481 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2483 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2484 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2485 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2487 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2492 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
,
2493 "Cannot set property %s, or unknown property.", name
);
2495 r
= sd_bus_message_exit_container(message
);
2499 r
= sd_bus_message_exit_container(message
);
2506 r
= sd_bus_message_exit_container(message
);
2510 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2511 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2516 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2519 /* Generates a pretty error if a unit isn't properly loaded. */
2521 switch (u
->load_state
) {
2526 case UNIT_NOT_FOUND
:
2527 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2529 case UNIT_BAD_SETTING
:
2530 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2532 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2533 return sd_bus_error_set_errnof(error
, u
->load_error
,
2534 "Unit %s failed to load properly, please adjust/correct and reload service manager: %m", u
->id
);
2537 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2542 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2546 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2552 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2554 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2556 unit_add_to_cgroup_empty_queue(u
);
2558 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2559 unit_add_to_gc_queue(u
);
2564 static int bus_unit_allocate_bus_track(Unit
*u
) {
2572 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2576 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2578 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2585 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2590 r
= bus_unit_allocate_bus_track(u
);
2594 return sd_bus_track_add_name(u
->bus_track
, name
);
2597 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2602 r
= bus_unit_allocate_bus_track(u
);
2606 return sd_bus_track_add_sender(u
->bus_track
, m
);
2609 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2612 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet,
2613 * return an error */
2617 return sd_bus_track_remove_sender(u
->bus_track
, m
);