2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "bus-common-errors.h"
24 #include "cgroup-util.h"
25 #include "dbus-unit.h"
28 #include "locale-util.h"
30 #include "process-util.h"
31 #include "selinux-access.h"
32 #include "signal-util.h"
34 #include "string-util.h"
36 #include "user-util.h"
38 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
39 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
40 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_failure_action
, failure_action
, FailureAction
);
42 static int property_get_names(
45 const char *interface
,
47 sd_bus_message
*reply
,
49 sd_bus_error
*error
) {
60 r
= sd_bus_message_open_container(reply
, 'a', "s");
64 SET_FOREACH(t
, u
->names
, i
) {
65 r
= sd_bus_message_append(reply
, "s", t
);
70 return sd_bus_message_close_container(reply
);
73 static int property_get_following(
76 const char *interface
,
78 sd_bus_message
*reply
,
80 sd_bus_error
*error
) {
82 Unit
*u
= userdata
, *f
;
88 f
= unit_following(u
);
89 return sd_bus_message_append(reply
, "s", f
? f
->id
: "");
92 static int property_get_dependencies(
95 const char *interface
,
97 sd_bus_message
*reply
,
99 sd_bus_error
*error
) {
101 Set
*s
= *(Set
**) userdata
;
109 r
= sd_bus_message_open_container(reply
, 'a', "s");
113 SET_FOREACH(u
, s
, j
) {
114 r
= sd_bus_message_append(reply
, "s", u
->id
);
119 return sd_bus_message_close_container(reply
);
122 static int property_get_obsolete_dependencies(
125 const char *interface
,
126 const char *property
,
127 sd_bus_message
*reply
,
129 sd_bus_error
*error
) {
134 /* For dependency types we don't support anymore always return an empty array */
135 return sd_bus_message_append(reply
, "as", 0);
138 static int property_get_description(
141 const char *interface
,
142 const char *property
,
143 sd_bus_message
*reply
,
145 sd_bus_error
*error
) {
153 return sd_bus_message_append(reply
, "s", unit_description(u
));
156 static int property_get_active_state(
159 const char *interface
,
160 const char *property
,
161 sd_bus_message
*reply
,
163 sd_bus_error
*error
) {
171 return sd_bus_message_append(reply
, "s", unit_active_state_to_string(unit_active_state(u
)));
174 static int property_get_sub_state(
177 const char *interface
,
178 const char *property
,
179 sd_bus_message
*reply
,
181 sd_bus_error
*error
) {
189 return sd_bus_message_append(reply
, "s", unit_sub_state_to_string(u
));
192 static int property_get_unit_file_preset(
195 const char *interface
,
196 const char *property
,
197 sd_bus_message
*reply
,
199 sd_bus_error
*error
) {
208 r
= unit_get_unit_file_preset(u
);
210 return sd_bus_message_append(reply
, "s",
212 r
> 0 ? "enabled" : "disabled");
215 static int property_get_unit_file_state(
218 const char *interface
,
219 const char *property
,
220 sd_bus_message
*reply
,
222 sd_bus_error
*error
) {
230 return sd_bus_message_append(reply
, "s", unit_file_state_to_string(unit_get_unit_file_state(u
)));
233 static int property_get_can_start(
236 const char *interface
,
237 const char *property
,
238 sd_bus_message
*reply
,
240 sd_bus_error
*error
) {
248 return sd_bus_message_append(reply
, "b", unit_can_start(u
) && !u
->refuse_manual_start
);
251 static int property_get_can_stop(
254 const char *interface
,
255 const char *property
,
256 sd_bus_message
*reply
,
258 sd_bus_error
*error
) {
266 /* On the lower levels we assume that every unit we can start
267 * we can also stop */
269 return sd_bus_message_append(reply
, "b", unit_can_start(u
) && !u
->refuse_manual_stop
);
272 static int property_get_can_reload(
275 const char *interface
,
276 const char *property
,
277 sd_bus_message
*reply
,
279 sd_bus_error
*error
) {
287 return sd_bus_message_append(reply
, "b", unit_can_reload(u
));
290 static int property_get_can_isolate(
293 const char *interface
,
294 const char *property
,
295 sd_bus_message
*reply
,
297 sd_bus_error
*error
) {
305 return sd_bus_message_append(reply
, "b", unit_can_isolate(u
) && !u
->refuse_manual_start
);
308 static int property_get_job(
311 const char *interface
,
312 const char *property
,
313 sd_bus_message
*reply
,
315 sd_bus_error
*error
) {
317 _cleanup_free_
char *p
= NULL
;
325 return sd_bus_message_append(reply
, "(uo)", 0, "/");
327 p
= job_dbus_path(u
->job
);
331 return sd_bus_message_append(reply
, "(uo)", u
->job
->id
, p
);
334 static int property_get_need_daemon_reload(
337 const char *interface
,
338 const char *property
,
339 sd_bus_message
*reply
,
341 sd_bus_error
*error
) {
349 return sd_bus_message_append(reply
, "b", unit_need_daemon_reload(u
));
352 static int property_get_conditions(
355 const char *interface
,
356 const char *property
,
357 sd_bus_message
*reply
,
359 sd_bus_error
*error
) {
361 const char *(*to_string
)(ConditionType type
) = NULL
;
362 Condition
**list
= userdata
, *c
;
369 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
371 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
375 LIST_FOREACH(conditions
, c
, *list
) {
379 c
->result
== CONDITION_UNTESTED
? 0 :
380 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
382 r
= sd_bus_message_append(reply
, "(sbbsi)",
384 c
->trigger
, c
->negate
,
385 c
->parameter
, tristate
);
391 return sd_bus_message_close_container(reply
);
394 static int property_get_load_error(
397 const char *interface
,
398 const char *property
,
399 sd_bus_message
*reply
,
401 sd_bus_error
*error
) {
403 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
410 if (u
->load_error
!= 0)
411 sd_bus_error_set_errno(&e
, u
->load_error
);
413 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
416 static int bus_verify_manage_units_async_full(
420 const char *polkit_message
,
421 sd_bus_message
*call
,
422 sd_bus_error
*error
) {
424 const char *details
[9] = {
429 if (polkit_message
) {
430 details
[4] = "polkit.message";
431 details
[5] = polkit_message
;
432 details
[6] = "polkit.gettext_domain";
433 details
[7] = GETTEXT_PACKAGE
;
436 return bus_verify_polkit_async(call
, capability
, "org.freedesktop.systemd1.manage-units", details
, false, UID_INVALID
, &u
->manager
->polkit_registry
, error
);
439 int bus_unit_method_start_generic(
440 sd_bus_message
*message
,
443 bool reload_if_possible
,
444 sd_bus_error
*error
) {
448 _cleanup_free_
char *verb
= NULL
;
449 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
450 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
451 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
452 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
453 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
454 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
460 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
462 r
= mac_selinux_unit_access_check(
464 job_type_to_access_method(job_type
),
469 r
= sd_bus_message_read(message
, "s", &smode
);
473 mode
= job_mode_from_string(smode
);
475 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
477 if (reload_if_possible
)
478 verb
= strjoin("reload-or-", job_type_to_string(job_type
), NULL
);
480 verb
= strdup(job_type_to_string(job_type
));
484 r
= bus_verify_manage_units_async_full(
488 job_type
< _JOB_TYPE_MAX
? polkit_message_for_job
[job_type
] : NULL
,
494 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
496 return bus_unit_queue_job(message
, u
, job_type
, mode
, reload_if_possible
, error
);
499 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
500 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
503 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
504 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
507 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
508 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
511 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
512 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
515 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
516 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
519 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
520 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
523 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
524 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
527 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
537 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
541 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
548 who
= kill_who_from_string(swho
);
550 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
553 if (!SIGNAL_VALID(signo
))
554 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
556 r
= bus_verify_manage_units_async_full(
560 N_("Authentication is required to kill '$(unit)'."),
566 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
568 r
= unit_kill(u
, who
, signo
, error
);
572 return sd_bus_reply_method_return(message
, NULL
);
575 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
582 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
586 r
= bus_verify_manage_units_async_full(
590 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
596 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
598 unit_reset_failed(u
);
600 return sd_bus_reply_method_return(message
, NULL
);
603 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
610 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
614 r
= sd_bus_message_read(message
, "b", &runtime
);
618 r
= bus_verify_manage_units_async_full(
622 N_("Authentication is required to set properties on '$(unit)'."),
628 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
630 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
634 return sd_bus_reply_method_return(message
, NULL
);
637 const sd_bus_vtable bus_unit_vtable
[] = {
638 SD_BUS_VTABLE_START(0),
640 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
641 SD_BUS_PROPERTY("Names", "as", property_get_names
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
642 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
643 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
644 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
645 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
646 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
647 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
648 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
649 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
650 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
651 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
652 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
653 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
654 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
655 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
656 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
657 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
658 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
659 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
660 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
661 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
662 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
663 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
664 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
665 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
666 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
667 SD_BUS_PROPERTY("RequiresMountsFor", "as", NULL
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
668 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
669 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
670 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
671 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
672 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
673 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
674 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
675 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
676 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
677 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
678 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
679 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
680 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
681 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
682 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
683 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
684 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
685 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
686 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
687 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
688 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
689 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
690 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
691 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
692 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
693 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
694 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
695 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
696 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
697 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_failure_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
698 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
699 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
700 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
701 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
702 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
703 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), 0),
704 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), 0),
705 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
706 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
707 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
708 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
), /* obsolete alias name */
709 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_limit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
710 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_failure_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
711 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
713 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
714 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
715 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
716 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
717 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
718 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
719 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
720 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
721 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
722 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
727 static int property_get_slice(
730 const char *interface
,
731 const char *property
,
732 sd_bus_message
*reply
,
734 sd_bus_error
*error
) {
742 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
745 static int property_get_current_memory(
748 const char *interface
,
749 const char *property
,
750 sd_bus_message
*reply
,
752 sd_bus_error
*error
) {
754 uint64_t sz
= (uint64_t) -1;
762 r
= unit_get_memory_current(u
, &sz
);
763 if (r
< 0 && r
!= -ENODATA
)
764 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
766 return sd_bus_message_append(reply
, "t", sz
);
769 static int property_get_current_tasks(
772 const char *interface
,
773 const char *property
,
774 sd_bus_message
*reply
,
776 sd_bus_error
*error
) {
778 uint64_t cn
= (uint64_t) -1;
786 r
= unit_get_tasks_current(u
, &cn
);
787 if (r
< 0 && r
!= -ENODATA
)
788 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
790 return sd_bus_message_append(reply
, "t", cn
);
793 static int property_get_cpu_usage(
796 const char *interface
,
797 const char *property
,
798 sd_bus_message
*reply
,
800 sd_bus_error
*error
) {
802 nsec_t ns
= (nsec_t
) -1;
810 r
= unit_get_cpu_usage(u
, &ns
);
811 if (r
< 0 && r
!= -ENODATA
)
812 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
814 return sd_bus_message_append(reply
, "t", ns
);
817 static int property_get_cgroup(
820 const char *interface
,
821 const char *property
,
822 sd_bus_message
*reply
,
824 sd_bus_error
*error
) {
833 /* Three cases: a) u->cgroup_path is NULL, in which case the
834 * unit has no control group, which we report as the empty
835 * string. b) u->cgroup_path is the empty string, which
836 * indicates the root cgroup, which we report as "/". c) all
837 * other cases we report as-is. */
840 t
= isempty(u
->cgroup_path
) ? "/" : u
->cgroup_path
;
844 return sd_bus_message_append(reply
, "s", t
);
847 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
848 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
854 r
= set_put(pids
, PID_TO_PTR(pid
));
855 if (r
== -EEXIST
|| r
== 0)
861 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
870 (void) get_process_cmdline(pid
, 0, true, &cmdline
);
872 return sd_bus_message_append(reply
,
879 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
880 _cleanup_closedir_
DIR *d
= NULL
;
881 _cleanup_fclose_
FILE *f
= NULL
;
887 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
896 r
= cg_read_pid(f
, &pid
);
902 if (is_kernel_thread(pid
) > 0)
905 r
= append_process(reply
, p
, pid
, pids
);
910 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
917 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
919 r
= cg_read_subgroup(d
, &g
);
925 j
= strjoin(p
, "/", g
, NULL
);
929 r
= append_cgroup(reply
, j
, pids
);
937 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
938 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
939 _cleanup_(set_freep
) Set
*pids
= NULL
;
946 pids
= set_new(NULL
);
950 r
= sd_bus_message_new_method_return(message
, &reply
);
954 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
958 if (u
->cgroup_path
) {
959 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
964 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
965 pid
= unit_main_pid(u
);
967 r
= append_process(reply
, NULL
, pid
, pids
);
972 pid
= unit_control_pid(u
);
974 r
= append_process(reply
, NULL
, pid
, pids
);
979 r
= sd_bus_message_close_container(reply
);
983 return sd_bus_send(NULL
, reply
, NULL
);
986 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
987 SD_BUS_VTABLE_START(0),
988 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
989 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
990 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
991 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
992 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
993 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
997 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
998 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
999 _cleanup_free_
char *p
= NULL
;
1006 p
= unit_dbus_path(u
);
1010 r
= sd_bus_message_new_signal(
1013 "/org/freedesktop/systemd1",
1014 "org.freedesktop.systemd1.Manager",
1019 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1023 return sd_bus_send(bus
, m
, NULL
);
1026 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1027 _cleanup_free_
char *p
= NULL
;
1034 p
= unit_dbus_path(u
);
1038 /* Send a properties changed signal. First for the specific
1039 * type, then for the generic unit. The clients may rely on
1040 * this order to get atomic behavior if needed. */
1042 r
= sd_bus_emit_properties_changed_strv(
1044 unit_dbus_interface_from_type(u
->type
),
1049 return sd_bus_emit_properties_changed_strv(
1051 "org.freedesktop.systemd1.Unit",
1055 void bus_unit_send_change_signal(Unit
*u
) {
1059 if (u
->in_dbus_queue
) {
1060 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1061 u
->in_dbus_queue
= false;
1067 r
= bus_foreach_bus(u
->manager
, NULL
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1069 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1071 u
->sent_dbus_new_signal
= true;
1074 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1075 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1076 _cleanup_free_
char *p
= NULL
;
1083 p
= unit_dbus_path(u
);
1087 r
= sd_bus_message_new_signal(
1090 "/org/freedesktop/systemd1",
1091 "org.freedesktop.systemd1.Manager",
1096 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1100 return sd_bus_send(bus
, m
, NULL
);
1103 void bus_unit_send_removed_signal(Unit
*u
) {
1107 if (!u
->sent_dbus_new_signal
)
1108 bus_unit_send_change_signal(u
);
1113 r
= bus_foreach_bus(u
->manager
, NULL
, send_removed_signal
, u
);
1115 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1118 int bus_unit_queue_job(
1119 sd_bus_message
*message
,
1123 bool reload_if_possible
,
1124 sd_bus_error
*error
) {
1126 _cleanup_free_
char *path
= NULL
;
1132 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1133 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1135 r
= mac_selinux_unit_access_check(
1137 job_type_to_access_method(type
),
1142 if (reload_if_possible
&& unit_can_reload(u
)) {
1143 if (type
== JOB_RESTART
)
1144 type
= JOB_RELOAD_OR_START
;
1145 else if (type
== JOB_TRY_RESTART
)
1146 type
= JOB_TRY_RELOAD
;
1149 if (type
== JOB_STOP
&&
1150 (u
->load_state
== UNIT_NOT_FOUND
|| u
->load_state
== UNIT_ERROR
) &&
1151 unit_active_state(u
) == UNIT_INACTIVE
)
1152 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1154 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1155 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1156 ((type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1157 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1158 return sd_bus_error_setf(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
, "Operation refused, unit %s may be requested by dependency only.", u
->id
);
1160 r
= manager_add_job(u
->manager
, type
, u
, mode
, error
, &j
);
1164 if (sd_bus_message_get_bus(message
) == u
->manager
->api_bus
) {
1166 r
= sd_bus_track_new(sd_bus_message_get_bus(message
), &j
->clients
, NULL
, NULL
);
1171 r
= sd_bus_track_add_sender(j
->clients
, message
);
1176 path
= job_dbus_path(j
);
1180 return sd_bus_reply_method_return(message
, "o", path
);
1183 static int bus_unit_set_transient_property(
1186 sd_bus_message
*message
,
1187 UnitSetPropertiesMode mode
,
1188 sd_bus_error
*error
) {
1196 if (streq(name
, "Description")) {
1199 r
= sd_bus_message_read(message
, "s", &d
);
1203 if (mode
!= UNIT_CHECK
) {
1204 r
= unit_set_description(u
, d
);
1208 unit_write_drop_in_format(u
, mode
, name
, "[Unit]\nDescription=%s", d
);
1213 } else if (streq(name
, "DefaultDependencies")) {
1216 r
= sd_bus_message_read(message
, "b", &b
);
1220 if (mode
!= UNIT_CHECK
) {
1221 u
->default_dependencies
= b
;
1222 unit_write_drop_in_format(u
, mode
, name
, "[Unit]\nDefaultDependencies=%s", yes_no(b
));
1227 } else if (streq(name
, "Slice")) {
1231 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1232 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1233 if (u
->type
== UNIT_SLICE
)
1234 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1235 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1236 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1238 r
= sd_bus_message_read(message
, "s", &s
);
1242 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1243 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1245 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1246 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1247 * instead of manager_load_unit() on purpose, here. */
1248 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1252 if (slice
->type
!= UNIT_SLICE
)
1253 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1255 if (mode
!= UNIT_CHECK
) {
1256 r
= unit_set_slice(u
, slice
);
1260 unit_write_drop_in_private_format(u
, mode
, name
, "Slice=%s", s
);
1265 } else if (STR_IN_SET(name
,
1266 "Requires", "RequiresOverridable",
1267 "Requisite", "RequisiteOverridable",
1273 "PropagatesReloadTo", "ReloadPropagatedFrom",
1279 if (streq(name
, "RequiresOverridable"))
1280 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1281 else if (streq(name
, "RequisiteOverridable"))
1282 d
= UNIT_REQUISITE
; /* same here */
1284 d
= unit_dependency_from_string(name
);
1286 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit dependency: %s", name
);
1289 r
= sd_bus_message_enter_container(message
, 'a', "s");
1293 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1294 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1295 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1297 if (mode
!= UNIT_CHECK
) {
1298 _cleanup_free_
char *label
= NULL
;
1300 r
= unit_add_dependency_by_name(u
, d
, other
, NULL
, true);
1304 label
= strjoin(name
, "-", other
, NULL
);
1308 unit_write_drop_in_format(u
, mode
, label
, "[Unit]\n%s=%s", name
, other
);
1315 r
= sd_bus_message_exit_container(message
);
1325 int bus_unit_set_properties(
1327 sd_bus_message
*message
,
1328 UnitSetPropertiesMode mode
,
1330 sd_bus_error
*error
) {
1332 bool for_real
= false;
1339 /* We iterate through the array twice. First run we just check
1340 * if all passed data is valid, second run actually applies
1341 * it. This is to implement transaction-like behaviour without
1342 * actually providing full transactions. */
1344 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1351 r
= sd_bus_message_enter_container(message
, 'r', "sv");
1355 if (for_real
|| mode
== UNIT_CHECK
)
1358 /* Reached EOF. Let's try again, and this time for realz... */
1359 r
= sd_bus_message_rewind(message
, false);
1367 r
= sd_bus_message_read(message
, "s", &name
);
1371 if (!UNIT_VTABLE(u
)->bus_set_property
)
1372 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
1374 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
1378 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, for_real
? mode
: UNIT_CHECK
, error
);
1379 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
1380 r
= bus_unit_set_transient_property(u
, name
, message
, for_real
? mode
: UNIT_CHECK
, error
);
1384 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
1386 r
= sd_bus_message_exit_container(message
);
1390 r
= sd_bus_message_exit_container(message
);
1397 r
= sd_bus_message_exit_container(message
);
1401 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
1402 UNIT_VTABLE(u
)->bus_commit_properties(u
);
1407 int bus_unit_check_load_state(Unit
*u
, sd_bus_error
*error
) {
1410 if (u
->load_state
== UNIT_LOADED
)
1413 /* Give a better description of the unit error when
1414 * possible. Note that in the case of UNIT_MASKED, load_error
1416 if (u
->load_state
== UNIT_MASKED
)
1417 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
1419 if (u
->load_state
== UNIT_NOT_FOUND
)
1420 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
1422 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s is not loaded properly: %m.", u
->id
);