1abbee
From c55a7f9448378c10a7e8074db908502ae5ff60aa Mon Sep 17 00:00:00 2001
1abbee
From: Franck Bui <fbui@suse.com>
1abbee
Date: Fri, 13 Nov 2015 14:12:19 +0100
1abbee
Subject: [PATCH] Introduce bus_unit_check_load_state() helper
1abbee
1abbee
This function is used to check that a previous unit load succeed and
1abbee
returns 0 in this case.
1abbee
1abbee
In the case the load failed, the function setup a bus error
1abbee
accordingly and returns -errno.
1abbee
1abbee
(cherry picked from commit 000a996dc46c187f803b67b0b0d51ad4d0bc1658)
1abbee
Related: #1256858
1abbee
---
1abbee
 src/core/dbus-unit.c | 17 +++++++++++++++++
1abbee
 src/core/dbus-unit.h |  2 ++
1abbee
 2 files changed, 19 insertions(+)
1abbee
1abbee
diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c
1abbee
index 49770bf..c3654db 100644
1abbee
--- a/src/core/dbus-unit.c
1abbee
+++ b/src/core/dbus-unit.c
1abbee
@@ -1083,3 +1083,20 @@ int bus_unit_set_properties(
1abbee
 
1abbee
         return n;
1abbee
 }
1abbee
+
1abbee
+int bus_unit_check_load_state(Unit *u, sd_bus_error *error) {
1abbee
+
1abbee
+        if (u->load_state == UNIT_LOADED)
1abbee
+                return 0;
1abbee
+
1abbee
+        /* Give a better description of the unit error when
1abbee
+         * possible. Note that in the case of UNIT_MASKED, load_error
1abbee
+         * is not set. */
1abbee
+        if (u->load_state == UNIT_MASKED)
1abbee
+                return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit is masked.");
1abbee
+
1abbee
+        if (u->load_state == UNIT_NOT_FOUND)
1abbee
+                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit not found.");
1abbee
+
1abbee
+        return sd_bus_error_set_errnof(error, u->load_error, "Unit is not loaded properly: %m.");
1abbee
+}
1abbee
diff --git a/src/core/dbus-unit.h b/src/core/dbus-unit.h
1abbee
index 57a5e19..4338496 100644
1abbee
--- a/src/core/dbus-unit.h
1abbee
+++ b/src/core/dbus-unit.h
1abbee
@@ -37,3 +37,5 @@ int bus_unit_method_reset_failed(sd_bus *bus, sd_bus_message *message, void *use
1abbee
 int bus_unit_queue_job(sd_bus *bus, sd_bus_message *message, Unit *u, JobType type, JobMode mode, bool reload_if_possible, sd_bus_error *error);
1abbee
 int bus_unit_set_properties(Unit *u, sd_bus_message *message, UnitSetPropertiesMode mode, bool commit, sd_bus_error *error);
1abbee
 int bus_unit_method_set_properties(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
1abbee
+
1abbee
+int bus_unit_check_load_state(Unit *u, sd_bus_error *error);