unit_control: Ensure API always returns valid error code 61/211561/1 accepted/tizen/unified/20190807.111823 submit/tizen/20190807.020938
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 6 Aug 2019 10:18:57 +0000 (12:18 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 6 Aug 2019 12:05:34 +0000 (14:05 +0200)
Additionally, introduce explicit return code for asynchronous
requests - UNIT_CONTROL_REQUEST_SENT, which does only mean
that request was posted, not completed.

Change-Id: Ia5a8f7141ede8d196c8af5e3c0ec65d2058f9f73

include/unit_control.h
src/libactd/unit_control.c

index 6a034503c5845ca995c2f71edaa8fde16e944169..0ec916c5e926ec98965ed8a7f2e49f2ed9be63c6 100644 (file)
@@ -2,6 +2,7 @@
 #define UNIT_CONTROL_H
 
 enum {
+       UNIT_CONTROL_REQUEST_SENT = 0,
        UNIT_CONTROL_OK,
        UNIT_CONTROL_ERROR,
 };
index e548d0f1e5609e00b73eb16b24dfb89e93e2bf86..fd83b80969fd5103fd664694f0041027a2d7fd49 100644 (file)
@@ -19,6 +19,7 @@
 #include <glib.h>
 #include <gio/gio.h>
 #include <errno.h>
+#include <stdbool.h>
 
 #include "unit_control.h"
 
@@ -104,14 +105,14 @@ const char *translate_to_systemd(const char *method)
 
 static int call_uc(GBusType bus_type, const char *method, const char *unit, int timeout_ms)
 {
-       int ret;
        GVariant *msg = NULL;
-       GDBusConnection *bus;
-       const char *result;
+       int ret = UNIT_CONTROL_ERROR;
 
-       bus = g_bus_get_sync(bus_type, NULL, NULL);
+       GDBusConnection *bus = g_bus_get_sync(bus_type, NULL, NULL);
        if (!bus)
-               return -1;
+               return ret;
+
+       GError *err = NULL;
 
        if (bus_type == G_BUS_TYPE_SESSION) {
                msg = g_dbus_connection_call_sync(bus,
@@ -124,13 +125,13 @@ static int call_uc(GBusType bus_type, const char *method, const char *unit, int
                                G_DBUS_CALL_FLAGS_NONE,
                                timeout2glib(timeout_ms),
                                NULL,
-                               NULL);
+                               &err);
                if (!msg)
-                       return -EBADMSG;
-
-               ret = 0;
+                       goto out;
        } else {
                /* we assume that activationd runs on system bus and can be used as a proxy */
+               const char *result = NULL;
+
                msg = g_dbus_connection_call_sync(bus,
                                UNIT_CONTROL_NAME,
                                UNIT_CONTROL_OBJ_PATH,
@@ -141,19 +142,24 @@ static int call_uc(GBusType bus_type, const char *method, const char *unit, int
                                G_DBUS_CALL_FLAGS_NONE,
                                timeout2glib(timeout_ms),
                                NULL,
-                               NULL);
+                               &err);
                if (!msg)
-                       return -EBADMSG;
+                       goto out;
 
                g_variant_get(msg, "(s)", &result);
-               if (g_strcmp0(result, "ok") == 0)
-                       ret = 0;
-               else
-                       ret = -1;
-
+               bool is_ok = g_strcmp0(result, "ok") == 0;
                g_free(result);
+
+               if (!is_ok)
+                       goto out;
        }
 
+       ret = UNIT_CONTROL_OK;
+
+out:
+       if (err)
+               g_error_free(err);
+
        return ret;
 }
 
@@ -161,14 +167,15 @@ static int call_uc_async(GBusType bus_type, const char *method, const char *unit
 {
        GDBusConnection *bus;
        struct generic_user_data *data = user_data;
+       int ret = UNIT_CONTROL_ERROR;
 
        bus = g_bus_get_sync(bus_type, NULL, NULL);
        if (!bus)
-               return -1;
+               return ret;
 
        data = malloc(sizeof(*data));
        if (!data)
-               return -ENOMEM;
+               return ret;
 
        data->user_data = user_data;
        data->cb = cb;
@@ -203,7 +210,7 @@ static int call_uc_async(GBusType bus_type, const char *method, const char *unit
                                data);
        }
 
-       return 0;
+       return UNIT_CONTROL_REQUEST_SENT;
 }
 
 int actd_start_unit(BusType bus_type, const char *unit, int timeout_ms)