1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "bpf-firewall.h"
7 #include "bus-common-errors.h"
8 #include "bus-get-properties.h"
9 #include "bus-polkit.h"
10 #include "cgroup-util.h"
11 #include "condition.h"
13 #include "dbus-manager.h"
14 #include "dbus-unit.h"
15 #include "dbus-util.h"
19 #include "locale-util.h"
21 #include "path-util.h"
22 #include "process-util.h"
23 #include "selinux-access.h"
24 #include "signal-util.h"
26 #include "string-table.h"
27 #include "string-util.h"
29 #include "user-util.h"
32 static bool unit_can_start_refuse_manual(Unit
*u
) {
33 return unit_can_start(u
) && !u
->refuse_manual_start
;
36 static bool unit_can_stop_refuse_manual(Unit
*u
) {
37 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
40 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
41 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
46 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
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
) {
105 Unit
*u
= ASSERT_PTR(userdata
);
112 r
= sd_bus_message_open_container(reply
, 'a', "s");
116 r
= sd_bus_message_append(reply
, "s", u
->id
);
120 SET_FOREACH(t
, u
->aliases
) {
121 r
= sd_bus_message_append(reply
, "s", t
);
126 return sd_bus_message_close_container(reply
);
129 static int property_get_following(
132 const char *interface
,
133 const char *property
,
134 sd_bus_message
*reply
,
136 sd_bus_error
*error
) {
138 Unit
*u
= userdata
, *f
;
144 f
= unit_following(u
);
145 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
148 static int property_get_dependencies(
151 const char *interface
,
152 const char *property
,
153 sd_bus_message
*reply
,
155 sd_bus_error
*error
) {
157 Unit
*u
= userdata
, *other
;
167 d
= unit_dependency_from_string(property
);
170 deps
= unit_get_dependencies(u
, d
);
172 r
= sd_bus_message_open_container(reply
, 'a', "s");
176 HASHMAP_FOREACH_KEY(v
, other
, deps
) {
177 r
= sd_bus_message_append(reply
, "s", other
->id
);
182 return sd_bus_message_close_container(reply
);
185 static int property_get_requires_mounts_for(
188 const char *interface
,
189 const char *property
,
190 sd_bus_message
*reply
,
192 sd_bus_error
*error
) {
194 Hashmap
**h
= ASSERT_PTR(userdata
);
202 r
= sd_bus_message_open_container(reply
, 'a', "s");
206 HASHMAP_FOREACH_KEY(v
, p
, *h
) {
207 r
= sd_bus_message_append(reply
, "s", p
);
212 return sd_bus_message_close_container(reply
);
215 static int property_get_unit_file_preset(
218 const char *interface
,
219 const char *property
,
220 sd_bus_message
*reply
,
222 sd_bus_error
*error
) {
224 Unit
*u
= ASSERT_PTR(userdata
);
230 r
= unit_get_unit_file_preset(u
);
232 return sd_bus_message_append(reply
, "s",
234 r
> 0 ? "enabled" : "disabled");
237 static int property_get_job(
240 const char *interface
,
241 const char *property
,
242 sd_bus_message
*reply
,
244 sd_bus_error
*error
) {
246 _cleanup_free_
char *p
= NULL
;
247 Job
**j
= ASSERT_PTR(userdata
);
253 return sd_bus_message_append(reply
, "(uo)", 0, "/");
255 p
= job_dbus_path(*j
);
259 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
262 static int property_get_conditions(
265 const char *interface
,
266 const char *property
,
267 sd_bus_message
*reply
,
269 sd_bus_error
*error
) {
271 const char *(*to_string
)(ConditionType type
) = NULL
;
272 Condition
**list
= ASSERT_PTR(userdata
);
278 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
280 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
284 LIST_FOREACH(conditions
, c
, *list
) {
288 c
->result
== CONDITION_UNTESTED
? 0 :
289 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
291 r
= sd_bus_message_append(reply
, "(sbbsi)",
293 c
->trigger
, c
->negate
,
294 c
->parameter
, tristate
);
300 return sd_bus_message_close_container(reply
);
303 static int property_get_load_error(
306 const char *interface
,
307 const char *property
,
308 sd_bus_message
*reply
,
310 sd_bus_error
*error
) {
312 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
313 Unit
*u
= ASSERT_PTR(userdata
);
319 r
= bus_unit_validate_load_state(u
, &e
);
321 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
323 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
326 static int property_get_markers(
329 const char *interface
,
330 const char *property
,
331 sd_bus_message
*reply
,
333 sd_bus_error
*error
) {
335 unsigned *markers
= ASSERT_PTR(userdata
);
341 r
= sd_bus_message_open_container(reply
, 'a', "s");
345 /* Make sure out values fit in the bitfield. */
346 assert_cc(_UNIT_MARKER_MAX
<= sizeof(((Unit
){}).markers
) * 8);
348 for (UnitMarker m
= 0; m
< _UNIT_MARKER_MAX
; m
++)
349 if (FLAGS_SET(*markers
, 1u << m
)) {
350 r
= sd_bus_message_append(reply
, "s", unit_marker_to_string(m
));
355 return sd_bus_message_close_container(reply
);
358 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
359 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
360 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
361 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
362 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
363 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
366 int bus_unit_method_start_generic(
367 sd_bus_message
*message
,
370 bool reload_if_possible
,
371 sd_bus_error
*error
) {
373 BusUnitQueueFlags job_flags
= reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0;
374 const char *smode
, *verb
;
380 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
382 r
= mac_selinux_unit_access_check(
384 job_type_to_access_method(job_type
),
389 r
= sd_bus_message_read(message
, "s", &smode
);
393 mode
= job_mode_from_string(smode
);
395 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
397 if (reload_if_possible
)
398 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
400 verb
= job_type_to_string(job_type
);
402 if (sd_bus_message_is_method_call(message
, NULL
, "StartUnitWithFlags")) {
403 uint64_t input_flags
= 0;
405 r
= sd_bus_message_read(message
, "t", &input_flags
);
408 /* Let clients know that this version doesn't support any flags at the moment. */
409 if (input_flags
!= 0)
410 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
411 "Invalid 'flags' parameter '%" PRIu64
"'",
415 r
= bus_verify_manage_units_async_full(
419 polkit_message_for_job
[job_type
],
426 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
428 return bus_unit_queue_job(message
, u
, job_type
, mode
, job_flags
, error
);
431 static int bus_unit_method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
432 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
435 static int bus_unit_method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
436 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
439 static int bus_unit_method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
440 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
443 static int bus_unit_method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
444 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
447 static int bus_unit_method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
448 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
451 static int bus_unit_method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
452 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
455 static int bus_unit_method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
456 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
459 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
460 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
461 const char *jtype
, *smode
;
462 Unit
*u
= ASSERT_PTR(userdata
);
469 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
473 /* Parse the two magic reload types "reload-or-…" manually */
474 if (streq(jtype
, "reload-or-restart")) {
476 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
477 } else if (streq(jtype
, "reload-or-try-restart")) {
478 type
= JOB_TRY_RESTART
;
479 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
481 /* And the rest generically */
482 type
= job_type_from_string(jtype
);
484 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
487 mode
= job_mode_from_string(smode
);
489 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
491 r
= mac_selinux_unit_access_check(
493 job_type_to_access_method(type
),
498 r
= bus_verify_manage_units_async_full(
502 polkit_message_for_job
[type
],
509 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
511 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
514 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
515 Unit
*u
= ASSERT_PTR(userdata
);
523 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
527 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
534 who
= kill_who_from_string(swho
);
536 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
539 if (!SIGNAL_VALID(signo
))
540 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
542 r
= bus_verify_manage_units_async_full(
546 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
553 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
555 r
= unit_kill(u
, who
, signo
, error
);
559 return sd_bus_reply_method_return(message
, NULL
);
562 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
563 Unit
*u
= ASSERT_PTR(userdata
);
568 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
572 r
= bus_verify_manage_units_async_full(
576 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
583 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
585 unit_reset_failed(u
);
587 return sd_bus_reply_method_return(message
, NULL
);
590 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
591 Unit
*u
= ASSERT_PTR(userdata
);
596 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
600 r
= sd_bus_message_read(message
, "b", &runtime
);
604 r
= bus_verify_manage_units_async_full(
608 N_("Authentication is required to set properties on '$(unit)'."),
615 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
617 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
621 return sd_bus_reply_method_return(message
, NULL
);
624 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
625 Unit
*u
= ASSERT_PTR(userdata
);
630 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
634 r
= bus_verify_manage_units_async_full(
645 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
647 r
= bus_unit_track_add_sender(u
, message
);
651 return sd_bus_reply_method_return(message
, NULL
);
654 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
655 Unit
*u
= ASSERT_PTR(userdata
);
660 r
= bus_unit_track_remove_sender(u
, message
);
662 return sd_bus_error_set(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
666 return sd_bus_reply_method_return(message
, NULL
);
669 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
670 ExecCleanMask mask
= 0;
671 Unit
*u
= ASSERT_PTR(userdata
);
676 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
680 r
= sd_bus_message_enter_container(message
, 'a', "s");
687 r
= sd_bus_message_read(message
, "s", &i
);
694 mask
|= EXEC_CLEAN_ALL
;
698 t
= exec_resource_type_from_string(i
);
700 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
706 r
= sd_bus_message_exit_container(message
);
710 r
= bus_verify_manage_units_async_full(
714 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
721 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
723 r
= unit_clean(u
, mask
);
724 if (r
== -EOPNOTSUPP
)
725 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support cleaning.", u
->id
);
727 return sd_bus_error_set(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
729 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
733 return sd_bus_reply_method_return(message
, NULL
);
736 static int bus_unit_method_freezer_generic(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
, FreezerAction action
) {
738 int (*method
)(Unit
*);
739 Unit
*u
= ASSERT_PTR(userdata
);
740 bool reply_no_delay
= false;
744 assert(IN_SET(action
, FREEZER_FREEZE
, FREEZER_THAW
));
746 if (action
== FREEZER_FREEZE
) {
748 method
= unit_freeze
;
754 r
= mac_selinux_unit_access_check(u
, message
, perm
, error
);
758 r
= bus_verify_manage_units_async_full(
762 N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
769 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
772 if (r
== -EOPNOTSUPP
)
773 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support freezing.", u
->id
);
775 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit has a pending job.");
777 return sd_bus_error_set(error
, BUS_ERROR_UNIT_INACTIVE
, "Unit is inactive.");
779 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Previously requested freezer operation for unit '%s' is still in progress.", u
->id
);
783 reply_no_delay
= true;
785 assert(!u
->pending_freezer_message
);
787 r
= sd_bus_message_new_method_return(message
, &u
->pending_freezer_message
);
791 if (reply_no_delay
) {
792 r
= bus_unit_send_pending_freezer_message(u
);
800 int bus_unit_method_thaw(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
801 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_THAW
);
804 int bus_unit_method_freeze(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
805 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_FREEZE
);
808 static int property_get_refs(
811 const char *interface
,
812 const char *property
,
813 sd_bus_message
*reply
,
815 sd_bus_error
*error
) {
823 r
= sd_bus_message_open_container(reply
, 'a', "s");
827 for (const char *i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
830 c
= sd_bus_track_count_name(u
->bus_track
, i
);
834 /* Add the item multiple times if the ref count for each is above 1 */
835 for (int k
= 0; k
< c
; k
++) {
836 r
= sd_bus_message_append(reply
, "s", i
);
842 return sd_bus_message_close_container(reply
);
845 const sd_bus_vtable bus_unit_vtable
[] = {
846 SD_BUS_VTABLE_START(0),
848 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
849 SD_BUS_PROPERTY("Names", "as", property_get_names
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
850 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
851 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
852 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
853 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
854 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
855 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
856 SD_BUS_PROPERTY("Upholds", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
857 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
858 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
859 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
860 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
861 SD_BUS_PROPERTY("UpheldBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
862 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
863 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
864 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
865 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
866 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
867 SD_BUS_PROPERTY("OnSuccess", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
868 SD_BUS_PROPERTY("OnSuccessOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
869 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
870 SD_BUS_PROPERTY("OnFailureOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
871 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
872 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
873 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
874 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
875 SD_BUS_PROPERTY("PropagatesStopTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
876 SD_BUS_PROPERTY("StopPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
877 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
878 SD_BUS_PROPERTY("SliceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
879 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
880 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
881 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
882 SD_BUS_PROPERTY("AccessSELinuxContext", "s", NULL
, offsetof(Unit
, access_selinux_context
), SD_BUS_VTABLE_PROPERTY_CONST
),
883 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
884 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
885 SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
886 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
887 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
888 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
889 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
890 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
891 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
892 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
893 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
894 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
895 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
896 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
897 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
898 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
899 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
900 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
901 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
902 SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
903 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
904 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
905 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
906 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
907 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
908 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
909 SD_BUS_PROPERTY("OnSuccesJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_success_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
), /* deprecated */
910 SD_BUS_PROPERTY("OnSuccessJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_success_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
911 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
912 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
913 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, 0),
914 SD_BUS_PROPERTY("Markers", "as", property_get_markers
, offsetof(Unit
, markers
), 0),
915 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
916 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
917 SD_BUS_PROPERTY("JobTimeoutAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
918 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
919 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
920 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
921 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
922 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
923 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
924 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
925 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
926 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
927 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
928 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
929 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
930 SD_BUS_PROPERTY("StartLimitAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
931 SD_BUS_PROPERTY("FailureAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
932 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
933 SD_BUS_PROPERTY("SuccessAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
934 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
935 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
936 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
937 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
938 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
939 SD_BUS_PROPERTY("ActivationDetails", "a(ss)", bus_property_get_activation_details
, offsetof(Unit
, activation_details
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
941 SD_BUS_METHOD_WITH_ARGS("Start",
942 SD_BUS_ARGS("s", mode
),
943 SD_BUS_RESULT("o", job
),
944 bus_unit_method_start
,
945 SD_BUS_VTABLE_UNPRIVILEGED
),
946 SD_BUS_METHOD_WITH_ARGS("Stop",
947 SD_BUS_ARGS("s", mode
),
948 SD_BUS_RESULT("o", job
),
949 bus_unit_method_stop
,
950 SD_BUS_VTABLE_UNPRIVILEGED
),
951 SD_BUS_METHOD_WITH_ARGS("Reload",
952 SD_BUS_ARGS("s", mode
),
953 SD_BUS_RESULT("o", job
),
954 bus_unit_method_reload
,
955 SD_BUS_VTABLE_UNPRIVILEGED
),
956 SD_BUS_METHOD_WITH_ARGS("Restart",
957 SD_BUS_ARGS("s", mode
),
958 SD_BUS_RESULT("o", job
),
959 bus_unit_method_restart
,
960 SD_BUS_VTABLE_UNPRIVILEGED
),
961 SD_BUS_METHOD_WITH_ARGS("TryRestart",
962 SD_BUS_ARGS("s", mode
),
963 SD_BUS_RESULT("o", job
),
964 bus_unit_method_try_restart
,
965 SD_BUS_VTABLE_UNPRIVILEGED
),
966 SD_BUS_METHOD_WITH_ARGS("ReloadOrRestart",
967 SD_BUS_ARGS("s", mode
),
968 SD_BUS_RESULT("o", job
),
969 bus_unit_method_reload_or_restart
,
970 SD_BUS_VTABLE_UNPRIVILEGED
),
971 SD_BUS_METHOD_WITH_ARGS("ReloadOrTryRestart",
972 SD_BUS_ARGS("s", mode
),
973 SD_BUS_RESULT("o", job
),
974 bus_unit_method_reload_or_try_restart
,
975 SD_BUS_VTABLE_UNPRIVILEGED
),
976 SD_BUS_METHOD_WITH_ARGS("EnqueueJob",
977 SD_BUS_ARGS("s", job_type
, "s", job_mode
),
978 SD_BUS_RESULT("u", job_id
, "o", job_path
, "s", unit_id
, "o", unit_path
, "s", job_type
, "a(uosos)", affected_jobs
),
979 bus_unit_method_enqueue_job
,
980 SD_BUS_VTABLE_UNPRIVILEGED
),
981 SD_BUS_METHOD_WITH_ARGS("Kill",
982 SD_BUS_ARGS("s", whom
, "i", signal
),
984 bus_unit_method_kill
,
985 SD_BUS_VTABLE_UNPRIVILEGED
),
986 SD_BUS_METHOD("ResetFailed",
989 bus_unit_method_reset_failed
,
990 SD_BUS_VTABLE_UNPRIVILEGED
),
991 SD_BUS_METHOD_WITH_ARGS("SetProperties",
992 SD_BUS_ARGS("b", runtime
, "a(sv)", properties
),
994 bus_unit_method_set_properties
,
995 SD_BUS_VTABLE_UNPRIVILEGED
),
1000 SD_BUS_VTABLE_UNPRIVILEGED
),
1001 SD_BUS_METHOD("Unref",
1004 bus_unit_method_unref
,
1005 SD_BUS_VTABLE_UNPRIVILEGED
),
1006 SD_BUS_METHOD_WITH_ARGS("Clean",
1007 SD_BUS_ARGS("as", mask
),
1009 bus_unit_method_clean
,
1010 SD_BUS_VTABLE_UNPRIVILEGED
),
1011 SD_BUS_METHOD("Freeze",
1014 bus_unit_method_freeze
,
1015 SD_BUS_VTABLE_UNPRIVILEGED
),
1016 SD_BUS_METHOD("Thaw",
1019 bus_unit_method_thaw
,
1020 SD_BUS_VTABLE_UNPRIVILEGED
),
1022 /* For dependency types we don't support anymore always return an empty array */
1023 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1024 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1025 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1026 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1027 /* Obsolete alias names */
1028 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1029 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1034 static int property_get_slice(
1037 const char *interface
,
1038 const char *property
,
1039 sd_bus_message
*reply
,
1041 sd_bus_error
*error
) {
1043 Unit
*u
= ASSERT_PTR(userdata
);
1048 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
1051 static int property_get_current_memory(
1054 const char *interface
,
1055 const char *property
,
1056 sd_bus_message
*reply
,
1058 sd_bus_error
*error
) {
1060 uint64_t sz
= UINT64_MAX
;
1061 Unit
*u
= ASSERT_PTR(userdata
);
1067 r
= unit_get_memory_current(u
, &sz
);
1068 if (r
< 0 && r
!= -ENODATA
)
1069 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
1071 return sd_bus_message_append(reply
, "t", sz
);
1074 static int property_get_available_memory(
1077 const char *interface
,
1078 const char *property
,
1079 sd_bus_message
*reply
,
1081 sd_bus_error
*error
) {
1083 uint64_t sz
= UINT64_MAX
;
1084 Unit
*u
= ASSERT_PTR(userdata
);
1090 r
= unit_get_memory_available(u
, &sz
);
1091 if (r
< 0 && r
!= -ENODATA
)
1092 log_unit_warning_errno(u
, r
, "Failed to get total available memory from cgroup: %m");
1094 return sd_bus_message_append(reply
, "t", sz
);
1097 static int property_get_current_tasks(
1100 const char *interface
,
1101 const char *property
,
1102 sd_bus_message
*reply
,
1104 sd_bus_error
*error
) {
1106 uint64_t cn
= UINT64_MAX
;
1107 Unit
*u
= ASSERT_PTR(userdata
);
1113 r
= unit_get_tasks_current(u
, &cn
);
1114 if (r
< 0 && r
!= -ENODATA
)
1115 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
1117 return sd_bus_message_append(reply
, "t", cn
);
1120 static int property_get_cpu_usage(
1123 const char *interface
,
1124 const char *property
,
1125 sd_bus_message
*reply
,
1127 sd_bus_error
*error
) {
1129 nsec_t ns
= NSEC_INFINITY
;
1130 Unit
*u
= ASSERT_PTR(userdata
);
1136 r
= unit_get_cpu_usage(u
, &ns
);
1137 if (r
< 0 && r
!= -ENODATA
)
1138 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
1140 return sd_bus_message_append(reply
, "t", ns
);
1143 static int property_get_cpuset_cpus(
1146 const char *interface
,
1147 const char *property
,
1148 sd_bus_message
*reply
,
1150 sd_bus_error
*error
) {
1152 Unit
*u
= ASSERT_PTR(userdata
);
1153 _cleanup_(cpu_set_reset
) CPUSet cpus
= {};
1154 _cleanup_free_
uint8_t *array
= NULL
;
1160 (void) unit_get_cpuset(u
, &cpus
, "cpuset.cpus.effective");
1161 (void) cpu_set_to_dbus(&cpus
, &array
, &allocated
);
1162 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1165 static int property_get_cpuset_mems(
1168 const char *interface
,
1169 const char *property
,
1170 sd_bus_message
*reply
,
1172 sd_bus_error
*error
) {
1174 Unit
*u
= ASSERT_PTR(userdata
);
1175 _cleanup_(cpu_set_reset
) CPUSet mems
= {};
1176 _cleanup_free_
uint8_t *array
= NULL
;
1182 (void) unit_get_cpuset(u
, &mems
, "cpuset.mems.effective");
1183 (void) cpu_set_to_dbus(&mems
, &array
, &allocated
);
1184 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1187 static int property_get_cgroup(
1190 const char *interface
,
1191 const char *property
,
1192 sd_bus_message
*reply
,
1194 sd_bus_error
*error
) {
1196 Unit
*u
= ASSERT_PTR(userdata
);
1197 const char *t
= NULL
;
1202 /* Three cases: a) u->cgroup_path is NULL, in which case the
1203 * unit has no control group, which we report as the empty
1204 * string. b) u->cgroup_path is the empty string, which
1205 * indicates the root cgroup, which we report as "/". c) all
1206 * other cases we report as-is. */
1209 t
= empty_to_root(u
->cgroup_path
);
1211 return sd_bus_message_append(reply
, "s", t
);
1214 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
1215 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
1221 r
= set_put(pids
, PID_TO_PTR(pid
));
1222 if (IN_SET(r
, 0, -EEXIST
))
1228 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1237 (void) get_process_cmdline(pid
, SIZE_MAX
,
1238 PROCESS_CMDLINE_COMM_FALLBACK
| PROCESS_CMDLINE_QUOTE
,
1241 return sd_bus_message_append(reply
,
1248 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1249 _cleanup_closedir_
DIR *d
= NULL
;
1250 _cleanup_fclose_
FILE *f
= NULL
;
1256 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1265 /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
1266 * From https://docs.kernel.org/admin-guide/cgroup-v2.html#threads,
1267 * “cgroup.procs” in a threaded domain cgroup contains the PIDs of all processes in
1268 * the subtree and is not readable in the subtree proper. */
1269 r
= cg_read_pid(f
, &pid
);
1270 if (IN_SET(r
, 0, -EOPNOTSUPP
))
1275 if (is_kernel_thread(pid
) > 0)
1278 r
= append_process(reply
, p
, pid
, pids
);
1283 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1290 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1292 r
= cg_read_subgroup(d
, &g
);
1298 j
= path_join(empty_to_root(p
), g
);
1302 r
= append_cgroup(reply
, j
, pids
);
1310 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1311 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1312 _cleanup_set_free_ Set
*pids
= NULL
;
1319 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1323 pids
= set_new(NULL
);
1327 r
= sd_bus_message_new_method_return(message
, &reply
);
1331 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1335 if (u
->cgroup_path
) {
1336 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1341 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1342 pid
= unit_main_pid(u
);
1344 r
= append_process(reply
, NULL
, pid
, pids
);
1349 pid
= unit_control_pid(u
);
1351 r
= append_process(reply
, NULL
, pid
, pids
);
1356 r
= sd_bus_message_close_container(reply
);
1360 return sd_bus_send(NULL
, reply
, NULL
);
1363 static int property_get_ip_counter(
1366 const char *interface
,
1367 const char *property
,
1368 sd_bus_message
*reply
,
1370 sd_bus_error
*error
) {
1372 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1373 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1374 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1375 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1376 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1379 uint64_t value
= UINT64_MAX
;
1380 Unit
*u
= ASSERT_PTR(userdata
);
1387 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1388 (void) unit_get_ip_accounting(u
, metric
, &value
);
1389 return sd_bus_message_append(reply
, "t", value
);
1392 static int property_get_io_counter(
1395 const char *interface
,
1396 const char *property
,
1397 sd_bus_message
*reply
,
1399 sd_bus_error
*error
) {
1401 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1402 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1403 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1404 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1405 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1408 uint64_t value
= UINT64_MAX
;
1409 Unit
*u
= ASSERT_PTR(userdata
);
1416 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1417 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1418 return sd_bus_message_append(reply
, "t", value
);
1421 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1423 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1424 _cleanup_set_free_ Set
*pids
= NULL
;
1431 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1432 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1433 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1434 * subcgroup path has no effect! */
1436 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1440 r
= sd_bus_message_read(message
, "s", &path
);
1444 path
= empty_to_null(path
);
1446 if (!path_is_absolute(path
))
1447 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1449 if (!path_is_normalized(path
))
1450 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1453 if (!unit_cgroup_delegate(u
))
1454 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1456 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1457 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1459 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1463 r
= sd_bus_message_enter_container(message
, 'a', "u");
1467 uid_t process_uid
, sender_uid
;
1471 r
= sd_bus_message_read(message
, "u", &upid
);
1478 r
= sd_bus_creds_get_pid(creds
, &pid
);
1484 /* Filter out duplicates */
1485 if (set_contains(pids
, PID_TO_PTR(pid
)))
1488 /* Check if this process is suitable for attaching to this unit */
1489 r
= unit_pid_attachable(u
, pid
, error
);
1493 /* Let's query the sender's UID, so that we can make our security decisions */
1494 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1498 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1499 * then the process' UID and the target unit's UID have to match the sender's UID */
1500 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1501 r
= get_process_uid(pid
, &process_uid
);
1503 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1505 if (process_uid
!= sender_uid
)
1506 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1507 if (process_uid
!= u
->ref_uid
)
1508 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1512 pids
= set_new(NULL
);
1517 r
= set_put(pids
, PID_TO_PTR(pid
));
1522 r
= sd_bus_message_exit_container(message
);
1526 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1528 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1530 return sd_bus_reply_method_return(message
, NULL
);
1533 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1534 SD_BUS_VTABLE_START(0),
1535 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1536 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1537 SD_BUS_PROPERTY("ControlGroupId", "t", NULL
, offsetof(Unit
, cgroup_id
), 0),
1538 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1539 SD_BUS_PROPERTY("MemoryAvailable", "t", property_get_available_memory
, 0, 0),
1540 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1541 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus
, 0, 0),
1542 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems
, 0, 0),
1543 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1544 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1545 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1546 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1547 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1548 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1549 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1550 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1551 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1553 SD_BUS_METHOD_WITH_ARGS("GetProcesses",
1555 SD_BUS_ARGS("a(sus)", processes
),
1556 bus_unit_method_get_processes
,
1557 SD_BUS_VTABLE_UNPRIVILEGED
),
1559 SD_BUS_METHOD_WITH_ARGS("AttachProcesses",
1560 SD_BUS_ARGS("s", subcgroup
, "au", pids
),
1562 bus_unit_method_attach_processes
,
1563 SD_BUS_VTABLE_UNPRIVILEGED
),
1568 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1569 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1570 _cleanup_free_
char *p
= NULL
;
1571 Unit
*u
= ASSERT_PTR(userdata
);
1576 p
= unit_dbus_path(u
);
1580 r
= sd_bus_message_new_signal(
1583 "/org/freedesktop/systemd1",
1584 "org.freedesktop.systemd1.Manager",
1589 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1593 return sd_bus_send(bus
, m
, NULL
);
1596 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1597 _cleanup_free_
char *p
= NULL
;
1598 Unit
*u
= ASSERT_PTR(userdata
);
1603 p
= unit_dbus_path(u
);
1607 /* Send a properties changed signal. First for the specific
1608 * type, then for the generic unit. The clients may rely on
1609 * this order to get atomic behavior if needed. */
1611 r
= sd_bus_emit_properties_changed_strv(
1613 unit_dbus_interface_from_type(u
->type
),
1618 return sd_bus_emit_properties_changed_strv(
1620 "org.freedesktop.systemd1.Unit",
1624 void bus_unit_send_change_signal(Unit
*u
) {
1628 if (u
->in_dbus_queue
) {
1629 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1630 u
->in_dbus_queue
= false;
1636 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1638 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1640 u
->sent_dbus_new_signal
= true;
1643 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1645 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1646 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1647 * so that clients can follow the full state transition */
1649 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1652 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1653 * the unit appears in the new state right-away (except if the
1654 * caller explicitly asked us to send it anyway) */
1657 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1658 * when we are reloading. */
1661 bus_unit_send_change_signal(u
);
1664 int bus_unit_send_pending_freezer_message(Unit
*u
) {
1669 if (!u
->pending_freezer_message
)
1672 r
= sd_bus_send(NULL
, u
->pending_freezer_message
, NULL
);
1674 log_warning_errno(r
, "Failed to send queued message, ignoring: %m");
1676 u
->pending_freezer_message
= sd_bus_message_unref(u
->pending_freezer_message
);
1681 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1682 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1683 _cleanup_free_
char *p
= NULL
;
1684 Unit
*u
= ASSERT_PTR(userdata
);
1689 p
= unit_dbus_path(u
);
1693 r
= sd_bus_message_new_signal(
1696 "/org/freedesktop/systemd1",
1697 "org.freedesktop.systemd1.Manager",
1702 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1706 return sd_bus_send(bus
, m
, NULL
);
1709 void bus_unit_send_removed_signal(Unit
*u
) {
1713 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1714 bus_unit_send_change_signal(u
);
1719 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1721 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1724 int bus_unit_queue_job_one(
1725 sd_bus_message
*message
,
1729 BusUnitQueueFlags flags
,
1730 sd_bus_message
*reply
,
1731 sd_bus_error
*error
) {
1733 _cleanup_set_free_ Set
*affected
= NULL
;
1734 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1738 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1739 affected
= set_new(NULL
);
1744 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1748 r
= bus_job_track_sender(j
, message
);
1752 /* Before we send the method reply, force out the announcement JobNew for this job */
1753 bus_job_send_pending_change_signal(j
, true);
1755 job_path
= job_dbus_path(j
);
1759 /* The classic response is just a job object path */
1760 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1761 return sd_bus_message_append(reply
, "o", job_path
);
1763 /* In verbose mode respond with the anchor job plus everything that has been affected */
1765 unit_path
= unit_dbus_path(j
->unit
);
1769 r
= sd_bus_message_append(reply
, "uosos",
1771 j
->unit
->id
, unit_path
,
1772 job_type_to_string(j
->type
));
1776 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1780 SET_FOREACH(a
, affected
) {
1784 /* Free paths from previous iteration */
1785 job_path
= mfree(job_path
);
1786 unit_path
= mfree(unit_path
);
1788 job_path
= job_dbus_path(a
);
1792 unit_path
= unit_dbus_path(a
->unit
);
1796 r
= sd_bus_message_append(reply
, "(uosos)",
1798 a
->unit
->id
, unit_path
,
1799 job_type_to_string(a
->type
));
1804 return sd_bus_message_close_container(reply
);
1807 int bus_unit_queue_job(
1808 sd_bus_message
*message
,
1812 BusUnitQueueFlags flags
,
1813 sd_bus_error
*error
) {
1815 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1820 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1821 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1823 r
= mac_selinux_unit_access_check(
1825 job_type_to_access_method(type
),
1830 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1831 if (type
== JOB_RESTART
)
1832 type
= JOB_RELOAD_OR_START
;
1833 else if (type
== JOB_TRY_RESTART
)
1834 type
= JOB_TRY_RELOAD
;
1837 if (type
== JOB_STOP
&&
1838 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1839 unit_active_state(u
) == UNIT_INACTIVE
)
1840 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1842 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1843 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1844 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1845 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1846 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
);
1848 r
= sd_bus_message_new_method_return(message
, &reply
);
1852 r
= bus_unit_queue_job_one(message
, u
, type
, mode
, flags
, reply
, error
);
1856 return sd_bus_send(NULL
, reply
, NULL
);
1859 static int bus_unit_set_live_property(
1862 sd_bus_message
*message
,
1863 UnitWriteFlags flags
,
1864 sd_bus_error
*error
) {
1872 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for
1873 * transient units that are being created). */
1875 if (streq(name
, "Description")) {
1878 r
= sd_bus_message_read(message
, "s", &d
);
1882 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1883 r
= unit_set_description(u
, d
);
1887 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1893 /* A setting that only applies to active units. We don't actually write this to /run, this state is
1894 * managed internally. "+foo" sets flag foo, "-foo" unsets flag foo, just "foo" resets flags to
1895 * foo. The last type cannot be mixed with "+" or "-". */
1897 if (streq(name
, "Markers")) {
1898 unsigned settings
= 0, mask
= 0;
1899 bool some_plus_minus
= false, some_absolute
= false;
1901 r
= sd_bus_message_enter_container(message
, 'a', "s");
1909 r
= sd_bus_message_read(message
, "s", &word
);
1915 if (IN_SET(word
[0], '+', '-')) {
1918 some_plus_minus
= true;
1921 some_absolute
= true;
1924 UnitMarker m
= unit_marker_from_string(word
);
1926 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
,
1927 "Unknown marker \"%s\".", word
);
1929 SET_FLAG(settings
, 1u << m
, b
);
1930 SET_FLAG(mask
, 1u << m
, true);
1933 r
= sd_bus_message_exit_container(message
);
1937 if (some_plus_minus
&& some_absolute
)
1938 return sd_bus_error_set(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Bad marker syntax.");
1940 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1942 u
->markers
= settings
;
1944 u
->markers
= settings
| (u
->markers
& ~mask
);
1953 static int bus_set_transient_emergency_action(
1957 sd_bus_message
*message
,
1958 UnitWriteFlags flags
,
1959 sd_bus_error
*error
) {
1968 r
= sd_bus_message_read(message
, "s", &s
);
1972 system
= MANAGER_IS_SYSTEM(u
->manager
);
1973 r
= parse_emergency_action(s
, system
, &v
);
1975 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1976 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
1977 : "Invalid %s setting: %s",
1980 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1982 unit_write_settingf(u
, flags
, name
,
1989 static int bus_set_transient_exit_status(
1993 sd_bus_message
*message
,
1994 UnitWriteFlags flags
,
1995 sd_bus_error
*error
) {
2002 r
= sd_bus_message_read(message
, "i", &k
);
2007 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
2009 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2010 *p
= k
< 0 ? -1 : k
;
2013 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2015 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
2021 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
2022 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
2024 static int bus_set_transient_conditions(
2029 sd_bus_message
*message
,
2030 UnitWriteFlags flags
,
2031 sd_bus_error
*error
) {
2033 const char *type_name
, *param
;
2034 int trigger
, negate
, r
;
2039 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
2043 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
2046 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
2048 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
2051 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
2053 if (condition_takes_path(t
) && !path_is_absolute(param
))
2054 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
2056 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2059 c
= condition_new(t
, param
, trigger
, negate
);
2063 LIST_PREPEND(conditions
, *list
, c
);
2065 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
2066 "%s=%s%s%s", type_name
,
2067 trigger
? "|" : "", negate
? "!" : "", param
);
2075 r
= sd_bus_message_exit_container(message
);
2079 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
2080 *list
= condition_free_list(*list
);
2081 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
2087 static int bus_unit_set_transient_property(
2090 sd_bus_message
*message
,
2091 UnitWriteFlags flags
,
2092 sd_bus_error
*error
) {
2101 /* Handles settings when transient units are created. This settings cannot be altered anymore after
2102 * the unit has been created. */
2104 if (streq(name
, "SourcePath"))
2105 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
2107 if (streq(name
, "StopWhenUnneeded"))
2108 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
2110 if (streq(name
, "RefuseManualStart"))
2111 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
2113 if (streq(name
, "RefuseManualStop"))
2114 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
2116 if (streq(name
, "AllowIsolate"))
2117 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
2119 if (streq(name
, "DefaultDependencies"))
2120 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
2122 if (streq(name
, "OnSuccessJobMode"))
2123 return bus_set_transient_job_mode(u
, name
, &u
->on_success_job_mode
, message
, flags
, error
);
2125 if (streq(name
, "OnFailureJobMode"))
2126 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
2128 if (streq(name
, "IgnoreOnIsolate"))
2129 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
2131 if (streq(name
, "JobTimeoutUSec")) {
2132 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
2133 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
2134 u
->job_running_timeout
= u
->job_timeout
;
2137 if (streq(name
, "JobRunningTimeoutUSec")) {
2138 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
2139 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
2140 u
->job_running_timeout_set
= true;
2145 if (streq(name
, "JobTimeoutAction"))
2146 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
2148 if (streq(name
, "JobTimeoutRebootArgument"))
2149 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
2151 if (streq(name
, "StartLimitIntervalUSec"))
2152 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
2154 if (streq(name
, "StartLimitBurst"))
2155 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
2157 if (streq(name
, "StartLimitAction"))
2158 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
2160 if (streq(name
, "FailureAction"))
2161 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
2163 if (streq(name
, "SuccessAction"))
2164 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
2166 if (streq(name
, "FailureActionExitStatus"))
2167 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
2169 if (streq(name
, "SuccessActionExitStatus"))
2170 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
2172 if (streq(name
, "RebootArgument"))
2173 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
2175 if (streq(name
, "CollectMode"))
2176 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
2178 if (streq(name
, "Conditions"))
2179 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
2181 if (streq(name
, "Asserts"))
2182 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
2184 if (streq(name
, "Documentation")) {
2185 _cleanup_strv_free_
char **l
= NULL
;
2187 r
= sd_bus_message_read_strv(message
, &l
);
2192 if (!documentation_url_is_valid(*p
))
2193 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
2195 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2196 if (strv_isempty(l
)) {
2197 u
->documentation
= strv_free(u
->documentation
);
2198 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2200 strv_extend_strv(&u
->documentation
, l
, false);
2203 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2209 } else if (streq(name
, "Slice")) {
2213 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
2214 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
2215 if (u
->type
== UNIT_SLICE
)
2216 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
2217 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
2218 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
2220 r
= sd_bus_message_read(message
, "s", &s
);
2224 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
2225 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
2227 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2228 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2229 * instead of manager_load_unit() on purpose, here. */
2230 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
2234 if (slice
->type
!= UNIT_SLICE
)
2235 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
2237 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2238 r
= unit_set_slice(u
, slice
);
2242 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
2247 } else if (streq(name
, "RequiresMountsFor")) {
2248 _cleanup_strv_free_
char **l
= NULL
;
2250 r
= sd_bus_message_read_strv(message
, &l
);
2254 STRV_FOREACH(p
, l
) {
2257 if (!path_is_absolute(*p
))
2258 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
2260 if (!path_is_valid(*p
))
2261 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
2263 if (!path_is_normalized(*p
))
2264 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
2266 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2267 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
2269 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
2271 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2278 if (streq(name
, "RequiresOverridable"))
2279 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
2280 else if (streq(name
, "RequisiteOverridable"))
2281 d
= UNIT_REQUISITE
; /* same here */
2283 d
= unit_dependency_from_string(name
);
2300 UNIT_PROPAGATES_RELOAD_TO
,
2301 UNIT_RELOAD_PROPAGATED_FROM
,
2302 UNIT_PROPAGATES_STOP_TO
,
2303 UNIT_STOP_PROPAGATED_FROM
,
2304 UNIT_JOINS_NAMESPACE_OF
))
2305 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d
));
2307 r
= sd_bus_message_enter_container(message
, 'a', "s");
2311 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
2312 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
2313 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
2315 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2316 _cleanup_free_
char *label
= NULL
;
2318 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
2322 label
= strjoin(name
, "-", other
);
2326 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
2333 r
= sd_bus_message_exit_container(message
);
2339 } else if (streq(name
, "AddRef")) {
2343 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2344 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2345 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2346 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2347 * dependency type, hence let's not confuse things with that.
2349 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2350 * the transient unit is complete, so that setting this property multiple times in the same transient
2351 * unit creation call doesn't count as individual references. */
2353 r
= sd_bus_message_read(message
, "b", &b
);
2357 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2358 u
->bus_track_add
= b
;
2366 int bus_unit_set_properties(
2368 sd_bus_message
*message
,
2369 UnitWriteFlags flags
,
2371 sd_bus_error
*error
) {
2373 bool for_real
= false;
2380 /* We iterate through the array twice. First run we just check
2381 * if all passed data is valid, second run actually applies
2382 * it. This is to implement transaction-like behaviour without
2383 * actually providing full transactions. */
2385 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2393 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2397 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2400 /* Reached EOF. Let's try again, and this time for realz... */
2401 r
= sd_bus_message_rewind(message
, false);
2409 r
= sd_bus_message_read(message
, "s", &name
);
2413 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2417 /* If not for real, then mask out the two target flags */
2418 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2420 if (UNIT_VTABLE(u
)->bus_set_property
)
2421 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2424 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2425 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2427 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2432 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
,
2433 "Cannot set property %s, or unknown property.", name
);
2435 r
= sd_bus_message_exit_container(message
);
2439 r
= sd_bus_message_exit_container(message
);
2446 r
= sd_bus_message_exit_container(message
);
2450 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2451 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2456 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2459 /* Generates a pretty error if a unit isn't properly loaded. */
2461 switch (u
->load_state
) {
2466 case UNIT_NOT_FOUND
:
2467 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2469 case UNIT_BAD_SETTING
:
2470 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2472 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2473 return sd_bus_error_set_errnof(error
, u
->load_error
,
2474 "Unit %s failed to load properly, please adjust/correct and reload service manager: %m", u
->id
);
2477 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2482 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2486 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2487 Unit
*u
= ASSERT_PTR(userdata
);
2491 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2493 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2495 unit_add_to_cgroup_empty_queue(u
);
2497 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2498 unit_add_to_gc_queue(u
);
2503 static int bus_unit_allocate_bus_track(Unit
*u
) {
2511 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2515 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2517 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2524 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2529 r
= bus_unit_allocate_bus_track(u
);
2533 return sd_bus_track_add_name(u
->bus_track
, name
);
2536 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2541 r
= bus_unit_allocate_bus_track(u
);
2545 return sd_bus_track_add_sender(u
->bus_track
, m
);
2548 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2551 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet,
2552 * return an error */
2556 return sd_bus_track_remove_sender(u
->bus_track
, m
);