1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bpf-firewall.h"
7 #include "bus-common-errors.h"
8 #include "bus-polkit.h"
9 #include "cgroup-util.h"
10 #include "condition.h"
12 #include "dbus-unit.h"
13 #include "dbus-util.h"
17 #include "locale-util.h"
19 #include "path-util.h"
20 #include "process-util.h"
21 #include "selinux-access.h"
22 #include "signal-util.h"
24 #include "string-table.h"
25 #include "string-util.h"
27 #include "user-util.h"
30 static bool unit_can_start_refuse_manual(Unit
*u
) {
31 return unit_can_start(u
) && !u
->refuse_manual_start
;
34 static bool unit_can_stop_refuse_manual(Unit
*u
) {
35 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
38 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
39 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
42 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
46 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
47 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
48 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
49 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
53 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
54 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
55 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
57 static int property_get_can_clean(
60 const char *interface
,
62 sd_bus_message
*reply
,
64 sd_bus_error
*error
) {
73 r
= unit_can_clean(u
, &mask
);
77 r
= sd_bus_message_open_container(reply
, 'a', "s");
81 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
82 if (!FLAGS_SET(mask
, 1U << t
))
85 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
90 return sd_bus_message_close_container(reply
);
93 static int property_get_names(
96 const char *interface
,
98 sd_bus_message
*reply
,
100 sd_bus_error
*error
) {
111 r
= sd_bus_message_open_container(reply
, 'a', "s");
115 SET_FOREACH(t
, *s
, i
) {
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 Hashmap
**h
= userdata
;
162 r
= sd_bus_message_open_container(reply
, 'a', "s");
166 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
167 r
= sd_bus_message_append(reply
, "s", u
->id
);
172 return sd_bus_message_close_container(reply
);
175 static int property_get_requires_mounts_for(
178 const char *interface
,
179 const char *property
,
180 sd_bus_message
*reply
,
182 sd_bus_error
*error
) {
184 Hashmap
**h
= userdata
;
194 r
= sd_bus_message_open_container(reply
, 'a', "s");
198 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
199 r
= sd_bus_message_append(reply
, "s", p
);
204 return sd_bus_message_close_container(reply
);
207 static int property_get_unit_file_preset(
210 const char *interface
,
211 const char *property
,
212 sd_bus_message
*reply
,
214 sd_bus_error
*error
) {
223 r
= unit_get_unit_file_preset(u
);
225 return sd_bus_message_append(reply
, "s",
227 r
> 0 ? "enabled" : "disabled");
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
;
247 return sd_bus_message_append(reply
, "(uo)", 0, "/");
249 p
= job_dbus_path(*j
);
253 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
256 static int property_get_conditions(
259 const char *interface
,
260 const char *property
,
261 sd_bus_message
*reply
,
263 sd_bus_error
*error
) {
265 const char *(*to_string
)(ConditionType type
) = NULL
;
266 Condition
**list
= userdata
, *c
;
273 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
275 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
279 LIST_FOREACH(conditions
, c
, *list
) {
283 c
->result
== CONDITION_UNTESTED
? 0 :
284 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
286 r
= sd_bus_message_append(reply
, "(sbbsi)",
288 c
->trigger
, c
->negate
,
289 c
->parameter
, tristate
);
295 return sd_bus_message_close_container(reply
);
298 static int property_get_load_error(
301 const char *interface
,
302 const char *property
,
303 sd_bus_message
*reply
,
305 sd_bus_error
*error
) {
307 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
315 r
= bus_unit_validate_load_state(u
, &e
);
317 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
319 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
322 static int bus_verify_manage_units_async_full(
326 const char *polkit_message
,
328 sd_bus_message
*call
,
329 sd_bus_error
*error
) {
331 const char *details
[9] = {
336 if (polkit_message
) {
337 details
[4] = "polkit.message";
338 details
[5] = polkit_message
;
339 details
[6] = "polkit.gettext_domain";
340 details
[7] = GETTEXT_PACKAGE
;
343 return bus_verify_polkit_async(
346 "org.freedesktop.systemd1.manage-units",
350 &u
->manager
->polkit_registry
,
354 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
355 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
356 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
357 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
358 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
359 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
362 int bus_unit_method_start_generic(
363 sd_bus_message
*message
,
366 bool reload_if_possible
,
367 sd_bus_error
*error
) {
369 const char *smode
, *verb
;
375 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
377 r
= mac_selinux_unit_access_check(
379 job_type_to_access_method(job_type
),
384 r
= sd_bus_message_read(message
, "s", &smode
);
388 mode
= job_mode_from_string(smode
);
390 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
392 if (reload_if_possible
)
393 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
395 verb
= job_type_to_string(job_type
);
397 r
= bus_verify_manage_units_async_full(
401 polkit_message_for_job
[job_type
],
408 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
410 return bus_unit_queue_job(message
, u
, job_type
, mode
,
411 reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0, error
);
414 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
415 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
418 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
419 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
422 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
423 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
426 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
427 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
430 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
431 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
434 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
435 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
438 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
439 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
442 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
443 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
444 const char *jtype
, *smode
;
453 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
457 /* Parse the two magic reload types "reload-or-…" manually */
458 if (streq(jtype
, "reload-or-restart")) {
460 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
461 } else if (streq(jtype
, "reload-or-try-restart")) {
462 type
= JOB_TRY_RESTART
;
463 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
465 /* And the rest generically */
466 type
= job_type_from_string(jtype
);
468 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
471 mode
= job_mode_from_string(smode
);
473 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
475 r
= mac_selinux_unit_access_check(
477 job_type_to_access_method(type
),
482 r
= bus_verify_manage_units_async_full(
486 polkit_message_for_job
[type
],
493 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
495 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
498 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
508 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
512 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
519 who
= kill_who_from_string(swho
);
521 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
524 if (!SIGNAL_VALID(signo
))
525 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
527 r
= bus_verify_manage_units_async_full(
531 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
538 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
540 r
= unit_kill(u
, who
, signo
, error
);
544 return sd_bus_reply_method_return(message
, NULL
);
547 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
554 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
558 r
= bus_verify_manage_units_async_full(
562 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
569 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
571 unit_reset_failed(u
);
573 return sd_bus_reply_method_return(message
, NULL
);
576 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
583 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
587 r
= sd_bus_message_read(message
, "b", &runtime
);
591 r
= bus_verify_manage_units_async_full(
595 N_("Authentication is required to set properties on '$(unit)'."),
602 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
604 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
608 return sd_bus_reply_method_return(message
, NULL
);
611 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
618 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
622 r
= bus_verify_manage_units_async_full(
633 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
635 r
= bus_unit_track_add_sender(u
, message
);
639 return sd_bus_reply_method_return(message
, NULL
);
642 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
649 r
= bus_unit_track_remove_sender(u
, message
);
651 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
655 return sd_bus_reply_method_return(message
, NULL
);
658 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
659 ExecCleanMask mask
= 0;
666 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
670 r
= sd_bus_message_enter_container(message
, 'a', "s");
677 r
= sd_bus_message_read(message
, "s", &i
);
684 mask
|= EXEC_CLEAN_ALL
;
688 t
= exec_resource_type_from_string(i
);
690 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(
704 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
711 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
713 r
= unit_clean(u
, mask
);
714 if (r
== -EOPNOTSUPP
)
715 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not supporting cleaning.", u
->id
);
717 return sd_bus_error_setf(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
719 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
723 return sd_bus_reply_method_return(message
, NULL
);
726 static int property_get_refs(
729 const char *interface
,
730 const char *property
,
731 sd_bus_message
*reply
,
733 sd_bus_error
*error
) {
742 r
= sd_bus_message_open_container(reply
, 'a', "s");
746 for (i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
749 c
= sd_bus_track_count_name(u
->bus_track
, i
);
753 /* Add the item multiple times if the ref count for each is above 1 */
754 for (k
= 0; k
< c
; k
++) {
755 r
= sd_bus_message_append(reply
, "s", i
);
761 return sd_bus_message_close_container(reply
);
764 const sd_bus_vtable bus_unit_vtable
[] = {
765 SD_BUS_VTABLE_START(0),
767 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
768 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
769 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
770 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
771 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
772 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
773 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
774 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
775 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
776 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
777 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
778 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
779 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
780 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
781 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
782 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
783 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
784 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
785 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
786 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
787 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
788 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
789 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
790 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
791 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
792 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
793 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
794 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
795 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
796 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
797 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
798 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
799 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
800 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
801 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
802 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
803 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
804 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
805 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
806 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
807 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
808 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
809 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
810 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
811 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
812 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
813 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
814 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
815 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
816 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
817 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
818 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
819 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
820 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
821 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
822 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
823 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
824 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
825 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
826 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
827 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
828 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
829 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
830 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
831 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
832 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
833 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
834 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
835 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
836 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
837 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
838 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
839 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
840 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
841 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
842 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
843 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
845 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
846 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
847 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
848 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
849 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
850 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
851 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
852 SD_BUS_METHOD("EnqueueJob", "ss", "uososa(uosos)", bus_unit_method_enqueue_job
, SD_BUS_VTABLE_UNPRIVILEGED
),
853 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
854 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
855 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
856 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
857 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
858 SD_BUS_METHOD("Clean", "as", NULL
, bus_unit_method_clean
, SD_BUS_VTABLE_UNPRIVILEGED
),
860 /* For dependency types we don't support anymore always return an empty array */
861 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
862 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
863 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
864 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
865 /* Obsolete alias names */
866 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
867 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
871 static int property_get_slice(
874 const char *interface
,
875 const char *property
,
876 sd_bus_message
*reply
,
878 sd_bus_error
*error
) {
886 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
889 static int property_get_current_memory(
892 const char *interface
,
893 const char *property
,
894 sd_bus_message
*reply
,
896 sd_bus_error
*error
) {
898 uint64_t sz
= (uint64_t) -1;
906 r
= unit_get_memory_current(u
, &sz
);
907 if (r
< 0 && r
!= -ENODATA
)
908 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
910 return sd_bus_message_append(reply
, "t", sz
);
913 static int property_get_current_tasks(
916 const char *interface
,
917 const char *property
,
918 sd_bus_message
*reply
,
920 sd_bus_error
*error
) {
922 uint64_t cn
= (uint64_t) -1;
930 r
= unit_get_tasks_current(u
, &cn
);
931 if (r
< 0 && r
!= -ENODATA
)
932 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
934 return sd_bus_message_append(reply
, "t", cn
);
937 static int property_get_cpu_usage(
940 const char *interface
,
941 const char *property
,
942 sd_bus_message
*reply
,
944 sd_bus_error
*error
) {
946 nsec_t ns
= (nsec_t
) -1;
954 r
= unit_get_cpu_usage(u
, &ns
);
955 if (r
< 0 && r
!= -ENODATA
)
956 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
958 return sd_bus_message_append(reply
, "t", ns
);
961 static int property_get_cpuset_cpus(
964 const char *interface
,
965 const char *property
,
966 sd_bus_message
*reply
,
968 sd_bus_error
*error
) {
971 _cleanup_(cpu_set_reset
) CPUSet cpus
= {};
972 _cleanup_free_
uint8_t *array
= NULL
;
979 (void) unit_get_cpuset(u
, &cpus
, "cpuset.cpus.effective");
980 (void) cpu_set_to_dbus(&cpus
, &array
, &allocated
);
981 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
984 static int property_get_cpuset_mems(
987 const char *interface
,
988 const char *property
,
989 sd_bus_message
*reply
,
991 sd_bus_error
*error
) {
994 _cleanup_(cpu_set_reset
) CPUSet mems
= {};
995 _cleanup_free_
uint8_t *array
= NULL
;
1002 (void) unit_get_cpuset(u
, &mems
, "cpuset.mems.effective");
1003 (void) cpu_set_to_dbus(&mems
, &array
, &allocated
);
1004 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1007 static int property_get_cgroup(
1010 const char *interface
,
1011 const char *property
,
1012 sd_bus_message
*reply
,
1014 sd_bus_error
*error
) {
1017 const char *t
= NULL
;
1023 /* Three cases: a) u->cgroup_path is NULL, in which case the
1024 * unit has no control group, which we report as the empty
1025 * string. b) u->cgroup_path is the empty string, which
1026 * indicates the root cgroup, which we report as "/". c) all
1027 * other cases we report as-is. */
1030 t
= empty_to_root(u
->cgroup_path
);
1032 return sd_bus_message_append(reply
, "s", t
);
1035 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
1036 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
1042 r
= set_put(pids
, PID_TO_PTR(pid
));
1043 if (IN_SET(r
, 0, -EEXIST
))
1049 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1058 (void) get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &cmdline
);
1060 return sd_bus_message_append(reply
,
1067 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1068 _cleanup_closedir_
DIR *d
= NULL
;
1069 _cleanup_fclose_
FILE *f
= NULL
;
1075 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1084 r
= cg_read_pid(f
, &pid
);
1090 if (is_kernel_thread(pid
) > 0)
1093 r
= append_process(reply
, p
, pid
, pids
);
1098 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1105 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1107 r
= cg_read_subgroup(d
, &g
);
1113 j
= path_join(empty_to_root(p
), g
);
1117 r
= append_cgroup(reply
, j
, pids
);
1125 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1126 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1127 _cleanup_set_free_ Set
*pids
= NULL
;
1134 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1138 pids
= set_new(NULL
);
1142 r
= sd_bus_message_new_method_return(message
, &reply
);
1146 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1150 if (u
->cgroup_path
) {
1151 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1156 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1157 pid
= unit_main_pid(u
);
1159 r
= append_process(reply
, NULL
, pid
, pids
);
1164 pid
= unit_control_pid(u
);
1166 r
= append_process(reply
, NULL
, pid
, pids
);
1171 r
= sd_bus_message_close_container(reply
);
1175 return sd_bus_send(NULL
, reply
, NULL
);
1178 static int property_get_ip_counter(
1181 const char *interface
,
1182 const char *property
,
1183 sd_bus_message
*reply
,
1185 sd_bus_error
*error
) {
1187 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1188 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1189 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1190 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1191 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1194 uint64_t value
= UINT64_MAX
;
1203 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1204 (void) unit_get_ip_accounting(u
, metric
, &value
);
1205 return sd_bus_message_append(reply
, "t", value
);
1208 static int property_get_io_counter(
1211 const char *interface
,
1212 const char *property
,
1213 sd_bus_message
*reply
,
1215 sd_bus_error
*error
) {
1217 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1218 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1219 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1220 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1221 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1224 uint64_t value
= UINT64_MAX
;
1233 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1234 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1235 return sd_bus_message_append(reply
, "t", value
);
1238 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1240 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1241 _cleanup_set_free_ Set
*pids
= NULL
;
1248 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1249 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1250 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1251 * subcgroup path has no effect! */
1253 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1257 r
= sd_bus_message_read(message
, "s", &path
);
1261 path
= empty_to_null(path
);
1263 if (!path_is_absolute(path
))
1264 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1266 if (!path_is_normalized(path
))
1267 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1270 if (!unit_cgroup_delegate(u
))
1271 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1273 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1274 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1276 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1280 r
= sd_bus_message_enter_container(message
, 'a', "u");
1284 uid_t process_uid
, sender_uid
;
1288 r
= sd_bus_message_read(message
, "u", &upid
);
1295 r
= sd_bus_creds_get_pid(creds
, &pid
);
1301 /* Filter out duplicates */
1302 if (set_contains(pids
, PID_TO_PTR(pid
)))
1305 /* Check if this process is suitable for attaching to this unit */
1306 r
= unit_pid_attachable(u
, pid
, error
);
1310 /* Let's query the sender's UID, so that we can make our security decisions */
1311 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1315 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1316 * then the process' UID and the target unit's UID have to match the sender's UID */
1317 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1318 r
= get_process_uid(pid
, &process_uid
);
1320 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1322 if (process_uid
!= sender_uid
)
1323 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1324 if (process_uid
!= u
->ref_uid
)
1325 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1329 pids
= set_new(NULL
);
1334 r
= set_put(pids
, PID_TO_PTR(pid
));
1339 r
= sd_bus_message_exit_container(message
);
1343 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1345 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1347 return sd_bus_reply_method_return(message
, NULL
);
1350 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1351 SD_BUS_VTABLE_START(0),
1352 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1353 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1354 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1355 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1356 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus
, 0, 0),
1357 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems
, 0, 0),
1358 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1359 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1360 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1361 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1362 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1363 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1364 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1365 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1366 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1367 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1368 SD_BUS_METHOD("AttachProcesses", "sau", NULL
, bus_unit_method_attach_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1372 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1373 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1374 _cleanup_free_
char *p
= NULL
;
1381 p
= unit_dbus_path(u
);
1385 r
= sd_bus_message_new_signal(
1388 "/org/freedesktop/systemd1",
1389 "org.freedesktop.systemd1.Manager",
1394 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1398 return sd_bus_send(bus
, m
, NULL
);
1401 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1402 _cleanup_free_
char *p
= NULL
;
1409 p
= unit_dbus_path(u
);
1413 /* Send a properties changed signal. First for the specific
1414 * type, then for the generic unit. The clients may rely on
1415 * this order to get atomic behavior if needed. */
1417 r
= sd_bus_emit_properties_changed_strv(
1419 unit_dbus_interface_from_type(u
->type
),
1424 return sd_bus_emit_properties_changed_strv(
1426 "org.freedesktop.systemd1.Unit",
1430 void bus_unit_send_change_signal(Unit
*u
) {
1434 if (u
->in_dbus_queue
) {
1435 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1436 u
->in_dbus_queue
= false;
1442 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1444 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1446 u
->sent_dbus_new_signal
= true;
1449 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1451 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1452 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1453 * so that clients can follow the full state transition */
1455 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1458 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1459 * the unit appears in the new state right-away (except if the
1460 * caller explicitly asked us to send it anyway) */
1463 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1464 * when we are reloading. */
1467 bus_unit_send_change_signal(u
);
1470 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1471 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1472 _cleanup_free_
char *p
= NULL
;
1479 p
= unit_dbus_path(u
);
1483 r
= sd_bus_message_new_signal(
1486 "/org/freedesktop/systemd1",
1487 "org.freedesktop.systemd1.Manager",
1492 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1496 return sd_bus_send(bus
, m
, NULL
);
1499 void bus_unit_send_removed_signal(Unit
*u
) {
1503 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1504 bus_unit_send_change_signal(u
);
1509 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1511 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1514 int bus_unit_queue_job(
1515 sd_bus_message
*message
,
1519 BusUnitQueueFlags flags
,
1520 sd_bus_error
*error
) {
1522 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1523 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1524 _cleanup_(set_freep
) Set
*affected
= NULL
;
1531 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1532 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1534 r
= mac_selinux_unit_access_check(
1536 job_type_to_access_method(type
),
1541 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1542 if (type
== JOB_RESTART
)
1543 type
= JOB_RELOAD_OR_START
;
1544 else if (type
== JOB_TRY_RESTART
)
1545 type
= JOB_TRY_RELOAD
;
1548 if (type
== JOB_STOP
&&
1549 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1550 unit_active_state(u
) == UNIT_INACTIVE
)
1551 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1553 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1554 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1555 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1556 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1557 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
);
1559 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1560 affected
= set_new(NULL
);
1565 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1569 r
= bus_job_track_sender(j
, message
);
1573 /* Before we send the method reply, force out the announcement JobNew for this job */
1574 bus_job_send_pending_change_signal(j
, true);
1576 job_path
= job_dbus_path(j
);
1580 /* The classic response is just a job object path */
1581 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1582 return sd_bus_reply_method_return(message
, "o", job_path
);
1584 /* In verbose mode respond with the anchor job plus everything that has been affected */
1585 r
= sd_bus_message_new_method_return(message
, &reply
);
1589 unit_path
= unit_dbus_path(j
->unit
);
1593 r
= sd_bus_message_append(reply
, "uosos",
1595 j
->unit
->id
, unit_path
,
1596 job_type_to_string(j
->type
));
1600 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1604 SET_FOREACH(a
, affected
, i
) {
1609 /* Free paths from previous iteration */
1610 job_path
= mfree(job_path
);
1611 unit_path
= mfree(unit_path
);
1613 job_path
= job_dbus_path(a
);
1617 unit_path
= unit_dbus_path(a
->unit
);
1621 r
= sd_bus_message_append(reply
, "(uosos)",
1623 a
->unit
->id
, unit_path
,
1624 job_type_to_string(a
->type
));
1629 r
= sd_bus_message_close_container(reply
);
1633 return sd_bus_send(NULL
, reply
, NULL
);
1636 static int bus_unit_set_live_property(
1639 sd_bus_message
*message
,
1640 UnitWriteFlags flags
,
1641 sd_bus_error
*error
) {
1649 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1650 * units that are being created). */
1652 if (streq(name
, "Description")) {
1655 r
= sd_bus_message_read(message
, "s", &d
);
1659 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1660 r
= unit_set_description(u
, d
);
1664 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1673 static int bus_set_transient_emergency_action(
1677 sd_bus_message
*message
,
1678 UnitWriteFlags flags
,
1679 sd_bus_error
*error
) {
1688 r
= sd_bus_message_read(message
, "s", &s
);
1692 system
= MANAGER_IS_SYSTEM(u
->manager
);
1693 r
= parse_emergency_action(s
, system
, &v
);
1695 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1696 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
1697 : "Invalid %s setting: %s",
1700 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1702 unit_write_settingf(u
, flags
, name
,
1709 static int bus_set_transient_exit_status(
1713 sd_bus_message
*message
,
1714 UnitWriteFlags flags
,
1715 sd_bus_error
*error
) {
1722 r
= sd_bus_message_read(message
, "i", &k
);
1727 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
1729 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1730 *p
= k
< 0 ? -1 : k
;
1733 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1735 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
1741 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1742 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1744 static int bus_set_transient_conditions(
1749 sd_bus_message
*message
,
1750 UnitWriteFlags flags
,
1751 sd_bus_error
*error
) {
1753 const char *type_name
, *param
;
1754 int trigger
, negate
, r
;
1759 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1763 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1766 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1768 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1770 if (t
!= CONDITION_NULL
) {
1772 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1774 if (condition_takes_path(t
) && !path_is_absolute(param
))
1775 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1779 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1782 c
= condition_new(t
, param
, trigger
, negate
);
1786 LIST_PREPEND(conditions
, *list
, c
);
1788 if (t
!= CONDITION_NULL
)
1789 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1790 "%s=%s%s%s", type_name
,
1791 trigger
? "|" : "", negate
? "!" : "", param
);
1793 unit_write_settingf(u
, flags
, name
,
1794 "%s=%s%s", type_name
,
1795 trigger
? "|" : "", yes_no(!negate
));
1803 r
= sd_bus_message_exit_container(message
);
1807 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1808 *list
= condition_free_list(*list
);
1809 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1815 static int bus_unit_set_transient_property(
1818 sd_bus_message
*message
,
1819 UnitWriteFlags flags
,
1820 sd_bus_error
*error
) {
1822 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1829 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1830 * has been created. */
1832 if (streq(name
, "SourcePath"))
1833 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1835 if (streq(name
, "StopWhenUnneeded"))
1836 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1838 if (streq(name
, "RefuseManualStart"))
1839 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1841 if (streq(name
, "RefuseManualStop"))
1842 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1844 if (streq(name
, "AllowIsolate"))
1845 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1847 if (streq(name
, "DefaultDependencies"))
1848 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1850 if (streq(name
, "OnFailureJobMode"))
1851 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1853 if (streq(name
, "IgnoreOnIsolate"))
1854 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1856 if (streq(name
, "JobTimeoutUSec")) {
1857 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1858 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1859 u
->job_running_timeout
= u
->job_timeout
;
1862 if (streq(name
, "JobRunningTimeoutUSec")) {
1863 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1864 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1865 u
->job_running_timeout_set
= true;
1870 if (streq(name
, "JobTimeoutAction"))
1871 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1873 if (streq(name
, "JobTimeoutRebootArgument"))
1874 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1876 if (streq(name
, "StartLimitIntervalUSec"))
1877 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
1879 if (streq(name
, "StartLimitBurst"))
1880 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
1882 if (streq(name
, "StartLimitAction"))
1883 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1885 if (streq(name
, "FailureAction"))
1886 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1888 if (streq(name
, "SuccessAction"))
1889 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1891 if (streq(name
, "FailureActionExitStatus"))
1892 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
1894 if (streq(name
, "SuccessActionExitStatus"))
1895 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
1897 if (streq(name
, "RebootArgument"))
1898 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1900 if (streq(name
, "CollectMode"))
1901 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
1903 if (streq(name
, "Conditions"))
1904 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
1906 if (streq(name
, "Asserts"))
1907 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
1909 if (streq(name
, "Documentation")) {
1910 _cleanup_strv_free_
char **l
= NULL
;
1913 r
= sd_bus_message_read_strv(message
, &l
);
1917 STRV_FOREACH(p
, l
) {
1918 if (!documentation_url_is_valid(*p
))
1919 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
1922 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1923 if (strv_isempty(l
)) {
1924 u
->documentation
= strv_free(u
->documentation
);
1925 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1927 strv_extend_strv(&u
->documentation
, l
, false);
1930 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1936 } else if (streq(name
, "Slice")) {
1940 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1941 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1942 if (u
->type
== UNIT_SLICE
)
1943 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1944 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1945 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1947 r
= sd_bus_message_read(message
, "s", &s
);
1951 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1952 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1954 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1955 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1956 * instead of manager_load_unit() on purpose, here. */
1957 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1961 if (slice
->type
!= UNIT_SLICE
)
1962 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1964 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1965 r
= unit_set_slice(u
, slice
);
1969 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
1974 } else if (streq(name
, "RequiresMountsFor")) {
1975 _cleanup_strv_free_
char **l
= NULL
;
1978 r
= sd_bus_message_read_strv(message
, &l
);
1982 STRV_FOREACH(p
, l
) {
1983 path_simplify(*p
, true);
1985 if (!path_is_absolute(*p
))
1986 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
1988 if (!path_is_valid(*p
))
1989 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
1991 if (!path_is_normalized(*p
))
1992 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
1994 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1995 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
1997 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
1999 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2006 if (streq(name
, "RequiresOverridable"))
2007 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
2008 else if (streq(name
, "RequisiteOverridable"))
2009 d
= UNIT_REQUISITE
; /* same here */
2011 d
= unit_dependency_from_string(name
);
2026 UNIT_PROPAGATES_RELOAD_TO
,
2027 UNIT_RELOAD_PROPAGATED_FROM
,
2028 UNIT_JOINS_NAMESPACE_OF
))
2029 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d
));
2031 r
= sd_bus_message_enter_container(message
, 'a', "s");
2035 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
2036 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
2037 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
2039 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2040 _cleanup_free_
char *label
= NULL
;
2042 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
2046 label
= strjoin(name
, "-", other
);
2050 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
2057 r
= sd_bus_message_exit_container(message
);
2063 } else if (streq(name
, "AddRef")) {
2067 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2068 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2069 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2070 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2071 * dependency type, hence let's not confuse things with that.
2073 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2074 * the transient unit is complete, so that setting this property multiple times in the same transient
2075 * unit creation call doesn't count as individual references. */
2077 r
= sd_bus_message_read(message
, "b", &b
);
2081 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2082 u
->bus_track_add
= b
;
2090 int bus_unit_set_properties(
2092 sd_bus_message
*message
,
2093 UnitWriteFlags flags
,
2095 sd_bus_error
*error
) {
2097 bool for_real
= false;
2104 /* We iterate through the array twice. First run we just check
2105 * if all passed data is valid, second run actually applies
2106 * it. This is to implement transaction-like behaviour without
2107 * actually providing full transactions. */
2109 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2117 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2121 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2124 /* Reached EOF. Let's try again, and this time for realz... */
2125 r
= sd_bus_message_rewind(message
, false);
2133 r
= sd_bus_message_read(message
, "s", &name
);
2137 if (!UNIT_VTABLE(u
)->bus_set_property
)
2138 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
2140 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2144 /* If not for real, then mask out the two target flags */
2145 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2147 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2148 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2149 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2151 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2156 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
2158 r
= sd_bus_message_exit_container(message
);
2162 r
= sd_bus_message_exit_container(message
);
2169 r
= sd_bus_message_exit_container(message
);
2173 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2174 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2179 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2182 /* Generates a pretty error if a unit isn't properly loaded. */
2184 switch (u
->load_state
) {
2189 case UNIT_NOT_FOUND
:
2190 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2192 case UNIT_BAD_SETTING
:
2193 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2195 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2196 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to load properly: %m.", u
->id
);
2199 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2204 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2208 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2214 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2216 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2218 unit_add_to_cgroup_empty_queue(u
);
2220 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2221 unit_add_to_gc_queue(u
);
2226 static int bus_unit_allocate_bus_track(Unit
*u
) {
2234 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2238 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2240 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2247 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2252 r
= bus_unit_allocate_bus_track(u
);
2256 return sd_bus_track_add_name(u
->bus_track
, name
);
2259 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2264 r
= bus_unit_allocate_bus_track(u
);
2268 return sd_bus_track_add_sender(u
->bus_track
, m
);
2271 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2274 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
2279 return sd_bus_track_remove_sender(u
->bus_track
, m
);