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-table.h"
23 #include "string-util.h"
25 #include "user-util.h"
28 static bool unit_can_start_refuse_manual(Unit
*u
) {
29 return unit_can_start(u
) && !u
->refuse_manual_start
;
32 static bool unit_can_stop_refuse_manual(Unit
*u
) {
33 return unit_can_stop(u
) && !u
->refuse_manual_stop
;
36 static bool unit_can_isolate_refuse_manual(Unit
*u
) {
37 return unit_can_isolate(u
) && !u
->refuse_manual_start
;
40 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode
, collect_mode
, CollectMode
);
41 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
42 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
44 static BUS_DEFINE_PROPERTY_GET(property_get_description
, "s", Unit
, unit_description
);
45 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state
, "s", Unit
, unit_active_state
, unit_active_state_to_string
);
46 static BUS_DEFINE_PROPERTY_GET(property_get_sub_state
, "s", Unit
, unit_sub_state_to_string
);
47 static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state
, "s", Unit
, unit_get_unit_file_state
, unit_file_state_to_string
);
48 static BUS_DEFINE_PROPERTY_GET(property_get_can_reload
, "b", Unit
, unit_can_reload
);
49 static BUS_DEFINE_PROPERTY_GET(property_get_can_start
, "b", Unit
, unit_can_start_refuse_manual
);
50 static BUS_DEFINE_PROPERTY_GET(property_get_can_stop
, "b", Unit
, unit_can_stop_refuse_manual
);
51 static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate
, "b", Unit
, unit_can_isolate_refuse_manual
);
52 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload
, "b", Unit
, unit_need_daemon_reload
);
53 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv
, "as", 0);
55 static int property_get_names(
58 const char *interface
,
60 sd_bus_message
*reply
,
62 sd_bus_error
*error
) {
73 r
= sd_bus_message_open_container(reply
, 'a', "s");
77 SET_FOREACH(t
, *s
, i
) {
78 r
= sd_bus_message_append(reply
, "s", t
);
83 return sd_bus_message_close_container(reply
);
86 static int property_get_following(
89 const char *interface
,
91 sd_bus_message
*reply
,
93 sd_bus_error
*error
) {
95 Unit
*u
= userdata
, *f
;
101 f
= unit_following(u
);
102 return sd_bus_message_append(reply
, "s", f
? f
->id
: NULL
);
105 static int property_get_dependencies(
108 const char *interface
,
109 const char *property
,
110 sd_bus_message
*reply
,
112 sd_bus_error
*error
) {
114 Hashmap
**h
= userdata
;
124 r
= sd_bus_message_open_container(reply
, 'a', "s");
128 HASHMAP_FOREACH_KEY(v
, u
, *h
, j
) {
129 r
= sd_bus_message_append(reply
, "s", u
->id
);
134 return sd_bus_message_close_container(reply
);
137 static int property_get_requires_mounts_for(
140 const char *interface
,
141 const char *property
,
142 sd_bus_message
*reply
,
144 sd_bus_error
*error
) {
146 Hashmap
**h
= userdata
;
156 r
= sd_bus_message_open_container(reply
, 'a', "s");
160 HASHMAP_FOREACH_KEY(v
, p
, *h
, j
) {
161 r
= sd_bus_message_append(reply
, "s", p
);
166 return sd_bus_message_close_container(reply
);
169 static int property_get_unit_file_preset(
172 const char *interface
,
173 const char *property
,
174 sd_bus_message
*reply
,
176 sd_bus_error
*error
) {
185 r
= unit_get_unit_file_preset(u
);
187 return sd_bus_message_append(reply
, "s",
189 r
> 0 ? "enabled" : "disabled");
192 static int property_get_job(
195 const char *interface
,
196 const char *property
,
197 sd_bus_message
*reply
,
199 sd_bus_error
*error
) {
201 _cleanup_free_
char *p
= NULL
;
209 return sd_bus_message_append(reply
, "(uo)", 0, "/");
211 p
= job_dbus_path(*j
);
215 return sd_bus_message_append(reply
, "(uo)", (*j
)->id
, p
);
218 static int property_get_conditions(
221 const char *interface
,
222 const char *property
,
223 sd_bus_message
*reply
,
225 sd_bus_error
*error
) {
227 const char *(*to_string
)(ConditionType type
) = NULL
;
228 Condition
**list
= userdata
, *c
;
235 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
237 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
241 LIST_FOREACH(conditions
, c
, *list
) {
245 c
->result
== CONDITION_UNTESTED
? 0 :
246 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
248 r
= sd_bus_message_append(reply
, "(sbbsi)",
250 c
->trigger
, c
->negate
,
251 c
->parameter
, tristate
);
257 return sd_bus_message_close_container(reply
);
260 static int property_get_load_error(
263 const char *interface
,
264 const char *property
,
265 sd_bus_message
*reply
,
267 sd_bus_error
*error
) {
269 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
277 r
= bus_unit_validate_load_state(u
, &e
);
279 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
281 return sd_bus_message_append(reply
, "(ss)", NULL
, NULL
);
284 static int bus_verify_manage_units_async_full(
288 const char *polkit_message
,
290 sd_bus_message
*call
,
291 sd_bus_error
*error
) {
293 const char *details
[9] = {
298 if (polkit_message
) {
299 details
[4] = "polkit.message";
300 details
[5] = polkit_message
;
301 details
[6] = "polkit.gettext_domain";
302 details
[7] = GETTEXT_PACKAGE
;
305 return bus_verify_polkit_async(
308 "org.freedesktop.systemd1.manage-units",
312 &u
->manager
->polkit_registry
,
316 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
317 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
318 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
319 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
320 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
321 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
324 int bus_unit_method_start_generic(
325 sd_bus_message
*message
,
328 bool reload_if_possible
,
329 sd_bus_error
*error
) {
331 const char *smode
, *verb
;
337 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
339 r
= mac_selinux_unit_access_check(
341 job_type_to_access_method(job_type
),
346 r
= sd_bus_message_read(message
, "s", &smode
);
350 mode
= job_mode_from_string(smode
);
352 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
354 if (reload_if_possible
)
355 verb
= strjoina("reload-or-", job_type_to_string(job_type
));
357 verb
= job_type_to_string(job_type
);
359 r
= bus_verify_manage_units_async_full(
363 polkit_message_for_job
[job_type
],
370 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
372 return bus_unit_queue_job(message
, u
, job_type
, mode
,
373 reload_if_possible
? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
: 0, 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_enqueue_job(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
405 BusUnitQueueFlags flags
= BUS_UNIT_QUEUE_VERBOSE_REPLY
;
406 const char *jtype
, *smode
;
415 r
= sd_bus_message_read(message
, "ss", &jtype
, &smode
);
419 /* Parse the two magic reload types "reload-or-…" manually */
420 if (streq(jtype
, "reload-or-restart")) {
422 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
423 } else if (streq(jtype
, "reload-or-try-restart")) {
424 type
= JOB_TRY_RESTART
;
425 flags
|= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
;
427 /* And the rest generically */
428 type
= job_type_from_string(jtype
);
430 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job type %s invalid", jtype
);
433 mode
= job_mode_from_string(smode
);
435 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
437 r
= mac_selinux_unit_access_check(
439 job_type_to_access_method(type
),
444 r
= bus_verify_manage_units_async_full(
448 polkit_message_for_job
[type
],
455 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
457 return bus_unit_queue_job(message
, u
, type
, mode
, flags
, error
);
460 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
470 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
474 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
481 who
= kill_who_from_string(swho
);
483 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
486 if (!SIGNAL_VALID(signo
))
487 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
489 r
= bus_verify_manage_units_async_full(
493 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
500 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
502 r
= unit_kill(u
, who
, signo
, error
);
506 return sd_bus_reply_method_return(message
, NULL
);
509 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
516 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
520 r
= bus_verify_manage_units_async_full(
524 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
531 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
533 unit_reset_failed(u
);
535 return sd_bus_reply_method_return(message
, NULL
);
538 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
545 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
549 r
= sd_bus_message_read(message
, "b", &runtime
);
553 r
= bus_verify_manage_units_async_full(
557 N_("Authentication is required to set properties on '$(unit)'."),
564 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
566 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
570 return sd_bus_reply_method_return(message
, NULL
);
573 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
580 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
584 r
= bus_verify_manage_units_async_full(
595 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
597 r
= bus_unit_track_add_sender(u
, message
);
601 return sd_bus_reply_method_return(message
, NULL
);
604 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
611 r
= bus_unit_track_remove_sender(u
, message
);
613 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
617 return sd_bus_reply_method_return(message
, NULL
);
620 static int property_get_refs(
623 const char *interface
,
624 const char *property
,
625 sd_bus_message
*reply
,
627 sd_bus_error
*error
) {
636 r
= sd_bus_message_open_container(reply
, 'a', "s");
640 for (i
= sd_bus_track_first(u
->bus_track
); i
; i
= sd_bus_track_next(u
->bus_track
)) {
643 c
= sd_bus_track_count_name(u
->bus_track
, i
);
647 /* Add the item multiple times if the ref count for each is above 1 */
648 for (k
= 0; k
< c
; k
++) {
649 r
= sd_bus_message_append(reply
, "s", i
);
655 return sd_bus_message_close_container(reply
);
658 const sd_bus_vtable bus_unit_vtable
[] = {
659 SD_BUS_VTABLE_START(0),
661 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
662 SD_BUS_PROPERTY("Names", "as", property_get_names
, offsetof(Unit
, names
), SD_BUS_VTABLE_PROPERTY_CONST
),
663 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
664 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
665 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
666 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
667 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
668 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
669 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
670 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
671 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
672 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
673 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
674 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
675 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
676 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
677 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
678 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
679 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
680 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
681 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
682 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
683 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
684 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
685 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
686 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
687 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
688 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
689 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
690 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
691 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
692 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
693 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
694 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
695 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
696 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
697 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
698 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
699 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
700 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
701 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
702 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
703 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
704 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, offsetof(Unit
, job
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
705 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
706 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
707 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
708 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
709 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
710 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
711 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
712 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
713 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
714 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_running_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
715 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
716 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
717 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
718 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
719 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
720 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
721 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
722 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
723 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
724 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
725 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
726 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
727 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_limit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
728 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
729 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action
, offsetof(Unit
, failure_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
730 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, failure_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
731 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action
, offsetof(Unit
, success_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
732 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int
, offsetof(Unit
, success_action_exit_status
), SD_BUS_VTABLE_PROPERTY_CONST
),
733 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
734 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
735 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode
, offsetof(Unit
, collect_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
736 SD_BUS_PROPERTY("Refs", "as", property_get_refs
, 0, 0),
738 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
739 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
740 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
741 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
742 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
743 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
744 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
745 SD_BUS_METHOD("EnqueueJob", "ss", "uososa(uosos)", bus_unit_method_enqueue_job
, SD_BUS_VTABLE_UNPRIVILEGED
),
746 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
747 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
748 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
749 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
750 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
752 /* For dependency types we don't support anymore always return an empty array */
753 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
754 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
755 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
756 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv
, 0, SD_BUS_VTABLE_HIDDEN
),
757 /* Obsolete alias names */
758 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
759 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
763 static int property_get_slice(
766 const char *interface
,
767 const char *property
,
768 sd_bus_message
*reply
,
770 sd_bus_error
*error
) {
778 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
781 static int property_get_current_memory(
784 const char *interface
,
785 const char *property
,
786 sd_bus_message
*reply
,
788 sd_bus_error
*error
) {
790 uint64_t sz
= (uint64_t) -1;
798 r
= unit_get_memory_current(u
, &sz
);
799 if (r
< 0 && r
!= -ENODATA
)
800 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
802 return sd_bus_message_append(reply
, "t", sz
);
805 static int property_get_current_tasks(
808 const char *interface
,
809 const char *property
,
810 sd_bus_message
*reply
,
812 sd_bus_error
*error
) {
814 uint64_t cn
= (uint64_t) -1;
822 r
= unit_get_tasks_current(u
, &cn
);
823 if (r
< 0 && r
!= -ENODATA
)
824 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
826 return sd_bus_message_append(reply
, "t", cn
);
829 static int property_get_cpu_usage(
832 const char *interface
,
833 const char *property
,
834 sd_bus_message
*reply
,
836 sd_bus_error
*error
) {
838 nsec_t ns
= (nsec_t
) -1;
846 r
= unit_get_cpu_usage(u
, &ns
);
847 if (r
< 0 && r
!= -ENODATA
)
848 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
850 return sd_bus_message_append(reply
, "t", ns
);
853 static int property_get_cgroup(
856 const char *interface
,
857 const char *property
,
858 sd_bus_message
*reply
,
860 sd_bus_error
*error
) {
863 const char *t
= NULL
;
869 /* Three cases: a) u->cgroup_path is NULL, in which case the
870 * unit has no control group, which we report as the empty
871 * string. b) u->cgroup_path is the empty string, which
872 * indicates the root cgroup, which we report as "/". c) all
873 * other cases we report as-is. */
876 t
= empty_to_root(u
->cgroup_path
);
878 return sd_bus_message_append(reply
, "s", t
);
881 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
882 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
888 r
= set_put(pids
, PID_TO_PTR(pid
));
889 if (IN_SET(r
, 0, -EEXIST
))
895 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
904 (void) get_process_cmdline(pid
, SIZE_MAX
, true, &cmdline
);
906 return sd_bus_message_append(reply
,
913 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
914 _cleanup_closedir_
DIR *d
= NULL
;
915 _cleanup_fclose_
FILE *f
= NULL
;
921 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
930 r
= cg_read_pid(f
, &pid
);
936 if (is_kernel_thread(pid
) > 0)
939 r
= append_process(reply
, p
, pid
, pids
);
944 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
951 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
953 r
= cg_read_subgroup(d
, &g
);
959 j
= strjoin(p
, "/", g
);
963 r
= append_cgroup(reply
, j
, pids
);
971 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
972 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
973 _cleanup_set_free_ Set
*pids
= NULL
;
980 r
= mac_selinux_unit_access_check(u
, message
, "status", error
);
984 pids
= set_new(NULL
);
988 r
= sd_bus_message_new_method_return(message
, &reply
);
992 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
996 if (u
->cgroup_path
) {
997 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1002 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1003 pid
= unit_main_pid(u
);
1005 r
= append_process(reply
, NULL
, pid
, pids
);
1010 pid
= unit_control_pid(u
);
1012 r
= append_process(reply
, NULL
, pid
, pids
);
1017 r
= sd_bus_message_close_container(reply
);
1021 return sd_bus_send(NULL
, reply
, NULL
);
1024 static int property_get_ip_counter(
1027 const char *interface
,
1028 const char *property
,
1029 sd_bus_message
*reply
,
1031 sd_bus_error
*error
) {
1033 static const char *const table
[_CGROUP_IP_ACCOUNTING_METRIC_MAX
] = {
1034 [CGROUP_IP_INGRESS_BYTES
] = "IPIngressBytes",
1035 [CGROUP_IP_EGRESS_BYTES
] = "IPEgressBytes",
1036 [CGROUP_IP_INGRESS_PACKETS
] = "IPIngressPackets",
1037 [CGROUP_IP_EGRESS_PACKETS
] = "IPEgressPackets",
1040 uint64_t value
= UINT64_MAX
;
1049 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1050 (void) unit_get_ip_accounting(u
, metric
, &value
);
1051 return sd_bus_message_append(reply
, "t", value
);
1054 static int property_get_io_counter(
1057 const char *interface
,
1058 const char *property
,
1059 sd_bus_message
*reply
,
1061 sd_bus_error
*error
) {
1063 static const char *const table
[_CGROUP_IO_ACCOUNTING_METRIC_MAX
] = {
1064 [CGROUP_IO_READ_BYTES
] = "IOReadBytes",
1065 [CGROUP_IO_WRITE_BYTES
] = "IOWriteBytes",
1066 [CGROUP_IO_READ_OPERATIONS
] = "IOReadOperations",
1067 [CGROUP_IO_WRITE_OPERATIONS
] = "IOWriteOperations",
1070 uint64_t value
= UINT64_MAX
;
1079 assert_se((metric
= string_table_lookup(table
, ELEMENTSOF(table
), property
)) >= 0);
1080 (void) unit_get_io_accounting(u
, metric
, false, &value
);
1081 return sd_bus_message_append(reply
, "t", value
);
1084 int bus_unit_method_attach_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1086 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1087 _cleanup_set_free_ Set
*pids
= NULL
;
1094 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1095 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1096 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1097 * subcgroup path has no effect! */
1099 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
1103 r
= sd_bus_message_read(message
, "s", &path
);
1107 path
= empty_to_null(path
);
1109 if (!path_is_absolute(path
))
1110 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not absolute: %s", path
);
1112 if (!path_is_normalized(path
))
1113 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Control group path is not normalized: %s", path
);
1116 if (!unit_cgroup_delegate(u
))
1117 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Process migration not available on non-delegated units.");
1119 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
1120 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit is not active, refusing.");
1122 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
1126 r
= sd_bus_message_enter_container(message
, 'a', "u");
1130 uid_t process_uid
, sender_uid
;
1134 r
= sd_bus_message_read(message
, "u", &upid
);
1141 r
= sd_bus_creds_get_pid(creds
, &pid
);
1147 /* Filter out duplicates */
1148 if (set_contains(pids
, PID_TO_PTR(pid
)))
1151 /* Check if this process is suitable for attaching to this unit */
1152 r
= unit_pid_attachable(u
, pid
, error
);
1156 /* Let's query the sender's UID, so that we can make our security decisions */
1157 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
1161 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1162 * then the process' UID and the target unit's UID have to match the sender's UID */
1163 if (sender_uid
!= 0 && sender_uid
!= getuid()) {
1164 r
= get_process_uid(pid
, &process_uid
);
1166 return sd_bus_error_set_errnof(error
, r
, "Failed to retrieve process UID: %m");
1168 if (process_uid
!= sender_uid
)
1169 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by client's UID. Refusing.", pid
);
1170 if (process_uid
!= u
->ref_uid
)
1171 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Process " PID_FMT
" not owned by target unit's UID. Refusing.", pid
);
1175 pids
= set_new(NULL
);
1180 r
= set_put(pids
, PID_TO_PTR(pid
));
1185 r
= sd_bus_message_exit_container(message
);
1189 r
= unit_attach_pids_to_cgroup(u
, pids
, path
);
1191 return sd_bus_error_set_errnof(error
, r
, "Failed to attach processes to control group: %m");
1193 return sd_bus_reply_method_return(message
, NULL
);
1196 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1197 SD_BUS_VTABLE_START(0),
1198 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1199 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1200 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1201 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1202 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1203 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter
, 0, 0),
1204 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter
, 0, 0),
1205 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter
, 0, 0),
1206 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter
, 0, 0),
1207 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter
, 0, 0),
1208 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter
, 0, 0),
1209 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter
, 0, 0),
1210 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter
, 0, 0),
1211 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1212 SD_BUS_METHOD("AttachProcesses", "sau", NULL
, bus_unit_method_attach_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1216 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1217 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1218 _cleanup_free_
char *p
= NULL
;
1225 p
= unit_dbus_path(u
);
1229 r
= sd_bus_message_new_signal(
1232 "/org/freedesktop/systemd1",
1233 "org.freedesktop.systemd1.Manager",
1238 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1242 return sd_bus_send(bus
, m
, NULL
);
1245 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1246 _cleanup_free_
char *p
= NULL
;
1253 p
= unit_dbus_path(u
);
1257 /* Send a properties changed signal. First for the specific
1258 * type, then for the generic unit. The clients may rely on
1259 * this order to get atomic behavior if needed. */
1261 r
= sd_bus_emit_properties_changed_strv(
1263 unit_dbus_interface_from_type(u
->type
),
1268 return sd_bus_emit_properties_changed_strv(
1270 "org.freedesktop.systemd1.Unit",
1274 void bus_unit_send_change_signal(Unit
*u
) {
1278 if (u
->in_dbus_queue
) {
1279 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1280 u
->in_dbus_queue
= false;
1286 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1288 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1290 u
->sent_dbus_new_signal
= true;
1293 void bus_unit_send_pending_change_signal(Unit
*u
, bool including_new
) {
1295 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1296 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1297 * so that clients can follow the full state transition */
1299 if (!u
->in_dbus_queue
) /* If not enqueued, don't bother */
1302 if (!u
->sent_dbus_new_signal
&& !including_new
) /* If the unit was never announced, don't bother, it's fine if
1303 * the unit appears in the new state right-away (except if the
1304 * caller explicitly asked us to send it anyway) */
1307 if (MANAGER_IS_RELOADING(u
->manager
)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1308 * when we are reloading. */
1311 bus_unit_send_change_signal(u
);
1314 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1315 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1316 _cleanup_free_
char *p
= NULL
;
1323 p
= unit_dbus_path(u
);
1327 r
= sd_bus_message_new_signal(
1330 "/org/freedesktop/systemd1",
1331 "org.freedesktop.systemd1.Manager",
1336 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1340 return sd_bus_send(bus
, m
, NULL
);
1343 void bus_unit_send_removed_signal(Unit
*u
) {
1347 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1348 bus_unit_send_change_signal(u
);
1353 r
= bus_foreach_bus(u
->manager
, u
->bus_track
, send_removed_signal
, u
);
1355 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1358 int bus_unit_queue_job(
1359 sd_bus_message
*message
,
1363 BusUnitQueueFlags flags
,
1364 sd_bus_error
*error
) {
1366 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1367 _cleanup_free_
char *job_path
= NULL
, *unit_path
= NULL
;
1368 _cleanup_(set_freep
) Set
*affected
= NULL
;
1375 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1376 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1378 r
= mac_selinux_unit_access_check(
1380 job_type_to_access_method(type
),
1385 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE
) && unit_can_reload(u
)) {
1386 if (type
== JOB_RESTART
)
1387 type
= JOB_RELOAD_OR_START
;
1388 else if (type
== JOB_TRY_RESTART
)
1389 type
= JOB_TRY_RELOAD
;
1392 if (type
== JOB_STOP
&&
1393 IN_SET(u
->load_state
, UNIT_NOT_FOUND
, UNIT_ERROR
, UNIT_BAD_SETTING
) &&
1394 unit_active_state(u
) == UNIT_INACTIVE
)
1395 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1397 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1398 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1399 (IN_SET(type
, JOB_RESTART
, JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1400 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1401 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
);
1403 if (FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
)) {
1404 affected
= set_new(NULL
);
1409 r
= manager_add_job(u
->manager
, type
, u
, mode
, affected
, error
, &j
);
1413 r
= bus_job_track_sender(j
, message
);
1417 /* Before we send the method reply, force out the announcement JobNew for this job */
1418 bus_job_send_pending_change_signal(j
, true);
1420 job_path
= job_dbus_path(j
);
1424 /* The classic response is just a job object path */
1425 if (!FLAGS_SET(flags
, BUS_UNIT_QUEUE_VERBOSE_REPLY
))
1426 return sd_bus_reply_method_return(message
, "o", job_path
);
1428 /* In verbose mode respond with the anchor job plus everything that has been affected */
1429 r
= sd_bus_message_new_method_return(message
, &reply
);
1433 unit_path
= unit_dbus_path(j
->unit
);
1437 r
= sd_bus_message_append(reply
, "uosos",
1439 j
->unit
->id
, unit_path
,
1440 job_type_to_string(j
->type
));
1444 r
= sd_bus_message_open_container(reply
, 'a', "(uosos)");
1448 SET_FOREACH(a
, affected
, i
) {
1453 /* Free paths from previous iteration */
1454 job_path
= mfree(job_path
);
1455 unit_path
= mfree(unit_path
);
1457 job_path
= job_dbus_path(a
);
1461 unit_path
= unit_dbus_path(a
->unit
);
1465 r
= sd_bus_message_append(reply
, "(uosos)",
1467 a
->unit
->id
, unit_path
,
1468 job_type_to_string(a
->type
));
1473 r
= sd_bus_message_close_container(reply
);
1477 return sd_bus_send(NULL
, reply
, NULL
);
1480 static int bus_unit_set_live_property(
1483 sd_bus_message
*message
,
1484 UnitWriteFlags flags
,
1485 sd_bus_error
*error
) {
1493 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1494 * units that are being created). */
1496 if (streq(name
, "Description")) {
1499 r
= sd_bus_message_read(message
, "s", &d
);
1503 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1504 r
= unit_set_description(u
, d
);
1508 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "Description=%s", d
);
1517 static int bus_set_transient_emergency_action(
1521 sd_bus_message
*message
,
1522 UnitWriteFlags flags
,
1523 sd_bus_error
*error
) {
1532 r
= sd_bus_message_read(message
, "s", &s
);
1536 system
= MANAGER_IS_SYSTEM(u
->manager
);
1537 r
= parse_emergency_action(s
, system
, &v
);
1539 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1540 r
== -EOPNOTSUPP
? "%s setting invalid for manager type: %s"
1541 : "Invalid %s setting: %s",
1544 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1546 unit_write_settingf(u
, flags
, name
,
1553 static int bus_set_transient_exit_status(
1557 sd_bus_message
*message
,
1558 UnitWriteFlags flags
,
1559 sd_bus_error
*error
) {
1566 r
= sd_bus_message_read(message
, "i", &k
);
1571 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Exit status must be in range 0…255 or negative.");
1573 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1574 *p
= k
< 0 ? -1 : k
;
1577 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1579 unit_write_settingf(u
, flags
, name
, "%s=%i", name
, k
);
1585 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode
, CollectMode
, collect_mode_from_string
);
1586 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode
, JobMode
, job_mode_from_string
);
1588 static int bus_set_transient_conditions(
1593 sd_bus_message
*message
,
1594 UnitWriteFlags flags
,
1595 sd_bus_error
*error
) {
1597 const char *type_name
, *param
;
1598 int trigger
, negate
, r
;
1603 r
= sd_bus_message_enter_container(message
, 'a', "(sbbs)");
1607 while ((r
= sd_bus_message_read(message
, "(sbbs)", &type_name
, &trigger
, &negate
, ¶m
)) > 0) {
1610 t
= is_condition
? condition_type_from_string(type_name
) : assert_type_from_string(type_name
);
1612 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid condition type: %s", type_name
);
1614 if (t
!= CONDITION_NULL
) {
1616 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Condition parameter in %s is empty", type_name
);
1618 if (condition_takes_path(t
) && !path_is_absolute(param
))
1619 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in condition %s is not absolute: %s", type_name
, param
);
1623 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1626 c
= condition_new(t
, param
, trigger
, negate
);
1630 LIST_PREPEND(conditions
, *list
, c
);
1632 if (t
!= CONDITION_NULL
)
1633 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
,
1634 "%s=%s%s%s", type_name
,
1635 trigger
? "|" : "", negate
? "!" : "", param
);
1637 unit_write_settingf(u
, flags
, name
,
1638 "%s=%s%s", type_name
,
1639 trigger
? "|" : "", yes_no(!negate
));
1647 r
= sd_bus_message_exit_container(message
);
1651 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
1652 *list
= condition_free_list(*list
);
1653 unit_write_settingf(u
, flags
, name
, "%sNull=", is_condition
? "Condition" : "Assert");
1659 static int bus_unit_set_transient_property(
1662 sd_bus_message
*message
,
1663 UnitWriteFlags flags
,
1664 sd_bus_error
*error
) {
1666 UnitDependency d
= _UNIT_DEPENDENCY_INVALID
;
1673 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1674 * has been created. */
1676 if (streq(name
, "SourcePath"))
1677 return bus_set_transient_path(u
, name
, &u
->source_path
, message
, flags
, error
);
1679 if (streq(name
, "StopWhenUnneeded"))
1680 return bus_set_transient_bool(u
, name
, &u
->stop_when_unneeded
, message
, flags
, error
);
1682 if (streq(name
, "RefuseManualStart"))
1683 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_start
, message
, flags
, error
);
1685 if (streq(name
, "RefuseManualStop"))
1686 return bus_set_transient_bool(u
, name
, &u
->refuse_manual_stop
, message
, flags
, error
);
1688 if (streq(name
, "AllowIsolate"))
1689 return bus_set_transient_bool(u
, name
, &u
->allow_isolate
, message
, flags
, error
);
1691 if (streq(name
, "DefaultDependencies"))
1692 return bus_set_transient_bool(u
, name
, &u
->default_dependencies
, message
, flags
, error
);
1694 if (streq(name
, "OnFailureJobMode"))
1695 return bus_set_transient_job_mode(u
, name
, &u
->on_failure_job_mode
, message
, flags
, error
);
1697 if (streq(name
, "IgnoreOnIsolate"))
1698 return bus_set_transient_bool(u
, name
, &u
->ignore_on_isolate
, message
, flags
, error
);
1700 if (streq(name
, "JobTimeoutUSec")) {
1701 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_timeout
, message
, flags
, error
);
1702 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
) && !u
->job_running_timeout_set
)
1703 u
->job_running_timeout
= u
->job_timeout
;
1706 if (streq(name
, "JobRunningTimeoutUSec")) {
1707 r
= bus_set_transient_usec_fix_0(u
, name
, &u
->job_running_timeout
, message
, flags
, error
);
1708 if (r
>= 0 && !UNIT_WRITE_FLAGS_NOOP(flags
))
1709 u
->job_running_timeout_set
= true;
1714 if (streq(name
, "JobTimeoutAction"))
1715 return bus_set_transient_emergency_action(u
, name
, &u
->job_timeout_action
, message
, flags
, error
);
1717 if (streq(name
, "JobTimeoutRebootArgument"))
1718 return bus_set_transient_string(u
, name
, &u
->job_timeout_reboot_arg
, message
, flags
, error
);
1720 if (streq(name
, "StartLimitIntervalUSec"))
1721 return bus_set_transient_usec(u
, name
, &u
->start_limit
.interval
, message
, flags
, error
);
1723 if (streq(name
, "StartLimitBurst"))
1724 return bus_set_transient_unsigned(u
, name
, &u
->start_limit
.burst
, message
, flags
, error
);
1726 if (streq(name
, "StartLimitAction"))
1727 return bus_set_transient_emergency_action(u
, name
, &u
->start_limit_action
, message
, flags
, error
);
1729 if (streq(name
, "FailureAction"))
1730 return bus_set_transient_emergency_action(u
, name
, &u
->failure_action
, message
, flags
, error
);
1732 if (streq(name
, "SuccessAction"))
1733 return bus_set_transient_emergency_action(u
, name
, &u
->success_action
, message
, flags
, error
);
1735 if (streq(name
, "FailureActionExitStatus"))
1736 return bus_set_transient_exit_status(u
, name
, &u
->failure_action_exit_status
, message
, flags
, error
);
1738 if (streq(name
, "SuccessActionExitStatus"))
1739 return bus_set_transient_exit_status(u
, name
, &u
->success_action_exit_status
, message
, flags
, error
);
1741 if (streq(name
, "RebootArgument"))
1742 return bus_set_transient_string(u
, name
, &u
->reboot_arg
, message
, flags
, error
);
1744 if (streq(name
, "CollectMode"))
1745 return bus_set_transient_collect_mode(u
, name
, &u
->collect_mode
, message
, flags
, error
);
1747 if (streq(name
, "Conditions"))
1748 return bus_set_transient_conditions(u
, name
, &u
->conditions
, true, message
, flags
, error
);
1750 if (streq(name
, "Asserts"))
1751 return bus_set_transient_conditions(u
, name
, &u
->asserts
, false, message
, flags
, error
);
1753 if (streq(name
, "Documentation")) {
1754 _cleanup_strv_free_
char **l
= NULL
;
1757 r
= sd_bus_message_read_strv(message
, &l
);
1761 STRV_FOREACH(p
, l
) {
1762 if (!documentation_url_is_valid(*p
))
1763 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid URL in %s: %s", name
, *p
);
1766 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1767 if (strv_isempty(l
)) {
1768 u
->documentation
= strv_free(u
->documentation
);
1769 unit_write_settingf(u
, flags
, name
, "%s=", name
);
1771 strv_extend_strv(&u
->documentation
, l
, false);
1774 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1780 } else if (streq(name
, "Slice")) {
1784 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1785 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1786 if (u
->type
== UNIT_SLICE
)
1787 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1788 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1789 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1791 r
= sd_bus_message_read(message
, "s", &s
);
1795 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1796 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1798 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1799 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1800 * instead of manager_load_unit() on purpose, here. */
1801 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1805 if (slice
->type
!= UNIT_SLICE
)
1806 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1808 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1809 r
= unit_set_slice(u
, slice
);
1813 unit_write_settingf(u
, flags
|UNIT_PRIVATE
, name
, "Slice=%s", s
);
1818 } else if (streq(name
, "RequiresMountsFor")) {
1819 _cleanup_strv_free_
char **l
= NULL
;
1822 r
= sd_bus_message_read_strv(message
, &l
);
1826 STRV_FOREACH(p
, l
) {
1827 path_simplify(*p
, true);
1829 if (!path_is_absolute(*p
))
1830 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not absolute: %s", name
, *p
);
1832 if (!path_is_valid(*p
))
1833 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s has invalid length: %s", name
, *p
);
1835 if (!path_is_normalized(*p
))
1836 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path specified in %s is not normalized: %s", name
, *p
);
1838 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1839 r
= unit_require_mounts_for(u
, *p
, UNIT_DEPENDENCY_FILE
);
1841 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Failed to add required mount \"%s\": %m", *p
);
1843 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, *p
);
1850 if (streq(name
, "RequiresOverridable"))
1851 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1852 else if (streq(name
, "RequisiteOverridable"))
1853 d
= UNIT_REQUISITE
; /* same here */
1855 d
= unit_dependency_from_string(name
);
1860 r
= sd_bus_message_enter_container(message
, 'a', "s");
1864 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1865 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1866 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1868 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1869 _cleanup_free_
char *label
= NULL
;
1871 r
= unit_add_dependency_by_name(u
, d
, other
, true, UNIT_DEPENDENCY_FILE
);
1875 label
= strjoin(name
, "-", other
);
1879 unit_write_settingf(u
, flags
, label
, "%s=%s", unit_dependency_to_string(d
), other
);
1886 r
= sd_bus_message_exit_container(message
);
1892 } else if (streq(name
, "AddRef")) {
1896 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
1897 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
1898 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
1899 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
1900 * dependency type, hence let's not confuse things with that.
1902 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
1903 * the transient unit is complete, so that setting this property multiple times in the same transient
1904 * unit creation call doesn't count as individual references. */
1906 r
= sd_bus_message_read(message
, "b", &b
);
1910 if (!UNIT_WRITE_FLAGS_NOOP(flags
))
1911 u
->bus_track_add
= b
;
1919 int bus_unit_set_properties(
1921 sd_bus_message
*message
,
1922 UnitWriteFlags flags
,
1924 sd_bus_error
*error
) {
1926 bool for_real
= false;
1933 /* We iterate through the array twice. First run we just check
1934 * if all passed data is valid, second run actually applies
1935 * it. This is to implement transaction-like behaviour without
1936 * actually providing full transactions. */
1938 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1946 r
= sd_bus_message_enter_container(message
, 'r', "sv");
1950 if (for_real
|| UNIT_WRITE_FLAGS_NOOP(flags
))
1953 /* Reached EOF. Let's try again, and this time for realz... */
1954 r
= sd_bus_message_rewind(message
, false);
1962 r
= sd_bus_message_read(message
, "s", &name
);
1966 if (!UNIT_VTABLE(u
)->bus_set_property
)
1967 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
1969 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
1973 /* If not for real, then mask out the two target flags */
1974 f
= for_real
? flags
: (flags
& ~(UNIT_RUNTIME
|UNIT_PERSISTENT
));
1976 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, f
, error
);
1977 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
1978 r
= bus_unit_set_transient_property(u
, name
, message
, f
, error
);
1980 r
= bus_unit_set_live_property(u
, name
, message
, f
, error
);
1985 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
1987 r
= sd_bus_message_exit_container(message
);
1991 r
= sd_bus_message_exit_container(message
);
1998 r
= sd_bus_message_exit_container(message
);
2002 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
2003 UNIT_VTABLE(u
)->bus_commit_properties(u
);
2008 int bus_unit_validate_load_state(Unit
*u
, sd_bus_error
*error
) {
2011 /* Generates a pretty error if a unit isn't properly loaded. */
2013 switch (u
->load_state
) {
2018 case UNIT_NOT_FOUND
:
2019 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
2021 case UNIT_BAD_SETTING
:
2022 return sd_bus_error_setf(error
, BUS_ERROR_BAD_UNIT_SETTING
, "Unit %s has a bad unit file setting.", u
->id
);
2024 case UNIT_ERROR
: /* Only show .load_error in UNIT_ERROR state */
2025 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s failed to load properly: %m.", u
->id
);
2028 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
2033 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unexpected load state of unit %s", u
->id
);
2037 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
2043 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
2045 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2047 unit_add_to_cgroup_empty_queue(u
);
2049 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2050 unit_add_to_gc_queue(u
);
2055 static int bus_unit_allocate_bus_track(Unit
*u
) {
2063 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
2067 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
2069 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
2076 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
2081 r
= bus_unit_allocate_bus_track(u
);
2085 return sd_bus_track_add_name(u
->bus_track
, name
);
2088 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
2093 r
= bus_unit_allocate_bus_track(u
);
2097 return sd_bus_track_add_sender(u
->bus_track
, m
);
2100 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
2103 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
2108 return sd_bus_track_remove_sender(u
->bus_track
, m
);