1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bpf-firewall.h"
7 #include "bus-common-errors.h"
8 #include "cgroup-util.h"
11 #include "dbus-unit.h"
12 #include "dbus-util.h"
15 #include "locale-util.h"
17 #include "path-util.h"
18 #include "process-util.h"
19 #include "selinux-access.h"
20 #include "signal-util.h"
22 #include "string-util.h"
24 #include "user-util.h"
27 static bool unit_can_start_refuse_manual(Unit
*u
) {
28 return unit_can_start(u
) && !u
->refuse_manual_start
;
31 static bool unit_can_stop_refuse_manual(Unit
*u
) {
32 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
35 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
36 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
39 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
40 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
41 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
42 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
43 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
44 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
45 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
46 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
47 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
48 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
49 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
52 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
54 static int property_get_names(
57 const char *interface
,
59 sd_bus_message
*reply
,
61 sd_bus_error
*error
) {
72 r
= sd_bus_message_open_container(reply
, 'a', "s");
76 SET_FOREACH(t
, *s
, i
) {
77 r
= sd_bus_message_append(reply
, "s", t
);
82 return sd_bus_message_close_container(reply
);
85 static int property_get_following(
88 const char *interface
,
90 sd_bus_message
*reply
,
92 sd_bus_error
*error
) {
94 Unit
*u
= userdata
, *f
;
100 f
= unit_following(u
);
101 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
104 static int property_get_dependencies(
107 const char *interface
,
108 const char *property
,
109 sd_bus_message
*reply
,
111 sd_bus_error
*error
) {
113 Hashmap
**h
= userdata
;
123 r
= sd_bus_message_open_container(reply
, 'a', "s");
127 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
128 r
= sd_bus_message_append(reply
, "s", u
->id
);
133 return sd_bus_message_close_container(reply
);
136 static int property_get_requires_mounts_for(
139 const char *interface
,
140 const char *property
,
141 sd_bus_message
*reply
,
143 sd_bus_error
*error
) {
145 Hashmap
**h
= userdata
;
155 r
= sd_bus_message_open_container(reply
, 'a', "s");
159 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
160 r
= sd_bus_message_append(reply
, "s", p
);
165 return sd_bus_message_close_container(reply
);
168 static int property_get_unit_file_preset(
171 const char *interface
,
172 const char *property
,
173 sd_bus_message
*reply
,
175 sd_bus_error
*error
) {
184 r
= unit_get_unit_file_preset(u
);
186 return sd_bus_message_append(reply
, "s",
188 r
> 0 ? "enabled" : "disabled");
191 static int property_get_job(
194 const char *interface
,
195 const char *property
,
196 sd_bus_message
*reply
,
198 sd_bus_error
*error
) {
200 _cleanup_free_
char *p
= NULL
;
208 return sd_bus_message_append(reply
, "(uo)", 0, "/");
210 p
= job_dbus_path(*j
);
214 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
217 static int property_get_conditions(
220 const char *interface
,
221 const char *property
,
222 sd_bus_message
*reply
,
224 sd_bus_error
*error
) {
226 const char *(*to_string
)(ConditionType type
) = NULL
;
227 Condition
**list
= userdata
, *c
;
234 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
236 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
240 LIST_FOREACH(conditions
, c
, *list
) {
244 c
->result
== CONDITION_UNTESTED
? 0 :
245 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
247 r
= sd_bus_message_append(reply
, "(sbbsi)",
249 c
->trigger
, c
->negate
,
250 c
->parameter
, tristate
);
256 return sd_bus_message_close_container(reply
);
259 static int property_get_load_error(
262 const char *interface
,
263 const char *property
,
264 sd_bus_message
*reply
,
266 sd_bus_error
*error
) {
268 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
276 r
= bus_unit_validate_load_state(u
, &e
);
278 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
280 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
283 static int bus_verify_manage_units_async_full(
287 const char *polkit_message
,
289 sd_bus_message
*call
,
290 sd_bus_error
*error
) {
292 const char *details
[9] = {
297 if (polkit_message
) {
298 details
[4] = "polkit.message";
299 details
[5] = polkit_message
;
300 details
[6] = "polkit.gettext_domain";
301 details
[7] = GETTEXT_PACKAGE
;
304 return bus_verify_polkit_async(
307 "org.freedesktop.systemd1.manage-units",
311 &u
->manager
->polkit_registry
,
315 int bus_unit_method_start_generic(
316 sd_bus_message
*message
,
319 bool reload_if_possible
,
320 sd_bus_error
*error
) {
324 _cleanup_free_
char *verb
= NULL
;
325 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
326 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
327 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
328 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
329 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
330 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
336 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
338 r
= mac_selinux_unit_access_check(
340 job_type_to_access_method(job_type
),
345 r
= sd_bus_message_read(message
, "s", &smode
);
349 mode
= job_mode_from_string(smode
);
351 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
353 if (reload_if_possible
)
354 verb
= strjoin("reload-or-", job_type_to_string(job_type
));
356 verb
= strdup(job_type_to_string(job_type
));
360 r
= bus_verify_manage_units_async_full(
364 job_type
< _JOB_TYPE_MAX
? polkit_message_for_job
[job_type
] : NULL
,
371 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
373 return bus_unit_queue_job(message
, u
, job_type
, mode
, reload_if_possible
, error
);
376 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
377 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
380 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
381 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
384 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
385 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
388 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
389 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
392 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
393 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
396 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
397 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
400 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
401 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
404 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
414 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
418 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
425 who
= kill_who_from_string(swho
);
427 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
430 if (!SIGNAL_VALID(signo
))
431 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
433 r
= bus_verify_manage_units_async_full(
437 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
444 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
446 r
= unit_kill(u
, who
, signo
, error
);
450 return sd_bus_reply_method_return(message
, NULL
);
453 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
460 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
464 r
= bus_verify_manage_units_async_full(
468 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
475 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
477 unit_reset_failed(u
);
479 return sd_bus_reply_method_return(message
, NULL
);
482 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
489 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
493 r
= sd_bus_message_read(message
, "b", &runtime
);
497 r
= bus_verify_manage_units_async_full(
501 N_("Authentication is required to set properties on '$(unit)'."),
508 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
510 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
514 return sd_bus_reply_method_return(message
, NULL
);
517 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
524 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
528 r
= bus_verify_manage_units_async_full(
539 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
541 r
= bus_unit_track_add_sender(u
, message
);
545 return sd_bus_reply_method_return(message
, NULL
);
548 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
555 r
= bus_unit_track_remove_sender(u
, message
);
557 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
561 return sd_bus_reply_method_return(message
, NULL
);
564 const sd_bus_vtable bus_unit_vtable
[] = {
565 SD_BUS_VTABLE_START(0),
567 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
568 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
569 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
570 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
571 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
572 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
573 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
574 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
575 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
576 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
577 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
578 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
579 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
580 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
581 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
582 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
583 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
584 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
585 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
586 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
587 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
588 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
589 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
590 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
591 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
592 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
593 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
594 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
595 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
596 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
597 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
598 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
599 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
600 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
601 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
602 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
603 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
604 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
605 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
606 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
607 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
608 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
609 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
610 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
611 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
612 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
613 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
614 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
615 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
616 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
617 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
618 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
619 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
620 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
621 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
622 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
623 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
624 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
625 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
626 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
627 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), 0),
628 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), 0),
629 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
630 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
631 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
632 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
633 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_limit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
634 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
635 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
636 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
637 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
638 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), 0),
639 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), 0),
641 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
642 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
643 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
644 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
645 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
646 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
647 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
648 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
649 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
650 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
651 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
652 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
654 /* For dependency types we don't support anymore always return an empty array */
655 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
656 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
657 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
658 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
659 /* Obsolete alias names */
660 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
661 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
665 static int property_get_slice(
668 const char *interface
,
669 const char *property
,
670 sd_bus_message
*reply
,
672 sd_bus_error
*error
) {
680 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
683 static int property_get_current_memory(
686 const char *interface
,
687 const char *property
,
688 sd_bus_message
*reply
,
690 sd_bus_error
*error
) {
692 uint64_t sz
= (uint64_t) -1;
700 r
= unit_get_memory_current(u
, &sz
);
701 if (r
< 0 && r
!= -ENODATA
)
702 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
704 return sd_bus_message_append(reply
, "t", sz
);
707 static int property_get_current_tasks(
710 const char *interface
,
711 const char *property
,
712 sd_bus_message
*reply
,
714 sd_bus_error
*error
) {
716 uint64_t cn
= (uint64_t) -1;
724 r
= unit_get_tasks_current(u
, &cn
);
725 if (r
< 0 && r
!= -ENODATA
)
726 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
728 return sd_bus_message_append(reply
, "t", cn
);
731 static int property_get_cpu_usage(
734 const char *interface
,
735 const char *property
,
736 sd_bus_message
*reply
,
738 sd_bus_error
*error
) {
740 nsec_t ns
= (nsec_t
) -1;
748 r
= unit_get_cpu_usage(u
, &ns
);
749 if (r
< 0 && r
!= -ENODATA
)
750 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
752 return sd_bus_message_append(reply
, "t", ns
);
755 static int property_get_cgroup(
758 const char *interface
,
759 const char *property
,
760 sd_bus_message
*reply
,
762 sd_bus_error
*error
) {
765 const char *t
= NULL
;
771 /* Three cases: a) u->cgroup_path is NULL, in which case the
772 * unit has no control group, which we report as the empty
773 * string. b) u->cgroup_path is the empty string, which
774 * indicates the root cgroup, which we report as "/". c) all
775 * other cases we report as-is. */
778 t
= empty_to_root(u
->cgroup_path
);
780 return sd_bus_message_append(reply
, "s", t
);
783 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
784 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
790 r
= set_put(pids
, PID_TO_PTR(pid
));
791 if (IN_SET(r
, 0, -EEXIST
))
797 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
806 (void) get_process_cmdline(pid
, 0, true, &cmdline
);
808 return sd_bus_message_append(reply
,
815 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
816 _cleanup_closedir_
DIR *d
= NULL
;
817 _cleanup_fclose_
FILE *f
= NULL
;
823 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
832 r
= cg_read_pid(f
, &pid
);
838 if (is_kernel_thread(pid
) > 0)
841 r
= append_process(reply
, p
, pid
, pids
);
846 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
853 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
855 r
= cg_read_subgroup(d
, &g
);
861 j
= strjoin(p
, "/", g
);
865 r
= append_cgroup(reply
, j
, pids
);
873 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
874 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
875 _cleanup_set_free_ Set
*pids
= NULL
;
882 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
886 pids
= set_new(NULL
);
890 r
= sd_bus_message_new_method_return(message
, &reply
);
894 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
898 if (u
->cgroup_path
) {
899 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
904 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
905 pid
= unit_main_pid(u
);
907 r
= append_process(reply
, NULL
, pid
, pids
);
912 pid
= unit_control_pid(u
);
914 r
= append_process(reply
, NULL
, pid
, pids
);
919 r
= sd_bus_message_close_container(reply
);
923 return sd_bus_send(NULL
, reply
, NULL
);
926 static int property_get_ip_counter(
929 const char *interface
,
930 const char *property
,
931 sd_bus_message
*reply
,
933 sd_bus_error
*error
) {
935 CGroupIPAccountingMetric metric
;
936 uint64_t value
= (uint64_t) -1;
944 if (streq(property
, "IPIngressBytes"))
945 metric
= CGROUP_IP_INGRESS_BYTES
;
946 else if (streq(property
, "IPIngressPackets"))
947 metric
= CGROUP_IP_INGRESS_PACKETS
;
948 else if (streq(property
, "IPEgressBytes"))
949 metric
= CGROUP_IP_EGRESS_BYTES
;
951 assert(streq(property
, "IPEgressPackets"));
952 metric
= CGROUP_IP_EGRESS_PACKETS
;
955 (void) unit_get_ip_accounting(u
, metric
, &value
);
956 return sd_bus_message_append(reply
, "t", value
);
959 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
961 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
962 _cleanup_set_free_ Set
*pids
= NULL
;
969 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
970 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
971 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
972 * subcgroup path has no effect! */
974 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
978 r
= sd_bus_message_read(message
, "s", &path
);
982 path
= empty_to_null(path
);
984 if (!path_is_absolute(path
))
985 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
987 if (!path_is_normalized(path
))
988 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
991 if (!unit_cgroup_delegate(u
))
992 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
994 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
995 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
997 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1001 r
= sd_bus_message_enter_container(message
, 'a', "u");
1005 uid_t process_uid
, sender_uid
;
1009 r
= sd_bus_message_read(message
, "u", &upid
);
1016 r
= sd_bus_creds_get_pid(creds
, &pid
);
1022 /* Filter out duplicates */
1023 if (set_contains(pids
, PID_TO_PTR(pid
)))
1026 /* Check if this process is suitable for attaching to this unit */
1027 r
= unit_pid_attachable(u
, pid
, error
);
1031 /* Let's query the sender's UID, so that we can make our security decisions */
1032 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1036 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1037 * then the process' UID and the target unit's UID have to match the sender's UID */
1038 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1039 r
= get_process_uid(pid
, &process_uid
);
1041 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1043 if (process_uid
!= sender_uid
)
1044 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1045 if (process_uid
!= u
->ref_uid
)
1046 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1050 pids
= set_new(NULL
);
1055 r
= set_put(pids
, PID_TO_PTR(pid
));
1060 r
= sd_bus_message_exit_container(message
);
1064 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1066 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1068 return sd_bus_reply_method_return(message
, NULL
);
1071 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1072 SD_BUS_VTABLE_START(0),
1073 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1074 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1075 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1076 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1077 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1078 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1079 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1080 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1081 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1082 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1083 SD_BUS_METHOD("AttachProcesses", "sau", NULL
, bus_unit_method_attach_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1087 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1088 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1089 _cleanup_free_
char *p
= NULL
;
1096 p
= unit_dbus_path(u
);
1100 r
= sd_bus_message_new_signal(
1103 "/org/freedesktop/systemd1",
1104 "org.freedesktop.systemd1.Manager",
1109 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1113 return sd_bus_send(bus
, m
, NULL
);
1116 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1117 _cleanup_free_
char *p
= NULL
;
1124 p
= unit_dbus_path(u
);
1128 /* Send a properties changed signal. First for the specific
1129 * type, then for the generic unit. The clients may rely on
1130 * this order to get atomic behavior if needed. */
1132 r
= sd_bus_emit_properties_changed_strv(
1134 unit_dbus_interface_from_type(u
->type
),
1139 return sd_bus_emit_properties_changed_strv(
1141 "org.freedesktop.systemd1.Unit",
1145 void bus_unit_send_change_signal(Unit
*u
) {
1149 if (u
->in_dbus_queue
) {
1150 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1151 u
->in_dbus_queue
= false;
1157 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1159 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1161 u
->sent_dbus_new_signal
= true;
1164 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1165 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1166 _cleanup_free_
char *p
= NULL
;
1173 p
= unit_dbus_path(u
);
1177 r
= sd_bus_message_new_signal(
1180 "/org/freedesktop/systemd1",
1181 "org.freedesktop.systemd1.Manager",
1186 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1190 return sd_bus_send(bus
, m
, NULL
);
1193 void bus_unit_send_removed_signal(Unit
*u
) {
1197 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1198 bus_unit_send_change_signal(u
);
1203 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1205 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1208 int bus_unit_queue_job(
1209 sd_bus_message
*message
,
1213 bool reload_if_possible
,
1214 sd_bus_error
*error
) {
1216 _cleanup_free_
char *path
= NULL
;
1222 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1223 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1225 r
= mac_selinux_unit_access_check(
1227 job_type_to_access_method(type
),
1232 if (reload_if_possible
&& unit_can_reload(u
)) {
1233 if (type
== JOB_RESTART
)
1234 type
= JOB_RELOAD_OR_START
;
1235 else if (type
== JOB_TRY_RESTART
)
1236 type
= JOB_TRY_RELOAD
;
1239 if (type
== JOB_STOP
&&
1240 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1241 unit_active_state(u
) == UNIT_INACTIVE
)
1242 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1244 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1245 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1246 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1247 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1248 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
);
1250 r
= manager_add_job(u
->manager
, type
, u
, mode
, error
, &j
);
1254 r
= bus_job_track_sender(j
, message
);
1258 path
= job_dbus_path(j
);
1262 return sd_bus_reply_method_return(message
, "o", path
);
1265 static int bus_unit_set_live_property(
1268 sd_bus_message
*message
,
1269 UnitWriteFlags flags
,
1270 sd_bus_error
*error
) {
1278 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1279 * units that are being created). */
1281 if (streq(name
, "Description")) {
1284 r
= sd_bus_message_read(message
, "s", &d
);
1288 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1289 r
= unit_set_description(u
, d
);
1293 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1302 static int bus_set_transient_emergency_action(
1306 sd_bus_message
*message
,
1307 UnitWriteFlags flags
,
1308 sd_bus_error
*error
) {
1317 r
= sd_bus_message_read(message
, "s", &s
);
1321 system
= MANAGER_IS_SYSTEM(u
->manager
);
1322 r
= parse_emergency_action(s
, system
, &v
);
1324 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1325 v
== -EOPNOTSUPP
? "EmergencyAction setting invalid for manager type: %s"
1326 : "Invalid %s setting: %s",
1329 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1331 unit_write_settingf(u
, flags
, name
,
1338 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1339 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1341 static int bus_set_transient_conditions(
1346 sd_bus_message
*message
,
1347 UnitWriteFlags flags
,
1348 sd_bus_error
*error
) {
1350 const char *type_name
, *param
;
1351 int trigger
, negate
, r
;
1356 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1360 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1363 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1365 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1367 if (t
!= CONDITION_NULL
) {
1369 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1371 if (condition_takes_path(t
) && !path_is_absolute(param
))
1372 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1376 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1379 c
= condition_new(t
, param
, trigger
, negate
);
1383 LIST_PREPEND(conditions
, *list
, c
);
1385 if (t
!= CONDITION_NULL
)
1386 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1387 "%s=%s%s%s", type_name
,
1388 trigger
? "|" : "", negate
? "!" : "", param
);
1390 unit_write_settingf(u
, flags
, name
,
1391 "%s=%s%s", type_name
,
1392 trigger
? "|" : "", yes_no(!negate
));
1400 r
= sd_bus_message_exit_container(message
);
1404 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1405 *list
= condition_free_list(*list
);
1406 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1412 static int bus_unit_set_transient_property(
1415 sd_bus_message
*message
,
1416 UnitWriteFlags flags
,
1417 sd_bus_error
*error
) {
1419 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1426 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1427 * has been created. */
1429 if (streq(name
, "SourcePath"))
1430 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1432 if (streq(name
, "StopWhenUnneeded"))
1433 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1435 if (streq(name
, "RefuseManualStart"))
1436 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1438 if (streq(name
, "RefuseManualStop"))
1439 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1441 if (streq(name
, "AllowIsolate"))
1442 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1444 if (streq(name
, "DefaultDependencies"))
1445 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1447 if (streq(name
, "OnFailureJobMode"))
1448 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1450 if (streq(name
, "IgnoreOnIsolate"))
1451 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1453 if (streq(name
, "JobTimeoutUSec")) {
1454 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1455 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1456 u
->job_running_timeout
= u
->job_timeout
;
1459 if (streq(name
, "JobRunningTimeoutUSec")) {
1460 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1461 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1462 u
->job_running_timeout_set
= true;
1467 if (streq(name
, "JobTimeoutAction"))
1468 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1470 if (streq(name
, "JobTimeoutRebootArgument"))
1471 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1473 if (streq(name
, "StartLimitIntervalUSec"))
1474 return bus_set_transient_usec(u
, name
, &u
->start_limit
.interval
, message
, flags
, error
);
1476 if (streq(name
, "StartLimitBurst"))
1477 return bus_set_transient_unsigned(u
, name
, &u
->start_limit
.burst
, message
, flags
, error
);
1479 if (streq(name
, "StartLimitAction"))
1480 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1482 if (streq(name
, "FailureAction"))
1483 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1485 if (streq(name
, "SuccessAction"))
1486 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1488 if (streq(name
, "RebootArgument"))
1489 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1491 if (streq(name
, "CollectMode"))
1492 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
1494 if (streq(name
, "Conditions"))
1495 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
1497 if (streq(name
, "Asserts"))
1498 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
1500 if (streq(name
, "Documentation")) {
1501 _cleanup_strv_free_
char **l
= NULL
;
1504 r
= sd_bus_message_read_strv(message
, &l
);
1508 STRV_FOREACH(p
, l
) {
1509 if (!documentation_url_is_valid(*p
))
1510 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
1513 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1514 if (strv_isempty(l
)) {
1515 u
->documentation
= strv_free(u
->documentation
);
1516 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1518 strv_extend_strv(&u
->documentation
, l
, false);
1521 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1527 } else if (streq(name
, "Slice")) {
1531 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1532 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1533 if (u
->type
== UNIT_SLICE
)
1534 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1535 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1536 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1538 r
= sd_bus_message_read(message
, "s", &s
);
1542 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1543 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1545 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1546 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1547 * instead of manager_load_unit() on purpose, here. */
1548 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1552 if (slice
->type
!= UNIT_SLICE
)
1553 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1555 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1556 r
= unit_set_slice(u
, slice
);
1560 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
1565 } else if (streq(name
, "RequiresMountsFor")) {
1566 _cleanup_strv_free_
char **l
= NULL
;
1569 r
= sd_bus_message_read_strv(message
, &l
);
1573 STRV_FOREACH(p
, l
) {
1574 if (!path_is_absolute(*p
))
1575 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
1577 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1578 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
1580 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
1582 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1589 if (streq(name
, "RequiresOverridable"))
1590 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1591 else if (streq(name
, "RequisiteOverridable"))
1592 d
= UNIT_REQUISITE
; /* same here */
1594 d
= unit_dependency_from_string(name
);
1599 r
= sd_bus_message_enter_container(message
, 'a', "s");
1603 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1604 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1605 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1607 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1608 _cleanup_free_
char *label
= NULL
;
1610 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
1614 label
= strjoin(name
, "-", other
);
1618 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
1625 r
= sd_bus_message_exit_container(message
);
1631 } else if (streq(name
, "AddRef")) {
1635 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
1636 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
1637 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
1638 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
1639 * dependency type, hence let's not confuse things with that.
1641 * Note that we don't acually add the reference to the bus track. We do that only after the setup of
1642 * the transient unit is complete, so that setting this property multiple times in the same transient
1643 * unit creation call doesn't count as individual references. */
1645 r
= sd_bus_message_read(message
, "b", &b
);
1649 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
1650 u
->bus_track_add
= b
;
1658 int bus_unit_set_properties(
1660 sd_bus_message
*message
,
1661 UnitWriteFlags flags
,
1663 sd_bus_error
*error
) {
1665 bool for_real
= false;
1672 /* We iterate through the array twice. First run we just check
1673 * if all passed data is valid, second run actually applies
1674 * it. This is to implement transaction-like behaviour without
1675 * actually providing full transactions. */
1677 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1685 r
= sd_bus_message_enter_container(message
, 'r', "sv");
1689 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
1692 /* Reached EOF. Let's try again, and this time for realz... */
1693 r
= sd_bus_message_rewind(message
, false);
1701 r
= sd_bus_message_read(message
, "s", &name
);
1705 if (!UNIT_VTABLE(u
)->bus_set_property
)
1706 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
1708 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
1712 /* If not for real, then mask out the two target flags */
1713 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
1715 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
1716 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
1717 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
1719 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
1724 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
1726 r
= sd_bus_message_exit_container(message
);
1730 r
= sd_bus_message_exit_container(message
);
1737 r
= sd_bus_message_exit_container(message
);
1741 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
1742 UNIT_VTABLE(u
)->bus_commit_properties(u
);
1747 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
1750 /* Generates a pretty error if a unit isn't properly loaded. */
1752 switch (u
->load_state
) {
1757 case UNIT_NOT_FOUND
:
1758 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
1760 case UNIT_BAD_SETTING
:
1761 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
1763 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
1764 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to load properly: %m.", u
->id
);
1767 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
1772 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
1776 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
1782 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
1784 unit_add_to_gc_queue(u
);
1788 static int bus_unit_allocate_bus_track(Unit
*u
) {
1796 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
1800 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
1802 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
1809 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
1814 r
= bus_unit_allocate_bus_track(u
);
1818 return sd_bus_track_add_name(u
->bus_track
, name
);
1821 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
1826 r
= bus_unit_allocate_bus_track(u
);
1830 return sd_bus_track_add_sender(u
->bus_track
, m
);
1833 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
1836 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
1841 return sd_bus_track_remove_sender(u
->bus_track
, m
);