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"
16 #include "locale-util.h"
18 #include "path-util.h"
19 #include "process-util.h"
20 #include "selinux-access.h"
21 #include "signal-util.h"
23 #include "string-table.h"
24 #include "string-util.h"
26 #include "user-util.h"
29 static bool unit_can_start_refuse_manual(Unit
*u
) {
30 return unit_can_start(u
) && !u
->refuse_manual_start
;
33 static bool unit_can_stop_refuse_manual(Unit
*u
) {
34 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
37 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
38 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
41 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
42 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
45 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
46 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
47 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
48 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
49 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
53 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
54 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
56 static int property_get_can_clean(
59 const char *interface
,
61 sd_bus_message
*reply
,
63 sd_bus_error
*error
) {
72 r
= unit_can_clean(u
, &mask
);
76 r
= sd_bus_message_open_container(reply
, 'a', "s");
80 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
81 if (!FLAGS_SET(mask
, 1U << t
))
84 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
89 return sd_bus_message_close_container(reply
);
92 static int property_get_names(
95 const char *interface
,
97 sd_bus_message
*reply
,
99 sd_bus_error
*error
) {
110 r
= sd_bus_message_open_container(reply
, 'a', "s");
114 SET_FOREACH(t
, *s
, i
) {
115 r
= sd_bus_message_append(reply
, "s", t
);
120 return sd_bus_message_close_container(reply
);
123 static int property_get_following(
126 const char *interface
,
127 const char *property
,
128 sd_bus_message
*reply
,
130 sd_bus_error
*error
) {
132 Unit
*u
= userdata
, *f
;
138 f
= unit_following(u
);
139 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
142 static int property_get_dependencies(
145 const char *interface
,
146 const char *property
,
147 sd_bus_message
*reply
,
149 sd_bus_error
*error
) {
151 Hashmap
**h
= userdata
;
161 r
= sd_bus_message_open_container(reply
, 'a', "s");
165 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
166 r
= sd_bus_message_append(reply
, "s", u
->id
);
171 return sd_bus_message_close_container(reply
);
174 static int property_get_requires_mounts_for(
177 const char *interface
,
178 const char *property
,
179 sd_bus_message
*reply
,
181 sd_bus_error
*error
) {
183 Hashmap
**h
= userdata
;
193 r
= sd_bus_message_open_container(reply
, 'a', "s");
197 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
198 r
= sd_bus_message_append(reply
, "s", p
);
203 return sd_bus_message_close_container(reply
);
206 static int property_get_unit_file_preset(
209 const char *interface
,
210 const char *property
,
211 sd_bus_message
*reply
,
213 sd_bus_error
*error
) {
222 r
= unit_get_unit_file_preset(u
);
224 return sd_bus_message_append(reply
, "s",
226 r
> 0 ? "enabled" : "disabled");
229 static int property_get_job(
232 const char *interface
,
233 const char *property
,
234 sd_bus_message
*reply
,
236 sd_bus_error
*error
) {
238 _cleanup_free_
char *p
= NULL
;
246 return sd_bus_message_append(reply
, "(uo)", 0, "/");
248 p
= job_dbus_path(*j
);
252 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
255 static int property_get_conditions(
258 const char *interface
,
259 const char *property
,
260 sd_bus_message
*reply
,
262 sd_bus_error
*error
) {
264 const char *(*to_string
)(ConditionType type
) = NULL
;
265 Condition
**list
= userdata
, *c
;
272 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
274 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
278 LIST_FOREACH(conditions
, c
, *list
) {
282 c
->result
== CONDITION_UNTESTED
? 0 :
283 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
285 r
= sd_bus_message_append(reply
, "(sbbsi)",
287 c
->trigger
, c
->negate
,
288 c
->parameter
, tristate
);
294 return sd_bus_message_close_container(reply
);
297 static int property_get_load_error(
300 const char *interface
,
301 const char *property
,
302 sd_bus_message
*reply
,
304 sd_bus_error
*error
) {
306 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
314 r
= bus_unit_validate_load_state(u
, &e
);
316 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
318 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
321 static int bus_verify_manage_units_async_full(
325 const char *polkit_message
,
327 sd_bus_message
*call
,
328 sd_bus_error
*error
) {
330 const char *details
[9] = {
335 if (polkit_message
) {
336 details
[4] = "polkit.message";
337 details
[5] = polkit_message
;
338 details
[6] = "polkit.gettext_domain";
339 details
[7] = GETTEXT_PACKAGE
;
342 return bus_verify_polkit_async(
345 "org.freedesktop.systemd1.manage-units",
349 &u
->manager
->polkit_registry
,
353 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
354 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
355 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
356 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
357 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
358 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
361 int bus_unit_method_start_generic(
362 sd_bus_message
*message
,
365 bool reload_if_possible
,
366 sd_bus_error
*error
) {
368 const char *smode
, *verb
;
374 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
376 r
= mac_selinux_unit_access_check(
378 job_type_to_access_method(job_type
),
383 r
= sd_bus_message_read(message
, "s", &smode
);
387 mode
= job_mode_from_string(smode
);
389 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
391 if (reload_if_possible
)
392 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
394 verb
= job_type_to_string(job_type
);
396 r
= bus_verify_manage_units_async_full(
400 polkit_message_for_job
[job_type
],
407 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
409 return bus_unit_queue_job(message
, u
, job_type
, mode
,
410 reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0, error
);
413 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
414 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
417 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
418 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
421 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
422 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
425 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
426 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
429 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
430 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
433 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
434 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
437 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
438 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
441 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
442 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
443 const char *jtype
, *smode
;
452 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
456 /* Parse the two magic reload types "reload-or-…" manually */
457 if (streq(jtype
, "reload-or-restart")) {
459 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
460 } else if (streq(jtype
, "reload-or-try-restart")) {
461 type
= JOB_TRY_RESTART
;
462 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
464 /* And the rest generically */
465 type
= job_type_from_string(jtype
);
467 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
470 mode
= job_mode_from_string(smode
);
472 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
474 r
= mac_selinux_unit_access_check(
476 job_type_to_access_method(type
),
481 r
= bus_verify_manage_units_async_full(
485 polkit_message_for_job
[type
],
492 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
494 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
497 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
507 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
511 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
518 who
= kill_who_from_string(swho
);
520 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
523 if (!SIGNAL_VALID(signo
))
524 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
526 r
= bus_verify_manage_units_async_full(
530 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
537 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
539 r
= unit_kill(u
, who
, signo
, error
);
543 return sd_bus_reply_method_return(message
, NULL
);
546 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
553 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
557 r
= bus_verify_manage_units_async_full(
561 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
568 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
570 unit_reset_failed(u
);
572 return sd_bus_reply_method_return(message
, NULL
);
575 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
582 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
586 r
= sd_bus_message_read(message
, "b", &runtime
);
590 r
= bus_verify_manage_units_async_full(
594 N_("Authentication is required to set properties on '$(unit)'."),
601 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
603 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
607 return sd_bus_reply_method_return(message
, NULL
);
610 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
617 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
621 r
= bus_verify_manage_units_async_full(
632 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
634 r
= bus_unit_track_add_sender(u
, message
);
638 return sd_bus_reply_method_return(message
, NULL
);
641 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
648 r
= bus_unit_track_remove_sender(u
, message
);
650 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
654 return sd_bus_reply_method_return(message
, NULL
);
657 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
658 ExecCleanMask mask
= 0;
665 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
669 r
= sd_bus_message_enter_container(message
, 'a', "s");
676 r
= sd_bus_message_read(message
, "s", &i
);
683 mask
|= EXEC_CLEAN_ALL
;
687 t
= exec_resource_type_from_string(i
);
689 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
695 r
= sd_bus_message_exit_container(message
);
699 r
= bus_verify_manage_units_async_full(
703 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
710 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
712 r
= unit_clean(u
, mask
);
713 if (r
== -EOPNOTSUPP
)
714 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not supporting cleaning.", u
->id
);
716 return sd_bus_error_setf(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
718 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
722 return sd_bus_reply_method_return(message
, NULL
);
725 static int property_get_refs(
728 const char *interface
,
729 const char *property
,
730 sd_bus_message
*reply
,
732 sd_bus_error
*error
) {
741 r
= sd_bus_message_open_container(reply
, 'a', "s");
745 for (i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
748 c
= sd_bus_track_count_name(u
->bus_track
, i
);
752 /* Add the item multiple times if the ref count for each is above 1 */
753 for (k
= 0; k
< c
; k
++) {
754 r
= sd_bus_message_append(reply
, "s", i
);
760 return sd_bus_message_close_container(reply
);
763 const sd_bus_vtable bus_unit_vtable
[] = {
764 SD_BUS_VTABLE_START(0),
766 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
767 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
768 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
769 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
770 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
771 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
772 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
773 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
774 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
775 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
776 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
777 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
778 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
779 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
780 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
781 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
782 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
783 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
784 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
785 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
786 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
787 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
788 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
789 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
790 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
791 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
792 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
793 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
794 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
795 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
796 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
797 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
798 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
799 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
800 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
801 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
802 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
803 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
804 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
805 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
806 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
807 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
808 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
809 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
810 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
811 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
812 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
813 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
814 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
815 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
816 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
817 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
818 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
819 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
820 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
821 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
822 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
823 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
824 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
825 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
826 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
827 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
828 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
829 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
830 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
831 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
832 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
833 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
834 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
835 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
836 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
837 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
838 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
839 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
840 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
841 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
842 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
844 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
845 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
846 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
847 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
848 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
849 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
850 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
851 SD_BUS_METHOD("EnqueueJob", "ss", "uososa(uosos)", bus_unit_method_enqueue_job
, SD_BUS_VTABLE_UNPRIVILEGED
),
852 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
853 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
854 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
855 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
856 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
857 SD_BUS_METHOD("Clean", "as", NULL
, bus_unit_method_clean
, SD_BUS_VTABLE_UNPRIVILEGED
),
859 /* For dependency types we don't support anymore always return an empty array */
860 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
861 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
862 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
863 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
864 /* Obsolete alias names */
865 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
866 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
870 static int property_get_slice(
873 const char *interface
,
874 const char *property
,
875 sd_bus_message
*reply
,
877 sd_bus_error
*error
) {
885 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
888 static int property_get_current_memory(
891 const char *interface
,
892 const char *property
,
893 sd_bus_message
*reply
,
895 sd_bus_error
*error
) {
897 uint64_t sz
= (uint64_t) -1;
905 r
= unit_get_memory_current(u
, &sz
);
906 if (r
< 0 && r
!= -ENODATA
)
907 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
909 return sd_bus_message_append(reply
, "t", sz
);
912 static int property_get_current_tasks(
915 const char *interface
,
916 const char *property
,
917 sd_bus_message
*reply
,
919 sd_bus_error
*error
) {
921 uint64_t cn
= (uint64_t) -1;
929 r
= unit_get_tasks_current(u
, &cn
);
930 if (r
< 0 && r
!= -ENODATA
)
931 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
933 return sd_bus_message_append(reply
, "t", cn
);
936 static int property_get_cpu_usage(
939 const char *interface
,
940 const char *property
,
941 sd_bus_message
*reply
,
943 sd_bus_error
*error
) {
945 nsec_t ns
= (nsec_t
) -1;
953 r
= unit_get_cpu_usage(u
, &ns
);
954 if (r
< 0 && r
!= -ENODATA
)
955 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
957 return sd_bus_message_append(reply
, "t", ns
);
960 static int property_get_cgroup(
963 const char *interface
,
964 const char *property
,
965 sd_bus_message
*reply
,
967 sd_bus_error
*error
) {
970 const char *t
= NULL
;
976 /* Three cases: a) u->cgroup_path is NULL, in which case the
977 * unit has no control group, which we report as the empty
978 * string. b) u->cgroup_path is the empty string, which
979 * indicates the root cgroup, which we report as "/". c) all
980 * other cases we report as-is. */
983 t
= empty_to_root(u
->cgroup_path
);
985 return sd_bus_message_append(reply
, "s", t
);
988 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
989 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
995 r
= set_put(pids
, PID_TO_PTR(pid
));
996 if (IN_SET(r
, 0, -EEXIST
))
1002 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1011 (void) get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &cmdline
);
1013 return sd_bus_message_append(reply
,
1020 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1021 _cleanup_closedir_
DIR *d
= NULL
;
1022 _cleanup_fclose_
FILE *f
= NULL
;
1028 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1037 r
= cg_read_pid(f
, &pid
);
1043 if (is_kernel_thread(pid
) > 0)
1046 r
= append_process(reply
, p
, pid
, pids
);
1051 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1058 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1060 r
= cg_read_subgroup(d
, &g
);
1066 j
= path_join(empty_to_root(p
), g
);
1070 r
= append_cgroup(reply
, j
, pids
);
1078 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1079 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1080 _cleanup_set_free_ Set
*pids
= NULL
;
1087 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1091 pids
= set_new(NULL
);
1095 r
= sd_bus_message_new_method_return(message
, &reply
);
1099 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1103 if (u
->cgroup_path
) {
1104 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1109 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1110 pid
= unit_main_pid(u
);
1112 r
= append_process(reply
, NULL
, pid
, pids
);
1117 pid
= unit_control_pid(u
);
1119 r
= append_process(reply
, NULL
, pid
, pids
);
1124 r
= sd_bus_message_close_container(reply
);
1128 return sd_bus_send(NULL
, reply
, NULL
);
1131 static int property_get_ip_counter(
1134 const char *interface
,
1135 const char *property
,
1136 sd_bus_message
*reply
,
1138 sd_bus_error
*error
) {
1140 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1141 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1142 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1143 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1144 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1147 uint64_t value
= UINT64_MAX
;
1156 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1157 (void) unit_get_ip_accounting(u
, metric
, &value
);
1158 return sd_bus_message_append(reply
, "t", value
);
1161 static int property_get_io_counter(
1164 const char *interface
,
1165 const char *property
,
1166 sd_bus_message
*reply
,
1168 sd_bus_error
*error
) {
1170 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1171 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1172 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1173 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1174 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1177 uint64_t value
= UINT64_MAX
;
1186 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1187 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1188 return sd_bus_message_append(reply
, "t", value
);
1191 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1193 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1194 _cleanup_set_free_ Set
*pids
= NULL
;
1201 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1202 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1203 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1204 * subcgroup path has no effect! */
1206 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1210 r
= sd_bus_message_read(message
, "s", &path
);
1214 path
= empty_to_null(path
);
1216 if (!path_is_absolute(path
))
1217 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1219 if (!path_is_normalized(path
))
1220 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1223 if (!unit_cgroup_delegate(u
))
1224 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1226 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1227 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1229 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1233 r
= sd_bus_message_enter_container(message
, 'a', "u");
1237 uid_t process_uid
, sender_uid
;
1241 r
= sd_bus_message_read(message
, "u", &upid
);
1248 r
= sd_bus_creds_get_pid(creds
, &pid
);
1254 /* Filter out duplicates */
1255 if (set_contains(pids
, PID_TO_PTR(pid
)))
1258 /* Check if this process is suitable for attaching to this unit */
1259 r
= unit_pid_attachable(u
, pid
, error
);
1263 /* Let's query the sender's UID, so that we can make our security decisions */
1264 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1268 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1269 * then the process' UID and the target unit's UID have to match the sender's UID */
1270 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1271 r
= get_process_uid(pid
, &process_uid
);
1273 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1275 if (process_uid
!= sender_uid
)
1276 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1277 if (process_uid
!= u
->ref_uid
)
1278 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1282 pids
= set_new(NULL
);
1287 r
= set_put(pids
, PID_TO_PTR(pid
));
1292 r
= sd_bus_message_exit_container(message
);
1296 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1298 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1300 return sd_bus_reply_method_return(message
, NULL
);
1303 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1304 SD_BUS_VTABLE_START(0),
1305 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1306 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1307 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1308 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1309 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1310 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1311 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1312 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1313 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1314 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1315 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1316 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1317 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1318 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1319 SD_BUS_METHOD("AttachProcesses", "sau", NULL
, bus_unit_method_attach_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1323 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1324 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1325 _cleanup_free_
char *p
= NULL
;
1332 p
= unit_dbus_path(u
);
1336 r
= sd_bus_message_new_signal(
1339 "/org/freedesktop/systemd1",
1340 "org.freedesktop.systemd1.Manager",
1345 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1349 return sd_bus_send(bus
, m
, NULL
);
1352 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1353 _cleanup_free_
char *p
= NULL
;
1360 p
= unit_dbus_path(u
);
1364 /* Send a properties changed signal. First for the specific
1365 * type, then for the generic unit. The clients may rely on
1366 * this order to get atomic behavior if needed. */
1368 r
= sd_bus_emit_properties_changed_strv(
1370 unit_dbus_interface_from_type(u
->type
),
1375 return sd_bus_emit_properties_changed_strv(
1377 "org.freedesktop.systemd1.Unit",
1381 void bus_unit_send_change_signal(Unit
*u
) {
1385 if (u
->in_dbus_queue
) {
1386 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1387 u
->in_dbus_queue
= false;
1393 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1395 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1397 u
->sent_dbus_new_signal
= true;
1400 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1402 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1403 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1404 * so that clients can follow the full state transition */
1406 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1409 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1410 * the unit appears in the new state right-away (except if the
1411 * caller explicitly asked us to send it anyway) */
1414 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1415 * when we are reloading. */
1418 bus_unit_send_change_signal(u
);
1421 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1422 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1423 _cleanup_free_
char *p
= NULL
;
1430 p
= unit_dbus_path(u
);
1434 r
= sd_bus_message_new_signal(
1437 "/org/freedesktop/systemd1",
1438 "org.freedesktop.systemd1.Manager",
1443 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1447 return sd_bus_send(bus
, m
, NULL
);
1450 void bus_unit_send_removed_signal(Unit
*u
) {
1454 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1455 bus_unit_send_change_signal(u
);
1460 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1462 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1465 int bus_unit_queue_job(
1466 sd_bus_message
*message
,
1470 BusUnitQueueFlags flags
,
1471 sd_bus_error
*error
) {
1473 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1474 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1475 _cleanup_(set_freep
) Set
*affected
= NULL
;
1482 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1483 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1485 r
= mac_selinux_unit_access_check(
1487 job_type_to_access_method(type
),
1492 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1493 if (type
== JOB_RESTART
)
1494 type
= JOB_RELOAD_OR_START
;
1495 else if (type
== JOB_TRY_RESTART
)
1496 type
= JOB_TRY_RELOAD
;
1499 if (type
== JOB_STOP
&&
1500 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1501 unit_active_state(u
) == UNIT_INACTIVE
)
1502 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1504 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1505 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1506 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1507 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1508 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
);
1510 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1511 affected
= set_new(NULL
);
1516 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1520 r
= bus_job_track_sender(j
, message
);
1524 /* Before we send the method reply, force out the announcement JobNew for this job */
1525 bus_job_send_pending_change_signal(j
, true);
1527 job_path
= job_dbus_path(j
);
1531 /* The classic response is just a job object path */
1532 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1533 return sd_bus_reply_method_return(message
, "o", job_path
);
1535 /* In verbose mode respond with the anchor job plus everything that has been affected */
1536 r
= sd_bus_message_new_method_return(message
, &reply
);
1540 unit_path
= unit_dbus_path(j
->unit
);
1544 r
= sd_bus_message_append(reply
, "uosos",
1546 j
->unit
->id
, unit_path
,
1547 job_type_to_string(j
->type
));
1551 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1555 SET_FOREACH(a
, affected
, i
) {
1560 /* Free paths from previous iteration */
1561 job_path
= mfree(job_path
);
1562 unit_path
= mfree(unit_path
);
1564 job_path
= job_dbus_path(a
);
1568 unit_path
= unit_dbus_path(a
->unit
);
1572 r
= sd_bus_message_append(reply
, "(uosos)",
1574 a
->unit
->id
, unit_path
,
1575 job_type_to_string(a
->type
));
1580 r
= sd_bus_message_close_container(reply
);
1584 return sd_bus_send(NULL
, reply
, NULL
);
1587 static int bus_unit_set_live_property(
1590 sd_bus_message
*message
,
1591 UnitWriteFlags flags
,
1592 sd_bus_error
*error
) {
1600 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1601 * units that are being created). */
1603 if (streq(name
, "Description")) {
1606 r
= sd_bus_message_read(message
, "s", &d
);
1610 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1611 r
= unit_set_description(u
, d
);
1615 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1624 static int bus_set_transient_emergency_action(
1628 sd_bus_message
*message
,
1629 UnitWriteFlags flags
,
1630 sd_bus_error
*error
) {
1639 r
= sd_bus_message_read(message
, "s", &s
);
1643 system
= MANAGER_IS_SYSTEM(u
->manager
);
1644 r
= parse_emergency_action(s
, system
, &v
);
1646 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1647 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
1648 : "Invalid %s setting: %s",
1651 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1653 unit_write_settingf(u
, flags
, name
,
1660 static int bus_set_transient_exit_status(
1664 sd_bus_message
*message
,
1665 UnitWriteFlags flags
,
1666 sd_bus_error
*error
) {
1673 r
= sd_bus_message_read(message
, "i", &k
);
1678 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
1680 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1681 *p
= k
< 0 ? -1 : k
;
1684 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1686 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
1692 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1693 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1695 static int bus_set_transient_conditions(
1700 sd_bus_message
*message
,
1701 UnitWriteFlags flags
,
1702 sd_bus_error
*error
) {
1704 const char *type_name
, *param
;
1705 int trigger
, negate
, r
;
1710 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1714 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1717 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1719 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1721 if (t
!= CONDITION_NULL
) {
1723 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1725 if (condition_takes_path(t
) && !path_is_absolute(param
))
1726 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1730 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1733 c
= condition_new(t
, param
, trigger
, negate
);
1737 LIST_PREPEND(conditions
, *list
, c
);
1739 if (t
!= CONDITION_NULL
)
1740 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1741 "%s=%s%s%s", type_name
,
1742 trigger
? "|" : "", negate
? "!" : "", param
);
1744 unit_write_settingf(u
, flags
, name
,
1745 "%s=%s%s", type_name
,
1746 trigger
? "|" : "", yes_no(!negate
));
1754 r
= sd_bus_message_exit_container(message
);
1758 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1759 *list
= condition_free_list(*list
);
1760 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1766 static int bus_unit_set_transient_property(
1769 sd_bus_message
*message
,
1770 UnitWriteFlags flags
,
1771 sd_bus_error
*error
) {
1773 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1780 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1781 * has been created. */
1783 if (streq(name
, "SourcePath"))
1784 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1786 if (streq(name
, "StopWhenUnneeded"))
1787 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1789 if (streq(name
, "RefuseManualStart"))
1790 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1792 if (streq(name
, "RefuseManualStop"))
1793 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1795 if (streq(name
, "AllowIsolate"))
1796 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1798 if (streq(name
, "DefaultDependencies"))
1799 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1801 if (streq(name
, "OnFailureJobMode"))
1802 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1804 if (streq(name
, "IgnoreOnIsolate"))
1805 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1807 if (streq(name
, "JobTimeoutUSec")) {
1808 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1809 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1810 u
->job_running_timeout
= u
->job_timeout
;
1813 if (streq(name
, "JobRunningTimeoutUSec")) {
1814 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1815 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1816 u
->job_running_timeout_set
= true;
1821 if (streq(name
, "JobTimeoutAction"))
1822 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1824 if (streq(name
, "JobTimeoutRebootArgument"))
1825 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1827 if (streq(name
, "StartLimitIntervalUSec"))
1828 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
1830 if (streq(name
, "StartLimitBurst"))
1831 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
1833 if (streq(name
, "StartLimitAction"))
1834 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1836 if (streq(name
, "FailureAction"))
1837 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1839 if (streq(name
, "SuccessAction"))
1840 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1842 if (streq(name
, "FailureActionExitStatus"))
1843 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
1845 if (streq(name
, "SuccessActionExitStatus"))
1846 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
1848 if (streq(name
, "RebootArgument"))
1849 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1851 if (streq(name
, "CollectMode"))
1852 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
1854 if (streq(name
, "Conditions"))
1855 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
1857 if (streq(name
, "Asserts"))
1858 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
1860 if (streq(name
, "Documentation")) {
1861 _cleanup_strv_free_
char **l
= NULL
;
1864 r
= sd_bus_message_read_strv(message
, &l
);
1868 STRV_FOREACH(p
, l
) {
1869 if (!documentation_url_is_valid(*p
))
1870 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
1873 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1874 if (strv_isempty(l
)) {
1875 u
->documentation
= strv_free(u
->documentation
);
1876 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1878 strv_extend_strv(&u
->documentation
, l
, false);
1881 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1887 } else if (streq(name
, "Slice")) {
1891 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1892 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1893 if (u
->type
== UNIT_SLICE
)
1894 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1895 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1896 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1898 r
= sd_bus_message_read(message
, "s", &s
);
1902 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1903 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1905 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1906 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1907 * instead of manager_load_unit() on purpose, here. */
1908 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1912 if (slice
->type
!= UNIT_SLICE
)
1913 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1915 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1916 r
= unit_set_slice(u
, slice
);
1920 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
1925 } else if (streq(name
, "RequiresMountsFor")) {
1926 _cleanup_strv_free_
char **l
= NULL
;
1929 r
= sd_bus_message_read_strv(message
, &l
);
1933 STRV_FOREACH(p
, l
) {
1934 path_simplify(*p
, true);
1936 if (!path_is_absolute(*p
))
1937 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
1939 if (!path_is_valid(*p
))
1940 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
1942 if (!path_is_normalized(*p
))
1943 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
1945 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1946 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
1948 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
1950 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1957 if (streq(name
, "RequiresOverridable"))
1958 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1959 else if (streq(name
, "RequisiteOverridable"))
1960 d
= UNIT_REQUISITE
; /* same here */
1962 d
= unit_dependency_from_string(name
);
1967 r
= sd_bus_message_enter_container(message
, 'a', "s");
1971 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1972 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1973 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1975 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1976 _cleanup_free_
char *label
= NULL
;
1978 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
1982 label
= strjoin(name
, "-", other
);
1986 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
1993 r
= sd_bus_message_exit_container(message
);
1999 } else if (streq(name
, "AddRef")) {
2003 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2004 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2005 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2006 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2007 * dependency type, hence let's not confuse things with that.
2009 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2010 * the transient unit is complete, so that setting this property multiple times in the same transient
2011 * unit creation call doesn't count as individual references. */
2013 r
= sd_bus_message_read(message
, "b", &b
);
2017 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2018 u
->bus_track_add
= b
;
2026 int bus_unit_set_properties(
2028 sd_bus_message
*message
,
2029 UnitWriteFlags flags
,
2031 sd_bus_error
*error
) {
2033 bool for_real
= false;
2040 /* We iterate through the array twice. First run we just check
2041 * if all passed data is valid, second run actually applies
2042 * it. This is to implement transaction-like behaviour without
2043 * actually providing full transactions. */
2045 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2053 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2057 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2060 /* Reached EOF. Let's try again, and this time for realz... */
2061 r
= sd_bus_message_rewind(message
, false);
2069 r
= sd_bus_message_read(message
, "s", &name
);
2073 if (!UNIT_VTABLE(u
)->bus_set_property
)
2074 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
2076 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2080 /* If not for real, then mask out the two target flags */
2081 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2083 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2084 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2085 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2087 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2092 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
2094 r
= sd_bus_message_exit_container(message
);
2098 r
= sd_bus_message_exit_container(message
);
2105 r
= sd_bus_message_exit_container(message
);
2109 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2110 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2115 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2118 /* Generates a pretty error if a unit isn't properly loaded. */
2120 switch (u
->load_state
) {
2125 case UNIT_NOT_FOUND
:
2126 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2128 case UNIT_BAD_SETTING
:
2129 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2131 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2132 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to load properly: %m.", u
->id
);
2135 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2140 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2144 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2150 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2152 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2154 unit_add_to_cgroup_empty_queue(u
);
2156 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2157 unit_add_to_gc_queue(u
);
2162 static int bus_unit_allocate_bus_track(Unit
*u
) {
2170 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2174 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2176 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2183 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2188 r
= bus_unit_allocate_bus_track(u
);
2192 return sd_bus_track_add_name(u
->bus_track
, name
);
2195 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2200 r
= bus_unit_allocate_bus_track(u
);
2204 return sd_bus_track_add_sender(u
->bus_track
, m
);
2207 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2210 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
2215 return sd_bus_track_remove_sender(u
->bus_track
, m
);