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"
10 #include "cgroup-util.h"
11 #include "condition.h"
13 #include "dbus-unit.h"
14 #include "dbus-util.h"
18 #include "locale-util.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "selinux-access.h"
23 #include "signal-util.h"
25 #include "string-table.h"
26 #include "string-util.h"
28 #include "user-util.h"
31 static bool unit_can_start_refuse_manual(Unit
*u
) {
32 return unit_can_start(u
) && !u
->refuse_manual_start
;
35 static bool unit_can_stop_refuse_manual(Unit
*u
) {
36 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
39 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
40 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
46 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
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_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
50 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
53 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
54 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
55 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
56 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
58 static int property_get_can_clean(
61 const char *interface
,
63 sd_bus_message
*reply
,
65 sd_bus_error
*error
) {
74 r
= unit_can_clean(u
, &mask
);
78 r
= sd_bus_message_open_container(reply
, 'a', "s");
82 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
83 if (!FLAGS_SET(mask
, 1U << t
))
86 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
91 return sd_bus_message_close_container(reply
);
94 static int property_get_names(
97 const char *interface
,
99 sd_bus_message
*reply
,
101 sd_bus_error
*error
) {
112 r
= sd_bus_message_open_container(reply
, 'a', "s");
116 SET_FOREACH(t
, *s
, i
) {
117 r
= sd_bus_message_append(reply
, "s", t
);
122 return sd_bus_message_close_container(reply
);
125 static int property_get_following(
128 const char *interface
,
129 const char *property
,
130 sd_bus_message
*reply
,
132 sd_bus_error
*error
) {
134 Unit
*u
= userdata
, *f
;
140 f
= unit_following(u
);
141 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
144 static int property_get_dependencies(
147 const char *interface
,
148 const char *property
,
149 sd_bus_message
*reply
,
151 sd_bus_error
*error
) {
153 Hashmap
**h
= userdata
;
163 r
= sd_bus_message_open_container(reply
, 'a', "s");
167 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
168 r
= sd_bus_message_append(reply
, "s", u
->id
);
173 return sd_bus_message_close_container(reply
);
176 static int property_get_requires_mounts_for(
179 const char *interface
,
180 const char *property
,
181 sd_bus_message
*reply
,
183 sd_bus_error
*error
) {
185 Hashmap
**h
= userdata
;
195 r
= sd_bus_message_open_container(reply
, 'a', "s");
199 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
200 r
= sd_bus_message_append(reply
, "s", p
);
205 return sd_bus_message_close_container(reply
);
208 static int property_get_unit_file_preset(
211 const char *interface
,
212 const char *property
,
213 sd_bus_message
*reply
,
215 sd_bus_error
*error
) {
224 r
= unit_get_unit_file_preset(u
);
226 return sd_bus_message_append(reply
, "s",
228 r
> 0 ? "enabled" : "disabled");
231 static int property_get_job(
234 const char *interface
,
235 const char *property
,
236 sd_bus_message
*reply
,
238 sd_bus_error
*error
) {
240 _cleanup_free_
char *p
= NULL
;
248 return sd_bus_message_append(reply
, "(uo)", 0, "/");
250 p
= job_dbus_path(*j
);
254 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
257 static int property_get_conditions(
260 const char *interface
,
261 const char *property
,
262 sd_bus_message
*reply
,
264 sd_bus_error
*error
) {
266 const char *(*to_string
)(ConditionType type
) = NULL
;
267 Condition
**list
= userdata
, *c
;
274 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
276 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
280 LIST_FOREACH(conditions
, c
, *list
) {
284 c
->result
== CONDITION_UNTESTED
? 0 :
285 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
287 r
= sd_bus_message_append(reply
, "(sbbsi)",
289 c
->trigger
, c
->negate
,
290 c
->parameter
, tristate
);
296 return sd_bus_message_close_container(reply
);
299 static int property_get_load_error(
302 const char *interface
,
303 const char *property
,
304 sd_bus_message
*reply
,
306 sd_bus_error
*error
) {
308 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
316 r
= bus_unit_validate_load_state(u
, &e
);
318 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
320 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
323 static int bus_verify_manage_units_async_full(
327 const char *polkit_message
,
329 sd_bus_message
*call
,
330 sd_bus_error
*error
) {
332 const char *details
[9] = {
337 if (polkit_message
) {
338 details
[4] = "polkit.message";
339 details
[5] = polkit_message
;
340 details
[6] = "polkit.gettext_domain";
341 details
[7] = GETTEXT_PACKAGE
;
344 return bus_verify_polkit_async(
347 "org.freedesktop.systemd1.manage-units",
351 &u
->manager
->polkit_registry
,
355 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
356 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
357 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
358 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
359 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
360 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
363 int bus_unit_method_start_generic(
364 sd_bus_message
*message
,
367 bool reload_if_possible
,
368 sd_bus_error
*error
) {
370 const char *smode
, *verb
;
376 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
378 r
= mac_selinux_unit_access_check(
380 job_type_to_access_method(job_type
),
385 r
= sd_bus_message_read(message
, "s", &smode
);
389 mode
= job_mode_from_string(smode
);
391 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
393 if (reload_if_possible
)
394 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
396 verb
= job_type_to_string(job_type
);
398 r
= bus_verify_manage_units_async_full(
402 polkit_message_for_job
[job_type
],
409 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
411 return bus_unit_queue_job(message
, u
, job_type
, mode
,
412 reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0, error
);
415 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
416 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
419 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
420 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
423 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
424 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
427 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
428 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
431 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
432 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
435 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
436 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
439 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
440 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
443 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
444 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
445 const char *jtype
, *smode
;
454 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
458 /* Parse the two magic reload types "reload-or-…" manually */
459 if (streq(jtype
, "reload-or-restart")) {
461 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
462 } else if (streq(jtype
, "reload-or-try-restart")) {
463 type
= JOB_TRY_RESTART
;
464 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
466 /* And the rest generically */
467 type
= job_type_from_string(jtype
);
469 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
472 mode
= job_mode_from_string(smode
);
474 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
476 r
= mac_selinux_unit_access_check(
478 job_type_to_access_method(type
),
483 r
= bus_verify_manage_units_async_full(
487 polkit_message_for_job
[type
],
494 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
496 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
499 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
509 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
513 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
520 who
= kill_who_from_string(swho
);
522 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
525 if (!SIGNAL_VALID(signo
))
526 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
528 r
= bus_verify_manage_units_async_full(
532 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
539 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
541 r
= unit_kill(u
, who
, signo
, error
);
545 return sd_bus_reply_method_return(message
, NULL
);
548 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
555 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
559 r
= bus_verify_manage_units_async_full(
563 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
570 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
572 unit_reset_failed(u
);
574 return sd_bus_reply_method_return(message
, NULL
);
577 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
584 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
588 r
= sd_bus_message_read(message
, "b", &runtime
);
592 r
= bus_verify_manage_units_async_full(
596 N_("Authentication is required to set properties on '$(unit)'."),
603 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
605 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
609 return sd_bus_reply_method_return(message
, NULL
);
612 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
619 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
623 r
= bus_verify_manage_units_async_full(
634 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
636 r
= bus_unit_track_add_sender(u
, message
);
640 return sd_bus_reply_method_return(message
, NULL
);
643 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
650 r
= bus_unit_track_remove_sender(u
, message
);
652 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
656 return sd_bus_reply_method_return(message
, NULL
);
659 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
660 ExecCleanMask mask
= 0;
667 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
671 r
= sd_bus_message_enter_container(message
, 'a', "s");
678 r
= sd_bus_message_read(message
, "s", &i
);
685 mask
|= EXEC_CLEAN_ALL
;
689 t
= exec_resource_type_from_string(i
);
691 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
697 r
= sd_bus_message_exit_container(message
);
701 r
= bus_verify_manage_units_async_full(
705 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
712 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
714 r
= unit_clean(u
, mask
);
715 if (r
== -EOPNOTSUPP
)
716 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not supporting cleaning.", u
->id
);
718 return sd_bus_error_setf(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
720 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
724 return sd_bus_reply_method_return(message
, NULL
);
727 static int property_get_refs(
730 const char *interface
,
731 const char *property
,
732 sd_bus_message
*reply
,
734 sd_bus_error
*error
) {
743 r
= sd_bus_message_open_container(reply
, 'a', "s");
747 for (i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
750 c
= sd_bus_track_count_name(u
->bus_track
, i
);
754 /* Add the item multiple times if the ref count for each is above 1 */
755 for (k
= 0; k
< c
; k
++) {
756 r
= sd_bus_message_append(reply
, "s", i
);
762 return sd_bus_message_close_container(reply
);
765 const sd_bus_vtable bus_unit_vtable
[] = {
766 SD_BUS_VTABLE_START(0),
768 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
769 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
770 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
771 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
772 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
773 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
774 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
775 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
776 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
777 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
778 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
779 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
780 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
781 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
782 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
783 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
784 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
785 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
786 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
787 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
788 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
789 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
790 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
791 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
792 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
793 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
794 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
795 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
796 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
797 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
798 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
799 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
800 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
801 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
802 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
803 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
804 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
805 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
806 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
807 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
808 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
809 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
810 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
811 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
812 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
813 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
814 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
815 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
816 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
817 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
818 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
819 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
820 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
821 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
822 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
823 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
824 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
825 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
826 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
827 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
828 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
829 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
830 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
831 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
832 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
833 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
834 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
835 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
836 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
837 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
838 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
839 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
840 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
841 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
842 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
843 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
844 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
846 SD_BUS_METHOD_WITH_NAMES("Start",
852 SD_BUS_VTABLE_UNPRIVILEGED
),
853 SD_BUS_METHOD_WITH_NAMES("Stop",
859 SD_BUS_VTABLE_UNPRIVILEGED
),
860 SD_BUS_METHOD_WITH_NAMES("Reload",
866 SD_BUS_VTABLE_UNPRIVILEGED
),
867 SD_BUS_METHOD_WITH_NAMES("Restart",
873 SD_BUS_VTABLE_UNPRIVILEGED
),
874 SD_BUS_METHOD_WITH_NAMES("TryRestart",
880 SD_BUS_VTABLE_UNPRIVILEGED
),
881 SD_BUS_METHOD_WITH_NAMES("ReloadOrRestart",
886 method_reload_or_restart
,
887 SD_BUS_VTABLE_UNPRIVILEGED
),
888 SD_BUS_METHOD_WITH_NAMES("ReloadOrTryRestart",
893 method_reload_or_try_restart
,
894 SD_BUS_VTABLE_UNPRIVILEGED
),
895 SD_BUS_METHOD_WITH_NAMES("EnqueueJob",
897 SD_BUS_PARAM(job_type
)
898 SD_BUS_PARAM(job_mode
),
901 SD_BUS_PARAM(job_path
)
902 SD_BUS_PARAM(unit_id
)
903 SD_BUS_PARAM(unit_path
)
904 SD_BUS_PARAM(job_type
)
905 SD_BUS_PARAM(affected_jobs
),
906 bus_unit_method_enqueue_job
,
907 SD_BUS_VTABLE_UNPRIVILEGED
),
908 SD_BUS_METHOD_WITH_NAMES("Kill",
911 SD_BUS_PARAM(signal
),
913 bus_unit_method_kill
,
914 SD_BUS_VTABLE_UNPRIVILEGED
),
915 SD_BUS_METHOD("ResetFailed",
918 bus_unit_method_reset_failed
,
919 SD_BUS_VTABLE_UNPRIVILEGED
),
920 SD_BUS_METHOD_WITH_NAMES("SetProperties",
922 SD_BUS_PARAM(runtime
)
923 SD_BUS_PARAM(properties
),
925 bus_unit_method_set_properties
,
926 SD_BUS_VTABLE_UNPRIVILEGED
),
931 SD_BUS_VTABLE_UNPRIVILEGED
),
932 SD_BUS_METHOD("Unref",
935 bus_unit_method_unref
,
936 SD_BUS_VTABLE_UNPRIVILEGED
),
937 SD_BUS_METHOD_WITH_NAMES("Clean",
941 bus_unit_method_clean
,
942 SD_BUS_VTABLE_UNPRIVILEGED
),
944 /* For dependency types we don't support anymore always return an empty array */
945 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
946 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
947 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
948 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
949 /* Obsolete alias names */
950 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
951 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
956 static int property_get_slice(
959 const char *interface
,
960 const char *property
,
961 sd_bus_message
*reply
,
963 sd_bus_error
*error
) {
971 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
974 static int property_get_current_memory(
977 const char *interface
,
978 const char *property
,
979 sd_bus_message
*reply
,
981 sd_bus_error
*error
) {
983 uint64_t sz
= (uint64_t) -1;
991 r
= unit_get_memory_current(u
, &sz
);
992 if (r
< 0 && r
!= -ENODATA
)
993 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
995 return sd_bus_message_append(reply
, "t", sz
);
998 static int property_get_current_tasks(
1001 const char *interface
,
1002 const char *property
,
1003 sd_bus_message
*reply
,
1005 sd_bus_error
*error
) {
1007 uint64_t cn
= (uint64_t) -1;
1015 r
= unit_get_tasks_current(u
, &cn
);
1016 if (r
< 0 && r
!= -ENODATA
)
1017 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
1019 return sd_bus_message_append(reply
, "t", cn
);
1022 static int property_get_cpu_usage(
1025 const char *interface
,
1026 const char *property
,
1027 sd_bus_message
*reply
,
1029 sd_bus_error
*error
) {
1031 nsec_t ns
= (nsec_t
) -1;
1039 r
= unit_get_cpu_usage(u
, &ns
);
1040 if (r
< 0 && r
!= -ENODATA
)
1041 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
1043 return sd_bus_message_append(reply
, "t", ns
);
1046 static int property_get_cpuset_cpus(
1049 const char *interface
,
1050 const char *property
,
1051 sd_bus_message
*reply
,
1053 sd_bus_error
*error
) {
1056 _cleanup_(cpu_set_reset
) CPUSet cpus
= {};
1057 _cleanup_free_
uint8_t *array
= NULL
;
1064 (void) unit_get_cpuset(u
, &cpus
, "cpuset.cpus.effective");
1065 (void) cpu_set_to_dbus(&cpus
, &array
, &allocated
);
1066 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1069 static int property_get_cpuset_mems(
1072 const char *interface
,
1073 const char *property
,
1074 sd_bus_message
*reply
,
1076 sd_bus_error
*error
) {
1079 _cleanup_(cpu_set_reset
) CPUSet mems
= {};
1080 _cleanup_free_
uint8_t *array
= NULL
;
1087 (void) unit_get_cpuset(u
, &mems
, "cpuset.mems.effective");
1088 (void) cpu_set_to_dbus(&mems
, &array
, &allocated
);
1089 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1092 static int property_get_cgroup(
1095 const char *interface
,
1096 const char *property
,
1097 sd_bus_message
*reply
,
1099 sd_bus_error
*error
) {
1102 const char *t
= NULL
;
1108 /* Three cases: a) u->cgroup_path is NULL, in which case the
1109 * unit has no control group, which we report as the empty
1110 * string. b) u->cgroup_path is the empty string, which
1111 * indicates the root cgroup, which we report as "/". c) all
1112 * other cases we report as-is. */
1115 t
= empty_to_root(u
->cgroup_path
);
1117 return sd_bus_message_append(reply
, "s", t
);
1120 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
1121 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
1127 r
= set_put(pids
, PID_TO_PTR(pid
));
1128 if (IN_SET(r
, 0, -EEXIST
))
1134 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1143 (void) get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &cmdline
);
1145 return sd_bus_message_append(reply
,
1152 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1153 _cleanup_closedir_
DIR *d
= NULL
;
1154 _cleanup_fclose_
FILE *f
= NULL
;
1160 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1169 r
= cg_read_pid(f
, &pid
);
1175 if (is_kernel_thread(pid
) > 0)
1178 r
= append_process(reply
, p
, pid
, pids
);
1183 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1190 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1192 r
= cg_read_subgroup(d
, &g
);
1198 j
= path_join(empty_to_root(p
), g
);
1202 r
= append_cgroup(reply
, j
, pids
);
1210 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1211 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1212 _cleanup_set_free_ Set
*pids
= NULL
;
1219 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1223 pids
= set_new(NULL
);
1227 r
= sd_bus_message_new_method_return(message
, &reply
);
1231 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1235 if (u
->cgroup_path
) {
1236 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1241 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1242 pid
= unit_main_pid(u
);
1244 r
= append_process(reply
, NULL
, pid
, pids
);
1249 pid
= unit_control_pid(u
);
1251 r
= append_process(reply
, NULL
, pid
, pids
);
1256 r
= sd_bus_message_close_container(reply
);
1260 return sd_bus_send(NULL
, reply
, NULL
);
1263 static int property_get_ip_counter(
1266 const char *interface
,
1267 const char *property
,
1268 sd_bus_message
*reply
,
1270 sd_bus_error
*error
) {
1272 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1273 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1274 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1275 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1276 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1279 uint64_t value
= UINT64_MAX
;
1288 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1289 (void) unit_get_ip_accounting(u
, metric
, &value
);
1290 return sd_bus_message_append(reply
, "t", value
);
1293 static int property_get_io_counter(
1296 const char *interface
,
1297 const char *property
,
1298 sd_bus_message
*reply
,
1300 sd_bus_error
*error
) {
1302 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1303 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1304 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1305 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1306 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1309 uint64_t value
= UINT64_MAX
;
1318 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1319 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1320 return sd_bus_message_append(reply
, "t", value
);
1323 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1325 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1326 _cleanup_set_free_ Set
*pids
= NULL
;
1333 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1334 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1335 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1336 * subcgroup path has no effect! */
1338 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1342 r
= sd_bus_message_read(message
, "s", &path
);
1346 path
= empty_to_null(path
);
1348 if (!path_is_absolute(path
))
1349 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1351 if (!path_is_normalized(path
))
1352 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1355 if (!unit_cgroup_delegate(u
))
1356 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1358 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1359 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1361 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1365 r
= sd_bus_message_enter_container(message
, 'a', "u");
1369 uid_t process_uid
, sender_uid
;
1373 r
= sd_bus_message_read(message
, "u", &upid
);
1380 r
= sd_bus_creds_get_pid(creds
, &pid
);
1386 /* Filter out duplicates */
1387 if (set_contains(pids
, PID_TO_PTR(pid
)))
1390 /* Check if this process is suitable for attaching to this unit */
1391 r
= unit_pid_attachable(u
, pid
, error
);
1395 /* Let's query the sender's UID, so that we can make our security decisions */
1396 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1400 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1401 * then the process' UID and the target unit's UID have to match the sender's UID */
1402 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1403 r
= get_process_uid(pid
, &process_uid
);
1405 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1407 if (process_uid
!= sender_uid
)
1408 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1409 if (process_uid
!= u
->ref_uid
)
1410 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1414 pids
= set_new(NULL
);
1419 r
= set_put(pids
, PID_TO_PTR(pid
));
1424 r
= sd_bus_message_exit_container(message
);
1428 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1430 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1432 return sd_bus_reply_method_return(message
, NULL
);
1435 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1436 SD_BUS_VTABLE_START(0),
1437 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1438 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1439 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1440 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1441 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus
, 0, 0),
1442 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems
, 0, 0),
1443 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1444 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1445 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1446 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1447 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1448 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1449 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1450 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1451 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1453 SD_BUS_METHOD_WITH_NAMES("GetProcesses",
1456 SD_BUS_PARAM(processes
),
1457 bus_unit_method_get_processes
,
1458 SD_BUS_VTABLE_UNPRIVILEGED
),
1460 SD_BUS_METHOD_WITH_NAMES("AttachProcesses",
1462 SD_BUS_PARAM(subcgroup
)
1465 bus_unit_method_attach_processes
,
1466 SD_BUS_VTABLE_UNPRIVILEGED
),
1471 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1472 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1473 _cleanup_free_
char *p
= NULL
;
1480 p
= unit_dbus_path(u
);
1484 r
= sd_bus_message_new_signal(
1487 "/org/freedesktop/systemd1",
1488 "org.freedesktop.systemd1.Manager",
1493 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1497 return sd_bus_send(bus
, m
, NULL
);
1500 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1501 _cleanup_free_
char *p
= NULL
;
1508 p
= unit_dbus_path(u
);
1512 /* Send a properties changed signal. First for the specific
1513 * type, then for the generic unit. The clients may rely on
1514 * this order to get atomic behavior if needed. */
1516 r
= sd_bus_emit_properties_changed_strv(
1518 unit_dbus_interface_from_type(u
->type
),
1523 return sd_bus_emit_properties_changed_strv(
1525 "org.freedesktop.systemd1.Unit",
1529 void bus_unit_send_change_signal(Unit
*u
) {
1533 if (u
->in_dbus_queue
) {
1534 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1535 u
->in_dbus_queue
= false;
1541 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1543 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1545 u
->sent_dbus_new_signal
= true;
1548 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1550 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1551 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1552 * so that clients can follow the full state transition */
1554 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1557 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1558 * the unit appears in the new state right-away (except if the
1559 * caller explicitly asked us to send it anyway) */
1562 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1563 * when we are reloading. */
1566 bus_unit_send_change_signal(u
);
1569 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1570 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1571 _cleanup_free_
char *p
= NULL
;
1578 p
= unit_dbus_path(u
);
1582 r
= sd_bus_message_new_signal(
1585 "/org/freedesktop/systemd1",
1586 "org.freedesktop.systemd1.Manager",
1591 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1595 return sd_bus_send(bus
, m
, NULL
);
1598 void bus_unit_send_removed_signal(Unit
*u
) {
1602 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1603 bus_unit_send_change_signal(u
);
1608 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1610 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1613 int bus_unit_queue_job(
1614 sd_bus_message
*message
,
1618 BusUnitQueueFlags flags
,
1619 sd_bus_error
*error
) {
1621 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1622 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1623 _cleanup_(set_freep
) Set
*affected
= NULL
;
1630 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1631 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1633 r
= mac_selinux_unit_access_check(
1635 job_type_to_access_method(type
),
1640 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1641 if (type
== JOB_RESTART
)
1642 type
= JOB_RELOAD_OR_START
;
1643 else if (type
== JOB_TRY_RESTART
)
1644 type
= JOB_TRY_RELOAD
;
1647 if (type
== JOB_STOP
&&
1648 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1649 unit_active_state(u
) == UNIT_INACTIVE
)
1650 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1652 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1653 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1654 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1655 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1656 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
);
1658 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1659 affected
= set_new(NULL
);
1664 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1668 r
= bus_job_track_sender(j
, message
);
1672 /* Before we send the method reply, force out the announcement JobNew for this job */
1673 bus_job_send_pending_change_signal(j
, true);
1675 job_path
= job_dbus_path(j
);
1679 /* The classic response is just a job object path */
1680 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1681 return sd_bus_reply_method_return(message
, "o", job_path
);
1683 /* In verbose mode respond with the anchor job plus everything that has been affected */
1684 r
= sd_bus_message_new_method_return(message
, &reply
);
1688 unit_path
= unit_dbus_path(j
->unit
);
1692 r
= sd_bus_message_append(reply
, "uosos",
1694 j
->unit
->id
, unit_path
,
1695 job_type_to_string(j
->type
));
1699 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1703 SET_FOREACH(a
, affected
, i
) {
1708 /* Free paths from previous iteration */
1709 job_path
= mfree(job_path
);
1710 unit_path
= mfree(unit_path
);
1712 job_path
= job_dbus_path(a
);
1716 unit_path
= unit_dbus_path(a
->unit
);
1720 r
= sd_bus_message_append(reply
, "(uosos)",
1722 a
->unit
->id
, unit_path
,
1723 job_type_to_string(a
->type
));
1728 r
= sd_bus_message_close_container(reply
);
1732 return sd_bus_send(NULL
, reply
, NULL
);
1735 static int bus_unit_set_live_property(
1738 sd_bus_message
*message
,
1739 UnitWriteFlags flags
,
1740 sd_bus_error
*error
) {
1748 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1749 * units that are being created). */
1751 if (streq(name
, "Description")) {
1754 r
= sd_bus_message_read(message
, "s", &d
);
1758 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1759 r
= unit_set_description(u
, d
);
1763 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1772 static int bus_set_transient_emergency_action(
1776 sd_bus_message
*message
,
1777 UnitWriteFlags flags
,
1778 sd_bus_error
*error
) {
1787 r
= sd_bus_message_read(message
, "s", &s
);
1791 system
= MANAGER_IS_SYSTEM(u
->manager
);
1792 r
= parse_emergency_action(s
, system
, &v
);
1794 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1795 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
1796 : "Invalid %s setting: %s",
1799 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1801 unit_write_settingf(u
, flags
, name
,
1808 static int bus_set_transient_exit_status(
1812 sd_bus_message
*message
,
1813 UnitWriteFlags flags
,
1814 sd_bus_error
*error
) {
1821 r
= sd_bus_message_read(message
, "i", &k
);
1826 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
1828 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1829 *p
= k
< 0 ? -1 : k
;
1832 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1834 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
1840 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1841 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1843 static int bus_set_transient_conditions(
1848 sd_bus_message
*message
,
1849 UnitWriteFlags flags
,
1850 sd_bus_error
*error
) {
1852 const char *type_name
, *param
;
1853 int trigger
, negate
, r
;
1858 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1862 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1865 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1867 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1869 if (t
!= CONDITION_NULL
) {
1871 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1873 if (condition_takes_path(t
) && !path_is_absolute(param
))
1874 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1878 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1881 c
= condition_new(t
, param
, trigger
, negate
);
1885 LIST_PREPEND(conditions
, *list
, c
);
1887 if (t
!= CONDITION_NULL
)
1888 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1889 "%s=%s%s%s", type_name
,
1890 trigger
? "|" : "", negate
? "!" : "", param
);
1892 unit_write_settingf(u
, flags
, name
,
1893 "%s=%s%s", type_name
,
1894 trigger
? "|" : "", yes_no(!negate
));
1902 r
= sd_bus_message_exit_container(message
);
1906 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1907 *list
= condition_free_list(*list
);
1908 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1914 static int bus_unit_set_transient_property(
1917 sd_bus_message
*message
,
1918 UnitWriteFlags flags
,
1919 sd_bus_error
*error
) {
1921 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1928 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1929 * has been created. */
1931 if (streq(name
, "SourcePath"))
1932 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1934 if (streq(name
, "StopWhenUnneeded"))
1935 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1937 if (streq(name
, "RefuseManualStart"))
1938 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1940 if (streq(name
, "RefuseManualStop"))
1941 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1943 if (streq(name
, "AllowIsolate"))
1944 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1946 if (streq(name
, "DefaultDependencies"))
1947 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1949 if (streq(name
, "OnFailureJobMode"))
1950 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1952 if (streq(name
, "IgnoreOnIsolate"))
1953 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1955 if (streq(name
, "JobTimeoutUSec")) {
1956 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1957 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1958 u
->job_running_timeout
= u
->job_timeout
;
1961 if (streq(name
, "JobRunningTimeoutUSec")) {
1962 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1963 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1964 u
->job_running_timeout_set
= true;
1969 if (streq(name
, "JobTimeoutAction"))
1970 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1972 if (streq(name
, "JobTimeoutRebootArgument"))
1973 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1975 if (streq(name
, "StartLimitIntervalUSec"))
1976 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
1978 if (streq(name
, "StartLimitBurst"))
1979 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
1981 if (streq(name
, "StartLimitAction"))
1982 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1984 if (streq(name
, "FailureAction"))
1985 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1987 if (streq(name
, "SuccessAction"))
1988 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1990 if (streq(name
, "FailureActionExitStatus"))
1991 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
1993 if (streq(name
, "SuccessActionExitStatus"))
1994 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
1996 if (streq(name
, "RebootArgument"))
1997 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1999 if (streq(name
, "CollectMode"))
2000 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
2002 if (streq(name
, "Conditions"))
2003 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
2005 if (streq(name
, "Asserts"))
2006 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
2008 if (streq(name
, "Documentation")) {
2009 _cleanup_strv_free_
char **l
= NULL
;
2012 r
= sd_bus_message_read_strv(message
, &l
);
2016 STRV_FOREACH(p
, l
) {
2017 if (!documentation_url_is_valid(*p
))
2018 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
2021 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2022 if (strv_isempty(l
)) {
2023 u
->documentation
= strv_free(u
->documentation
);
2024 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2026 strv_extend_strv(&u
->documentation
, l
, false);
2029 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2035 } else if (streq(name
, "Slice")) {
2039 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
2040 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
2041 if (u
->type
== UNIT_SLICE
)
2042 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
2043 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
2044 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
2046 r
= sd_bus_message_read(message
, "s", &s
);
2050 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
2051 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
2053 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2054 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2055 * instead of manager_load_unit() on purpose, here. */
2056 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
2060 if (slice
->type
!= UNIT_SLICE
)
2061 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
2063 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2064 r
= unit_set_slice(u
, slice
);
2068 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
2073 } else if (streq(name
, "RequiresMountsFor")) {
2074 _cleanup_strv_free_
char **l
= NULL
;
2077 r
= sd_bus_message_read_strv(message
, &l
);
2081 STRV_FOREACH(p
, l
) {
2082 path_simplify(*p
, true);
2084 if (!path_is_absolute(*p
))
2085 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
2087 if (!path_is_valid(*p
))
2088 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
2090 if (!path_is_normalized(*p
))
2091 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
2093 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2094 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
2096 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
2098 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2105 if (streq(name
, "RequiresOverridable"))
2106 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
2107 else if (streq(name
, "RequisiteOverridable"))
2108 d
= UNIT_REQUISITE
; /* same here */
2110 d
= unit_dependency_from_string(name
);
2125 UNIT_PROPAGATES_RELOAD_TO
,
2126 UNIT_RELOAD_PROPAGATED_FROM
,
2127 UNIT_JOINS_NAMESPACE_OF
))
2128 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d
));
2130 r
= sd_bus_message_enter_container(message
, 'a', "s");
2134 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
2135 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
2136 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
2138 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2139 _cleanup_free_
char *label
= NULL
;
2141 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
2145 label
= strjoin(name
, "-", other
);
2149 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
2156 r
= sd_bus_message_exit_container(message
);
2162 } else if (streq(name
, "AddRef")) {
2166 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2167 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2168 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2169 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2170 * dependency type, hence let's not confuse things with that.
2172 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2173 * the transient unit is complete, so that setting this property multiple times in the same transient
2174 * unit creation call doesn't count as individual references. */
2176 r
= sd_bus_message_read(message
, "b", &b
);
2180 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2181 u
->bus_track_add
= b
;
2189 int bus_unit_set_properties(
2191 sd_bus_message
*message
,
2192 UnitWriteFlags flags
,
2194 sd_bus_error
*error
) {
2196 bool for_real
= false;
2203 /* We iterate through the array twice. First run we just check
2204 * if all passed data is valid, second run actually applies
2205 * it. This is to implement transaction-like behaviour without
2206 * actually providing full transactions. */
2208 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2216 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2220 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2223 /* Reached EOF. Let's try again, and this time for realz... */
2224 r
= sd_bus_message_rewind(message
, false);
2232 r
= sd_bus_message_read(message
, "s", &name
);
2236 if (!UNIT_VTABLE(u
)->bus_set_property
)
2237 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
2239 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2243 /* If not for real, then mask out the two target flags */
2244 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2246 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2247 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2248 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2250 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2255 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
2257 r
= sd_bus_message_exit_container(message
);
2261 r
= sd_bus_message_exit_container(message
);
2268 r
= sd_bus_message_exit_container(message
);
2272 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2273 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2278 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2281 /* Generates a pretty error if a unit isn't properly loaded. */
2283 switch (u
->load_state
) {
2288 case UNIT_NOT_FOUND
:
2289 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2291 case UNIT_BAD_SETTING
:
2292 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2294 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2295 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to load properly: %m.", u
->id
);
2298 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2303 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2307 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2313 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2315 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2317 unit_add_to_cgroup_empty_queue(u
);
2319 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2320 unit_add_to_gc_queue(u
);
2325 static int bus_unit_allocate_bus_track(Unit
*u
) {
2333 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2337 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2339 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2346 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2351 r
= bus_unit_allocate_bus_track(u
);
2355 return sd_bus_track_add_name(u
->bus_track
, name
);
2358 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2363 r
= bus_unit_allocate_bus_track(u
);
2367 return sd_bus_track_add_sender(u
->bus_track
, m
);
2370 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2373 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
2378 return sd_bus_track_remove_sender(u
->bus_track
, m
);