1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bpf-firewall.h"
7 #include "bus-common-errors.h"
8 #include "bus-polkit.h"
10 #include "cgroup-util.h"
11 #include "condition.h"
13 #include "dbus-unit.h"
14 #include "dbus-util.h"
18 #include "locale-util.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "selinux-access.h"
23 #include "signal-util.h"
25 #include "string-table.h"
26 #include "string-util.h"
28 #include "user-util.h"
31 static bool unit_can_start_refuse_manual(Unit
*u
) {
32 return unit_can_start(u
) && !u
->refuse_manual_start
;
35 static bool unit_can_stop_refuse_manual(Unit
*u
) {
36 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
39 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
40 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
46 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
47 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
48 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
49 static BUS_DEFINE_PROPERTY_GET2(property_get_freezer_state
, "s", Unit
, unit_freezer_state
, freezer_state_to_string
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
51 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
53 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
54 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
55 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
56 static BUS_DEFINE_PROPERTY_GET(property_get_can_freeze
, "b", Unit
, unit_can_freeze
);
57 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
58 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
60 static int property_get_can_clean(
63 const char *interface
,
65 sd_bus_message
*reply
,
67 sd_bus_error
*error
) {
76 r
= unit_can_clean(u
, &mask
);
80 r
= sd_bus_message_open_container(reply
, 'a', "s");
84 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
85 if (!FLAGS_SET(mask
, 1U << t
))
88 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
93 return sd_bus_message_close_container(reply
);
96 static int property_get_names(
99 const char *interface
,
100 const char *property
,
101 sd_bus_message
*reply
,
103 sd_bus_error
*error
) {
114 r
= sd_bus_message_open_container(reply
, 'a', "s");
118 SET_FOREACH(t
, *s
, i
) {
119 r
= sd_bus_message_append(reply
, "s", t
);
124 return sd_bus_message_close_container(reply
);
127 static int property_get_following(
130 const char *interface
,
131 const char *property
,
132 sd_bus_message
*reply
,
134 sd_bus_error
*error
) {
136 Unit
*u
= userdata
, *f
;
142 f
= unit_following(u
);
143 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
146 static int property_get_dependencies(
149 const char *interface
,
150 const char *property
,
151 sd_bus_message
*reply
,
153 sd_bus_error
*error
) {
155 Hashmap
**h
= userdata
;
165 r
= sd_bus_message_open_container(reply
, 'a', "s");
169 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
170 r
= sd_bus_message_append(reply
, "s", u
->id
);
175 return sd_bus_message_close_container(reply
);
178 static int property_get_requires_mounts_for(
181 const char *interface
,
182 const char *property
,
183 sd_bus_message
*reply
,
185 sd_bus_error
*error
) {
187 Hashmap
**h
= userdata
;
197 r
= sd_bus_message_open_container(reply
, 'a', "s");
201 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
202 r
= sd_bus_message_append(reply
, "s", p
);
207 return sd_bus_message_close_container(reply
);
210 static int property_get_unit_file_preset(
213 const char *interface
,
214 const char *property
,
215 sd_bus_message
*reply
,
217 sd_bus_error
*error
) {
226 r
= unit_get_unit_file_preset(u
);
228 return sd_bus_message_append(reply
, "s",
230 r
> 0 ? "enabled" : "disabled");
233 static int property_get_job(
236 const char *interface
,
237 const char *property
,
238 sd_bus_message
*reply
,
240 sd_bus_error
*error
) {
242 _cleanup_free_
char *p
= NULL
;
250 return sd_bus_message_append(reply
, "(uo)", 0, "/");
252 p
= job_dbus_path(*j
);
256 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
259 static int property_get_conditions(
262 const char *interface
,
263 const char *property
,
264 sd_bus_message
*reply
,
266 sd_bus_error
*error
) {
268 const char *(*to_string
)(ConditionType type
) = NULL
;
269 Condition
**list
= userdata
, *c
;
276 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
278 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
282 LIST_FOREACH(conditions
, c
, *list
) {
286 c
->result
== CONDITION_UNTESTED
? 0 :
287 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
289 r
= sd_bus_message_append(reply
, "(sbbsi)",
291 c
->trigger
, c
->negate
,
292 c
->parameter
, tristate
);
298 return sd_bus_message_close_container(reply
);
301 static int property_get_load_error(
304 const char *interface
,
305 const char *property
,
306 sd_bus_message
*reply
,
308 sd_bus_error
*error
) {
310 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
318 r
= bus_unit_validate_load_state(u
, &e
);
320 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
322 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
325 static int bus_verify_manage_units_async_full(
329 const char *polkit_message
,
331 sd_bus_message
*call
,
332 sd_bus_error
*error
) {
334 const char *details
[9] = {
339 if (polkit_message
) {
340 details
[4] = "polkit.message";
341 details
[5] = polkit_message
;
342 details
[6] = "polkit.gettext_domain";
343 details
[7] = GETTEXT_PACKAGE
;
346 return bus_verify_polkit_async(
349 "org.freedesktop.systemd1.manage-units",
353 &u
->manager
->polkit_registry
,
357 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
358 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
359 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
360 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
361 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
362 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
365 int bus_unit_method_start_generic(
366 sd_bus_message
*message
,
369 bool reload_if_possible
,
370 sd_bus_error
*error
) {
372 const char *smode
, *verb
;
378 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
380 r
= mac_selinux_unit_access_check(
382 job_type_to_access_method(job_type
),
387 r
= sd_bus_message_read(message
, "s", &smode
);
391 mode
= job_mode_from_string(smode
);
393 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
395 if (reload_if_possible
)
396 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
398 verb
= job_type_to_string(job_type
);
400 r
= bus_verify_manage_units_async_full(
404 polkit_message_for_job
[job_type
],
411 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
413 return bus_unit_queue_job(message
, u
, job_type
, mode
,
414 reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0, error
);
417 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
418 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
421 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
422 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
425 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
426 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
429 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
430 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
433 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
434 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
437 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
438 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
441 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
442 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
445 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
446 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
447 const char *jtype
, *smode
;
456 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
460 /* Parse the two magic reload types "reload-or-…" manually */
461 if (streq(jtype
, "reload-or-restart")) {
463 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
464 } else if (streq(jtype
, "reload-or-try-restart")) {
465 type
= JOB_TRY_RESTART
;
466 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
468 /* And the rest generically */
469 type
= job_type_from_string(jtype
);
471 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
474 mode
= job_mode_from_string(smode
);
476 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
478 r
= mac_selinux_unit_access_check(
480 job_type_to_access_method(type
),
485 r
= bus_verify_manage_units_async_full(
489 polkit_message_for_job
[type
],
496 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
498 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
501 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
511 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
515 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
522 who
= kill_who_from_string(swho
);
524 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
527 if (!SIGNAL_VALID(signo
))
528 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
530 r
= bus_verify_manage_units_async_full(
534 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
541 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
543 r
= unit_kill(u
, who
, signo
, error
);
547 return sd_bus_reply_method_return(message
, NULL
);
550 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
557 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
561 r
= bus_verify_manage_units_async_full(
565 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
572 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
574 unit_reset_failed(u
);
576 return sd_bus_reply_method_return(message
, NULL
);
579 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
586 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
590 r
= sd_bus_message_read(message
, "b", &runtime
);
594 r
= bus_verify_manage_units_async_full(
598 N_("Authentication is required to set properties on '$(unit)'."),
605 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
607 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
611 return sd_bus_reply_method_return(message
, NULL
);
614 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
621 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
625 r
= bus_verify_manage_units_async_full(
636 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
638 r
= bus_unit_track_add_sender(u
, message
);
642 return sd_bus_reply_method_return(message
, NULL
);
645 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
652 r
= bus_unit_track_remove_sender(u
, message
);
654 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
658 return sd_bus_reply_method_return(message
, NULL
);
661 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
662 ExecCleanMask mask
= 0;
669 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
673 r
= sd_bus_message_enter_container(message
, 'a', "s");
680 r
= sd_bus_message_read(message
, "s", &i
);
687 mask
|= EXEC_CLEAN_ALL
;
691 t
= exec_resource_type_from_string(i
);
693 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
699 r
= sd_bus_message_exit_container(message
);
703 r
= bus_verify_manage_units_async_full(
707 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
714 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
716 r
= unit_clean(u
, mask
);
717 if (r
== -EOPNOTSUPP
)
718 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not supporting cleaning.", u
->id
);
720 return sd_bus_error_setf(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
722 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
726 return sd_bus_reply_method_return(message
, NULL
);
729 static int bus_unit_method_freezer_generic(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
, FreezerAction action
) {
731 int (*method
)(Unit
*);
733 bool reply_no_delay
= false;
738 assert(IN_SET(action
, FREEZER_FREEZE
, FREEZER_THAW
));
740 if (action
== FREEZER_FREEZE
) {
742 method
= unit_freeze
;
748 r
= mac_selinux_unit_access_check(u
, message
, perm
, error
);
752 r
= bus_verify_manage_units_async_full(
756 N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
763 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
766 if (r
== -EOPNOTSUPP
)
767 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support freezing.", u
->id
);
769 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_BUSY
, "Unit has a pending job.");
771 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_INACTIVE
, "Unit is inactive.");
773 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Previously requested freezer operation for unit '%s' is still in progress.", u
->id
);
777 reply_no_delay
= true;
779 assert(!u
->pending_freezer_message
);
781 r
= sd_bus_message_new_method_return(message
, &u
->pending_freezer_message
);
785 if (reply_no_delay
) {
786 r
= bus_unit_send_pending_freezer_message(u
);
794 int bus_unit_method_thaw(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
795 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_THAW
);
798 int bus_unit_method_freeze(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
799 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_FREEZE
);
802 static int property_get_refs(
805 const char *interface
,
806 const char *property
,
807 sd_bus_message
*reply
,
809 sd_bus_error
*error
) {
818 r
= sd_bus_message_open_container(reply
, 'a', "s");
822 for (i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
825 c
= sd_bus_track_count_name(u
->bus_track
, i
);
829 /* Add the item multiple times if the ref count for each is above 1 */
830 for (k
= 0; k
< c
; k
++) {
831 r
= sd_bus_message_append(reply
, "s", i
);
837 return sd_bus_message_close_container(reply
);
840 const sd_bus_vtable bus_unit_vtable
[] = {
841 SD_BUS_VTABLE_START(0),
843 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
844 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
845 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
846 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
847 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
848 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
849 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
850 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
851 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
852 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
853 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
854 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
855 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
856 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
857 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
858 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
859 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
860 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
861 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
862 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
863 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
864 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
865 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
866 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
867 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
868 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
869 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
870 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
871 SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
872 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
873 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
874 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
875 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
876 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
877 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
878 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
879 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
880 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
881 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
882 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
883 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
884 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
885 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
886 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
887 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
888 SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
889 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
890 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
891 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
892 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
893 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
894 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
895 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
896 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
897 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
898 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
899 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
900 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
901 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
902 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
903 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
904 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
905 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
906 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
907 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
908 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
909 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
910 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
911 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
912 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
913 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
914 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
915 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
916 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
917 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
918 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
919 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
920 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
921 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
923 SD_BUS_METHOD_WITH_NAMES("Start",
929 SD_BUS_VTABLE_UNPRIVILEGED
),
930 SD_BUS_METHOD_WITH_NAMES("Stop",
936 SD_BUS_VTABLE_UNPRIVILEGED
),
937 SD_BUS_METHOD_WITH_NAMES("Reload",
943 SD_BUS_VTABLE_UNPRIVILEGED
),
944 SD_BUS_METHOD_WITH_NAMES("Restart",
950 SD_BUS_VTABLE_UNPRIVILEGED
),
951 SD_BUS_METHOD_WITH_NAMES("TryRestart",
957 SD_BUS_VTABLE_UNPRIVILEGED
),
958 SD_BUS_METHOD_WITH_NAMES("ReloadOrRestart",
963 method_reload_or_restart
,
964 SD_BUS_VTABLE_UNPRIVILEGED
),
965 SD_BUS_METHOD_WITH_NAMES("ReloadOrTryRestart",
970 method_reload_or_try_restart
,
971 SD_BUS_VTABLE_UNPRIVILEGED
),
972 SD_BUS_METHOD_WITH_NAMES("EnqueueJob",
974 SD_BUS_PARAM(job_type
)
975 SD_BUS_PARAM(job_mode
),
978 SD_BUS_PARAM(job_path
)
979 SD_BUS_PARAM(unit_id
)
980 SD_BUS_PARAM(unit_path
)
981 SD_BUS_PARAM(job_type
)
982 SD_BUS_PARAM(affected_jobs
),
983 bus_unit_method_enqueue_job
,
984 SD_BUS_VTABLE_UNPRIVILEGED
),
985 SD_BUS_METHOD_WITH_NAMES("Kill",
988 SD_BUS_PARAM(signal
),
990 bus_unit_method_kill
,
991 SD_BUS_VTABLE_UNPRIVILEGED
),
992 SD_BUS_METHOD("ResetFailed",
995 bus_unit_method_reset_failed
,
996 SD_BUS_VTABLE_UNPRIVILEGED
),
997 SD_BUS_METHOD_WITH_NAMES("SetProperties",
999 SD_BUS_PARAM(runtime
)
1000 SD_BUS_PARAM(properties
),
1002 bus_unit_method_set_properties
,
1003 SD_BUS_VTABLE_UNPRIVILEGED
),
1004 SD_BUS_METHOD("Ref",
1007 bus_unit_method_ref
,
1008 SD_BUS_VTABLE_UNPRIVILEGED
),
1009 SD_BUS_METHOD("Unref",
1012 bus_unit_method_unref
,
1013 SD_BUS_VTABLE_UNPRIVILEGED
),
1014 SD_BUS_METHOD_WITH_NAMES("Clean",
1018 bus_unit_method_clean
,
1019 SD_BUS_VTABLE_UNPRIVILEGED
),
1020 SD_BUS_METHOD("Freeze",
1023 bus_unit_method_freeze
,
1024 SD_BUS_VTABLE_UNPRIVILEGED
),
1025 SD_BUS_METHOD("Thaw",
1028 bus_unit_method_thaw
,
1029 SD_BUS_VTABLE_UNPRIVILEGED
),
1031 /* For dependency types we don't support anymore always return an empty array */
1032 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1033 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1034 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1035 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1036 /* Obsolete alias names */
1037 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1038 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1043 static int property_get_slice(
1046 const char *interface
,
1047 const char *property
,
1048 sd_bus_message
*reply
,
1050 sd_bus_error
*error
) {
1058 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
1061 static int property_get_current_memory(
1064 const char *interface
,
1065 const char *property
,
1066 sd_bus_message
*reply
,
1068 sd_bus_error
*error
) {
1070 uint64_t sz
= (uint64_t) -1;
1078 r
= unit_get_memory_current(u
, &sz
);
1079 if (r
< 0 && r
!= -ENODATA
)
1080 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
1082 return sd_bus_message_append(reply
, "t", sz
);
1085 static int property_get_current_tasks(
1088 const char *interface
,
1089 const char *property
,
1090 sd_bus_message
*reply
,
1092 sd_bus_error
*error
) {
1094 uint64_t cn
= (uint64_t) -1;
1102 r
= unit_get_tasks_current(u
, &cn
);
1103 if (r
< 0 && r
!= -ENODATA
)
1104 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
1106 return sd_bus_message_append(reply
, "t", cn
);
1109 static int property_get_cpu_usage(
1112 const char *interface
,
1113 const char *property
,
1114 sd_bus_message
*reply
,
1116 sd_bus_error
*error
) {
1118 nsec_t ns
= (nsec_t
) -1;
1126 r
= unit_get_cpu_usage(u
, &ns
);
1127 if (r
< 0 && r
!= -ENODATA
)
1128 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
1130 return sd_bus_message_append(reply
, "t", ns
);
1133 static int property_get_cpuset_cpus(
1136 const char *interface
,
1137 const char *property
,
1138 sd_bus_message
*reply
,
1140 sd_bus_error
*error
) {
1143 _cleanup_(cpu_set_reset
) CPUSet cpus
= {};
1144 _cleanup_free_
uint8_t *array
= NULL
;
1151 (void) unit_get_cpuset(u
, &cpus
, "cpuset.cpus.effective");
1152 (void) cpu_set_to_dbus(&cpus
, &array
, &allocated
);
1153 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1156 static int property_get_cpuset_mems(
1159 const char *interface
,
1160 const char *property
,
1161 sd_bus_message
*reply
,
1163 sd_bus_error
*error
) {
1166 _cleanup_(cpu_set_reset
) CPUSet mems
= {};
1167 _cleanup_free_
uint8_t *array
= NULL
;
1174 (void) unit_get_cpuset(u
, &mems
, "cpuset.mems.effective");
1175 (void) cpu_set_to_dbus(&mems
, &array
, &allocated
);
1176 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1179 static int property_get_cgroup(
1182 const char *interface
,
1183 const char *property
,
1184 sd_bus_message
*reply
,
1186 sd_bus_error
*error
) {
1189 const char *t
= NULL
;
1195 /* Three cases: a) u->cgroup_path is NULL, in which case the
1196 * unit has no control group, which we report as the empty
1197 * string. b) u->cgroup_path is the empty string, which
1198 * indicates the root cgroup, which we report as "/". c) all
1199 * other cases we report as-is. */
1202 t
= empty_to_root(u
->cgroup_path
);
1204 return sd_bus_message_append(reply
, "s", t
);
1207 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
1208 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
1214 r
= set_put(pids
, PID_TO_PTR(pid
));
1215 if (IN_SET(r
, 0, -EEXIST
))
1221 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1230 (void) get_process_cmdline(pid
, SIZE_MAX
, PROCESS_CMDLINE_COMM_FALLBACK
, &cmdline
);
1232 return sd_bus_message_append(reply
,
1239 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1240 _cleanup_closedir_
DIR *d
= NULL
;
1241 _cleanup_fclose_
FILE *f
= NULL
;
1247 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1256 r
= cg_read_pid(f
, &pid
);
1262 if (is_kernel_thread(pid
) > 0)
1265 r
= append_process(reply
, p
, pid
, pids
);
1270 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1277 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1279 r
= cg_read_subgroup(d
, &g
);
1285 j
= path_join(empty_to_root(p
), g
);
1289 r
= append_cgroup(reply
, j
, pids
);
1297 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1298 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1299 _cleanup_set_free_ Set
*pids
= NULL
;
1306 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1310 pids
= set_new(NULL
);
1314 r
= sd_bus_message_new_method_return(message
, &reply
);
1318 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1322 if (u
->cgroup_path
) {
1323 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1328 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1329 pid
= unit_main_pid(u
);
1331 r
= append_process(reply
, NULL
, pid
, pids
);
1336 pid
= unit_control_pid(u
);
1338 r
= append_process(reply
, NULL
, pid
, pids
);
1343 r
= sd_bus_message_close_container(reply
);
1347 return sd_bus_send(NULL
, reply
, NULL
);
1350 static int property_get_ip_counter(
1353 const char *interface
,
1354 const char *property
,
1355 sd_bus_message
*reply
,
1357 sd_bus_error
*error
) {
1359 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1360 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1361 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1362 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1363 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1366 uint64_t value
= UINT64_MAX
;
1375 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1376 (void) unit_get_ip_accounting(u
, metric
, &value
);
1377 return sd_bus_message_append(reply
, "t", value
);
1380 static int property_get_io_counter(
1383 const char *interface
,
1384 const char *property
,
1385 sd_bus_message
*reply
,
1387 sd_bus_error
*error
) {
1389 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1390 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1391 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1392 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1393 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1396 uint64_t value
= UINT64_MAX
;
1405 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1406 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1407 return sd_bus_message_append(reply
, "t", value
);
1410 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1412 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1413 _cleanup_set_free_ Set
*pids
= NULL
;
1420 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1421 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1422 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1423 * subcgroup path has no effect! */
1425 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1429 r
= sd_bus_message_read(message
, "s", &path
);
1433 path
= empty_to_null(path
);
1435 if (!path_is_absolute(path
))
1436 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1438 if (!path_is_normalized(path
))
1439 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1442 if (!unit_cgroup_delegate(u
))
1443 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1445 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1446 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1448 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1452 r
= sd_bus_message_enter_container(message
, 'a', "u");
1456 uid_t process_uid
, sender_uid
;
1460 r
= sd_bus_message_read(message
, "u", &upid
);
1467 r
= sd_bus_creds_get_pid(creds
, &pid
);
1473 /* Filter out duplicates */
1474 if (set_contains(pids
, PID_TO_PTR(pid
)))
1477 /* Check if this process is suitable for attaching to this unit */
1478 r
= unit_pid_attachable(u
, pid
, error
);
1482 /* Let's query the sender's UID, so that we can make our security decisions */
1483 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1487 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1488 * then the process' UID and the target unit's UID have to match the sender's UID */
1489 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1490 r
= get_process_uid(pid
, &process_uid
);
1492 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1494 if (process_uid
!= sender_uid
)
1495 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1496 if (process_uid
!= u
->ref_uid
)
1497 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1501 pids
= set_new(NULL
);
1506 r
= set_put(pids
, PID_TO_PTR(pid
));
1511 r
= sd_bus_message_exit_container(message
);
1515 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1517 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1519 return sd_bus_reply_method_return(message
, NULL
);
1522 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1523 SD_BUS_VTABLE_START(0),
1524 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1525 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1526 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1527 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1528 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus
, 0, 0),
1529 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems
, 0, 0),
1530 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1531 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1532 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1533 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1534 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1535 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1536 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1537 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1538 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1540 SD_BUS_METHOD_WITH_NAMES("GetProcesses",
1543 SD_BUS_PARAM(processes
),
1544 bus_unit_method_get_processes
,
1545 SD_BUS_VTABLE_UNPRIVILEGED
),
1547 SD_BUS_METHOD_WITH_NAMES("AttachProcesses",
1549 SD_BUS_PARAM(subcgroup
)
1552 bus_unit_method_attach_processes
,
1553 SD_BUS_VTABLE_UNPRIVILEGED
),
1558 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1559 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1560 _cleanup_free_
char *p
= NULL
;
1567 p
= unit_dbus_path(u
);
1571 r
= sd_bus_message_new_signal(
1574 "/org/freedesktop/systemd1",
1575 "org.freedesktop.systemd1.Manager",
1580 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1584 return sd_bus_send(bus
, m
, NULL
);
1587 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1588 _cleanup_free_
char *p
= NULL
;
1595 p
= unit_dbus_path(u
);
1599 /* Send a properties changed signal. First for the specific
1600 * type, then for the generic unit. The clients may rely on
1601 * this order to get atomic behavior if needed. */
1603 r
= sd_bus_emit_properties_changed_strv(
1605 unit_dbus_interface_from_type(u
->type
),
1610 return sd_bus_emit_properties_changed_strv(
1612 "org.freedesktop.systemd1.Unit",
1616 void bus_unit_send_change_signal(Unit
*u
) {
1620 if (u
->in_dbus_queue
) {
1621 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1622 u
->in_dbus_queue
= false;
1628 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1630 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1632 u
->sent_dbus_new_signal
= true;
1635 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1637 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1638 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1639 * so that clients can follow the full state transition */
1641 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1644 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1645 * the unit appears in the new state right-away (except if the
1646 * caller explicitly asked us to send it anyway) */
1649 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1650 * when we are reloading. */
1653 bus_unit_send_change_signal(u
);
1656 int bus_unit_send_pending_freezer_message(Unit
*u
) {
1661 if (!u
->pending_freezer_message
)
1664 r
= sd_bus_send(NULL
, u
->pending_freezer_message
, NULL
);
1666 log_warning_errno(r
, "Failed to send queued message, ignoring: %m");
1668 u
->pending_freezer_message
= sd_bus_message_unref(u
->pending_freezer_message
);
1673 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1674 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1675 _cleanup_free_
char *p
= NULL
;
1682 p
= unit_dbus_path(u
);
1686 r
= sd_bus_message_new_signal(
1689 "/org/freedesktop/systemd1",
1690 "org.freedesktop.systemd1.Manager",
1695 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1699 return sd_bus_send(bus
, m
, NULL
);
1702 void bus_unit_send_removed_signal(Unit
*u
) {
1706 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1707 bus_unit_send_change_signal(u
);
1712 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1714 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1717 int bus_unit_queue_job(
1718 sd_bus_message
*message
,
1722 BusUnitQueueFlags flags
,
1723 sd_bus_error
*error
) {
1725 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1726 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1727 _cleanup_set_free_ Set
*affected
= NULL
;
1734 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1735 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1737 r
= mac_selinux_unit_access_check(
1739 job_type_to_access_method(type
),
1744 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1745 if (type
== JOB_RESTART
)
1746 type
= JOB_RELOAD_OR_START
;
1747 else if (type
== JOB_TRY_RESTART
)
1748 type
= JOB_TRY_RELOAD
;
1751 if (type
== JOB_STOP
&&
1752 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1753 unit_active_state(u
) == UNIT_INACTIVE
)
1754 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1756 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1757 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1758 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1759 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1760 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
);
1762 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1763 affected
= set_new(NULL
);
1768 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1772 r
= bus_job_track_sender(j
, message
);
1776 /* Before we send the method reply, force out the announcement JobNew for this job */
1777 bus_job_send_pending_change_signal(j
, true);
1779 job_path
= job_dbus_path(j
);
1783 /* The classic response is just a job object path */
1784 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1785 return sd_bus_reply_method_return(message
, "o", job_path
);
1787 /* In verbose mode respond with the anchor job plus everything that has been affected */
1788 r
= sd_bus_message_new_method_return(message
, &reply
);
1792 unit_path
= unit_dbus_path(j
->unit
);
1796 r
= sd_bus_message_append(reply
, "uosos",
1798 j
->unit
->id
, unit_path
,
1799 job_type_to_string(j
->type
));
1803 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1807 SET_FOREACH(a
, affected
, i
) {
1812 /* Free paths from previous iteration */
1813 job_path
= mfree(job_path
);
1814 unit_path
= mfree(unit_path
);
1816 job_path
= job_dbus_path(a
);
1820 unit_path
= unit_dbus_path(a
->unit
);
1824 r
= sd_bus_message_append(reply
, "(uosos)",
1826 a
->unit
->id
, unit_path
,
1827 job_type_to_string(a
->type
));
1832 r
= sd_bus_message_close_container(reply
);
1836 return sd_bus_send(NULL
, reply
, NULL
);
1839 static int bus_unit_set_live_property(
1842 sd_bus_message
*message
,
1843 UnitWriteFlags flags
,
1844 sd_bus_error
*error
) {
1852 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1853 * units that are being created). */
1855 if (streq(name
, "Description")) {
1858 r
= sd_bus_message_read(message
, "s", &d
);
1862 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1863 r
= unit_set_description(u
, d
);
1867 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1876 static int bus_set_transient_emergency_action(
1880 sd_bus_message
*message
,
1881 UnitWriteFlags flags
,
1882 sd_bus_error
*error
) {
1891 r
= sd_bus_message_read(message
, "s", &s
);
1895 system
= MANAGER_IS_SYSTEM(u
->manager
);
1896 r
= parse_emergency_action(s
, system
, &v
);
1898 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1899 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
1900 : "Invalid %s setting: %s",
1903 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1905 unit_write_settingf(u
, flags
, name
,
1912 static int bus_set_transient_exit_status(
1916 sd_bus_message
*message
,
1917 UnitWriteFlags flags
,
1918 sd_bus_error
*error
) {
1925 r
= sd_bus_message_read(message
, "i", &k
);
1930 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
1932 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1933 *p
= k
< 0 ? -1 : k
;
1936 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1938 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
1944 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1945 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1947 static int bus_set_transient_conditions(
1952 sd_bus_message
*message
,
1953 UnitWriteFlags flags
,
1954 sd_bus_error
*error
) {
1956 const char *type_name
, *param
;
1957 int trigger
, negate
, r
;
1962 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1966 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1969 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1971 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1973 if (t
!= CONDITION_NULL
) {
1975 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1977 if (condition_takes_path(t
) && !path_is_absolute(param
))
1978 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1982 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1985 c
= condition_new(t
, param
, trigger
, negate
);
1989 LIST_PREPEND(conditions
, *list
, c
);
1991 if (t
!= CONDITION_NULL
)
1992 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1993 "%s=%s%s%s", type_name
,
1994 trigger
? "|" : "", negate
? "!" : "", param
);
1996 unit_write_settingf(u
, flags
, name
,
1997 "%s=%s%s", type_name
,
1998 trigger
? "|" : "", yes_no(!negate
));
2006 r
= sd_bus_message_exit_container(message
);
2010 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
2011 *list
= condition_free_list(*list
);
2012 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
2018 static int bus_unit_set_transient_property(
2021 sd_bus_message
*message
,
2022 UnitWriteFlags flags
,
2023 sd_bus_error
*error
) {
2025 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
2032 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
2033 * has been created. */
2035 if (streq(name
, "SourcePath"))
2036 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
2038 if (streq(name
, "StopWhenUnneeded"))
2039 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
2041 if (streq(name
, "RefuseManualStart"))
2042 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
2044 if (streq(name
, "RefuseManualStop"))
2045 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
2047 if (streq(name
, "AllowIsolate"))
2048 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
2050 if (streq(name
, "DefaultDependencies"))
2051 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
2053 if (streq(name
, "OnFailureJobMode"))
2054 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
2056 if (streq(name
, "IgnoreOnIsolate"))
2057 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
2059 if (streq(name
, "JobTimeoutUSec")) {
2060 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
2061 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
2062 u
->job_running_timeout
= u
->job_timeout
;
2065 if (streq(name
, "JobRunningTimeoutUSec")) {
2066 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
2067 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
2068 u
->job_running_timeout_set
= true;
2073 if (streq(name
, "JobTimeoutAction"))
2074 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
2076 if (streq(name
, "JobTimeoutRebootArgument"))
2077 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
2079 if (streq(name
, "StartLimitIntervalUSec"))
2080 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
2082 if (streq(name
, "StartLimitBurst"))
2083 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
2085 if (streq(name
, "StartLimitAction"))
2086 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
2088 if (streq(name
, "FailureAction"))
2089 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
2091 if (streq(name
, "SuccessAction"))
2092 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
2094 if (streq(name
, "FailureActionExitStatus"))
2095 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
2097 if (streq(name
, "SuccessActionExitStatus"))
2098 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
2100 if (streq(name
, "RebootArgument"))
2101 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
2103 if (streq(name
, "CollectMode"))
2104 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
2106 if (streq(name
, "Conditions"))
2107 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
2109 if (streq(name
, "Asserts"))
2110 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
2112 if (streq(name
, "Documentation")) {
2113 _cleanup_strv_free_
char **l
= NULL
;
2116 r
= sd_bus_message_read_strv(message
, &l
);
2120 STRV_FOREACH(p
, l
) {
2121 if (!documentation_url_is_valid(*p
))
2122 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
2125 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2126 if (strv_isempty(l
)) {
2127 u
->documentation
= strv_free(u
->documentation
);
2128 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2130 strv_extend_strv(&u
->documentation
, l
, false);
2133 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2139 } else if (streq(name
, "Slice")) {
2143 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
2144 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
2145 if (u
->type
== UNIT_SLICE
)
2146 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
2147 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
2148 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
2150 r
= sd_bus_message_read(message
, "s", &s
);
2154 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
2155 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
2157 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2158 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2159 * instead of manager_load_unit() on purpose, here. */
2160 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
2164 if (slice
->type
!= UNIT_SLICE
)
2165 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
2167 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2168 r
= unit_set_slice(u
, slice
);
2172 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
2177 } else if (streq(name
, "RequiresMountsFor")) {
2178 _cleanup_strv_free_
char **l
= NULL
;
2181 r
= sd_bus_message_read_strv(message
, &l
);
2185 STRV_FOREACH(p
, l
) {
2186 path_simplify(*p
, true);
2188 if (!path_is_absolute(*p
))
2189 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
2191 if (!path_is_valid(*p
))
2192 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
2194 if (!path_is_normalized(*p
))
2195 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
2197 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2198 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
2200 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
2202 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2209 if (streq(name
, "RequiresOverridable"))
2210 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
2211 else if (streq(name
, "RequisiteOverridable"))
2212 d
= UNIT_REQUISITE
; /* same here */
2214 d
= unit_dependency_from_string(name
);
2229 UNIT_PROPAGATES_RELOAD_TO
,
2230 UNIT_RELOAD_PROPAGATED_FROM
,
2231 UNIT_JOINS_NAMESPACE_OF
))
2232 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d
));
2234 r
= sd_bus_message_enter_container(message
, 'a', "s");
2238 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
2239 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
2240 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
2242 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2243 _cleanup_free_
char *label
= NULL
;
2245 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
2249 label
= strjoin(name
, "-", other
);
2253 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
2260 r
= sd_bus_message_exit_container(message
);
2266 } else if (streq(name
, "AddRef")) {
2270 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2271 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2272 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2273 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2274 * dependency type, hence let's not confuse things with that.
2276 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2277 * the transient unit is complete, so that setting this property multiple times in the same transient
2278 * unit creation call doesn't count as individual references. */
2280 r
= sd_bus_message_read(message
, "b", &b
);
2284 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2285 u
->bus_track_add
= b
;
2293 int bus_unit_set_properties(
2295 sd_bus_message
*message
,
2296 UnitWriteFlags flags
,
2298 sd_bus_error
*error
) {
2300 bool for_real
= false;
2307 /* We iterate through the array twice. First run we just check
2308 * if all passed data is valid, second run actually applies
2309 * it. This is to implement transaction-like behaviour without
2310 * actually providing full transactions. */
2312 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2320 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2324 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2327 /* Reached EOF. Let's try again, and this time for realz... */
2328 r
= sd_bus_message_rewind(message
, false);
2336 r
= sd_bus_message_read(message
, "s", &name
);
2340 if (!UNIT_VTABLE(u
)->bus_set_property
)
2341 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
2343 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2347 /* If not for real, then mask out the two target flags */
2348 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2350 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2351 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2352 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2354 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2359 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
2361 r
= sd_bus_message_exit_container(message
);
2365 r
= sd_bus_message_exit_container(message
);
2372 r
= sd_bus_message_exit_container(message
);
2376 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2377 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2382 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2385 /* Generates a pretty error if a unit isn't properly loaded. */
2387 switch (u
->load_state
) {
2392 case UNIT_NOT_FOUND
:
2393 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2395 case UNIT_BAD_SETTING
:
2396 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2398 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2399 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to load properly: %m.", u
->id
);
2402 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2407 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2411 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2417 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2419 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2421 unit_add_to_cgroup_empty_queue(u
);
2423 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2424 unit_add_to_gc_queue(u
);
2429 static int bus_unit_allocate_bus_track(Unit
*u
) {
2437 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2441 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2443 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2450 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2455 r
= bus_unit_allocate_bus_track(u
);
2459 return sd_bus_track_add_name(u
->bus_track
, name
);
2462 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2467 r
= bus_unit_allocate_bus_track(u
);
2471 return sd_bus_track_add_sender(u
->bus_track
, m
);
2474 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2477 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
2482 return sd_bus_track_remove_sender(u
->bus_track
, m
);