1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
8 #include "alloc-util.h"
9 #include "bpf-firewall.h"
10 #include "bus-common-errors.h"
11 #include "cgroup-util.h"
12 #include "condition.h"
14 #include "dbus-unit.h"
15 #include "dbus-util.h"
18 #include "locale-util.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "selinux-access.h"
23 #include "signal-util.h"
25 #include "string-util.h"
27 #include "user-util.h"
30 static bool unit_can_start_refuse_manual(Unit
*u
) {
31 return unit_can_start(u
) && !u
->refuse_manual_start
;
34 static bool unit_can_stop_refuse_manual(Unit
*u
) {
35 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
38 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
39 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
42 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
46 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
47 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
48 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
49 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
53 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
54 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
55 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
57 static int property_get_names(
60 const char *interface
,
62 sd_bus_message
*reply
,
64 sd_bus_error
*error
) {
75 r
= sd_bus_message_open_container(reply
, 'a', "s");
79 SET_FOREACH(t
, *s
, i
) {
80 r
= sd_bus_message_append(reply
, "s", t
);
85 return sd_bus_message_close_container(reply
);
88 static int property_get_following(
91 const char *interface
,
93 sd_bus_message
*reply
,
95 sd_bus_error
*error
) {
97 Unit
*u
= userdata
, *f
;
103 f
= unit_following(u
);
104 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
107 static int property_get_dependencies(
110 const char *interface
,
111 const char *property
,
112 sd_bus_message
*reply
,
114 sd_bus_error
*error
) {
116 Hashmap
**h
= userdata
;
126 r
= sd_bus_message_open_container(reply
, 'a', "s");
130 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
131 r
= sd_bus_message_append(reply
, "s", u
->id
);
136 return sd_bus_message_close_container(reply
);
139 static int property_get_requires_mounts_for(
142 const char *interface
,
143 const char *property
,
144 sd_bus_message
*reply
,
146 sd_bus_error
*error
) {
148 Hashmap
**h
= userdata
;
158 r
= sd_bus_message_open_container(reply
, 'a', "s");
162 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
163 r
= sd_bus_message_append(reply
, "s", p
);
168 return sd_bus_message_close_container(reply
);
171 static int property_get_unit_file_preset(
174 const char *interface
,
175 const char *property
,
176 sd_bus_message
*reply
,
178 sd_bus_error
*error
) {
187 r
= unit_get_unit_file_preset(u
);
189 return sd_bus_message_append(reply
, "s",
191 r
> 0 ? "enabled" : "disabled");
194 static int property_get_job(
197 const char *interface
,
198 const char *property
,
199 sd_bus_message
*reply
,
201 sd_bus_error
*error
) {
203 _cleanup_free_
char *p
= NULL
;
211 return sd_bus_message_append(reply
, "(uo)", 0, "/");
213 p
= job_dbus_path(*j
);
217 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
220 static int property_get_conditions(
223 const char *interface
,
224 const char *property
,
225 sd_bus_message
*reply
,
227 sd_bus_error
*error
) {
229 const char *(*to_string
)(ConditionType type
) = NULL
;
230 Condition
**list
= userdata
, *c
;
237 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
239 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
243 LIST_FOREACH(conditions
, c
, *list
) {
247 c
->result
== CONDITION_UNTESTED
? 0 :
248 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
250 r
= sd_bus_message_append(reply
, "(sbbsi)",
252 c
->trigger
, c
->negate
,
253 c
->parameter
, tristate
);
259 return sd_bus_message_close_container(reply
);
262 static int property_get_load_error(
265 const char *interface
,
266 const char *property
,
267 sd_bus_message
*reply
,
269 sd_bus_error
*error
) {
271 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
279 r
= bus_unit_validate_load_state(u
, &e
);
281 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
283 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
286 static int bus_verify_manage_units_async_full(
290 const char *polkit_message
,
292 sd_bus_message
*call
,
293 sd_bus_error
*error
) {
295 const char *details
[9] = {
300 if (polkit_message
) {
301 details
[4] = "polkit.message";
302 details
[5] = polkit_message
;
303 details
[6] = "polkit.gettext_domain";
304 details
[7] = GETTEXT_PACKAGE
;
307 return bus_verify_polkit_async(
310 "org.freedesktop.systemd1.manage-units",
314 &u
->manager
->polkit_registry
,
318 int bus_unit_method_start_generic(
319 sd_bus_message
*message
,
322 bool reload_if_possible
,
323 sd_bus_error
*error
) {
327 _cleanup_free_
char *verb
= NULL
;
328 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
329 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
330 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
331 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
332 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
333 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
339 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
341 r
= mac_selinux_unit_access_check(
343 job_type_to_access_method(job_type
),
348 r
= sd_bus_message_read(message
, "s", &smode
);
352 mode
= job_mode_from_string(smode
);
354 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
356 if (reload_if_possible
)
357 verb
= strjoin("reload-or-", job_type_to_string(job_type
));
359 verb
= strdup(job_type_to_string(job_type
));
363 r
= bus_verify_manage_units_async_full(
367 job_type
< _JOB_TYPE_MAX
? polkit_message_for_job
[job_type
] : NULL
,
374 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
376 return bus_unit_queue_job(message
, u
, job_type
, mode
, reload_if_possible
, error
);
379 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
380 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
383 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
384 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
387 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
388 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
391 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
392 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
395 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
396 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
399 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
400 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
403 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
404 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
407 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
417 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
421 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
428 who
= kill_who_from_string(swho
);
430 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
433 if (!SIGNAL_VALID(signo
))
434 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
436 r
= bus_verify_manage_units_async_full(
440 N_("Authentication is required to kill '$(unit)'."),
447 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
449 r
= unit_kill(u
, who
, signo
, error
);
453 return sd_bus_reply_method_return(message
, NULL
);
456 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
463 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
467 r
= bus_verify_manage_units_async_full(
471 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
478 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
480 unit_reset_failed(u
);
482 return sd_bus_reply_method_return(message
, NULL
);
485 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
492 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
496 r
= sd_bus_message_read(message
, "b", &runtime
);
500 r
= bus_verify_manage_units_async_full(
504 N_("Authentication is required to set properties on '$(unit)'."),
511 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
513 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
517 return sd_bus_reply_method_return(message
, NULL
);
520 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
527 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
531 r
= bus_verify_manage_units_async_full(
542 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
544 r
= bus_unit_track_add_sender(u
, message
);
548 return sd_bus_reply_method_return(message
, NULL
);
551 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
558 r
= bus_unit_track_remove_sender(u
, message
);
560 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
564 return sd_bus_reply_method_return(message
, NULL
);
567 const sd_bus_vtable bus_unit_vtable
[] = {
568 SD_BUS_VTABLE_START(0),
570 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
571 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
572 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
573 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
574 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
575 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
576 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
577 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
578 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
579 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
580 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
581 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
582 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
583 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
584 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
585 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
586 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
587 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
588 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
589 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
590 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
591 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
592 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
593 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
594 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
595 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
596 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
597 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
598 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
599 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
600 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
601 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
602 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
603 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
604 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
605 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
606 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
607 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
608 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
609 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
610 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
611 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
612 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
613 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
614 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
615 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
616 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
617 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
618 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
619 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
620 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
621 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
622 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
623 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
624 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
625 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
626 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
627 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
628 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
629 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
630 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), 0),
631 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), 0),
632 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
633 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
634 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
635 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
636 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_limit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
637 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
638 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
639 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
640 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
641 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), 0),
642 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), 0),
644 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
645 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
646 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
647 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
648 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
649 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
650 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
651 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
652 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
653 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
654 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
655 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
657 /* For dependency types we don't support anymore always return an empty array */
658 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
659 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
660 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
661 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
662 /* Obsolete alias names */
663 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
664 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
668 static int property_get_slice(
671 const char *interface
,
672 const char *property
,
673 sd_bus_message
*reply
,
675 sd_bus_error
*error
) {
683 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
686 static int property_get_current_memory(
689 const char *interface
,
690 const char *property
,
691 sd_bus_message
*reply
,
693 sd_bus_error
*error
) {
695 uint64_t sz
= (uint64_t) -1;
703 r
= unit_get_memory_current(u
, &sz
);
704 if (r
< 0 && r
!= -ENODATA
)
705 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
707 return sd_bus_message_append(reply
, "t", sz
);
710 static int property_get_current_tasks(
713 const char *interface
,
714 const char *property
,
715 sd_bus_message
*reply
,
717 sd_bus_error
*error
) {
719 uint64_t cn
= (uint64_t) -1;
727 r
= unit_get_tasks_current(u
, &cn
);
728 if (r
< 0 && r
!= -ENODATA
)
729 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
731 return sd_bus_message_append(reply
, "t", cn
);
734 static int property_get_cpu_usage(
737 const char *interface
,
738 const char *property
,
739 sd_bus_message
*reply
,
741 sd_bus_error
*error
) {
743 nsec_t ns
= (nsec_t
) -1;
751 r
= unit_get_cpu_usage(u
, &ns
);
752 if (r
< 0 && r
!= -ENODATA
)
753 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
755 return sd_bus_message_append(reply
, "t", ns
);
758 static int property_get_cgroup(
761 const char *interface
,
762 const char *property
,
763 sd_bus_message
*reply
,
765 sd_bus_error
*error
) {
768 const char *t
= NULL
;
774 /* Three cases: a) u->cgroup_path is NULL, in which case the
775 * unit has no control group, which we report as the empty
776 * string. b) u->cgroup_path is the empty string, which
777 * indicates the root cgroup, which we report as "/". c) all
778 * other cases we report as-is. */
781 t
= empty_to_root(u
->cgroup_path
);
783 return sd_bus_message_append(reply
, "s", t
);
786 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
787 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
793 r
= set_put(pids
, PID_TO_PTR(pid
));
794 if (IN_SET(r
, 0, -EEXIST
))
800 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
809 (void) get_process_cmdline(pid
, 0, true, &cmdline
);
811 return sd_bus_message_append(reply
,
818 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
819 _cleanup_closedir_
DIR *d
= NULL
;
820 _cleanup_fclose_
FILE *f
= NULL
;
826 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
835 r
= cg_read_pid(f
, &pid
);
841 if (is_kernel_thread(pid
) > 0)
844 r
= append_process(reply
, p
, pid
, pids
);
849 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
856 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
858 r
= cg_read_subgroup(d
, &g
);
864 j
= strjoin(p
, "/", g
);
868 r
= append_cgroup(reply
, j
, pids
);
876 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
877 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
878 _cleanup_set_free_ Set
*pids
= NULL
;
885 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
889 pids
= set_new(NULL
);
893 r
= sd_bus_message_new_method_return(message
, &reply
);
897 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
901 if (u
->cgroup_path
) {
902 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
907 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
908 pid
= unit_main_pid(u
);
910 r
= append_process(reply
, NULL
, pid
, pids
);
915 pid
= unit_control_pid(u
);
917 r
= append_process(reply
, NULL
, pid
, pids
);
922 r
= sd_bus_message_close_container(reply
);
926 return sd_bus_send(NULL
, reply
, NULL
);
929 static int property_get_ip_counter(
932 const char *interface
,
933 const char *property
,
934 sd_bus_message
*reply
,
936 sd_bus_error
*error
) {
938 CGroupIPAccountingMetric metric
;
939 uint64_t value
= (uint64_t) -1;
947 if (streq(property
, "IPIngressBytes"))
948 metric
= CGROUP_IP_INGRESS_BYTES
;
949 else if (streq(property
, "IPIngressPackets"))
950 metric
= CGROUP_IP_INGRESS_PACKETS
;
951 else if (streq(property
, "IPEgressBytes"))
952 metric
= CGROUP_IP_EGRESS_BYTES
;
954 assert(streq(property
, "IPEgressPackets"));
955 metric
= CGROUP_IP_EGRESS_PACKETS
;
958 (void) unit_get_ip_accounting(u
, metric
, &value
);
959 return sd_bus_message_append(reply
, "t", value
);
962 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
964 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
965 _cleanup_set_free_ Set
*pids
= NULL
;
972 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
973 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
974 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
975 * subcgroup path has no effect! */
977 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
981 r
= sd_bus_message_read(message
, "s", &path
);
985 path
= empty_to_null(path
);
987 if (!path_is_absolute(path
))
988 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
990 if (!path_is_normalized(path
))
991 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
994 if (!unit_cgroup_delegate(u
))
995 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
997 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
998 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1000 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1004 r
= sd_bus_message_enter_container(message
, 'a', "u");
1008 uid_t process_uid
, sender_uid
;
1012 r
= sd_bus_message_read(message
, "u", &upid
);
1019 r
= sd_bus_creds_get_pid(creds
, &pid
);
1025 /* Filter out duplicates */
1026 if (set_contains(pids
, PID_TO_PTR(pid
)))
1029 /* Check if this process is suitable for attaching to this unit */
1030 r
= unit_pid_attachable(u
, pid
, error
);
1034 /* Let's query the sender's UID, so that we can make our security decisions */
1035 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1039 /* Let's validate security: if the sender is root, then all is OK. If the sender is is any other unit,
1040 * then the process' UID and the target unit's UID have to match the sender's UID */
1041 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1042 r
= get_process_uid(pid
, &process_uid
);
1044 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1046 if (process_uid
!= sender_uid
)
1047 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1048 if (process_uid
!= u
->ref_uid
)
1049 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1053 pids
= set_new(NULL
);
1058 r
= set_put(pids
, PID_TO_PTR(pid
));
1063 r
= sd_bus_message_exit_container(message
);
1067 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1069 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1071 return sd_bus_reply_method_return(message
, NULL
);
1074 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1075 SD_BUS_VTABLE_START(0),
1076 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1077 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1078 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1079 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1080 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1081 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1082 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1083 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1084 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1085 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1086 SD_BUS_METHOD("AttachProcesses", "sau", NULL
, bus_unit_method_attach_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1090 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1091 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1092 _cleanup_free_
char *p
= NULL
;
1099 p
= unit_dbus_path(u
);
1103 r
= sd_bus_message_new_signal(
1106 "/org/freedesktop/systemd1",
1107 "org.freedesktop.systemd1.Manager",
1112 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1116 return sd_bus_send(bus
, m
, NULL
);
1119 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1120 _cleanup_free_
char *p
= NULL
;
1127 p
= unit_dbus_path(u
);
1131 /* Send a properties changed signal. First for the specific
1132 * type, then for the generic unit. The clients may rely on
1133 * this order to get atomic behavior if needed. */
1135 r
= sd_bus_emit_properties_changed_strv(
1137 unit_dbus_interface_from_type(u
->type
),
1142 return sd_bus_emit_properties_changed_strv(
1144 "org.freedesktop.systemd1.Unit",
1148 void bus_unit_send_change_signal(Unit
*u
) {
1152 if (u
->in_dbus_queue
) {
1153 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1154 u
->in_dbus_queue
= false;
1160 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1162 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1164 u
->sent_dbus_new_signal
= true;
1167 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1168 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1169 _cleanup_free_
char *p
= NULL
;
1176 p
= unit_dbus_path(u
);
1180 r
= sd_bus_message_new_signal(
1183 "/org/freedesktop/systemd1",
1184 "org.freedesktop.systemd1.Manager",
1189 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1193 return sd_bus_send(bus
, m
, NULL
);
1196 void bus_unit_send_removed_signal(Unit
*u
) {
1200 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1201 bus_unit_send_change_signal(u
);
1206 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1208 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1211 int bus_unit_queue_job(
1212 sd_bus_message
*message
,
1216 bool reload_if_possible
,
1217 sd_bus_error
*error
) {
1219 _cleanup_free_
char *path
= NULL
;
1225 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1226 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1228 r
= mac_selinux_unit_access_check(
1230 job_type_to_access_method(type
),
1235 if (reload_if_possible
&& unit_can_reload(u
)) {
1236 if (type
== JOB_RESTART
)
1237 type
= JOB_RELOAD_OR_START
;
1238 else if (type
== JOB_TRY_RESTART
)
1239 type
= JOB_TRY_RELOAD
;
1242 if (type
== JOB_STOP
&&
1243 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1244 unit_active_state(u
) == UNIT_INACTIVE
)
1245 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1247 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1248 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1249 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1250 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1251 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
);
1253 r
= manager_add_job(u
->manager
, type
, u
, mode
, error
, &j
);
1257 r
= bus_job_track_sender(j
, message
);
1261 path
= job_dbus_path(j
);
1265 return sd_bus_reply_method_return(message
, "o", path
);
1268 static int bus_unit_set_live_property(
1271 sd_bus_message
*message
,
1272 UnitWriteFlags flags
,
1273 sd_bus_error
*error
) {
1281 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1282 * units that are being created). */
1284 if (streq(name
, "Description")) {
1287 r
= sd_bus_message_read(message
, "s", &d
);
1291 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1292 r
= unit_set_description(u
, d
);
1296 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1305 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1306 static BUS_DEFINE_SET_TRANSIENT_PARSE(emergency_action
, EmergencyAction
, emergency_action_from_string
);
1307 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1309 static int bus_set_transient_conditions(
1314 sd_bus_message
*message
,
1315 UnitWriteFlags flags
,
1316 sd_bus_error
*error
) {
1318 const char *type_name
, *param
;
1319 int trigger
, negate
, r
;
1324 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1328 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1331 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1333 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1335 if (t
!= CONDITION_NULL
) {
1337 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1339 if (condition_takes_path(t
) && !path_is_absolute(param
))
1340 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1344 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1347 c
= condition_new(t
, param
, trigger
, negate
);
1351 LIST_PREPEND(conditions
, *list
, c
);
1353 if (t
!= CONDITION_NULL
)
1354 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1355 "%s=%s%s%s", type_name
,
1356 trigger
? "|" : "", negate
? "!" : "", param
);
1358 unit_write_settingf(u
, flags
, name
,
1359 "%s=%s%s", type_name
,
1360 trigger
? "|" : "", yes_no(!negate
));
1368 r
= sd_bus_message_exit_container(message
);
1372 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1373 *list
= condition_free_list(*list
);
1374 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1380 static int bus_unit_set_transient_property(
1383 sd_bus_message
*message
,
1384 UnitWriteFlags flags
,
1385 sd_bus_error
*error
) {
1387 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1394 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1395 * has been created. */
1397 if (streq(name
, "SourcePath"))
1398 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1400 if (streq(name
, "StopWhenUnneeded"))
1401 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1403 if (streq(name
, "RefuseManualStart"))
1404 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1406 if (streq(name
, "RefuseManualStop"))
1407 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1409 if (streq(name
, "AllowIsolate"))
1410 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1412 if (streq(name
, "DefaultDependencies"))
1413 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1415 if (streq(name
, "OnFailureJobMode"))
1416 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1418 if (streq(name
, "IgnoreOnIsolate"))
1419 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1421 if (streq(name
, "JobTimeoutUSec")) {
1422 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1423 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1424 u
->job_running_timeout
= u
->job_timeout
;
1427 if (streq(name
, "JobRunningTimeoutUSec")) {
1428 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1429 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1430 u
->job_running_timeout_set
= true;
1435 if (streq(name
, "JobTimeoutAction"))
1436 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1438 if (streq(name
, "JobTimeoutRebootArgument"))
1439 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1441 if (streq(name
, "StartLimitIntervalUSec"))
1442 return bus_set_transient_usec(u
, name
, &u
->start_limit
.interval
, message
, flags
, error
);
1444 if (streq(name
, "StartLimitBurst"))
1445 return bus_set_transient_unsigned(u
, name
, &u
->start_limit
.burst
, message
, flags
, error
);
1447 if (streq(name
, "StartLimitAction"))
1448 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1450 if (streq(name
, "FailureAction"))
1451 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1453 if (streq(name
, "SuccessAction"))
1454 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1456 if (streq(name
, "RebootArgument"))
1457 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1459 if (streq(name
, "CollectMode"))
1460 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
1462 if (streq(name
, "Conditions"))
1463 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
1465 if (streq(name
, "Asserts"))
1466 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
1468 if (streq(name
, "Documentation")) {
1469 _cleanup_strv_free_
char **l
= NULL
;
1472 r
= sd_bus_message_read_strv(message
, &l
);
1476 STRV_FOREACH(p
, l
) {
1477 if (!documentation_url_is_valid(*p
))
1478 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
1481 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1482 if (strv_isempty(l
)) {
1483 u
->documentation
= strv_free(u
->documentation
);
1484 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1486 strv_extend_strv(&u
->documentation
, l
, false);
1489 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1495 } else if (streq(name
, "Slice")) {
1499 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1500 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1501 if (u
->type
== UNIT_SLICE
)
1502 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1503 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1504 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1506 r
= sd_bus_message_read(message
, "s", &s
);
1510 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1511 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1513 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1514 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1515 * instead of manager_load_unit() on purpose, here. */
1516 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1520 if (slice
->type
!= UNIT_SLICE
)
1521 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1523 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1524 r
= unit_set_slice(u
, slice
);
1528 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
1533 } else if (streq(name
, "RequiresMountsFor")) {
1534 _cleanup_strv_free_
char **l
= NULL
;
1537 r
= sd_bus_message_read_strv(message
, &l
);
1541 STRV_FOREACH(p
, l
) {
1542 if (!path_is_absolute(*p
))
1543 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
1545 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1546 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
1548 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
1550 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1557 if (streq(name
, "RequiresOverridable"))
1558 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1559 else if (streq(name
, "RequisiteOverridable"))
1560 d
= UNIT_REQUISITE
; /* same here */
1562 d
= unit_dependency_from_string(name
);
1567 r
= sd_bus_message_enter_container(message
, 'a', "s");
1571 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1572 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1573 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1575 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1576 _cleanup_free_
char *label
= NULL
;
1578 r
= unit_add_dependency_by_name(u
, d
, other
, NULL
, true, UNIT_DEPENDENCY_FILE
);
1582 label
= strjoin(name
, "-", other
);
1586 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
1593 r
= sd_bus_message_exit_container(message
);
1599 } else if (streq(name
, "AddRef")) {
1603 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
1604 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
1605 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
1606 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
1607 * dependency type, hence let's not confuse things with that.
1609 * Note that we don't acually add the reference to the bus track. We do that only after the setup of
1610 * the transient unit is complete, so that setting this property multiple times in the same transient
1611 * unit creation call doesn't count as individual references. */
1613 r
= sd_bus_message_read(message
, "b", &b
);
1617 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
1618 u
->bus_track_add
= b
;
1626 int bus_unit_set_properties(
1628 sd_bus_message
*message
,
1629 UnitWriteFlags flags
,
1631 sd_bus_error
*error
) {
1633 bool for_real
= false;
1640 /* We iterate through the array twice. First run we just check
1641 * if all passed data is valid, second run actually applies
1642 * it. This is to implement transaction-like behaviour without
1643 * actually providing full transactions. */
1645 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1653 r
= sd_bus_message_enter_container(message
, 'r', "sv");
1657 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
1660 /* Reached EOF. Let's try again, and this time for realz... */
1661 r
= sd_bus_message_rewind(message
, false);
1669 r
= sd_bus_message_read(message
, "s", &name
);
1673 if (!UNIT_VTABLE(u
)->bus_set_property
)
1674 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
1676 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
1680 /* If not for real, then mask out the two target flags */
1681 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
1683 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
1684 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
1685 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
1687 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
1692 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
1694 r
= sd_bus_message_exit_container(message
);
1698 r
= sd_bus_message_exit_container(message
);
1705 r
= sd_bus_message_exit_container(message
);
1709 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
1710 UNIT_VTABLE(u
)->bus_commit_properties(u
);
1715 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
1718 /* Generates a pretty error if a unit isn't properly loaded. */
1720 switch (u
->load_state
) {
1725 case UNIT_NOT_FOUND
:
1726 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
1728 case UNIT_BAD_SETTING
:
1729 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
1731 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
1732 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to loaded properly: %m.", u
->id
);
1735 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
1740 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
1744 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
1750 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
1752 unit_add_to_gc_queue(u
);
1756 static int bus_unit_allocate_bus_track(Unit
*u
) {
1764 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
1768 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
1770 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
1777 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
1782 r
= bus_unit_allocate_bus_track(u
);
1786 return sd_bus_track_add_name(u
->bus_track
, name
);
1789 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
1794 r
= bus_unit_allocate_bus_track(u
);
1798 return sd_bus_track_add_sender(u
->bus_track
, m
);
1801 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
1804 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
1809 return sd_bus_track_remove_sender(u
->bus_track
, m
);