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 "cgroup-util.h"
11 #include "dbus-unit.h"
12 #include "dbus-util.h"
15 #include "locale-util.h"
17 #include "path-util.h"
18 #include "process-util.h"
19 #include "selinux-access.h"
20 #include "signal-util.h"
22 #include "string-table.h"
23 #include "string-util.h"
25 #include "user-util.h"
28 static bool unit_can_start_refuse_manual(Unit
*u
) {
29 return unit_can_start(u
) && !u
->refuse_manual_start
;
32 static bool unit_can_stop_refuse_manual(Unit
*u
) {
33 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
36 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
37 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
40 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
41 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
42 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
44 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
45 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
46 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
47 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
48 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
49 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
53 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
55 static int property_get_can_clean(
58 const char *interface
,
60 sd_bus_message
*reply
,
62 sd_bus_error
*error
) {
71 r
= unit_can_clean(u
, &mask
);
75 r
= sd_bus_message_open_container(reply
, 'a', "s");
79 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
80 if (!FLAGS_SET(mask
, 1U << t
))
83 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
88 return sd_bus_message_close_container(reply
);
91 static int property_get_names(
94 const char *interface
,
96 sd_bus_message
*reply
,
98 sd_bus_error
*error
) {
109 r
= sd_bus_message_open_container(reply
, 'a', "s");
113 SET_FOREACH(t
, *s
, i
) {
114 r
= sd_bus_message_append(reply
, "s", t
);
119 return sd_bus_message_close_container(reply
);
122 static int property_get_following(
125 const char *interface
,
126 const char *property
,
127 sd_bus_message
*reply
,
129 sd_bus_error
*error
) {
131 Unit
*u
= userdata
, *f
;
137 f
= unit_following(u
);
138 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
141 static int property_get_dependencies(
144 const char *interface
,
145 const char *property
,
146 sd_bus_message
*reply
,
148 sd_bus_error
*error
) {
150 Hashmap
**h
= userdata
;
160 r
= sd_bus_message_open_container(reply
, 'a', "s");
164 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
165 r
= sd_bus_message_append(reply
, "s", u
->id
);
170 return sd_bus_message_close_container(reply
);
173 static int property_get_requires_mounts_for(
176 const char *interface
,
177 const char *property
,
178 sd_bus_message
*reply
,
180 sd_bus_error
*error
) {
182 Hashmap
**h
= userdata
;
192 r
= sd_bus_message_open_container(reply
, 'a', "s");
196 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
197 r
= sd_bus_message_append(reply
, "s", p
);
202 return sd_bus_message_close_container(reply
);
205 static int property_get_unit_file_preset(
208 const char *interface
,
209 const char *property
,
210 sd_bus_message
*reply
,
212 sd_bus_error
*error
) {
221 r
= unit_get_unit_file_preset(u
);
223 return sd_bus_message_append(reply
, "s",
225 r
> 0 ? "enabled" : "disabled");
228 static int property_get_job(
231 const char *interface
,
232 const char *property
,
233 sd_bus_message
*reply
,
235 sd_bus_error
*error
) {
237 _cleanup_free_
char *p
= NULL
;
245 return sd_bus_message_append(reply
, "(uo)", 0, "/");
247 p
= job_dbus_path(*j
);
251 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
254 static int property_get_conditions(
257 const char *interface
,
258 const char *property
,
259 sd_bus_message
*reply
,
261 sd_bus_error
*error
) {
263 const char *(*to_string
)(ConditionType type
) = NULL
;
264 Condition
**list
= userdata
, *c
;
271 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
273 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
277 LIST_FOREACH(conditions
, c
, *list
) {
281 c
->result
== CONDITION_UNTESTED
? 0 :
282 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
284 r
= sd_bus_message_append(reply
, "(sbbsi)",
286 c
->trigger
, c
->negate
,
287 c
->parameter
, tristate
);
293 return sd_bus_message_close_container(reply
);
296 static int property_get_load_error(
299 const char *interface
,
300 const char *property
,
301 sd_bus_message
*reply
,
303 sd_bus_error
*error
) {
305 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
313 r
= bus_unit_validate_load_state(u
, &e
);
315 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
317 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
320 static int bus_verify_manage_units_async_full(
324 const char *polkit_message
,
326 sd_bus_message
*call
,
327 sd_bus_error
*error
) {
329 const char *details
[9] = {
334 if (polkit_message
) {
335 details
[4] = "polkit.message";
336 details
[5] = polkit_message
;
337 details
[6] = "polkit.gettext_domain";
338 details
[7] = GETTEXT_PACKAGE
;
341 return bus_verify_polkit_async(
344 "org.freedesktop.systemd1.manage-units",
348 &u
->manager
->polkit_registry
,
352 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
353 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
354 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
355 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
356 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
357 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
360 int bus_unit_method_start_generic(
361 sd_bus_message
*message
,
364 bool reload_if_possible
,
365 sd_bus_error
*error
) {
367 const char *smode
, *verb
;
373 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
375 r
= mac_selinux_unit_access_check(
377 job_type_to_access_method(job_type
),
382 r
= sd_bus_message_read(message
, "s", &smode
);
386 mode
= job_mode_from_string(smode
);
388 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
390 if (reload_if_possible
)
391 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
393 verb
= job_type_to_string(job_type
);
395 r
= bus_verify_manage_units_async_full(
399 polkit_message_for_job
[job_type
],
406 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
408 return bus_unit_queue_job(message
, u
, job_type
, mode
,
409 reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0, error
);
412 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
413 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
416 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
417 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
420 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
421 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
424 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
425 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
428 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
429 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
432 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
433 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
436 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
437 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
440 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
441 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
442 const char *jtype
, *smode
;
451 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
455 /* Parse the two magic reload types "reload-or-…" manually */
456 if (streq(jtype
, "reload-or-restart")) {
458 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
459 } else if (streq(jtype
, "reload-or-try-restart")) {
460 type
= JOB_TRY_RESTART
;
461 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
463 /* And the rest generically */
464 type
= job_type_from_string(jtype
);
466 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
469 mode
= job_mode_from_string(smode
);
471 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
473 r
= mac_selinux_unit_access_check(
475 job_type_to_access_method(type
),
480 r
= bus_verify_manage_units_async_full(
484 polkit_message_for_job
[type
],
491 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
493 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
496 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
506 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
510 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
517 who
= kill_who_from_string(swho
);
519 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
522 if (!SIGNAL_VALID(signo
))
523 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
525 r
= bus_verify_manage_units_async_full(
529 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
536 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
538 r
= unit_kill(u
, who
, signo
, error
);
542 return sd_bus_reply_method_return(message
, NULL
);
545 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
552 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
556 r
= bus_verify_manage_units_async_full(
560 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
567 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
569 unit_reset_failed(u
);
571 return sd_bus_reply_method_return(message
, NULL
);
574 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
581 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
585 r
= sd_bus_message_read(message
, "b", &runtime
);
589 r
= bus_verify_manage_units_async_full(
593 N_("Authentication is required to set properties on '$(unit)'."),
600 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
602 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
606 return sd_bus_reply_method_return(message
, NULL
);
609 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
616 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
620 r
= bus_verify_manage_units_async_full(
631 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
633 r
= bus_unit_track_add_sender(u
, message
);
637 return sd_bus_reply_method_return(message
, NULL
);
640 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
647 r
= bus_unit_track_remove_sender(u
, message
);
649 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
653 return sd_bus_reply_method_return(message
, NULL
);
656 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
657 ExecCleanMask mask
= 0;
664 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
668 r
= sd_bus_message_enter_container(message
, 'a', "s");
675 r
= sd_bus_message_read(message
, "s", &i
);
682 mask
|= EXEC_CLEAN_ALL
;
686 t
= exec_resource_type_from_string(i
);
688 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
694 r
= sd_bus_message_exit_container(message
);
698 r
= bus_verify_manage_units_async_full(
702 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
709 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
711 r
= unit_clean(u
, mask
);
712 if (r
== -EOPNOTSUPP
)
713 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not supporting cleaning.", u
->id
);
715 return sd_bus_error_setf(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
717 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
721 return sd_bus_reply_method_return(message
, NULL
);
724 static int property_get_refs(
727 const char *interface
,
728 const char *property
,
729 sd_bus_message
*reply
,
731 sd_bus_error
*error
) {
740 r
= sd_bus_message_open_container(reply
, 'a', "s");
744 for (i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
747 c
= sd_bus_track_count_name(u
->bus_track
, i
);
751 /* Add the item multiple times if the ref count for each is above 1 */
752 for (k
= 0; k
< c
; k
++) {
753 r
= sd_bus_message_append(reply
, "s", i
);
759 return sd_bus_message_close_container(reply
);
762 const sd_bus_vtable bus_unit_vtable
[] = {
763 SD_BUS_VTABLE_START(0),
765 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
766 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
767 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
768 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
769 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
770 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
771 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
772 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
773 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
774 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
775 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
776 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
777 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
778 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
779 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
780 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
781 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
782 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
783 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
784 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
785 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
786 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
787 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
788 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
789 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
790 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
791 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
792 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
793 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
794 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
795 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
796 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
797 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
798 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
799 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
800 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
801 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
802 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
803 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
804 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
805 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
806 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
807 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
808 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
809 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
810 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
811 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
812 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
813 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
814 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
815 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
816 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
817 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
818 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
819 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
820 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
821 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
822 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
823 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
824 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
825 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
826 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
827 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
828 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
829 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
830 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
831 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
832 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_limit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
833 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
834 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
835 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
836 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
837 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
838 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
839 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
840 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
841 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
843 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
844 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
845 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
846 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
847 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
848 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
849 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
850 SD_BUS_METHOD("EnqueueJob", "ss", "uososa(uosos)", bus_unit_method_enqueue_job
, SD_BUS_VTABLE_UNPRIVILEGED
),
851 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
852 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
853 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
854 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
855 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
856 SD_BUS_METHOD("Clean", "as", NULL
, bus_unit_method_clean
, SD_BUS_VTABLE_UNPRIVILEGED
),
858 /* For dependency types we don't support anymore always return an empty array */
859 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
860 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
861 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
862 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
863 /* Obsolete alias names */
864 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
865 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
869 static int property_get_slice(
872 const char *interface
,
873 const char *property
,
874 sd_bus_message
*reply
,
876 sd_bus_error
*error
) {
884 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
887 static int property_get_current_memory(
890 const char *interface
,
891 const char *property
,
892 sd_bus_message
*reply
,
894 sd_bus_error
*error
) {
896 uint64_t sz
= (uint64_t) -1;
904 r
= unit_get_memory_current(u
, &sz
);
905 if (r
< 0 && r
!= -ENODATA
)
906 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
908 return sd_bus_message_append(reply
, "t", sz
);
911 static int property_get_current_tasks(
914 const char *interface
,
915 const char *property
,
916 sd_bus_message
*reply
,
918 sd_bus_error
*error
) {
920 uint64_t cn
= (uint64_t) -1;
928 r
= unit_get_tasks_current(u
, &cn
);
929 if (r
< 0 && r
!= -ENODATA
)
930 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
932 return sd_bus_message_append(reply
, "t", cn
);
935 static int property_get_cpu_usage(
938 const char *interface
,
939 const char *property
,
940 sd_bus_message
*reply
,
942 sd_bus_error
*error
) {
944 nsec_t ns
= (nsec_t
) -1;
952 r
= unit_get_cpu_usage(u
, &ns
);
953 if (r
< 0 && r
!= -ENODATA
)
954 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
956 return sd_bus_message_append(reply
, "t", ns
);
959 static int property_get_cgroup(
962 const char *interface
,
963 const char *property
,
964 sd_bus_message
*reply
,
966 sd_bus_error
*error
) {
969 const char *t
= NULL
;
975 /* Three cases: a) u->cgroup_path is NULL, in which case the
976 * unit has no control group, which we report as the empty
977 * string. b) u->cgroup_path is the empty string, which
978 * indicates the root cgroup, which we report as "/". c) all
979 * other cases we report as-is. */
982 t
= empty_to_root(u
->cgroup_path
);
984 return sd_bus_message_append(reply
, "s", t
);
987 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
988 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
994 r
= set_put(pids
, PID_TO_PTR(pid
));
995 if (IN_SET(r
, 0, -EEXIST
))
1001 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1010 (void) get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &cmdline
);
1012 return sd_bus_message_append(reply
,
1019 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1020 _cleanup_closedir_
DIR *d
= NULL
;
1021 _cleanup_fclose_
FILE *f
= NULL
;
1027 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1036 r
= cg_read_pid(f
, &pid
);
1042 if (is_kernel_thread(pid
) > 0)
1045 r
= append_process(reply
, p
, pid
, pids
);
1050 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1057 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1059 r
= cg_read_subgroup(d
, &g
);
1065 j
= path_join(empty_to_root(p
), g
);
1069 r
= append_cgroup(reply
, j
, pids
);
1077 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1078 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1079 _cleanup_set_free_ Set
*pids
= NULL
;
1086 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1090 pids
= set_new(NULL
);
1094 r
= sd_bus_message_new_method_return(message
, &reply
);
1098 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1102 if (u
->cgroup_path
) {
1103 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1108 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1109 pid
= unit_main_pid(u
);
1111 r
= append_process(reply
, NULL
, pid
, pids
);
1116 pid
= unit_control_pid(u
);
1118 r
= append_process(reply
, NULL
, pid
, pids
);
1123 r
= sd_bus_message_close_container(reply
);
1127 return sd_bus_send(NULL
, reply
, NULL
);
1130 static int property_get_ip_counter(
1133 const char *interface
,
1134 const char *property
,
1135 sd_bus_message
*reply
,
1137 sd_bus_error
*error
) {
1139 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1140 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1141 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1142 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1143 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1146 uint64_t value
= UINT64_MAX
;
1155 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1156 (void) unit_get_ip_accounting(u
, metric
, &value
);
1157 return sd_bus_message_append(reply
, "t", value
);
1160 static int property_get_io_counter(
1163 const char *interface
,
1164 const char *property
,
1165 sd_bus_message
*reply
,
1167 sd_bus_error
*error
) {
1169 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1170 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1171 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1172 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1173 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1176 uint64_t value
= UINT64_MAX
;
1185 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1186 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1187 return sd_bus_message_append(reply
, "t", value
);
1190 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1192 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1193 _cleanup_set_free_ Set
*pids
= NULL
;
1200 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1201 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1202 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1203 * subcgroup path has no effect! */
1205 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1209 r
= sd_bus_message_read(message
, "s", &path
);
1213 path
= empty_to_null(path
);
1215 if (!path_is_absolute(path
))
1216 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1218 if (!path_is_normalized(path
))
1219 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1222 if (!unit_cgroup_delegate(u
))
1223 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1225 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1226 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1228 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1232 r
= sd_bus_message_enter_container(message
, 'a', "u");
1236 uid_t process_uid
, sender_uid
;
1240 r
= sd_bus_message_read(message
, "u", &upid
);
1247 r
= sd_bus_creds_get_pid(creds
, &pid
);
1253 /* Filter out duplicates */
1254 if (set_contains(pids
, PID_TO_PTR(pid
)))
1257 /* Check if this process is suitable for attaching to this unit */
1258 r
= unit_pid_attachable(u
, pid
, error
);
1262 /* Let's query the sender's UID, so that we can make our security decisions */
1263 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1267 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1268 * then the process' UID and the target unit's UID have to match the sender's UID */
1269 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1270 r
= get_process_uid(pid
, &process_uid
);
1272 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1274 if (process_uid
!= sender_uid
)
1275 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1276 if (process_uid
!= u
->ref_uid
)
1277 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1281 pids
= set_new(NULL
);
1286 r
= set_put(pids
, PID_TO_PTR(pid
));
1291 r
= sd_bus_message_exit_container(message
);
1295 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1297 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1299 return sd_bus_reply_method_return(message
, NULL
);
1302 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1303 SD_BUS_VTABLE_START(0),
1304 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1305 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1306 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1307 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1308 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1309 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1310 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1311 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1312 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1313 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1314 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1315 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1316 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1317 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1318 SD_BUS_METHOD("AttachProcesses", "sau", NULL
, bus_unit_method_attach_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1322 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1323 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1324 _cleanup_free_
char *p
= NULL
;
1331 p
= unit_dbus_path(u
);
1335 r
= sd_bus_message_new_signal(
1338 "/org/freedesktop/systemd1",
1339 "org.freedesktop.systemd1.Manager",
1344 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1348 return sd_bus_send(bus
, m
, NULL
);
1351 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1352 _cleanup_free_
char *p
= NULL
;
1359 p
= unit_dbus_path(u
);
1363 /* Send a properties changed signal. First for the specific
1364 * type, then for the generic unit. The clients may rely on
1365 * this order to get atomic behavior if needed. */
1367 r
= sd_bus_emit_properties_changed_strv(
1369 unit_dbus_interface_from_type(u
->type
),
1374 return sd_bus_emit_properties_changed_strv(
1376 "org.freedesktop.systemd1.Unit",
1380 void bus_unit_send_change_signal(Unit
*u
) {
1384 if (u
->in_dbus_queue
) {
1385 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1386 u
->in_dbus_queue
= false;
1392 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1394 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1396 u
->sent_dbus_new_signal
= true;
1399 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1401 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1402 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1403 * so that clients can follow the full state transition */
1405 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1408 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1409 * the unit appears in the new state right-away (except if the
1410 * caller explicitly asked us to send it anyway) */
1413 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1414 * when we are reloading. */
1417 bus_unit_send_change_signal(u
);
1420 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1421 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1422 _cleanup_free_
char *p
= NULL
;
1429 p
= unit_dbus_path(u
);
1433 r
= sd_bus_message_new_signal(
1436 "/org/freedesktop/systemd1",
1437 "org.freedesktop.systemd1.Manager",
1442 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1446 return sd_bus_send(bus
, m
, NULL
);
1449 void bus_unit_send_removed_signal(Unit
*u
) {
1453 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1454 bus_unit_send_change_signal(u
);
1459 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1461 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1464 int bus_unit_queue_job(
1465 sd_bus_message
*message
,
1469 BusUnitQueueFlags flags
,
1470 sd_bus_error
*error
) {
1472 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1473 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1474 _cleanup_(set_freep
) Set
*affected
= NULL
;
1481 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1482 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1484 r
= mac_selinux_unit_access_check(
1486 job_type_to_access_method(type
),
1491 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1492 if (type
== JOB_RESTART
)
1493 type
= JOB_RELOAD_OR_START
;
1494 else if (type
== JOB_TRY_RESTART
)
1495 type
= JOB_TRY_RELOAD
;
1498 if (type
== JOB_STOP
&&
1499 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1500 unit_active_state(u
) == UNIT_INACTIVE
)
1501 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1503 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1504 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1505 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1506 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1507 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
);
1509 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1510 affected
= set_new(NULL
);
1515 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1519 r
= bus_job_track_sender(j
, message
);
1523 /* Before we send the method reply, force out the announcement JobNew for this job */
1524 bus_job_send_pending_change_signal(j
, true);
1526 job_path
= job_dbus_path(j
);
1530 /* The classic response is just a job object path */
1531 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1532 return sd_bus_reply_method_return(message
, "o", job_path
);
1534 /* In verbose mode respond with the anchor job plus everything that has been affected */
1535 r
= sd_bus_message_new_method_return(message
, &reply
);
1539 unit_path
= unit_dbus_path(j
->unit
);
1543 r
= sd_bus_message_append(reply
, "uosos",
1545 j
->unit
->id
, unit_path
,
1546 job_type_to_string(j
->type
));
1550 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1554 SET_FOREACH(a
, affected
, i
) {
1559 /* Free paths from previous iteration */
1560 job_path
= mfree(job_path
);
1561 unit_path
= mfree(unit_path
);
1563 job_path
= job_dbus_path(a
);
1567 unit_path
= unit_dbus_path(a
->unit
);
1571 r
= sd_bus_message_append(reply
, "(uosos)",
1573 a
->unit
->id
, unit_path
,
1574 job_type_to_string(a
->type
));
1579 r
= sd_bus_message_close_container(reply
);
1583 return sd_bus_send(NULL
, reply
, NULL
);
1586 static int bus_unit_set_live_property(
1589 sd_bus_message
*message
,
1590 UnitWriteFlags flags
,
1591 sd_bus_error
*error
) {
1599 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1600 * units that are being created). */
1602 if (streq(name
, "Description")) {
1605 r
= sd_bus_message_read(message
, "s", &d
);
1609 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1610 r
= unit_set_description(u
, d
);
1614 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1623 static int bus_set_transient_emergency_action(
1627 sd_bus_message
*message
,
1628 UnitWriteFlags flags
,
1629 sd_bus_error
*error
) {
1638 r
= sd_bus_message_read(message
, "s", &s
);
1642 system
= MANAGER_IS_SYSTEM(u
->manager
);
1643 r
= parse_emergency_action(s
, system
, &v
);
1645 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1646 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
1647 : "Invalid %s setting: %s",
1650 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1652 unit_write_settingf(u
, flags
, name
,
1659 static int bus_set_transient_exit_status(
1663 sd_bus_message
*message
,
1664 UnitWriteFlags flags
,
1665 sd_bus_error
*error
) {
1672 r
= sd_bus_message_read(message
, "i", &k
);
1677 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
1679 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1680 *p
= k
< 0 ? -1 : k
;
1683 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1685 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
1691 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1692 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1694 static int bus_set_transient_conditions(
1699 sd_bus_message
*message
,
1700 UnitWriteFlags flags
,
1701 sd_bus_error
*error
) {
1703 const char *type_name
, *param
;
1704 int trigger
, negate
, r
;
1709 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1713 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1716 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1718 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1720 if (t
!= CONDITION_NULL
) {
1722 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1724 if (condition_takes_path(t
) && !path_is_absolute(param
))
1725 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1729 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1732 c
= condition_new(t
, param
, trigger
, negate
);
1736 LIST_PREPEND(conditions
, *list
, c
);
1738 if (t
!= CONDITION_NULL
)
1739 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1740 "%s=%s%s%s", type_name
,
1741 trigger
? "|" : "", negate
? "!" : "", param
);
1743 unit_write_settingf(u
, flags
, name
,
1744 "%s=%s%s", type_name
,
1745 trigger
? "|" : "", yes_no(!negate
));
1753 r
= sd_bus_message_exit_container(message
);
1757 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1758 *list
= condition_free_list(*list
);
1759 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1765 static int bus_unit_set_transient_property(
1768 sd_bus_message
*message
,
1769 UnitWriteFlags flags
,
1770 sd_bus_error
*error
) {
1772 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1779 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1780 * has been created. */
1782 if (streq(name
, "SourcePath"))
1783 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1785 if (streq(name
, "StopWhenUnneeded"))
1786 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1788 if (streq(name
, "RefuseManualStart"))
1789 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1791 if (streq(name
, "RefuseManualStop"))
1792 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1794 if (streq(name
, "AllowIsolate"))
1795 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1797 if (streq(name
, "DefaultDependencies"))
1798 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1800 if (streq(name
, "OnFailureJobMode"))
1801 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1803 if (streq(name
, "IgnoreOnIsolate"))
1804 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1806 if (streq(name
, "JobTimeoutUSec")) {
1807 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1808 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1809 u
->job_running_timeout
= u
->job_timeout
;
1812 if (streq(name
, "JobRunningTimeoutUSec")) {
1813 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1814 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1815 u
->job_running_timeout_set
= true;
1820 if (streq(name
, "JobTimeoutAction"))
1821 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1823 if (streq(name
, "JobTimeoutRebootArgument"))
1824 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1826 if (streq(name
, "StartLimitIntervalUSec"))
1827 return bus_set_transient_usec(u
, name
, &u
->start_limit
.interval
, message
, flags
, error
);
1829 if (streq(name
, "StartLimitBurst"))
1830 return bus_set_transient_unsigned(u
, name
, &u
->start_limit
.burst
, message
, flags
, error
);
1832 if (streq(name
, "StartLimitAction"))
1833 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1835 if (streq(name
, "FailureAction"))
1836 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1838 if (streq(name
, "SuccessAction"))
1839 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1841 if (streq(name
, "FailureActionExitStatus"))
1842 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
1844 if (streq(name
, "SuccessActionExitStatus"))
1845 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
1847 if (streq(name
, "RebootArgument"))
1848 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1850 if (streq(name
, "CollectMode"))
1851 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
1853 if (streq(name
, "Conditions"))
1854 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
1856 if (streq(name
, "Asserts"))
1857 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
1859 if (streq(name
, "Documentation")) {
1860 _cleanup_strv_free_
char **l
= NULL
;
1863 r
= sd_bus_message_read_strv(message
, &l
);
1867 STRV_FOREACH(p
, l
) {
1868 if (!documentation_url_is_valid(*p
))
1869 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
1872 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1873 if (strv_isempty(l
)) {
1874 u
->documentation
= strv_free(u
->documentation
);
1875 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1877 strv_extend_strv(&u
->documentation
, l
, false);
1880 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1886 } else if (streq(name
, "Slice")) {
1890 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1891 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1892 if (u
->type
== UNIT_SLICE
)
1893 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1894 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1895 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1897 r
= sd_bus_message_read(message
, "s", &s
);
1901 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1902 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1904 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1905 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1906 * instead of manager_load_unit() on purpose, here. */
1907 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1911 if (slice
->type
!= UNIT_SLICE
)
1912 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1914 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1915 r
= unit_set_slice(u
, slice
);
1919 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
1924 } else if (streq(name
, "RequiresMountsFor")) {
1925 _cleanup_strv_free_
char **l
= NULL
;
1928 r
= sd_bus_message_read_strv(message
, &l
);
1932 STRV_FOREACH(p
, l
) {
1933 path_simplify(*p
, true);
1935 if (!path_is_absolute(*p
))
1936 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
1938 if (!path_is_valid(*p
))
1939 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
1941 if (!path_is_normalized(*p
))
1942 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
1944 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1945 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
1947 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
1949 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1956 if (streq(name
, "RequiresOverridable"))
1957 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1958 else if (streq(name
, "RequisiteOverridable"))
1959 d
= UNIT_REQUISITE
; /* same here */
1961 d
= unit_dependency_from_string(name
);
1966 r
= sd_bus_message_enter_container(message
, 'a', "s");
1970 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1971 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1972 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1974 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1975 _cleanup_free_
char *label
= NULL
;
1977 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
1981 label
= strjoin(name
, "-", other
);
1985 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
1992 r
= sd_bus_message_exit_container(message
);
1998 } else if (streq(name
, "AddRef")) {
2002 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2003 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2004 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2005 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2006 * dependency type, hence let's not confuse things with that.
2008 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2009 * the transient unit is complete, so that setting this property multiple times in the same transient
2010 * unit creation call doesn't count as individual references. */
2012 r
= sd_bus_message_read(message
, "b", &b
);
2016 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2017 u
->bus_track_add
= b
;
2025 int bus_unit_set_properties(
2027 sd_bus_message
*message
,
2028 UnitWriteFlags flags
,
2030 sd_bus_error
*error
) {
2032 bool for_real
= false;
2039 /* We iterate through the array twice. First run we just check
2040 * if all passed data is valid, second run actually applies
2041 * it. This is to implement transaction-like behaviour without
2042 * actually providing full transactions. */
2044 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2052 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2056 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2059 /* Reached EOF. Let's try again, and this time for realz... */
2060 r
= sd_bus_message_rewind(message
, false);
2068 r
= sd_bus_message_read(message
, "s", &name
);
2072 if (!UNIT_VTABLE(u
)->bus_set_property
)
2073 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
2075 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2079 /* If not for real, then mask out the two target flags */
2080 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2082 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2083 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2084 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2086 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2091 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
2093 r
= sd_bus_message_exit_container(message
);
2097 r
= sd_bus_message_exit_container(message
);
2104 r
= sd_bus_message_exit_container(message
);
2108 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2109 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2114 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2117 /* Generates a pretty error if a unit isn't properly loaded. */
2119 switch (u
->load_state
) {
2124 case UNIT_NOT_FOUND
:
2125 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2127 case UNIT_BAD_SETTING
:
2128 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2130 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2131 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to load properly: %m.", u
->id
);
2134 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2139 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2143 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2149 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2151 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2153 unit_add_to_cgroup_empty_queue(u
);
2155 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2156 unit_add_to_gc_queue(u
);
2161 static int bus_unit_allocate_bus_track(Unit
*u
) {
2169 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2173 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2175 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2182 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2187 r
= bus_unit_allocate_bus_track(u
);
2191 return sd_bus_track_add_name(u
->bus_track
, name
);
2194 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2199 r
= bus_unit_allocate_bus_track(u
);
2203 return sd_bus_track_add_sender(u
->bus_track
, m
);
2206 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2209 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
2214 return sd_bus_track_remove_sender(u
->bus_track
, m
);