1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
10 #include "alloc-util.h"
11 #include "bpf-firewall.h"
12 #include "bus-common-errors.h"
13 #include "cgroup-util.h"
14 #include "condition.h"
16 #include "dbus-unit.h"
17 #include "dbus-util.h"
20 #include "locale-util.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "selinux-access.h"
25 #include "signal-util.h"
27 #include "string-util.h"
29 #include "user-util.h"
32 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
33 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
34 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
35 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
37 static int property_get_names(
40 const char *interface
,
42 sd_bus_message
*reply
,
44 sd_bus_error
*error
) {
55 r
= sd_bus_message_open_container(reply
, 'a', "s");
59 SET_FOREACH(t
, u
->names
, i
) {
60 r
= sd_bus_message_append(reply
, "s", t
);
65 return sd_bus_message_close_container(reply
);
68 static int property_get_following(
71 const char *interface
,
73 sd_bus_message
*reply
,
75 sd_bus_error
*error
) {
77 Unit
*u
= userdata
, *f
;
83 f
= unit_following(u
);
84 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
87 static int property_get_dependencies(
90 const char *interface
,
92 sd_bus_message
*reply
,
94 sd_bus_error
*error
) {
96 Hashmap
*h
= *(Hashmap
**) userdata
;
105 r
= sd_bus_message_open_container(reply
, 'a', "s");
109 HASHMAP_FOREACH_KEY(v
, u
, h
, j
) {
110 r
= sd_bus_message_append(reply
, "s", u
->id
);
115 return sd_bus_message_close_container(reply
);
118 static int property_get_obsolete_dependencies(
121 const char *interface
,
122 const char *property
,
123 sd_bus_message
*reply
,
125 sd_bus_error
*error
) {
130 /* For dependency types we don't support anymore always return an empty array */
131 return sd_bus_message_append(reply
, "as", 0);
134 static int property_get_requires_mounts_for(
137 const char *interface
,
138 const char *property
,
139 sd_bus_message
*reply
,
141 sd_bus_error
*error
) {
143 Hashmap
*h
= *(Hashmap
**) userdata
;
152 r
= sd_bus_message_open_container(reply
, 'a', "s");
156 HASHMAP_FOREACH_KEY(v
, p
, h
, j
) {
157 r
= sd_bus_message_append(reply
, "s", p
);
162 return sd_bus_message_close_container(reply
);
165 static int property_get_description(
168 const char *interface
,
169 const char *property
,
170 sd_bus_message
*reply
,
172 sd_bus_error
*error
) {
180 return sd_bus_message_append(reply
, "s", unit_description(u
));
183 static int property_get_active_state(
186 const char *interface
,
187 const char *property
,
188 sd_bus_message
*reply
,
190 sd_bus_error
*error
) {
198 return sd_bus_message_append(reply
, "s", unit_active_state_to_string(unit_active_state(u
)));
201 static int property_get_sub_state(
204 const char *interface
,
205 const char *property
,
206 sd_bus_message
*reply
,
208 sd_bus_error
*error
) {
216 return sd_bus_message_append(reply
, "s", unit_sub_state_to_string(u
));
219 static int property_get_unit_file_preset(
222 const char *interface
,
223 const char *property
,
224 sd_bus_message
*reply
,
226 sd_bus_error
*error
) {
235 r
= unit_get_unit_file_preset(u
);
237 return sd_bus_message_append(reply
, "s",
239 r
> 0 ? "enabled" : "disabled");
242 static int property_get_unit_file_state(
245 const char *interface
,
246 const char *property
,
247 sd_bus_message
*reply
,
249 sd_bus_error
*error
) {
257 return sd_bus_message_append(reply
, "s", unit_file_state_to_string(unit_get_unit_file_state(u
)));
260 static int property_get_can_start(
263 const char *interface
,
264 const char *property
,
265 sd_bus_message
*reply
,
267 sd_bus_error
*error
) {
275 return sd_bus_message_append(reply
, "b", unit_can_start(u
) && !u
->refuse_manual_start
);
278 static int property_get_can_stop(
281 const char *interface
,
282 const char *property
,
283 sd_bus_message
*reply
,
285 sd_bus_error
*error
) {
293 return sd_bus_message_append(reply
, "b", unit_can_stop(u
) && !u
->refuse_manual_stop
);
296 static int property_get_can_reload(
299 const char *interface
,
300 const char *property
,
301 sd_bus_message
*reply
,
303 sd_bus_error
*error
) {
311 return sd_bus_message_append(reply
, "b", unit_can_reload(u
));
314 static int property_get_can_isolate(
317 const char *interface
,
318 const char *property
,
319 sd_bus_message
*reply
,
321 sd_bus_error
*error
) {
329 return sd_bus_message_append(reply
, "b", unit_can_isolate(u
) && !u
->refuse_manual_start
);
332 static int property_get_job(
335 const char *interface
,
336 const char *property
,
337 sd_bus_message
*reply
,
339 sd_bus_error
*error
) {
341 _cleanup_free_
char *p
= NULL
;
349 return sd_bus_message_append(reply
, "(uo)", 0, "/");
351 p
= job_dbus_path(u
->job
);
355 return sd_bus_message_append(reply
, "(uo)", u
->job
->id
, p
);
358 static int property_get_need_daemon_reload(
361 const char *interface
,
362 const char *property
,
363 sd_bus_message
*reply
,
365 sd_bus_error
*error
) {
373 return sd_bus_message_append(reply
, "b", unit_need_daemon_reload(u
));
376 static int property_get_conditions(
379 const char *interface
,
380 const char *property
,
381 sd_bus_message
*reply
,
383 sd_bus_error
*error
) {
385 const char *(*to_string
)(ConditionType type
) = NULL
;
386 Condition
**list
= userdata
, *c
;
393 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
395 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
399 LIST_FOREACH(conditions
, c
, *list
) {
403 c
->result
== CONDITION_UNTESTED
? 0 :
404 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
406 r
= sd_bus_message_append(reply
, "(sbbsi)",
408 c
->trigger
, c
->negate
,
409 c
->parameter
, tristate
);
415 return sd_bus_message_close_container(reply
);
418 static int property_get_load_error(
421 const char *interface
,
422 const char *property
,
423 sd_bus_message
*reply
,
425 sd_bus_error
*error
) {
427 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
434 if (u
->load_error
!= 0)
435 sd_bus_error_set_errno(&e
, u
->load_error
);
437 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
440 static int bus_verify_manage_units_async_full(
444 const char *polkit_message
,
446 sd_bus_message
*call
,
447 sd_bus_error
*error
) {
449 const char *details
[9] = {
454 if (polkit_message
) {
455 details
[4] = "polkit.message";
456 details
[5] = polkit_message
;
457 details
[6] = "polkit.gettext_domain";
458 details
[7] = GETTEXT_PACKAGE
;
461 return bus_verify_polkit_async(
464 "org.freedesktop.systemd1.manage-units",
468 &u
->manager
->polkit_registry
,
472 int bus_unit_method_start_generic(
473 sd_bus_message
*message
,
476 bool reload_if_possible
,
477 sd_bus_error
*error
) {
481 _cleanup_free_
char *verb
= NULL
;
482 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
483 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
484 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
485 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
486 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
487 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
493 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
495 r
= mac_selinux_unit_access_check(
497 job_type_to_access_method(job_type
),
502 r
= sd_bus_message_read(message
, "s", &smode
);
506 mode
= job_mode_from_string(smode
);
508 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
510 if (reload_if_possible
)
511 verb
= strjoin("reload-or-", job_type_to_string(job_type
));
513 verb
= strdup(job_type_to_string(job_type
));
517 r
= bus_verify_manage_units_async_full(
521 job_type
< _JOB_TYPE_MAX
? polkit_message_for_job
[job_type
] : NULL
,
528 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
530 return bus_unit_queue_job(message
, u
, job_type
, mode
, reload_if_possible
, error
);
533 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
534 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
537 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
538 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
541 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
542 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
545 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
546 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
549 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
550 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
553 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
554 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
557 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
558 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
561 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
571 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
575 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
582 who
= kill_who_from_string(swho
);
584 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
587 if (!SIGNAL_VALID(signo
))
588 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
590 r
= bus_verify_manage_units_async_full(
594 N_("Authentication is required to kill '$(unit)'."),
601 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
603 r
= unit_kill(u
, who
, signo
, error
);
607 return sd_bus_reply_method_return(message
, NULL
);
610 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
617 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
621 r
= bus_verify_manage_units_async_full(
625 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
632 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
634 unit_reset_failed(u
);
636 return sd_bus_reply_method_return(message
, NULL
);
639 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
646 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
650 r
= sd_bus_message_read(message
, "b", &runtime
);
654 r
= bus_verify_manage_units_async_full(
658 N_("Authentication is required to set properties on '$(unit)'."),
665 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
667 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
671 return sd_bus_reply_method_return(message
, NULL
);
674 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
681 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
685 r
= bus_verify_manage_units_async_full(
696 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
698 r
= bus_unit_track_add_sender(u
, message
);
702 return sd_bus_reply_method_return(message
, NULL
);
705 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
712 r
= bus_unit_track_remove_sender(u
, message
);
714 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
718 return sd_bus_reply_method_return(message
, NULL
);
721 const sd_bus_vtable bus_unit_vtable
[] = {
722 SD_BUS_VTABLE_START(0),
724 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
725 SD_BUS_PROPERTY("Names", "as", property_get_names
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
726 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
727 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
728 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
729 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
730 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
731 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
732 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
733 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
734 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
735 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
736 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
737 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
738 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
739 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
740 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
741 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
742 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
743 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
744 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
745 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
746 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
747 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
748 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
749 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
750 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
751 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
752 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
753 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
754 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
755 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
756 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
757 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
758 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
759 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
760 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
761 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
762 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
763 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
764 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
765 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
766 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
767 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
768 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
769 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
770 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
771 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
772 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
773 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
774 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
775 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
776 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
777 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
778 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
779 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
780 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
781 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
782 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
783 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
784 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), 0),
785 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), 0),
786 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
787 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
788 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
789 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
790 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_limit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
791 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
792 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
793 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
794 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
795 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), 0),
796 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), 0),
798 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
799 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
800 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
801 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
802 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
803 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
804 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
805 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
806 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
807 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
808 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
809 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
811 /* Obsolete properties or obsolete alias names */
812 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
813 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
814 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
815 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
816 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
817 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
821 static int property_get_slice(
824 const char *interface
,
825 const char *property
,
826 sd_bus_message
*reply
,
828 sd_bus_error
*error
) {
836 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
839 static int property_get_current_memory(
842 const char *interface
,
843 const char *property
,
844 sd_bus_message
*reply
,
846 sd_bus_error
*error
) {
848 uint64_t sz
= (uint64_t) -1;
856 r
= unit_get_memory_current(u
, &sz
);
857 if (r
< 0 && r
!= -ENODATA
)
858 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
860 return sd_bus_message_append(reply
, "t", sz
);
863 static int property_get_current_tasks(
866 const char *interface
,
867 const char *property
,
868 sd_bus_message
*reply
,
870 sd_bus_error
*error
) {
872 uint64_t cn
= (uint64_t) -1;
880 r
= unit_get_tasks_current(u
, &cn
);
881 if (r
< 0 && r
!= -ENODATA
)
882 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
884 return sd_bus_message_append(reply
, "t", cn
);
887 static int property_get_cpu_usage(
890 const char *interface
,
891 const char *property
,
892 sd_bus_message
*reply
,
894 sd_bus_error
*error
) {
896 nsec_t ns
= (nsec_t
) -1;
904 r
= unit_get_cpu_usage(u
, &ns
);
905 if (r
< 0 && r
!= -ENODATA
)
906 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
908 return sd_bus_message_append(reply
, "t", ns
);
911 static int property_get_cgroup(
914 const char *interface
,
915 const char *property
,
916 sd_bus_message
*reply
,
918 sd_bus_error
*error
) {
921 const char *t
= NULL
;
927 /* Three cases: a) u->cgroup_path is NULL, in which case the
928 * unit has no control group, which we report as the empty
929 * string. b) u->cgroup_path is the empty string, which
930 * indicates the root cgroup, which we report as "/". c) all
931 * other cases we report as-is. */
934 t
= empty_to_root(u
->cgroup_path
);
936 return sd_bus_message_append(reply
, "s", t
);
939 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
940 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
946 r
= set_put(pids
, PID_TO_PTR(pid
));
947 if (IN_SET(r
, 0, -EEXIST
))
953 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
962 (void) get_process_cmdline(pid
, 0, true, &cmdline
);
964 return sd_bus_message_append(reply
,
971 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
972 _cleanup_closedir_
DIR *d
= NULL
;
973 _cleanup_fclose_
FILE *f
= NULL
;
979 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
988 r
= cg_read_pid(f
, &pid
);
994 if (is_kernel_thread(pid
) > 0)
997 r
= append_process(reply
, p
, pid
, pids
);
1002 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
1009 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
1011 r
= cg_read_subgroup(d
, &g
);
1017 j
= strjoin(p
, "/", g
);
1021 r
= append_cgroup(reply
, j
, pids
);
1029 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1030 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1031 _cleanup_(set_freep
) Set
*pids
= NULL
;
1038 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
1042 pids
= set_new(NULL
);
1046 r
= sd_bus_message_new_method_return(message
, &reply
);
1050 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1054 if (u
->cgroup_path
) {
1055 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1060 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1061 pid
= unit_main_pid(u
);
1063 r
= append_process(reply
, NULL
, pid
, pids
);
1068 pid
= unit_control_pid(u
);
1070 r
= append_process(reply
, NULL
, pid
, pids
);
1075 r
= sd_bus_message_close_container(reply
);
1079 return sd_bus_send(NULL
, reply
, NULL
);
1082 static int property_get_ip_counter(
1085 const char *interface
,
1086 const char *property
,
1087 sd_bus_message
*reply
,
1089 sd_bus_error
*error
) {
1091 CGroupIPAccountingMetric metric
;
1092 uint64_t value
= (uint64_t) -1;
1100 if (streq(property
, "IPIngressBytes"))
1101 metric
= CGROUP_IP_INGRESS_BYTES
;
1102 else if (streq(property
, "IPIngressPackets"))
1103 metric
= CGROUP_IP_INGRESS_PACKETS
;
1104 else if (streq(property
, "IPEgressBytes"))
1105 metric
= CGROUP_IP_EGRESS_BYTES
;
1107 assert(streq(property
, "IPEgressPackets"));
1108 metric
= CGROUP_IP_EGRESS_PACKETS
;
1111 (void) unit_get_ip_accounting(u
, metric
, &value
);
1112 return sd_bus_message_append(reply
, "t", value
);
1115 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1117 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1118 _cleanup_(set_freep
) Set
*pids
= NULL
;
1125 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1126 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1127 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1128 * subcgroup path has no effect! */
1130 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1134 r
= sd_bus_message_read(message
, "s", &path
);
1138 path
= empty_to_null(path
);
1140 if (!path_is_absolute(path
))
1141 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1143 if (!path_is_normalized(path
))
1144 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1147 if (!unit_cgroup_delegate(u
))
1148 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1150 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1151 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1153 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1157 r
= sd_bus_message_enter_container(message
, 'a', "u");
1161 uid_t process_uid
, sender_uid
;
1165 r
= sd_bus_message_read(message
, "u", &upid
);
1172 r
= sd_bus_creds_get_pid(creds
, &pid
);
1178 /* Filter out duplicates */
1179 if (set_contains(pids
, PID_TO_PTR(pid
)))
1182 /* Check if this process is suitable for attaching to this unit */
1183 r
= unit_pid_attachable(u
, pid
, error
);
1187 /* Let's query the sender's UID, so that we can make our security decisions */
1188 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1192 /* Let's validate security: if the sender is root, then all is OK. If the sender is is any other unit,
1193 * then the process' UID and the target unit's UID have to match the sender's UID */
1194 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1195 r
= get_process_uid(pid
, &process_uid
);
1197 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1199 if (process_uid
!= sender_uid
)
1200 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1201 if (process_uid
!= u
->ref_uid
)
1202 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1206 pids
= set_new(NULL
);
1211 r
= set_put(pids
, PID_TO_PTR(pid
));
1216 r
= sd_bus_message_exit_container(message
);
1220 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1222 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1224 return sd_bus_reply_method_return(message
, NULL
);
1227 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1228 SD_BUS_VTABLE_START(0),
1229 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1230 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1231 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1232 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1233 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1234 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1235 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1236 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1237 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1238 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1239 SD_BUS_METHOD("AttachProcesses", "sau", NULL
, bus_unit_method_attach_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1243 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1244 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1245 _cleanup_free_
char *p
= NULL
;
1252 p
= unit_dbus_path(u
);
1256 r
= sd_bus_message_new_signal(
1259 "/org/freedesktop/systemd1",
1260 "org.freedesktop.systemd1.Manager",
1265 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1269 return sd_bus_send(bus
, m
, NULL
);
1272 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1273 _cleanup_free_
char *p
= NULL
;
1280 p
= unit_dbus_path(u
);
1284 /* Send a properties changed signal. First for the specific
1285 * type, then for the generic unit. The clients may rely on
1286 * this order to get atomic behavior if needed. */
1288 r
= sd_bus_emit_properties_changed_strv(
1290 unit_dbus_interface_from_type(u
->type
),
1295 return sd_bus_emit_properties_changed_strv(
1297 "org.freedesktop.systemd1.Unit",
1301 void bus_unit_send_change_signal(Unit
*u
) {
1305 if (u
->in_dbus_queue
) {
1306 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1307 u
->in_dbus_queue
= false;
1313 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1315 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1317 u
->sent_dbus_new_signal
= true;
1320 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1321 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1322 _cleanup_free_
char *p
= NULL
;
1329 p
= unit_dbus_path(u
);
1333 r
= sd_bus_message_new_signal(
1336 "/org/freedesktop/systemd1",
1337 "org.freedesktop.systemd1.Manager",
1342 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1346 return sd_bus_send(bus
, m
, NULL
);
1349 void bus_unit_send_removed_signal(Unit
*u
) {
1353 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1354 bus_unit_send_change_signal(u
);
1359 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1361 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1364 int bus_unit_queue_job(
1365 sd_bus_message
*message
,
1369 bool reload_if_possible
,
1370 sd_bus_error
*error
) {
1372 _cleanup_free_
char *path
= NULL
;
1378 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1379 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1381 r
= mac_selinux_unit_access_check(
1383 job_type_to_access_method(type
),
1388 if (reload_if_possible
&& unit_can_reload(u
)) {
1389 if (type
== JOB_RESTART
)
1390 type
= JOB_RELOAD_OR_START
;
1391 else if (type
== JOB_TRY_RESTART
)
1392 type
= JOB_TRY_RELOAD
;
1395 if (type
== JOB_STOP
&&
1396 (IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
)) &&
1397 unit_active_state(u
) == UNIT_INACTIVE
)
1398 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1400 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1401 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1402 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1403 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1404 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
);
1406 r
= manager_add_job(u
->manager
, type
, u
, mode
, error
, &j
);
1410 r
= bus_job_track_sender(j
, message
);
1414 path
= job_dbus_path(j
);
1418 return sd_bus_reply_method_return(message
, "o", path
);
1421 static int bus_unit_set_live_property(
1424 sd_bus_message
*message
,
1425 UnitWriteFlags flags
,
1426 sd_bus_error
*error
) {
1434 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1435 * units that are being created). */
1437 if (streq(name
, "Description")) {
1440 r
= sd_bus_message_read(message
, "s", &d
);
1444 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1445 r
= unit_set_description(u
, d
);
1449 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1458 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1459 static BUS_DEFINE_SET_TRANSIENT_PARSE(emergency_action
, EmergencyAction
, emergency_action_from_string
);
1460 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1462 static int bus_set_transient_conditions(
1467 sd_bus_message
*message
,
1468 UnitWriteFlags flags
,
1469 sd_bus_error
*error
) {
1471 const char *type_name
, *param
;
1472 int trigger
, negate
, r
;
1477 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1481 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1484 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1486 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1488 if (t
!= CONDITION_NULL
) {
1490 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1492 if (condition_takes_path(t
) && !path_is_absolute(param
))
1493 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1497 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1500 c
= condition_new(t
, param
, trigger
, negate
);
1504 LIST_PREPEND(conditions
, *list
, c
);
1506 if (t
!= CONDITION_NULL
)
1507 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1508 "%s=%s%s%s", type_name
,
1509 trigger
? "|" : "", negate
? "!" : "", param
);
1511 unit_write_settingf(u
, flags
, name
,
1512 "%s=%s%s", type_name
,
1513 trigger
? "|" : "", yes_no(!negate
));
1521 r
= sd_bus_message_exit_container(message
);
1525 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1526 *list
= condition_free_list(*list
);
1527 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1533 static int bus_unit_set_transient_property(
1536 sd_bus_message
*message
,
1537 UnitWriteFlags flags
,
1538 sd_bus_error
*error
) {
1540 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1547 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1548 * has been created. */
1550 if (streq(name
, "SourcePath"))
1551 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1553 if (streq(name
, "StopWhenUnneeded"))
1554 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1556 if (streq(name
, "RefuseManualStart"))
1557 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1559 if (streq(name
, "RefuseManualStop"))
1560 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1562 if (streq(name
, "AllowIsolate"))
1563 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1565 if (streq(name
, "DefaultDependencies"))
1566 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1568 if (streq(name
, "OnFailureJobMode"))
1569 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1571 if (streq(name
, "IgnoreOnIsolate"))
1572 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1574 if (streq(name
, "JobTimeoutUSec")) {
1575 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1576 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1577 u
->job_running_timeout
= u
->job_timeout
;
1580 if (streq(name
, "JobRunningTimeoutUSec")) {
1581 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1582 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1583 u
->job_running_timeout_set
= true;
1588 if (streq(name
, "JobTimeoutAction"))
1589 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1591 if (streq(name
, "JobTimeoutRebootArgument"))
1592 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1594 if (streq(name
, "StartLimitIntervalUSec"))
1595 return bus_set_transient_usec(u
, name
, &u
->start_limit
.interval
, message
, flags
, error
);
1597 if (streq(name
, "StartLimitBurst"))
1598 return bus_set_transient_unsigned(u
, name
, &u
->start_limit
.burst
, message
, flags
, error
);
1600 if (streq(name
, "StartLimitAction"))
1601 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1603 if (streq(name
, "FailureAction"))
1604 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1606 if (streq(name
, "SuccessAction"))
1607 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1609 if (streq(name
, "RebootArgument"))
1610 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1612 if (streq(name
, "CollectMode"))
1613 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
1615 if (streq(name
, "Conditions"))
1616 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
1618 if (streq(name
, "Asserts"))
1619 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
1621 if (streq(name
, "Documentation")) {
1622 _cleanup_strv_free_
char **l
= NULL
;
1625 r
= sd_bus_message_read_strv(message
, &l
);
1629 STRV_FOREACH(p
, l
) {
1630 if (!documentation_url_is_valid(*p
))
1631 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
1634 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1635 if (strv_isempty(l
)) {
1636 u
->documentation
= strv_free(u
->documentation
);
1637 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1639 strv_extend_strv(&u
->documentation
, l
, false);
1642 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1648 } else if (streq(name
, "Slice")) {
1652 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1653 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1654 if (u
->type
== UNIT_SLICE
)
1655 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1656 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1657 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1659 r
= sd_bus_message_read(message
, "s", &s
);
1663 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1664 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1666 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1667 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1668 * instead of manager_load_unit() on purpose, here. */
1669 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1673 if (slice
->type
!= UNIT_SLICE
)
1674 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1676 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1677 r
= unit_set_slice(u
, slice
);
1681 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
1686 } else if (streq(name
, "RequiresMountsFor")) {
1687 _cleanup_strv_free_
char **l
= NULL
;
1690 r
= sd_bus_message_read_strv(message
, &l
);
1694 STRV_FOREACH(p
, l
) {
1695 if (!path_is_absolute(*p
))
1696 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
1698 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1699 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
1701 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
1703 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1710 if (streq(name
, "RequiresOverridable"))
1711 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1712 else if (streq(name
, "RequisiteOverridable"))
1713 d
= UNIT_REQUISITE
; /* same here */
1715 d
= unit_dependency_from_string(name
);
1720 r
= sd_bus_message_enter_container(message
, 'a', "s");
1724 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1725 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1726 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1728 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1729 _cleanup_free_
char *label
= NULL
;
1731 r
= unit_add_dependency_by_name(u
, d
, other
, NULL
, true, UNIT_DEPENDENCY_FILE
);
1735 label
= strjoin(name
, "-", other
);
1739 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
1746 r
= sd_bus_message_exit_container(message
);
1752 } else if (streq(name
, "AddRef")) {
1756 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
1757 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
1758 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
1759 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
1760 * dependency type, hence let's not confuse things with that.
1762 * Note that we don't acually add the reference to the bus track. We do that only after the setup of
1763 * the transient unit is complete, so that setting this property multiple times in the same transient
1764 * unit creation call doesn't count as individual references. */
1766 r
= sd_bus_message_read(message
, "b", &b
);
1770 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
1771 u
->bus_track_add
= b
;
1779 int bus_unit_set_properties(
1781 sd_bus_message
*message
,
1782 UnitWriteFlags flags
,
1784 sd_bus_error
*error
) {
1786 bool for_real
= false;
1793 /* We iterate through the array twice. First run we just check
1794 * if all passed data is valid, second run actually applies
1795 * it. This is to implement transaction-like behaviour without
1796 * actually providing full transactions. */
1798 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1806 r
= sd_bus_message_enter_container(message
, 'r', "sv");
1810 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
1813 /* Reached EOF. Let's try again, and this time for realz... */
1814 r
= sd_bus_message_rewind(message
, false);
1822 r
= sd_bus_message_read(message
, "s", &name
);
1826 if (!UNIT_VTABLE(u
)->bus_set_property
)
1827 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
1829 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
1833 /* If not for real, then mask out the two target flags */
1834 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
1836 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
1837 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
1838 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
1840 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
1845 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
1847 r
= sd_bus_message_exit_container(message
);
1851 r
= sd_bus_message_exit_container(message
);
1858 r
= sd_bus_message_exit_container(message
);
1862 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
1863 UNIT_VTABLE(u
)->bus_commit_properties(u
);
1868 int bus_unit_check_load_state(Unit
*u
, sd_bus_error
*error
) {
1871 if (u
->load_state
== UNIT_LOADED
)
1874 /* Give a better description of the unit error when
1875 * possible. Note that in the case of UNIT_MASKED, load_error
1877 if (u
->load_state
== UNIT_MASKED
)
1878 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
1880 if (u
->load_state
== UNIT_NOT_FOUND
)
1881 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
1883 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s is not loaded properly: %m.", u
->id
);
1886 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
1892 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
1894 unit_add_to_gc_queue(u
);
1898 static int bus_unit_allocate_bus_track(Unit
*u
) {
1906 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
1910 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
1912 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
1919 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
1924 r
= bus_unit_allocate_bus_track(u
);
1928 return sd_bus_track_add_name(u
->bus_track
, name
);
1931 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
1936 r
= bus_unit_allocate_bus_track(u
);
1940 return sd_bus_track_add_sender(u
->bus_track
, m
);
1943 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
1946 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
1951 return sd_bus_track_remove_sender(u
->bus_track
, m
);