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"
26 #include "dbus-unit.h"
29 #include "locale-util.h"
31 #include "process-util.h"
32 #include "selinux-access.h"
33 #include "signal-util.h"
35 #include "string-util.h"
37 #include "user-util.h"
39 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state
, unit_load_state
, UnitLoadState
);
40 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode
, job_mode
, JobMode
);
41 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action
, emergency_action
, EmergencyAction
);
43 static int property_get_names(
46 const char *interface
,
48 sd_bus_message
*reply
,
50 sd_bus_error
*error
) {
61 r
= sd_bus_message_open_container(reply
, 'a', "s");
65 SET_FOREACH(t
, u
->names
, i
) {
66 r
= sd_bus_message_append(reply
, "s", t
);
71 return sd_bus_message_close_container(reply
);
74 static int property_get_following(
77 const char *interface
,
79 sd_bus_message
*reply
,
81 sd_bus_error
*error
) {
83 Unit
*u
= userdata
, *f
;
89 f
= unit_following(u
);
90 return sd_bus_message_append(reply
, "s", f
? f
->id
: "");
93 static int property_get_dependencies(
96 const char *interface
,
98 sd_bus_message
*reply
,
100 sd_bus_error
*error
) {
102 Set
*s
= *(Set
**) userdata
;
110 r
= sd_bus_message_open_container(reply
, 'a', "s");
114 SET_FOREACH(u
, s
, j
) {
115 r
= sd_bus_message_append(reply
, "s", u
->id
);
120 return sd_bus_message_close_container(reply
);
123 static int property_get_obsolete_dependencies(
126 const char *interface
,
127 const char *property
,
128 sd_bus_message
*reply
,
130 sd_bus_error
*error
) {
135 /* For dependency types we don't support anymore always return an empty array */
136 return sd_bus_message_append(reply
, "as", 0);
139 static int property_get_description(
142 const char *interface
,
143 const char *property
,
144 sd_bus_message
*reply
,
146 sd_bus_error
*error
) {
154 return sd_bus_message_append(reply
, "s", unit_description(u
));
157 static int property_get_active_state(
160 const char *interface
,
161 const char *property
,
162 sd_bus_message
*reply
,
164 sd_bus_error
*error
) {
172 return sd_bus_message_append(reply
, "s", unit_active_state_to_string(unit_active_state(u
)));
175 static int property_get_sub_state(
178 const char *interface
,
179 const char *property
,
180 sd_bus_message
*reply
,
182 sd_bus_error
*error
) {
190 return sd_bus_message_append(reply
, "s", unit_sub_state_to_string(u
));
193 static int property_get_unit_file_preset(
196 const char *interface
,
197 const char *property
,
198 sd_bus_message
*reply
,
200 sd_bus_error
*error
) {
209 r
= unit_get_unit_file_preset(u
);
211 return sd_bus_message_append(reply
, "s",
213 r
> 0 ? "enabled" : "disabled");
216 static int property_get_unit_file_state(
219 const char *interface
,
220 const char *property
,
221 sd_bus_message
*reply
,
223 sd_bus_error
*error
) {
231 return sd_bus_message_append(reply
, "s", unit_file_state_to_string(unit_get_unit_file_state(u
)));
234 static int property_get_can_start(
237 const char *interface
,
238 const char *property
,
239 sd_bus_message
*reply
,
241 sd_bus_error
*error
) {
249 return sd_bus_message_append(reply
, "b", unit_can_start(u
) && !u
->refuse_manual_start
);
252 static int property_get_can_stop(
255 const char *interface
,
256 const char *property
,
257 sd_bus_message
*reply
,
259 sd_bus_error
*error
) {
267 return sd_bus_message_append(reply
, "b", unit_can_stop(u
) && !u
->refuse_manual_stop
);
270 static int property_get_can_reload(
273 const char *interface
,
274 const char *property
,
275 sd_bus_message
*reply
,
277 sd_bus_error
*error
) {
285 return sd_bus_message_append(reply
, "b", unit_can_reload(u
));
288 static int property_get_can_isolate(
291 const char *interface
,
292 const char *property
,
293 sd_bus_message
*reply
,
295 sd_bus_error
*error
) {
303 return sd_bus_message_append(reply
, "b", unit_can_isolate(u
) && !u
->refuse_manual_start
);
306 static int property_get_job(
309 const char *interface
,
310 const char *property
,
311 sd_bus_message
*reply
,
313 sd_bus_error
*error
) {
315 _cleanup_free_
char *p
= NULL
;
323 return sd_bus_message_append(reply
, "(uo)", 0, "/");
325 p
= job_dbus_path(u
->job
);
329 return sd_bus_message_append(reply
, "(uo)", u
->job
->id
, p
);
332 static int property_get_need_daemon_reload(
335 const char *interface
,
336 const char *property
,
337 sd_bus_message
*reply
,
339 sd_bus_error
*error
) {
347 return sd_bus_message_append(reply
, "b", unit_need_daemon_reload(u
));
350 static int property_get_conditions(
353 const char *interface
,
354 const char *property
,
355 sd_bus_message
*reply
,
357 sd_bus_error
*error
) {
359 const char *(*to_string
)(ConditionType type
) = NULL
;
360 Condition
**list
= userdata
, *c
;
367 to_string
= streq(property
, "Asserts") ? assert_type_to_string
: condition_type_to_string
;
369 r
= sd_bus_message_open_container(reply
, 'a', "(sbbsi)");
373 LIST_FOREACH(conditions
, c
, *list
) {
377 c
->result
== CONDITION_UNTESTED
? 0 :
378 c
->result
== CONDITION_SUCCEEDED
? 1 : -1;
380 r
= sd_bus_message_append(reply
, "(sbbsi)",
382 c
->trigger
, c
->negate
,
383 c
->parameter
, tristate
);
389 return sd_bus_message_close_container(reply
);
392 static int property_get_load_error(
395 const char *interface
,
396 const char *property
,
397 sd_bus_message
*reply
,
399 sd_bus_error
*error
) {
401 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
408 if (u
->load_error
!= 0)
409 sd_bus_error_set_errno(&e
, u
->load_error
);
411 return sd_bus_message_append(reply
, "(ss)", e
.name
, e
.message
);
414 static int bus_verify_manage_units_async_full(
418 const char *polkit_message
,
420 sd_bus_message
*call
,
421 sd_bus_error
*error
) {
423 const char *details
[9] = {
428 if (polkit_message
) {
429 details
[4] = "polkit.message";
430 details
[5] = polkit_message
;
431 details
[6] = "polkit.gettext_domain";
432 details
[7] = GETTEXT_PACKAGE
;
435 return bus_verify_polkit_async(
438 "org.freedesktop.systemd1.manage-units",
442 &u
->manager
->polkit_registry
,
446 int bus_unit_method_start_generic(
447 sd_bus_message
*message
,
450 bool reload_if_possible
,
451 sd_bus_error
*error
) {
455 _cleanup_free_
char *verb
= NULL
;
456 static const char *const polkit_message_for_job
[_JOB_TYPE_MAX
] = {
457 [JOB_START
] = N_("Authentication is required to start '$(unit)'."),
458 [JOB_STOP
] = N_("Authentication is required to stop '$(unit)'."),
459 [JOB_RELOAD
] = N_("Authentication is required to reload '$(unit)'."),
460 [JOB_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
461 [JOB_TRY_RESTART
] = N_("Authentication is required to restart '$(unit)'."),
467 assert(job_type
>= 0 && job_type
< _JOB_TYPE_MAX
);
469 r
= mac_selinux_unit_access_check(
471 job_type_to_access_method(job_type
),
476 r
= sd_bus_message_read(message
, "s", &smode
);
480 mode
= job_mode_from_string(smode
);
482 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Job mode %s invalid", smode
);
484 if (reload_if_possible
)
485 verb
= strjoin("reload-or-", job_type_to_string(job_type
));
487 verb
= strdup(job_type_to_string(job_type
));
491 r
= bus_verify_manage_units_async_full(
495 job_type
< _JOB_TYPE_MAX
? polkit_message_for_job
[job_type
] : NULL
,
502 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
504 return bus_unit_queue_job(message
, u
, job_type
, mode
, reload_if_possible
, error
);
507 static int method_start(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
508 return bus_unit_method_start_generic(message
, userdata
, JOB_START
, false, error
);
511 static int method_stop(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
512 return bus_unit_method_start_generic(message
, userdata
, JOB_STOP
, false, error
);
515 static int method_reload(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
516 return bus_unit_method_start_generic(message
, userdata
, JOB_RELOAD
, false, error
);
519 static int method_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
520 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, false, error
);
523 static int method_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
524 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, false, error
);
527 static int method_reload_or_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
528 return bus_unit_method_start_generic(message
, userdata
, JOB_RESTART
, true, error
);
531 static int method_reload_or_try_restart(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
532 return bus_unit_method_start_generic(message
, userdata
, JOB_TRY_RESTART
, true, error
);
535 int bus_unit_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
545 r
= mac_selinux_unit_access_check(u
, message
, "stop", error
);
549 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
556 who
= kill_who_from_string(swho
);
558 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid who argument %s", swho
);
561 if (!SIGNAL_VALID(signo
))
562 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Signal number out of range.");
564 r
= bus_verify_manage_units_async_full(
568 N_("Authentication is required to kill '$(unit)'."),
575 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
577 r
= unit_kill(u
, who
, signo
, error
);
581 return sd_bus_reply_method_return(message
, NULL
);
584 int bus_unit_method_reset_failed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
591 r
= mac_selinux_unit_access_check(u
, message
, "reload", error
);
595 r
= bus_verify_manage_units_async_full(
599 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
606 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
608 unit_reset_failed(u
);
610 return sd_bus_reply_method_return(message
, NULL
);
613 int bus_unit_method_set_properties(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
620 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
624 r
= sd_bus_message_read(message
, "b", &runtime
);
628 r
= bus_verify_manage_units_async_full(
632 N_("Authentication is required to set properties on '$(unit)'."),
639 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
641 r
= bus_unit_set_properties(u
, message
, runtime
? UNIT_RUNTIME
: UNIT_PERSISTENT
, true, error
);
645 return sd_bus_reply_method_return(message
, NULL
);
648 int bus_unit_method_ref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
655 r
= mac_selinux_unit_access_check(u
, message
, "start", error
);
659 r
= bus_verify_manage_units_async_full(
670 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
672 r
= bus_unit_track_add_sender(u
, message
);
676 return sd_bus_reply_method_return(message
, NULL
);
679 int bus_unit_method_unref(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
686 r
= bus_unit_track_remove_sender(u
, message
);
688 return sd_bus_error_setf(error
, BUS_ERROR_NOT_REFERENCED
, "Unit has not been referenced yet.");
692 return sd_bus_reply_method_return(message
, NULL
);
695 const sd_bus_vtable bus_unit_vtable
[] = {
696 SD_BUS_VTABLE_START(0),
698 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Unit
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
699 SD_BUS_PROPERTY("Names", "as", property_get_names
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
700 SD_BUS_PROPERTY("Following", "s", property_get_following
, 0, 0),
701 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRES
]), SD_BUS_VTABLE_PROPERTY_CONST
),
702 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
703 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
704 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BINDS_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
705 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PART_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
706 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUIRED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
707 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_REQUISITE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
708 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_WANTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
709 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BOUND_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
710 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONSISTS_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
711 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
712 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_CONFLICTED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
713 SD_BUS_PROPERTY("Before", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_BEFORE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
714 SD_BUS_PROPERTY("After", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_AFTER
]), SD_BUS_VTABLE_PROPERTY_CONST
),
715 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_ON_FAILURE
]), SD_BUS_VTABLE_PROPERTY_CONST
),
716 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERS
]), SD_BUS_VTABLE_PROPERTY_CONST
),
717 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_TRIGGERED_BY
]), SD_BUS_VTABLE_PROPERTY_CONST
),
718 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_PROPAGATES_RELOAD_TO
]), SD_BUS_VTABLE_PROPERTY_CONST
),
719 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_RELOAD_PROPAGATED_FROM
]), SD_BUS_VTABLE_PROPERTY_CONST
),
720 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies
, offsetof(Unit
, dependencies
[UNIT_JOINS_NAMESPACE_OF
]), SD_BUS_VTABLE_PROPERTY_CONST
),
721 SD_BUS_PROPERTY("RequiresMountsFor", "as", NULL
, offsetof(Unit
, requires_mounts_for
), SD_BUS_VTABLE_PROPERTY_CONST
),
722 SD_BUS_PROPERTY("Documentation", "as", NULL
, offsetof(Unit
, documentation
), SD_BUS_VTABLE_PROPERTY_CONST
),
723 SD_BUS_PROPERTY("Description", "s", property_get_description
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
724 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state
, offsetof(Unit
, load_state
), SD_BUS_VTABLE_PROPERTY_CONST
),
725 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
726 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
727 SD_BUS_PROPERTY("FragmentPath", "s", NULL
, offsetof(Unit
, fragment_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
728 SD_BUS_PROPERTY("SourcePath", "s", NULL
, offsetof(Unit
, source_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
729 SD_BUS_PROPERTY("DropInPaths", "as", NULL
, offsetof(Unit
, dropin_paths
), SD_BUS_VTABLE_PROPERTY_CONST
),
730 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state
, 0, 0),
731 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset
, 0, 0),
732 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit
, state_change_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
733 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit
, inactive_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
734 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit
, active_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
735 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit
, active_exit_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
736 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit
, inactive_enter_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
737 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
738 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
739 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
740 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
741 SD_BUS_PROPERTY("Job", "(uo)", property_get_job
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
742 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool
, offsetof(Unit
, stop_when_unneeded
), SD_BUS_VTABLE_PROPERTY_CONST
),
743 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_start
), SD_BUS_VTABLE_PROPERTY_CONST
),
744 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool
, offsetof(Unit
, refuse_manual_stop
), SD_BUS_VTABLE_PROPERTY_CONST
),
745 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool
, offsetof(Unit
, allow_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
746 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool
, offsetof(Unit
, default_dependencies
), SD_BUS_VTABLE_PROPERTY_CONST
),
747 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode
, offsetof(Unit
, on_failure_job_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
748 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool
, offsetof(Unit
, ignore_on_isolate
), SD_BUS_VTABLE_PROPERTY_CONST
),
749 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
750 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec
, offsetof(Unit
, job_timeout
), SD_BUS_VTABLE_PROPERTY_CONST
),
751 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action
, offsetof(Unit
, job_timeout_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
752 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL
, offsetof(Unit
, job_timeout_reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
753 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool
, offsetof(Unit
, condition_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
754 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool
, offsetof(Unit
, assert_result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
755 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit
, condition_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
756 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit
, assert_timestamp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
757 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, conditions
), 0),
758 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions
, offsetof(Unit
, asserts
), 0),
759 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
760 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool
, offsetof(Unit
, transient
), SD_BUS_VTABLE_PROPERTY_CONST
),
761 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool
, offsetof(Unit
, perpetual
), SD_BUS_VTABLE_PROPERTY_CONST
),
762 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
),
763 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned
, offsetof(Unit
, start_limit
.burst
), SD_BUS_VTABLE_PROPERTY_CONST
),
764 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action
, offsetof(Unit
, start_limit_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
765 SD_BUS_PROPERTY("RebootArgument", "s", NULL
, offsetof(Unit
, reboot_arg
), SD_BUS_VTABLE_PROPERTY_CONST
),
766 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128
, offsetof(Unit
, invocation_id
), 0),
768 SD_BUS_METHOD("Start", "s", "o", method_start
, SD_BUS_VTABLE_UNPRIVILEGED
),
769 SD_BUS_METHOD("Stop", "s", "o", method_stop
, SD_BUS_VTABLE_UNPRIVILEGED
),
770 SD_BUS_METHOD("Reload", "s", "o", method_reload
, SD_BUS_VTABLE_UNPRIVILEGED
),
771 SD_BUS_METHOD("Restart", "s", "o", method_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
772 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
773 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
774 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart
, SD_BUS_VTABLE_UNPRIVILEGED
),
775 SD_BUS_METHOD("Kill", "si", NULL
, bus_unit_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
776 SD_BUS_METHOD("ResetFailed", NULL
, NULL
, bus_unit_method_reset_failed
, SD_BUS_VTABLE_UNPRIVILEGED
),
777 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL
, bus_unit_method_set_properties
, SD_BUS_VTABLE_UNPRIVILEGED
),
778 SD_BUS_METHOD("Ref", NULL
, NULL
, bus_unit_method_ref
, SD_BUS_VTABLE_UNPRIVILEGED
),
779 SD_BUS_METHOD("Unref", NULL
, NULL
, bus_unit_method_unref
, SD_BUS_VTABLE_UNPRIVILEGED
),
781 /* Obsolete properties or obsolete alias names */
782 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
783 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
784 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
785 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies
, 0, SD_BUS_VTABLE_HIDDEN
),
786 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec
, offsetof(Unit
, start_limit
.interval
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
790 static int property_get_slice(
793 const char *interface
,
794 const char *property
,
795 sd_bus_message
*reply
,
797 sd_bus_error
*error
) {
805 return sd_bus_message_append(reply
, "s", unit_slice_name(u
));
808 static int property_get_current_memory(
811 const char *interface
,
812 const char *property
,
813 sd_bus_message
*reply
,
815 sd_bus_error
*error
) {
817 uint64_t sz
= (uint64_t) -1;
825 r
= unit_get_memory_current(u
, &sz
);
826 if (r
< 0 && r
!= -ENODATA
)
827 log_unit_warning_errno(u
, r
, "Failed to get memory.usage_in_bytes attribute: %m");
829 return sd_bus_message_append(reply
, "t", sz
);
832 static int property_get_current_tasks(
835 const char *interface
,
836 const char *property
,
837 sd_bus_message
*reply
,
839 sd_bus_error
*error
) {
841 uint64_t cn
= (uint64_t) -1;
849 r
= unit_get_tasks_current(u
, &cn
);
850 if (r
< 0 && r
!= -ENODATA
)
851 log_unit_warning_errno(u
, r
, "Failed to get pids.current attribute: %m");
853 return sd_bus_message_append(reply
, "t", cn
);
856 static int property_get_cpu_usage(
859 const char *interface
,
860 const char *property
,
861 sd_bus_message
*reply
,
863 sd_bus_error
*error
) {
865 nsec_t ns
= (nsec_t
) -1;
873 r
= unit_get_cpu_usage(u
, &ns
);
874 if (r
< 0 && r
!= -ENODATA
)
875 log_unit_warning_errno(u
, r
, "Failed to get cpuacct.usage attribute: %m");
877 return sd_bus_message_append(reply
, "t", ns
);
880 static int property_get_cgroup(
883 const char *interface
,
884 const char *property
,
885 sd_bus_message
*reply
,
887 sd_bus_error
*error
) {
896 /* Three cases: a) u->cgroup_path is NULL, in which case the
897 * unit has no control group, which we report as the empty
898 * string. b) u->cgroup_path is the empty string, which
899 * indicates the root cgroup, which we report as "/". c) all
900 * other cases we report as-is. */
903 t
= isempty(u
->cgroup_path
) ? "/" : u
->cgroup_path
;
907 return sd_bus_message_append(reply
, "s", t
);
910 static int append_process(sd_bus_message
*reply
, const char *p
, pid_t pid
, Set
*pids
) {
911 _cleanup_free_
char *buf
= NULL
, *cmdline
= NULL
;
917 r
= set_put(pids
, PID_TO_PTR(pid
));
918 if (r
== -EEXIST
|| r
== 0)
924 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &buf
);
933 (void) get_process_cmdline(pid
, 0, true, &cmdline
);
935 return sd_bus_message_append(reply
,
942 static int append_cgroup(sd_bus_message
*reply
, const char *p
, Set
*pids
) {
943 _cleanup_closedir_
DIR *d
= NULL
;
944 _cleanup_fclose_
FILE *f
= NULL
;
950 r
= cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER
, p
, &f
);
959 r
= cg_read_pid(f
, &pid
);
965 if (is_kernel_thread(pid
) > 0)
968 r
= append_process(reply
, p
, pid
, pids
);
973 r
= cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER
, p
, &d
);
980 _cleanup_free_
char *g
= NULL
, *j
= NULL
;
982 r
= cg_read_subgroup(d
, &g
);
988 j
= strjoin(p
, "/", g
);
992 r
= append_cgroup(reply
, j
, pids
);
1000 int bus_unit_method_get_processes(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1001 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1002 _cleanup_(set_freep
) Set
*pids
= NULL
;
1009 pids
= set_new(NULL
);
1013 r
= sd_bus_message_new_method_return(message
, &reply
);
1017 r
= sd_bus_message_open_container(reply
, 'a', "(sus)");
1021 if (u
->cgroup_path
) {
1022 r
= append_cgroup(reply
, u
->cgroup_path
, pids
);
1027 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1028 pid
= unit_main_pid(u
);
1030 r
= append_process(reply
, NULL
, pid
, pids
);
1035 pid
= unit_control_pid(u
);
1037 r
= append_process(reply
, NULL
, pid
, pids
);
1042 r
= sd_bus_message_close_container(reply
);
1046 return sd_bus_send(NULL
, reply
, NULL
);
1049 const sd_bus_vtable bus_unit_cgroup_vtable
[] = {
1050 SD_BUS_VTABLE_START(0),
1051 SD_BUS_PROPERTY("Slice", "s", property_get_slice
, 0, 0),
1052 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup
, 0, 0),
1053 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory
, 0, 0),
1054 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage
, 0, 0),
1055 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks
, 0, 0),
1056 SD_BUS_METHOD("GetProcesses", NULL
, "a(sus)", bus_unit_method_get_processes
, SD_BUS_VTABLE_UNPRIVILEGED
),
1060 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
1061 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1062 _cleanup_free_
char *p
= NULL
;
1069 p
= unit_dbus_path(u
);
1073 r
= sd_bus_message_new_signal(
1076 "/org/freedesktop/systemd1",
1077 "org.freedesktop.systemd1.Manager",
1082 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1086 return sd_bus_send(bus
, m
, NULL
);
1089 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
1090 _cleanup_free_
char *p
= NULL
;
1097 p
= unit_dbus_path(u
);
1101 /* Send a properties changed signal. First for the specific
1102 * type, then for the generic unit. The clients may rely on
1103 * this order to get atomic behavior if needed. */
1105 r
= sd_bus_emit_properties_changed_strv(
1107 unit_dbus_interface_from_type(u
->type
),
1112 return sd_bus_emit_properties_changed_strv(
1114 "org.freedesktop.systemd1.Unit",
1118 void bus_unit_send_change_signal(Unit
*u
) {
1122 if (u
->in_dbus_queue
) {
1123 LIST_REMOVE(dbus_queue
, u
->manager
->dbus_unit_queue
, u
);
1124 u
->in_dbus_queue
= false;
1130 r
= bus_foreach_bus(u
->manager
, NULL
, u
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, u
);
1132 log_unit_debug_errno(u
, r
, "Failed to send unit change signal for %s: %m", u
->id
);
1134 u
->sent_dbus_new_signal
= true;
1137 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
1138 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1139 _cleanup_free_
char *p
= NULL
;
1146 p
= unit_dbus_path(u
);
1150 r
= sd_bus_message_new_signal(
1153 "/org/freedesktop/systemd1",
1154 "org.freedesktop.systemd1.Manager",
1159 r
= sd_bus_message_append(m
, "so", u
->id
, p
);
1163 return sd_bus_send(bus
, m
, NULL
);
1166 void bus_unit_send_removed_signal(Unit
*u
) {
1170 if (!u
->sent_dbus_new_signal
|| u
->in_dbus_queue
)
1171 bus_unit_send_change_signal(u
);
1176 r
= bus_foreach_bus(u
->manager
, NULL
, send_removed_signal
, u
);
1178 log_unit_debug_errno(u
, r
, "Failed to send unit remove signal for %s: %m", u
->id
);
1181 int bus_unit_queue_job(
1182 sd_bus_message
*message
,
1186 bool reload_if_possible
,
1187 sd_bus_error
*error
) {
1189 _cleanup_free_
char *path
= NULL
;
1195 assert(type
>= 0 && type
< _JOB_TYPE_MAX
);
1196 assert(mode
>= 0 && mode
< _JOB_MODE_MAX
);
1198 r
= mac_selinux_unit_access_check(
1200 job_type_to_access_method(type
),
1205 if (reload_if_possible
&& unit_can_reload(u
)) {
1206 if (type
== JOB_RESTART
)
1207 type
= JOB_RELOAD_OR_START
;
1208 else if (type
== JOB_TRY_RESTART
)
1209 type
= JOB_TRY_RELOAD
;
1212 if (type
== JOB_STOP
&&
1213 (u
->load_state
== UNIT_NOT_FOUND
|| u
->load_state
== UNIT_ERROR
) &&
1214 unit_active_state(u
) == UNIT_INACTIVE
)
1215 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not loaded.", u
->id
);
1217 if ((type
== JOB_START
&& u
->refuse_manual_start
) ||
1218 (type
== JOB_STOP
&& u
->refuse_manual_stop
) ||
1219 ((type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) && (u
->refuse_manual_start
|| u
->refuse_manual_stop
)) ||
1220 (type
== JOB_RELOAD_OR_START
&& job_type_collapse(type
, u
) == JOB_START
&& u
->refuse_manual_start
))
1221 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
);
1223 r
= manager_add_job(u
->manager
, type
, u
, mode
, error
, &j
);
1227 r
= bus_job_track_sender(j
, message
);
1231 path
= job_dbus_path(j
);
1235 return sd_bus_reply_method_return(message
, "o", path
);
1238 static int bus_unit_set_transient_property(
1241 sd_bus_message
*message
,
1242 UnitSetPropertiesMode mode
,
1243 sd_bus_error
*error
) {
1251 if (streq(name
, "Description")) {
1254 r
= sd_bus_message_read(message
, "s", &d
);
1258 if (mode
!= UNIT_CHECK
) {
1259 r
= unit_set_description(u
, d
);
1263 unit_write_drop_in_format(u
, mode
, name
, "[Unit]\nDescription=%s", d
);
1268 } else if (streq(name
, "DefaultDependencies")) {
1271 r
= sd_bus_message_read(message
, "b", &b
);
1275 if (mode
!= UNIT_CHECK
) {
1276 u
->default_dependencies
= b
;
1277 unit_write_drop_in_format(u
, mode
, name
, "[Unit]\nDefaultDependencies=%s", yes_no(b
));
1282 } else if (streq(name
, "Slice")) {
1286 if (!UNIT_HAS_CGROUP_CONTEXT(u
))
1287 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "The slice property is only available for units with control groups.");
1288 if (u
->type
== UNIT_SLICE
)
1289 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Slice may not be set for slice units.");
1290 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
1291 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set slice for init.scope");
1293 r
= sd_bus_message_read(message
, "s", &s
);
1297 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
1298 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name '%s'", s
);
1300 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1301 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1302 * instead of manager_load_unit() on purpose, here. */
1303 r
= manager_load_unit_prepare(u
->manager
, s
, NULL
, error
, &slice
);
1307 if (slice
->type
!= UNIT_SLICE
)
1308 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name '%s' is not a slice", s
);
1310 if (mode
!= UNIT_CHECK
) {
1311 r
= unit_set_slice(u
, slice
);
1315 unit_write_drop_in_private_format(u
, mode
, name
, "Slice=%s", s
);
1320 } else if (STR_IN_SET(name
,
1321 "Requires", "RequiresOverridable",
1322 "Requisite", "RequisiteOverridable",
1328 "PropagatesReloadTo", "ReloadPropagatedFrom",
1334 if (streq(name
, "RequiresOverridable"))
1335 d
= UNIT_REQUIRES
; /* redirect for obsolete unit dependency type */
1336 else if (streq(name
, "RequisiteOverridable"))
1337 d
= UNIT_REQUISITE
; /* same here */
1339 d
= unit_dependency_from_string(name
);
1341 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit dependency: %s", name
);
1344 r
= sd_bus_message_enter_container(message
, 'a', "s");
1348 while ((r
= sd_bus_message_read(message
, "s", &other
)) > 0) {
1349 if (!unit_name_is_valid(other
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1350 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid unit name %s", other
);
1352 if (mode
!= UNIT_CHECK
) {
1353 _cleanup_free_
char *label
= NULL
;
1355 r
= unit_add_dependency_by_name(u
, d
, other
, NULL
, true);
1359 label
= strjoin(name
, "-", other
);
1363 unit_write_drop_in_format(u
, mode
, label
, "[Unit]\n%s=%s", name
, other
);
1370 r
= sd_bus_message_exit_container(message
);
1376 } else if (streq(name
, "AddRef")) {
1380 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
1381 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
1382 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
1383 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
1384 * dependency type, hence let's not confuse things with that.
1386 * Note that we don't acually add the reference to the bus track. We do that only after the setup of
1387 * the transient unit is complete, so that setting this property multiple times in the same transient
1388 * unit creation call doesn't count as individual references. */
1390 r
= sd_bus_message_read(message
, "b", &b
);
1394 if (mode
!= UNIT_CHECK
)
1395 u
->bus_track_add
= b
;
1403 int bus_unit_set_properties(
1405 sd_bus_message
*message
,
1406 UnitSetPropertiesMode mode
,
1408 sd_bus_error
*error
) {
1410 bool for_real
= false;
1417 /* We iterate through the array twice. First run we just check
1418 * if all passed data is valid, second run actually applies
1419 * it. This is to implement transaction-like behaviour without
1420 * actually providing full transactions. */
1422 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1429 r
= sd_bus_message_enter_container(message
, 'r', "sv");
1433 if (for_real
|| mode
== UNIT_CHECK
)
1436 /* Reached EOF. Let's try again, and this time for realz... */
1437 r
= sd_bus_message_rewind(message
, false);
1445 r
= sd_bus_message_read(message
, "s", &name
);
1449 if (!UNIT_VTABLE(u
)->bus_set_property
)
1450 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Objects of this type do not support setting properties.");
1452 r
= sd_bus_message_enter_container(message
, 'v', NULL
);
1456 r
= UNIT_VTABLE(u
)->bus_set_property(u
, name
, message
, for_real
? mode
: UNIT_CHECK
, error
);
1457 if (r
== 0 && u
->transient
&& u
->load_state
== UNIT_STUB
)
1458 r
= bus_unit_set_transient_property(u
, name
, message
, for_real
? mode
: UNIT_CHECK
, error
);
1462 return sd_bus_error_setf(error
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Cannot set property %s, or unknown property.", name
);
1464 r
= sd_bus_message_exit_container(message
);
1468 r
= sd_bus_message_exit_container(message
);
1475 r
= sd_bus_message_exit_container(message
);
1479 if (commit
&& n
> 0 && UNIT_VTABLE(u
)->bus_commit_properties
)
1480 UNIT_VTABLE(u
)->bus_commit_properties(u
);
1485 int bus_unit_check_load_state(Unit
*u
, sd_bus_error
*error
) {
1488 if (u
->load_state
== UNIT_LOADED
)
1491 /* Give a better description of the unit error when
1492 * possible. Note that in the case of UNIT_MASKED, load_error
1494 if (u
->load_state
== UNIT_MASKED
)
1495 return sd_bus_error_setf(error
, BUS_ERROR_UNIT_MASKED
, "Unit %s is masked.", u
->id
);
1497 if (u
->load_state
== UNIT_NOT_FOUND
)
1498 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s not found.", u
->id
);
1500 return sd_bus_error_set_errnof(error
, u
->load_error
, "Unit %s is not loaded properly: %m.", u
->id
);
1503 static int bus_unit_track_handler(sd_bus_track
*t
, void *userdata
) {
1509 u
->bus_track
= sd_bus_track_unref(u
->bus_track
); /* make sure we aren't called again */
1511 unit_add_to_gc_queue(u
);
1515 static int bus_unit_allocate_bus_track(Unit
*u
) {
1523 r
= sd_bus_track_new(u
->manager
->api_bus
, &u
->bus_track
, bus_unit_track_handler
, u
);
1527 r
= sd_bus_track_set_recursive(u
->bus_track
, true);
1529 u
->bus_track
= sd_bus_track_unref(u
->bus_track
);
1536 int bus_unit_track_add_name(Unit
*u
, const char *name
) {
1541 r
= bus_unit_allocate_bus_track(u
);
1545 return sd_bus_track_add_name(u
->bus_track
, name
);
1548 int bus_unit_track_add_sender(Unit
*u
, sd_bus_message
*m
) {
1553 r
= bus_unit_allocate_bus_track(u
);
1557 return sd_bus_track_add_sender(u
->bus_track
, m
);
1560 int bus_unit_track_remove_sender(Unit
*u
, sd_bus_message
*m
) {
1563 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
1568 return sd_bus_track_remove_sender(u
->bus_track
, m
);