From a43757462acaffa902417a9876486763d0b7ed58 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 31 Jul 2011 18:28:02 +0200 Subject: [PATCH] dbus: export unit file state --- src/dbus-unit.c | 16 ++++++++++++++++ src/dbus-unit.h | 3 +++ src/systemctl.c | 5 +++++ src/unit.c | 12 ++++++++++++ src/unit.h | 6 ++++++ 5 files changed, 42 insertions(+) diff --git a/src/dbus-unit.c b/src/dbus-unit.c index bf240f97..b110e331 100644 --- a/src/dbus-unit.c +++ b/src/dbus-unit.c @@ -145,6 +145,22 @@ int bus_unit_append_sub_state(DBusMessageIter *i, const char *property, void *da return 0; } +int bus_unit_append_file_state(DBusMessageIter *i, const char *property, void *data) { + Unit *u = data; + const char *state; + + assert(i); + assert(property); + assert(u); + + state = strempty(unit_file_state_to_string(unit_get_unit_file_state(u))); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state)) + return -ENOMEM; + + return 0; +} + int bus_unit_append_can_start(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; dbus_bool_t b; diff --git a/src/dbus-unit.h b/src/dbus-unit.h index fc9b4f57..b5c30107 100644 --- a/src/dbus-unit.h +++ b/src/dbus-unit.h @@ -85,6 +85,7 @@ " \n" \ " \n" \ " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -144,6 +145,7 @@ { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state, "s", u }, \ { "org.freedesktop.systemd1.Unit", "SubState", bus_unit_append_sub_state, "s", u }, \ { "org.freedesktop.systemd1.Unit", "FragmentPath", bus_property_append_string, "s", u->meta.fragment_path }, \ + { "org.freedesktop.systemd1.Unit", "UnitFileState", bus_unit_append_file_state, "s", u }, \ { "org.freedesktop.systemd1.Unit", "InactiveExitTimestamp",bus_property_append_usec, "t", &u->meta.inactive_exit_timestamp.realtime }, \ { "org.freedesktop.systemd1.Unit", "InactiveExitTimestampMonotonic",bus_property_append_usec, "t", &u->meta.inactive_exit_timestamp.monotonic }, \ { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_usec, "t", &u->meta.active_enter_timestamp.realtime }, \ @@ -181,6 +183,7 @@ int bus_unit_append_description(DBusMessageIter *i, const char *property, void * int bus_unit_append_load_state(DBusMessageIter *i, const char *property, void *data); int bus_unit_append_active_state(DBusMessageIter *i, const char *property, void *data); int bus_unit_append_sub_state(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_file_state(DBusMessageIter *i, const char *property, void *data); int bus_unit_append_can_start(DBusMessageIter *i, const char *property, void *data); int bus_unit_append_can_stop(DBusMessageIter *i, const char *property, void *data); int bus_unit_append_can_reload(DBusMessageIter *i, const char *property, void *data); diff --git a/src/systemctl.c b/src/systemctl.c index 3eb8ab5b..4981433e 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -1962,6 +1962,7 @@ typedef struct UnitStatusInfo { const char *load_state; const char *active_state; const char *sub_state; + const char *unit_file_state; const char *description; const char *following; @@ -2043,6 +2044,8 @@ static void print_status_info(UnitStatusInfo *i) { if (i->load_error) printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error); + else if (i->path && i->unit_file_state) + printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state); else if (i->path) printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path); else @@ -2285,6 +2288,8 @@ static int status_property(const char *name, DBusMessageIter *iter, UnitStatusIn i->what = s; else if (streq(name, "Following")) i->following = s; + else if (streq(name, "UnitFileState")) + i->unit_file_state = s; } break; diff --git a/src/unit.c b/src/unit.c index d4142098..e494834a 100644 --- a/src/unit.c +++ b/src/unit.c @@ -73,6 +73,7 @@ Unit *unit_new(Manager *m) { u->meta.type = _UNIT_TYPE_INVALID; u->meta.deserialized_job = _JOB_TYPE_INVALID; u->meta.default_dependencies = true; + u->meta.unit_file_state = _UNIT_FILE_STATE_INVALID; return u; } @@ -2482,6 +2483,17 @@ int unit_following_set(Unit *u, Set **s) { return 0; } +UnitFileState unit_get_unit_file_state(Unit *u) { + assert(u); + + if (u->meta.unit_file_state < 0 && u->meta.fragment_path) + u->meta.unit_file_state = unit_file_get_state( + u->meta.manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, + NULL, file_name_from_path(u->meta.fragment_path)); + + return u->meta.unit_file_state; +} + static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = { [UNIT_STUB] = "stub", [UNIT_LOADED] = "loaded", diff --git a/src/unit.h b/src/unit.h index 79f15103..6893b689 100644 --- a/src/unit.h +++ b/src/unit.h @@ -39,6 +39,7 @@ typedef enum UnitDependency UnitDependency; #include "socket-util.h" #include "execute.h" #include "condition.h" +#include "install.h" enum UnitType { UNIT_SERVICE = 0, @@ -192,6 +193,9 @@ struct Meta { /* Error code when we didn't manage to load the unit (negative) */ int load_error; + /* Cached unit file state */ + UnitFileState unit_file_state; + /* Garbage collect us we nobody wants or requires us anymore */ bool stop_when_unneeded; @@ -523,6 +527,8 @@ void unit_trigger_on_failure(Unit *u); bool unit_condition_test(Unit *u); +UnitFileState unit_get_unit_file_state(Unit *u); + const char *unit_load_state_to_string(UnitLoadState i); UnitLoadState unit_load_state_from_string(const char *s); -- 2.39.5