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"
25 #include "signal-util.h"
27 #include "string-table.h"
28 #include "string-util.h"
30 #include "user-util.h"
33 static bool unit_can_start_refuse_manual(Unit
*u
) {
34 return unit_can_start(u
) && !u
->refuse_manual_start
;
37 static bool unit_can_stop_refuse_manual(Unit
*u
) {
38 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
41 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
42 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
46 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
47 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
48 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
49 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
50 static BUS_DEFINE_PROPERTY_GET2(property_get_freezer_state
, "s", Unit
, unit_freezer_state
, freezer_state_to_string
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
52 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
53 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
54 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
55 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
56 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
57 static BUS_DEFINE_PROPERTY_GET(property_get_can_freeze
, "b", Unit
, unit_can_freeze
);
58 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
59 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
61 static int property_get_can_clean(
64 const char *interface
,
66 sd_bus_message
*reply
,
68 sd_bus_error
*error
) {
77 r
= unit_can_clean(u
, &mask
);
81 r
= sd_bus_message_open_container(reply
, 'a', "s");
85 for (ExecDirectoryType t
= 0; t
< _EXEC_DIRECTORY_TYPE_MAX
; t
++) {
86 if (!FLAGS_SET(mask
, 1U << t
))
89 r
= sd_bus_message_append(reply
, "s", exec_resource_type_to_string(t
));
94 if (FLAGS_SET(mask
, EXEC_CLEAN_FDSTORE
)) {
95 r
= sd_bus_message_append(reply
, "s", "fdstore");
100 return sd_bus_message_close_container(reply
);
103 static int property_get_names(
106 const char *interface
,
107 const char *property
,
108 sd_bus_message
*reply
,
110 sd_bus_error
*error
) {
112 Unit
*u
= ASSERT_PTR(userdata
);
119 r
= sd_bus_message_open_container(reply
, 'a', "s");
123 r
= sd_bus_message_append(reply
, "s", u
->id
);
127 SET_FOREACH(t
, u
->aliases
) {
128 r
= sd_bus_message_append(reply
, "s", t
);
133 return sd_bus_message_close_container(reply
);
136 static int property_get_following(
139 const char *interface
,
140 const char *property
,
141 sd_bus_message
*reply
,
143 sd_bus_error
*error
) {
145 Unit
*u
= userdata
, *f
;
151 f
= unit_following(u
);
152 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
155 static int property_get_dependencies(
158 const char *interface
,
159 const char *property
,
160 sd_bus_message
*reply
,
162 sd_bus_error
*error
) {
164 Unit
*u
= userdata
, *other
;
174 d
= unit_dependency_from_string(property
);
177 deps
= unit_get_dependencies(u
, d
);
179 r
= sd_bus_message_open_container(reply
, 'a', "s");
183 HASHMAP_FOREACH_KEY(v
, other
, deps
) {
184 r
= sd_bus_message_append(reply
, "s", other
->id
);
189 return sd_bus_message_close_container(reply
);
192 static int property_get_requires_mounts_for(
195 const char *interface
,
196 const char *property
,
197 sd_bus_message
*reply
,
199 sd_bus_error
*error
) {
201 Hashmap
**h
= ASSERT_PTR(userdata
);
209 r
= sd_bus_message_open_container(reply
, 'a', "s");
213 HASHMAP_FOREACH_KEY(v
, p
, *h
) {
214 r
= sd_bus_message_append(reply
, "s", p
);
219 return sd_bus_message_close_container(reply
);
222 static int property_get_unit_file_preset(
225 const char *interface
,
226 const char *property
,
227 sd_bus_message
*reply
,
229 sd_bus_error
*error
) {
231 Unit
*u
= ASSERT_PTR(userdata
);
237 r
= unit_get_unit_file_preset(u
);
239 return sd_bus_message_append(reply
, "s", preset_action_past_tense_to_string(r
));
242 static int property_get_job(
245 const char *interface
,
246 const char *property
,
247 sd_bus_message
*reply
,
249 sd_bus_error
*error
) {
251 _cleanup_free_
char *p
= NULL
;
252 Job
**j
= ASSERT_PTR(userdata
);
258 return sd_bus_message_append(reply
, "(uo)", 0, "/");
260 p
= job_dbus_path(*j
);
264 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
267 static int property_get_conditions(
270 const char *interface
,
271 const char *property
,
272 sd_bus_message
*reply
,
274 sd_bus_error
*error
) {
276 const char *(*to_string
)(ConditionType type
) = NULL
;
277 Condition
**list
= ASSERT_PTR(userdata
);
283 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
285 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
289 LIST_FOREACH(conditions
, c
, *list
) {
293 c
->result
== CONDITION_UNTESTED
? 0 :
294 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
296 r
= sd_bus_message_append(reply
, "(sbbsi)",
298 c
->trigger
, c
->negate
,
299 c
->parameter
, tristate
);
305 return sd_bus_message_close_container(reply
);
308 static int property_get_load_error(
311 const char *interface
,
312 const char *property
,
313 sd_bus_message
*reply
,
315 sd_bus_error
*error
) {
317 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
318 Unit
*u
= ASSERT_PTR(userdata
);
324 r
= bus_unit_validate_load_state(u
, &e
);
326 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
328 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
331 static int property_get_markers(
334 const char *interface
,
335 const char *property
,
336 sd_bus_message
*reply
,
338 sd_bus_error
*error
) {
340 unsigned *markers
= ASSERT_PTR(userdata
);
346 r
= sd_bus_message_open_container(reply
, 'a', "s");
350 /* Make sure out values fit in the bitfield. */
351 assert_cc(_UNIT_MARKER_MAX
<= sizeof(((Unit
){}).markers
) * 8);
353 for (UnitMarker m
= 0; m
< _UNIT_MARKER_MAX
; m
++)
354 if (FLAGS_SET(*markers
, 1u << m
)) {
355 r
= sd_bus_message_append(reply
, "s", unit_marker_to_string(m
));
360 return sd_bus_message_close_container(reply
);
363 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
364 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
365 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
366 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
367 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
368 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
371 int bus_unit_method_start_generic(
372 sd_bus_message
*message
,
375 bool reload_if_possible
,
376 sd_bus_error
*error
) {
378 BusUnitQueueFlags job_flags
= reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0;
379 const char *smode
, *verb
;
385 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
387 r
= mac_selinux_unit_access_check(
389 job_type_to_access_method(job_type
),
394 r
= sd_bus_message_read(message
, "s", &smode
);
398 mode
= job_mode_from_string(smode
);
400 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
402 if (reload_if_possible
)
403 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
405 verb
= job_type_to_string(job_type
);
407 if (sd_bus_message_is_method_call(message
, NULL
, "StartUnitWithFlags")) {
408 uint64_t input_flags
= 0;
410 r
= sd_bus_message_read(message
, "t", &input_flags
);
413 /* Let clients know that this version doesn't support any flags at the moment. */
414 if (input_flags
!= 0)
415 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
416 "Invalid 'flags' parameter '%" PRIu64
"'",
420 r
= bus_verify_manage_units_async_full(
424 polkit_message_for_job
[job_type
],
431 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
433 return bus_unit_queue_job(message
, u
, job_type
, mode
, job_flags
, error
);
436 static int bus_unit_method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
437 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
440 static int bus_unit_method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
441 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
444 static int bus_unit_method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
445 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
448 static int bus_unit_method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
449 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
452 static int bus_unit_method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
453 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
456 static int bus_unit_method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
457 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
460 static int bus_unit_method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
461 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
464 int bus_unit_method_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
465 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
466 const char *jtype
, *smode
;
467 Unit
*u
= ASSERT_PTR(userdata
);
474 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
478 /* Parse the two magic reload types "reload-or-…" manually */
479 if (streq(jtype
, "reload-or-restart")) {
481 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
482 } else if (streq(jtype
, "reload-or-try-restart")) {
483 type
= JOB_TRY_RESTART
;
484 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
486 /* And the rest generically */
487 type
= job_type_from_string(jtype
);
489 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
492 mode
= job_mode_from_string(smode
);
494 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
496 r
= mac_selinux_unit_access_check(
498 job_type_to_access_method(type
),
503 r
= bus_verify_manage_units_async_full(
507 polkit_message_for_job
[type
],
514 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
516 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
519 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
520 Unit
*u
= ASSERT_PTR(userdata
);
529 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
533 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
537 if (startswith(sd_bus_message_get_member(message
), "QueueSignal")) {
538 r
= sd_bus_message_read(message
, "i", &value
);
549 who
= kill_who_from_string(swho
);
551 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument: %s", swho
);
554 if (!SIGNAL_VALID(signo
))
555 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
557 if (code
== SI_QUEUE
&& !((signo
>= SIGRTMIN
) && (signo
<= SIGRTMAX
)))
558 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
559 "Value parameter only accepted for realtime signals (SIGRTMIN…SIGRTMAX), refusing for signal SIG%s.", signal_to_string(signo
));
561 r
= bus_verify_manage_units_async_full(
565 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
572 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
574 r
= unit_kill(u
, who
, signo
, code
, value
, error
);
578 return sd_bus_reply_method_return(message
, NULL
);
581 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
582 Unit
*u
= ASSERT_PTR(userdata
);
587 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
591 r
= bus_verify_manage_units_async_full(
595 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
602 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
604 unit_reset_failed(u
);
606 return sd_bus_reply_method_return(message
, NULL
);
609 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
610 Unit
*u
= ASSERT_PTR(userdata
);
615 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
619 r
= sd_bus_message_read(message
, "b", &runtime
);
623 r
= bus_verify_manage_units_async_full(
627 N_("Authentication is required to set properties on '$(unit)'."),
634 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
636 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
640 return sd_bus_reply_method_return(message
, NULL
);
643 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
644 Unit
*u
= ASSERT_PTR(userdata
);
649 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
653 r
= bus_verify_manage_units_async_full(
664 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
666 r
= bus_unit_track_add_sender(u
, message
);
670 return sd_bus_reply_method_return(message
, NULL
);
673 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
674 Unit
*u
= ASSERT_PTR(userdata
);
679 r
= bus_unit_track_remove_sender(u
, message
);
681 return sd_bus_error_set(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
685 return sd_bus_reply_method_return(message
, NULL
);
688 int bus_unit_method_clean(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
689 ExecCleanMask mask
= 0;
690 Unit
*u
= ASSERT_PTR(userdata
);
695 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
699 r
= sd_bus_message_enter_container(message
, 'a', "s");
707 r
= sd_bus_message_read(message
, "s", &i
);
713 m
= exec_clean_mask_from_string(i
);
715 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid resource type: %s", i
);
720 r
= sd_bus_message_exit_container(message
);
724 r
= bus_verify_manage_units_async_full(
728 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
735 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
737 r
= unit_clean(u
, mask
);
738 if (r
== -EOPNOTSUPP
)
739 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support cleaning.", u
->id
);
741 return sd_bus_error_set(error
, BUS_ERROR_NOTHING_TO_CLEAN
, "No matching resources found.");
743 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit is not inactive or has pending job.");
747 return sd_bus_reply_method_return(message
, NULL
);
750 static int bus_unit_method_freezer_generic(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
, FreezerAction action
) {
752 int (*method
)(Unit
*);
753 Unit
*u
= ASSERT_PTR(userdata
);
754 bool reply_no_delay
= false;
758 assert(IN_SET(action
, FREEZER_FREEZE
, FREEZER_THAW
));
760 if (action
== FREEZER_FREEZE
) {
762 method
= unit_freeze
;
768 r
= mac_selinux_unit_access_check(u
, message
, perm
, error
);
772 r
= bus_verify_manage_units_async_full(
776 N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
783 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
786 if (r
== -EOPNOTSUPP
)
787 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Unit '%s' does not support freezing.", u
->id
);
789 return sd_bus_error_set(error
, BUS_ERROR_UNIT_BUSY
, "Unit has a pending job.");
791 return sd_bus_error_set(error
, BUS_ERROR_UNIT_INACTIVE
, "Unit is inactive.");
793 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Previously requested freezer operation for unit '%s' is still in progress.", u
->id
);
797 reply_no_delay
= true;
799 if (u
->pending_freezer_invocation
) {
800 bus_unit_send_pending_freezer_message(u
, true);
801 assert(!u
->pending_freezer_invocation
);
804 u
->pending_freezer_invocation
= sd_bus_message_ref(message
);
806 if (reply_no_delay
) {
807 r
= bus_unit_send_pending_freezer_message(u
, false);
815 int bus_unit_method_thaw(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
816 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_THAW
);
819 int bus_unit_method_freeze(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
820 return bus_unit_method_freezer_generic(message
, userdata
, error
, FREEZER_FREEZE
);
823 static int property_get_refs(
826 const char *interface
,
827 const char *property
,
828 sd_bus_message
*reply
,
830 sd_bus_error
*error
) {
838 r
= sd_bus_message_open_container(reply
, 'a', "s");
842 for (const char *i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
845 c
= sd_bus_track_count_name(u
->bus_track
, i
);
849 /* Add the item multiple times if the ref count for each is above 1 */
850 for (int k
= 0; k
< c
; k
++) {
851 r
= sd_bus_message_append(reply
, "s", i
);
857 return sd_bus_message_close_container(reply
);
860 const sd_bus_vtable bus_unit_vtable
[] = {
861 SD_BUS_VTABLE_START(0),
863 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
864 SD_BUS_PROPERTY("Names", "as", property_get_names
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
865 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
866 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
867 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
868 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
869 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
870 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
871 SD_BUS_PROPERTY("Upholds", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
872 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
873 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
874 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
875 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
876 SD_BUS_PROPERTY("UpheldBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
877 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
878 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
879 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
880 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
881 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
882 SD_BUS_PROPERTY("OnSuccess", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
883 SD_BUS_PROPERTY("OnSuccessOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
884 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
885 SD_BUS_PROPERTY("OnFailureOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
886 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
887 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
888 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
889 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
890 SD_BUS_PROPERTY("PropagatesStopTo", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
891 SD_BUS_PROPERTY("StopPropagatedFrom", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
892 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
893 SD_BUS_PROPERTY("SliceOf", "as", property_get_dependencies
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
894 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
895 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
896 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
897 SD_BUS_PROPERTY("AccessSELinuxContext", "s", NULL
, offsetof(Unit
, access_selinux_context
), SD_BUS_VTABLE_PROPERTY_CONST
),
898 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
899 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
900 SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
901 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
902 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
903 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
904 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
905 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
906 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
907 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
908 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
909 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
910 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
911 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
912 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
913 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
914 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
915 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
916 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
917 SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
918 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
919 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
920 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
921 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
922 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
923 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
924 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 */
925 SD_BUS_PROPERTY("OnSuccessJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_success_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
926 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
927 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
928 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, 0),
929 SD_BUS_PROPERTY("Markers", "as", property_get_markers
, offsetof(Unit
, markers
), 0),
930 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
931 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
932 SD_BUS_PROPERTY("JobTimeoutAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
933 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
934 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
935 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
936 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
937 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
938 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
939 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
940 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
941 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
942 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
943 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
944 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_ratelimit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
945 SD_BUS_PROPERTY("StartLimitAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
946 SD_BUS_PROPERTY("FailureAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
947 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
948 SD_BUS_PROPERTY("SuccessAction", "s", bus_property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
949 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
950 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
951 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
952 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
953 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
954 SD_BUS_PROPERTY("ActivationDetails", "a(ss)", bus_property_get_activation_details
, offsetof(Unit
, activation_details
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
956 SD_BUS_METHOD_WITH_ARGS("Start",
957 SD_BUS_ARGS("s", mode
),
958 SD_BUS_RESULT("o", job
),
959 bus_unit_method_start
,
960 SD_BUS_VTABLE_UNPRIVILEGED
),
961 SD_BUS_METHOD_WITH_ARGS("Stop",
962 SD_BUS_ARGS("s", mode
),
963 SD_BUS_RESULT("o", job
),
964 bus_unit_method_stop
,
965 SD_BUS_VTABLE_UNPRIVILEGED
),
966 SD_BUS_METHOD_WITH_ARGS("Reload",
967 SD_BUS_ARGS("s", mode
),
968 SD_BUS_RESULT("o", job
),
969 bus_unit_method_reload
,
970 SD_BUS_VTABLE_UNPRIVILEGED
),
971 SD_BUS_METHOD_WITH_ARGS("Restart",
972 SD_BUS_ARGS("s", mode
),
973 SD_BUS_RESULT("o", job
),
974 bus_unit_method_restart
,
975 SD_BUS_VTABLE_UNPRIVILEGED
),
976 SD_BUS_METHOD_WITH_ARGS("TryRestart",
977 SD_BUS_ARGS("s", mode
),
978 SD_BUS_RESULT("o", job
),
979 bus_unit_method_try_restart
,
980 SD_BUS_VTABLE_UNPRIVILEGED
),
981 SD_BUS_METHOD_WITH_ARGS("ReloadOrRestart",
982 SD_BUS_ARGS("s", mode
),
983 SD_BUS_RESULT("o", job
),
984 bus_unit_method_reload_or_restart
,
985 SD_BUS_VTABLE_UNPRIVILEGED
),
986 SD_BUS_METHOD_WITH_ARGS("ReloadOrTryRestart",
987 SD_BUS_ARGS("s", mode
),
988 SD_BUS_RESULT("o", job
),
989 bus_unit_method_reload_or_try_restart
,
990 SD_BUS_VTABLE_UNPRIVILEGED
),
991 SD_BUS_METHOD_WITH_ARGS("EnqueueJob",
992 SD_BUS_ARGS("s", job_type
, "s", job_mode
),
993 SD_BUS_RESULT("u", job_id
, "o", job_path
, "s", unit_id
, "o", unit_path
, "s", job_type
, "a(uosos)", affected_jobs
),
994 bus_unit_method_enqueue_job
,
995 SD_BUS_VTABLE_UNPRIVILEGED
),
996 SD_BUS_METHOD_WITH_ARGS("Kill",
997 SD_BUS_ARGS("s", whom
, "i", signal
),
999 bus_unit_method_kill
,
1000 SD_BUS_VTABLE_UNPRIVILEGED
),
1001 SD_BUS_METHOD_WITH_ARGS("QueueSignal",
1002 SD_BUS_ARGS("s", whom
, "i", signal
, "i", value
),
1004 bus_unit_method_kill
,
1005 SD_BUS_VTABLE_UNPRIVILEGED
),
1006 SD_BUS_METHOD("ResetFailed",
1009 bus_unit_method_reset_failed
,
1010 SD_BUS_VTABLE_UNPRIVILEGED
),
1011 SD_BUS_METHOD_WITH_ARGS("SetProperties",
1012 SD_BUS_ARGS("b", runtime
, "a(sv)", properties
),
1014 bus_unit_method_set_properties
,
1015 SD_BUS_VTABLE_UNPRIVILEGED
),
1016 SD_BUS_METHOD("Ref",
1019 bus_unit_method_ref
,
1020 SD_BUS_VTABLE_UNPRIVILEGED
),
1021 SD_BUS_METHOD("Unref",
1024 bus_unit_method_unref
,
1025 SD_BUS_VTABLE_UNPRIVILEGED
),
1026 SD_BUS_METHOD_WITH_ARGS("Clean",
1027 SD_BUS_ARGS("as", mask
),
1029 bus_unit_method_clean
,
1030 SD_BUS_VTABLE_UNPRIVILEGED
),
1031 SD_BUS_METHOD("Freeze",
1034 bus_unit_method_freeze
,
1035 SD_BUS_VTABLE_UNPRIVILEGED
),
1036 SD_BUS_METHOD("Thaw",
1039 bus_unit_method_thaw
,
1040 SD_BUS_VTABLE_UNPRIVILEGED
),
1042 /* For dependency types we don't support anymore always return an empty array */
1043 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1044 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1045 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1046 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
1047 /* Obsolete alias names */
1048 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1049 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_ratelimit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1054 static int property_get_slice(
1057 const char *interface
,
1058 const char *property
,
1059 sd_bus_message
*reply
,
1061 sd_bus_error
*error
) {
1063 Unit
*u
= ASSERT_PTR(userdata
);
1068 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
1071 static int property_get_current_memory(
1074 const char *interface
,
1075 const char *property
,
1076 sd_bus_message
*reply
,
1078 sd_bus_error
*error
) {
1080 uint64_t sz
= UINT64_MAX
;
1081 Unit
*u
= ASSERT_PTR(userdata
);
1087 r
= unit_get_memory_current(u
, &sz
);
1088 if (r
< 0 && r
!= -ENODATA
)
1089 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
1091 return sd_bus_message_append(reply
, "t", sz
);
1094 static int property_get_available_memory(
1097 const char *interface
,
1098 const char *property
,
1099 sd_bus_message
*reply
,
1101 sd_bus_error
*error
) {
1103 uint64_t sz
= UINT64_MAX
;
1104 Unit
*u
= ASSERT_PTR(userdata
);
1110 r
= unit_get_memory_available(u
, &sz
);
1111 if (r
< 0 && r
!= -ENODATA
)
1112 log_unit_warning_errno(u
, r
, "Failed to get total available memory from cgroup: %m");
1114 return sd_bus_message_append(reply
, "t", sz
);
1117 static int property_get_current_tasks(
1120 const char *interface
,
1121 const char *property
,
1122 sd_bus_message
*reply
,
1124 sd_bus_error
*error
) {
1126 uint64_t cn
= UINT64_MAX
;
1127 Unit
*u
= ASSERT_PTR(userdata
);
1133 r
= unit_get_tasks_current(u
, &cn
);
1134 if (r
< 0 && r
!= -ENODATA
)
1135 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
1137 return sd_bus_message_append(reply
, "t", cn
);
1140 static int property_get_cpu_usage(
1143 const char *interface
,
1144 const char *property
,
1145 sd_bus_message
*reply
,
1147 sd_bus_error
*error
) {
1149 nsec_t ns
= NSEC_INFINITY
;
1150 Unit
*u
= ASSERT_PTR(userdata
);
1156 r
= unit_get_cpu_usage(u
, &ns
);
1157 if (r
< 0 && r
!= -ENODATA
)
1158 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
1160 return sd_bus_message_append(reply
, "t", ns
);
1163 static int property_get_cpuset_cpus(
1166 const char *interface
,
1167 const char *property
,
1168 sd_bus_message
*reply
,
1170 sd_bus_error
*error
) {
1172 Unit
*u
= ASSERT_PTR(userdata
);
1173 _cleanup_(cpu_set_reset
) CPUSet cpus
= {};
1174 _cleanup_free_
uint8_t *array
= NULL
;
1180 (void) unit_get_cpuset(u
, &cpus
, "cpuset.cpus.effective");
1181 (void) cpu_set_to_dbus(&cpus
, &array
, &allocated
);
1182 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1185 static int property_get_cpuset_mems(
1188 const char *interface
,
1189 const char *property
,
1190 sd_bus_message
*reply
,
1192 sd_bus_error
*error
) {
1194 Unit
*u
= ASSERT_PTR(userdata
);
1195 _cleanup_(cpu_set_reset
) CPUSet mems
= {};
1196 _cleanup_free_
uint8_t *array
= NULL
;
1202 (void) unit_get_cpuset(u
, &mems
, "cpuset.mems.effective");
1203 (void) cpu_set_to_dbus(&mems
, &array
, &allocated
);
1204 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
1207 static int property_get_cgroup(
1210 const char *interface
,
1211 const char *property
,
1212 sd_bus_message
*reply
,
1214 sd_bus_error
*error
) {
1216 Unit
*u
= ASSERT_PTR(userdata
);
1217 const char *t
= NULL
;
1222 /* Three cases: a) u->cgroup_path is NULL, in which case the
1223 * unit has no control group, which we report as the empty
1224 * string. b) u->cgroup_path is the empty string, which
1225 * indicates the root cgroup, which we report as "/". c) all
1226 * other cases we report as-is. */
1229 t
= empty_to_root(u
->cgroup_path
);
1231 return sd_bus_message_append(reply
, "s", t
);
1234 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
1235 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
1241 r
= set_put(pids
, PID_TO_PTR(pid
));
1242 if (IN_SET(r
, 0, -EEXIST
))
1248 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
1257 (void) get_process_cmdline(pid
, SIZE_MAX
,
1258 PROCESS_CMDLINE_COMM_FALLBACK
| PROCESS_CMDLINE_QUOTE
,
1261 return sd_bus_message_append(reply
,
1268 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
1269 _cleanup_closedir_
DIR *d
= NULL
;
1270 _cleanup_fclose_
FILE *f
= NULL
;
1276 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
1285 /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
1286 * From https://docs.kernel.org/admin-guide/cgroup-v2.html#threads,
1287 * “cgroup.procs” in a threaded domain cgroup contains the PIDs of all processes in
1288 * the subtree and is not readable in the subtree proper. */
1289 r
= cg_read_pid(f
, &pid
);
1290 if (IN_SET(r
, 0, -EOPNOTSUPP
))
1295 if (is_kernel_thread(pid
) > 0)
1298 r
= append_process(reply
, p
, pid
, pids
);
1303 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1310 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1312 r
= cg_read_subgroup(d
, &g
);
1318 j
= path_join(empty_to_root(p
), g
);
1322 r
= append_cgroup(reply
, j
, pids
);
1330 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1331 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1332 _cleanup_set_free_ Set
*pids
= NULL
;
1338 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1342 pids
= set_new(NULL
);
1346 r
= sd_bus_message_new_method_return(message
, &reply
);
1350 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1354 if (u
->cgroup_path
) {
1355 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1360 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1361 PidRef
*pid
= unit_main_pid(u
);
1362 if (pidref_is_set(pid
)) {
1363 r
= append_process(reply
, NULL
, pid
->pid
, pids
);
1368 pid
= unit_control_pid(u
);
1369 if (pidref_is_set(pid
)) {
1370 r
= append_process(reply
, NULL
, pid
->pid
, pids
);
1375 r
= sd_bus_message_close_container(reply
);
1379 return sd_bus_send(NULL
, reply
, NULL
);
1382 static int property_get_ip_counter(
1385 const char *interface
,
1386 const char *property
,
1387 sd_bus_message
*reply
,
1389 sd_bus_error
*error
) {
1391 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1392 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1393 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1394 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1395 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1398 uint64_t value
= UINT64_MAX
;
1399 Unit
*u
= ASSERT_PTR(userdata
);
1406 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1407 (void) unit_get_ip_accounting(u
, metric
, &value
);
1408 return sd_bus_message_append(reply
, "t", value
);
1411 static int property_get_io_counter(
1414 const char *interface
,
1415 const char *property
,
1416 sd_bus_message
*reply
,
1418 sd_bus_error
*error
) {
1420 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1421 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1422 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1423 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1424 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1427 uint64_t value
= UINT64_MAX
;
1428 Unit
*u
= ASSERT_PTR(userdata
);
1435 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1436 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1437 return sd_bus_message_append(reply
, "t", value
);
1440 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1442 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1443 _cleanup_set_free_ Set
*pids
= NULL
;
1450 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1451 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1452 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1453 * subcgroup path has no effect! */
1455 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1459 r
= sd_bus_message_read(message
, "s", &path
);
1463 path
= empty_to_null(path
);
1465 if (!path_is_absolute(path
))
1466 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1468 if (!path_is_normalized(path
))
1469 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1472 if (!unit_cgroup_delegate(u
))
1473 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1475 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1476 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1478 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1482 r
= sd_bus_message_enter_container(message
, 'a', "u");
1486 uid_t process_uid
, sender_uid
;
1490 r
= sd_bus_message_read(message
, "u", &upid
);
1497 r
= sd_bus_creds_get_pid(creds
, &pid
);
1503 /* Filter out duplicates */
1504 if (set_contains(pids
, PID_TO_PTR(pid
)))
1507 /* Check if this process is suitable for attaching to this unit */
1508 r
= unit_pid_attachable(u
, pid
, error
);
1512 /* Let's query the sender's UID, so that we can make our security decisions */
1513 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1517 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1518 * then the process' UID and the target unit's UID have to match the sender's UID */
1519 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1520 r
= get_process_uid(pid
, &process_uid
);
1522 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1524 if (process_uid
!= sender_uid
)
1525 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1526 if (process_uid
!= u
->ref_uid
)
1527 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1531 pids
= set_new(NULL
);
1536 r
= set_put(pids
, PID_TO_PTR(pid
));
1541 r
= sd_bus_message_exit_container(message
);
1545 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1547 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1549 return sd_bus_reply_method_return(message
, NULL
);
1552 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1553 SD_BUS_VTABLE_START(0),
1554 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1555 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1556 SD_BUS_PROPERTY("ControlGroupId", "t", NULL
, offsetof(Unit
, cgroup_id
), 0),
1557 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1558 SD_BUS_PROPERTY("MemoryAvailable", "t", property_get_available_memory
, 0, 0),
1559 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1560 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus
, 0, 0),
1561 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems
, 0, 0),
1562 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1563 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1564 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1565 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1566 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1567 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1568 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1569 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1570 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1572 SD_BUS_METHOD_WITH_ARGS("GetProcesses",
1574 SD_BUS_ARGS("a(sus)", processes
),
1575 bus_unit_method_get_processes
,
1576 SD_BUS_VTABLE_UNPRIVILEGED
),
1578 SD_BUS_METHOD_WITH_ARGS("AttachProcesses",
1579 SD_BUS_ARGS("s", subcgroup
, "au", pids
),
1581 bus_unit_method_attach_processes
,
1582 SD_BUS_VTABLE_UNPRIVILEGED
),
1587 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1588 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1589 _cleanup_free_
char *p
= NULL
;
1590 Unit
*u
= ASSERT_PTR(userdata
);
1595 p
= unit_dbus_path(u
);
1599 r
= sd_bus_message_new_signal(
1602 "/org/freedesktop/systemd1",
1603 "org.freedesktop.systemd1.Manager",
1608 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1612 return sd_bus_send(bus
, m
, NULL
);
1615 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1616 _cleanup_free_
char *p
= NULL
;
1617 Unit
*u
= ASSERT_PTR(userdata
);
1622 p
= unit_dbus_path(u
);
1626 /* Send a properties changed signal. First for the specific
1627 * type, then for the generic unit. The clients may rely on
1628 * this order to get atomic behavior if needed. */
1630 r
= sd_bus_emit_properties_changed_strv(
1632 unit_dbus_interface_from_type(u
->type
),
1637 return sd_bus_emit_properties_changed_strv(
1639 "org.freedesktop.systemd1.Unit",
1643 void bus_unit_send_change_signal(Unit
*u
) {
1647 if (u
->in_dbus_queue
) {
1648 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1649 u
->in_dbus_queue
= false;
1651 /* The unit might be good to be GC once its pending signals have been sent */
1652 unit_add_to_gc_queue(u
);
1658 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1660 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1662 u
->sent_dbus_new_signal
= true;
1665 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1667 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1668 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1669 * so that clients can follow the full state transition */
1671 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1674 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1675 * the unit appears in the new state right-away (except if the
1676 * caller explicitly asked us to send it anyway) */
1679 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1680 * when we are reloading. */
1683 bus_unit_send_change_signal(u
);
1686 int bus_unit_send_pending_freezer_message(Unit
*u
, bool cancelled
) {
1687 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1692 if (!u
->pending_freezer_invocation
)
1696 r
= sd_bus_message_new_method_error(
1697 u
->pending_freezer_invocation
,
1699 &SD_BUS_ERROR_MAKE_CONST(
1700 BUS_ERROR_FREEZE_CANCELLED
, "Freeze operation aborted"));
1702 r
= sd_bus_message_new_method_return(u
->pending_freezer_invocation
, &reply
);
1706 r
= sd_bus_send(NULL
, reply
, NULL
);
1708 log_warning_errno(r
, "Failed to send queued message, ignoring: %m");
1710 u
->pending_freezer_invocation
= sd_bus_message_unref(u
->pending_freezer_invocation
);
1715 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1716 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1717 _cleanup_free_
char *p
= NULL
;
1718 Unit
*u
= ASSERT_PTR(userdata
);
1723 p
= unit_dbus_path(u
);
1727 r
= sd_bus_message_new_signal(
1730 "/org/freedesktop/systemd1",
1731 "org.freedesktop.systemd1.Manager",
1736 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1740 return sd_bus_send(bus
, m
, NULL
);
1743 void bus_unit_send_removed_signal(Unit
*u
) {
1747 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1748 bus_unit_send_change_signal(u
);
1753 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1755 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1758 int bus_unit_queue_job_one(
1759 sd_bus_message
*message
,
1763 BusUnitQueueFlags flags
,
1764 sd_bus_message
*reply
,
1765 sd_bus_error
*error
) {
1767 _cleanup_set_free_ Set
*affected
= NULL
;
1768 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1772 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1773 if (type
== JOB_RESTART
)
1774 type
= JOB_RELOAD_OR_START
;
1775 else if (type
== JOB_TRY_RESTART
)
1776 type
= JOB_TRY_RELOAD
;
1779 if (type
== JOB_STOP
&&
1780 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1781 unit_active_state(u
) == UNIT_INACTIVE
)
1782 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1784 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1785 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1786 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1787 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1788 return sd_bus_error_setf(error
,
1789 BUS_ERROR_ONLY_BY_DEPENDENCY
,
1790 "Operation refused, unit %s may be requested by dependency only (it is configured to refuse manual start/stop).",
1793 /* dbus-broker issues StartUnit for activation requests, and Type=dbus services automatically
1794 * gain dependency on dbus.socket. Therefore, if dbus has a pending stop job, the new start
1795 * job that pulls in dbus again would cause job type conflict. Let's avoid that by rejecting
1796 * job enqueuing early.
1798 * Note that unlike signal_activation_request(), we can't use unit_inactive_or_pending()
1799 * here. StartUnit is a more generic interface, and thus users are allowed to use e.g. systemctl
1800 * to start Type=dbus services even when dbus is inactive. */
1801 if (type
== JOB_START
&& u
->type
== UNIT_SERVICE
&& SERVICE(u
)->type
== SERVICE_DBUS
)
1802 FOREACH_STRING(dbus_unit
, SPECIAL_DBUS_SOCKET
, SPECIAL_DBUS_SERVICE
) {
1805 dbus
= manager_get_unit(u
->manager
, dbus_unit
);
1806 if (dbus
&& unit_stop_pending(dbus
))
1807 return sd_bus_error_setf(error
,
1808 BUS_ERROR_SHUTTING_DOWN
,
1809 "Operation for unit %s refused, D-Bus is shutting down.",
1813 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1814 affected
= set_new(NULL
);
1819 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1823 r
= bus_job_track_sender(j
, message
);
1827 /* Before we send the method reply, force out the announcement JobNew for this job */
1828 bus_job_send_pending_change_signal(j
, true);
1830 job_path
= job_dbus_path(j
);
1834 /* The classic response is just a job object path */
1835 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1836 return sd_bus_message_append(reply
, "o", job_path
);
1838 /* In verbose mode respond with the anchor job plus everything that has been affected */
1840 unit_path
= unit_dbus_path(j
->unit
);
1844 r
= sd_bus_message_append(reply
, "uosos",
1846 j
->unit
->id
, unit_path
,
1847 job_type_to_string(j
->type
));
1851 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1855 SET_FOREACH(a
, affected
) {
1859 /* Free paths from previous iteration */
1860 job_path
= mfree(job_path
);
1861 unit_path
= mfree(unit_path
);
1863 job_path
= job_dbus_path(a
);
1867 unit_path
= unit_dbus_path(a
->unit
);
1871 r
= sd_bus_message_append(reply
, "(uosos)",
1873 a
->unit
->id
, unit_path
,
1874 job_type_to_string(a
->type
));
1879 return sd_bus_message_close_container(reply
);
1882 int bus_unit_queue_job(
1883 sd_bus_message
*message
,
1887 BusUnitQueueFlags flags
,
1888 sd_bus_error
*error
) {
1890 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1895 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1896 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1898 r
= mac_selinux_unit_access_check(
1900 job_type_to_access_method(type
),
1905 r
= sd_bus_message_new_method_return(message
, &reply
);
1909 r
= bus_unit_queue_job_one(message
, u
, type
, mode
, flags
, reply
, error
);
1913 return sd_bus_send(NULL
, reply
, NULL
);
1916 static int bus_unit_set_live_property(
1919 sd_bus_message
*message
,
1920 UnitWriteFlags flags
,
1921 sd_bus_error
*error
) {
1929 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for
1930 * transient units that are being created). */
1932 if (streq(name
, "Description")) {
1935 r
= sd_bus_message_read(message
, "s", &d
);
1939 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1940 r
= unit_set_description(u
, d
);
1944 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1950 /* A setting that only applies to active units. We don't actually write this to /run, this state is
1951 * managed internally. "+foo" sets flag foo, "-foo" unsets flag foo, just "foo" resets flags to
1952 * foo. The last type cannot be mixed with "+" or "-". */
1954 if (streq(name
, "Markers")) {
1955 unsigned settings
= 0, mask
= 0;
1956 bool some_plus_minus
= false, some_absolute
= false;
1958 r
= sd_bus_message_enter_container(message
, 'a', "s");
1966 r
= sd_bus_message_read(message
, "s", &word
);
1972 if (IN_SET(word
[0], '+', '-')) {
1975 some_plus_minus
= true;
1978 some_absolute
= true;
1981 UnitMarker m
= unit_marker_from_string(word
);
1983 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
,
1984 "Unknown marker \"%s\".", word
);
1986 SET_FLAG(settings
, 1u << m
, b
);
1987 SET_FLAG(mask
, 1u << m
, true);
1990 r
= sd_bus_message_exit_container(message
);
1994 if (some_plus_minus
&& some_absolute
)
1995 return sd_bus_error_set(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Bad marker syntax.");
1997 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1999 u
->markers
= settings
;
2001 u
->markers
= settings
| (u
->markers
& ~mask
);
2010 static int bus_set_transient_emergency_action(
2014 sd_bus_message
*message
,
2015 UnitWriteFlags flags
,
2016 sd_bus_error
*error
) {
2024 r
= sd_bus_message_read(message
, "s", &s
);
2028 r
= parse_emergency_action(s
, u
->manager
->runtime_scope
, &v
);
2030 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
2031 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
2032 : "Invalid %s setting: %s",
2035 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2037 unit_write_settingf(u
, flags
, name
,
2044 static int bus_set_transient_exit_status(
2048 sd_bus_message
*message
,
2049 UnitWriteFlags flags
,
2050 sd_bus_error
*error
) {
2057 r
= sd_bus_message_read(message
, "i", &k
);
2062 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
2064 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2065 *p
= k
< 0 ? -1 : k
;
2068 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2070 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
2076 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
2077 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
2079 static int bus_set_transient_conditions(
2084 sd_bus_message
*message
,
2085 UnitWriteFlags flags
,
2086 sd_bus_error
*error
) {
2088 const char *type_name
, *param
;
2089 int trigger
, negate
, r
;
2094 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
2098 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
2101 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
2103 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
2106 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
2108 if (condition_takes_path(t
) && !path_is_absolute(param
))
2109 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
2111 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2114 c
= condition_new(t
, param
, trigger
, negate
);
2118 LIST_PREPEND(conditions
, *list
, c
);
2120 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
2121 "%s=%s%s%s", type_name
,
2122 trigger
? "|" : "", negate
? "!" : "", param
);
2130 r
= sd_bus_message_exit_container(message
);
2134 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
2135 *list
= condition_free_list(*list
);
2136 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
2142 static int bus_unit_set_transient_property(
2145 sd_bus_message
*message
,
2146 UnitWriteFlags flags
,
2147 sd_bus_error
*error
) {
2156 /* Handles settings when transient units are created. This settings cannot be altered anymore after
2157 * the unit has been created. */
2159 if (streq(name
, "SourcePath"))
2160 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
2162 if (streq(name
, "StopWhenUnneeded"))
2163 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
2165 if (streq(name
, "RefuseManualStart"))
2166 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
2168 if (streq(name
, "RefuseManualStop"))
2169 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
2171 if (streq(name
, "AllowIsolate"))
2172 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
2174 if (streq(name
, "DefaultDependencies"))
2175 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
2177 if (streq(name
, "OnSuccessJobMode"))
2178 return bus_set_transient_job_mode(u
, name
, &u
->on_success_job_mode
, message
, flags
, error
);
2180 if (streq(name
, "OnFailureJobMode"))
2181 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
2183 if (streq(name
, "IgnoreOnIsolate"))
2184 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
2186 if (streq(name
, "JobTimeoutUSec")) {
2187 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
2188 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
2189 u
->job_running_timeout
= u
->job_timeout
;
2192 if (streq(name
, "JobRunningTimeoutUSec")) {
2193 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
2194 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
2195 u
->job_running_timeout_set
= true;
2200 if (streq(name
, "JobTimeoutAction"))
2201 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
2203 if (streq(name
, "JobTimeoutRebootArgument"))
2204 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
2206 if (streq(name
, "StartLimitIntervalUSec"))
2207 return bus_set_transient_usec(u
, name
, &u
->start_ratelimit
.interval
, message
, flags
, error
);
2209 if (streq(name
, "StartLimitBurst"))
2210 return bus_set_transient_unsigned(u
, name
, &u
->start_ratelimit
.burst
, message
, flags
, error
);
2212 if (streq(name
, "StartLimitAction"))
2213 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
2215 if (streq(name
, "FailureAction"))
2216 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
2218 if (streq(name
, "SuccessAction"))
2219 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
2221 if (streq(name
, "FailureActionExitStatus"))
2222 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
2224 if (streq(name
, "SuccessActionExitStatus"))
2225 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
2227 if (streq(name
, "RebootArgument"))
2228 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
2230 if (streq(name
, "CollectMode"))
2231 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
2233 if (streq(name
, "Conditions"))
2234 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
2236 if (streq(name
, "Asserts"))
2237 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
2239 if (streq(name
, "Documentation")) {
2240 _cleanup_strv_free_
char **l
= NULL
;
2242 r
= sd_bus_message_read_strv(message
, &l
);
2247 if (!documentation_url_is_valid(*p
))
2248 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
2250 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2251 if (strv_isempty(l
)) {
2252 u
->documentation
= strv_free(u
->documentation
);
2253 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2255 strv_extend_strv(&u
->documentation
, l
, false);
2258 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2264 } else if (streq(name
, "Slice")) {
2268 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
2269 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
2270 if (u
->type
== UNIT_SLICE
)
2271 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
2272 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
2273 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
2275 r
= sd_bus_message_read(message
, "s", &s
);
2279 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
2280 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
2282 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2283 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2284 * instead of manager_load_unit() on purpose, here. */
2285 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
2289 if (slice
->type
!= UNIT_SLICE
)
2290 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
2292 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2293 r
= unit_set_slice(u
, slice
);
2297 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
2302 } else if (streq(name
, "RequiresMountsFor")) {
2303 _cleanup_strv_free_
char **l
= NULL
;
2305 r
= sd_bus_message_read_strv(message
, &l
);
2309 STRV_FOREACH(p
, l
) {
2312 if (!path_is_absolute(*p
))
2313 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
2315 if (!path_is_valid(*p
))
2316 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
2318 if (!path_is_normalized(*p
))
2319 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
2321 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2322 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
2324 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
2326 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
2333 if (streq(name
, "RequiresOverridable"))
2334 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
2335 else if (streq(name
, "RequisiteOverridable"))
2336 d
= UNIT_REQUISITE
; /* same here */
2338 d
= unit_dependency_from_string(name
);
2355 UNIT_PROPAGATES_RELOAD_TO
,
2356 UNIT_RELOAD_PROPAGATED_FROM
,
2357 UNIT_PROPAGATES_STOP_TO
,
2358 UNIT_STOP_PROPAGATED_FROM
,
2359 UNIT_JOINS_NAMESPACE_OF
))
2360 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d
));
2362 r
= sd_bus_message_enter_container(message
, 'a', "s");
2366 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
2367 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
2368 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
2370 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2371 _cleanup_free_
char *label
= NULL
;
2373 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
2377 label
= strjoin(name
, "-", other
);
2381 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
2388 r
= sd_bus_message_exit_container(message
);
2394 } else if (streq(name
, "AddRef")) {
2398 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2399 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2400 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2401 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2402 * dependency type, hence let's not confuse things with that.
2404 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2405 * the transient unit is complete, so that setting this property multiple times in the same transient
2406 * unit creation call doesn't count as individual references. */
2408 r
= sd_bus_message_read(message
, "b", &b
);
2412 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
2413 u
->bus_track_add
= b
;
2421 int bus_unit_set_properties(
2423 sd_bus_message
*message
,
2424 UnitWriteFlags flags
,
2426 sd_bus_error
*error
) {
2428 bool for_real
= false;
2435 /* We iterate through the array twice. First run just checks if all passed data is valid, second run
2436 * actually applies it. This implements transaction-like behaviour without actually providing full
2439 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
2447 r
= sd_bus_message_enter_container(message
, 'r', "sv");
2451 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
2454 /* Reached EOF. Let's try again, and this time for realz... */
2455 r
= sd_bus_message_rewind(message
, false);
2463 r
= sd_bus_message_read(message
, "s", &name
);
2467 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
2471 /* If not for real, then mask out the two target flags */
2472 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
2474 if (UNIT_VTABLE(u
)->bus_set_property
)
2475 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
2478 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
2479 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
2481 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
2486 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
,
2487 "Cannot set property %s, or unknown property.", name
);
2489 r
= sd_bus_message_exit_container(message
);
2493 r
= sd_bus_message_exit_container(message
);
2500 r
= sd_bus_message_exit_container(message
);
2504 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2505 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2510 /* Pretty much any of the calls above can fail if the message is not formed properly
2511 * or if it has unexpected contents. Fill in a more informative error message here. */
2512 if (sd_bus_error_is_set(error
))
2514 return sd_bus_error_set_errnof(error
, r
,
2515 r
== -ENXIO
? "Failed to set unit properties: Unexpected message contents"
2516 : "Failed to set unit properties: %m");
2519 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2522 /* Generates a pretty error if a unit isn't properly loaded. */
2524 switch (u
->load_state
) {
2529 case UNIT_NOT_FOUND
:
2530 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2532 case UNIT_BAD_SETTING
:
2533 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2535 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2536 return sd_bus_error_set_errnof(error
, u
->load_error
,
2537 "Unit %s failed to load properly, please adjust/correct and reload service manager: %m", u
->id
);
2540 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2545 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2549 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2550 Unit
*u
= ASSERT_PTR(userdata
);
2554 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2556 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2558 unit_add_to_cgroup_empty_queue(u
);
2560 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2561 unit_add_to_gc_queue(u
);
2566 static int bus_unit_allocate_bus_track(Unit
*u
) {
2574 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2578 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2580 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2587 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2592 r
= bus_unit_allocate_bus_track(u
);
2596 return sd_bus_track_add_name(u
->bus_track
, name
);
2599 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2604 r
= bus_unit_allocate_bus_track(u
);
2608 return sd_bus_track_add_sender(u
->bus_track
, m
);
2611 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2614 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet,
2615 * return an error */
2619 return sd_bus_track_remove_sender(u
->bus_track
, m
);