Replace libdbus to gdbus 76/129776/8
authorKichan Kwon <k_c.kwon@samsung.com>
Thu, 18 May 2017 05:30:02 +0000 (14:30 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 24 May 2017 05:58:53 +0000 (14:58 +0900)
- To unify with other system modules

Change-Id: I85a0158f6fe5246e8416a3ec7063cd6f64b9fea9
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/runtime_info_usage.c

index fa72ec8..811d0d0 100644 (file)
@@ -24,7 +24,8 @@
 #include <runtime_info.h>
 #include <runtime_info_private.h>
 
-#include <E_DBus.h>
+#include <gio/gio.h>
+#include <glib.h>
 
 #define RESOURCED_BUS_NAME "org.tizen.resourced"
 #define RESOURCED_USAGE_OBJECT_NAME "/Org/Tizen/ResourceD/Process"
@@ -47,63 +48,50 @@ static const runtime_info_dbus_info_s dbus_info[] = {
        { "ProcCpuUsage", "process cpu" },
 };
 
-#define MIN(x, y) (x) > (y) ? (x) : (y)
 #define kBtoKiB(val) (int)MIN(((long long)(val) * 1024 / 1000), INT_MAX)
 #define pagetoKiB(val) (int)MIN(((long long)(val) * 4096 / 1000), INT_MAX)
 
-//LCOV_EXCL_START : system error
-static int runtime_info_get_dbus_error(const char *err_name)
+/* Convert int array to GVariant("ai") */
+static GVariant *runtime_info_append_args(int *args, int size)
 {
-       int size;
+       GVariantBuilder builder, *sub_builder;
+       int i;
 
-       if (!err_name)
-               return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
+       g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
+       sub_builder = g_variant_builder_new(G_VARIANT_TYPE("ai"));
 
-       size = strlen(err_name);
-       if (!strncmp(err_name, DBUS_ERROR_IO_ERROR, size))
-               return RUNTIME_INFO_ERROR_IO_ERROR;
-       else if (!strncmp(err_name, DBUS_ERROR_NO_MEMORY, size))
-               return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
-       else if (!strncmp(err_name, DBUS_ERROR_ACCESS_DENIED, size))
-               return RUNTIME_INFO_ERROR_PERMISSION_DENIED;
-       else
-               return RUNTIME_INFO_ERROR_REMOTE_IO;
+       for (i = 0; i < size; i++)
+               g_variant_builder_add(sub_builder, "i", args[i]);
+
+       g_variant_builder_add_value(&builder, g_variant_new("ai", sub_builder));
+       g_variant_builder_unref(sub_builder);
+
+       return g_variant_builder_end(&builder);
 }
-//LCOV_EXCL_STOP
 
 /* Handler function which handles dbus related instructions
- * for both per process memory and cpu requests.
  * Creates the method call to resourced and receives the reply (if successful)
- * Return the received reply (if received) else NULL to signify failed call to resourced
+ * Return the received usage information (if received) else NULL to signify failed call to resourced
  */
-static DBusMessage *runtime_info_dbus_process_usage_info(int *pid, int size,
-               runtime_info_usage_type_e type, int *error)
+static GVariant *runtime_info_dbus_request_usage_info(runtime_info_usage_type_e type,
+               int *pid, int size, int *error)
 {
-       DBusConnection *conn;
-       DBusMessage *msg;
-       DBusMessage *reply;
-       DBusError err;
-       int ret;
+       static GDBusConnection *conn;
+       GError *err = NULL;
+       GVariant *args_in = NULL;
+       GVariant *args_out = NULL;
+       GVariant *usage;
 
+       /* Check parameter */
        if (!pid || !error) {
                //LCOV_EXCL_START : system error
                _E("INVALID_PARAMETER(0x%08x): pid list and error params cannot be null");
-
                if (error)
                        *error = RUNTIME_INFO_ERROR_INVALID_PARAMETER;
                return NULL;
                //LCOV_EXCL_STOP
        }
 
-       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
-       if (!conn) {
-               //LCOV_EXCL_START : system error
-               _E("DBUS_CONNECTION_ERROR");
-               *error = RUNTIME_INFO_ERROR_REMOTE_IO;
-               return NULL;
-               //LCOV_EXCL_STOP
-       }
-
        if (type != USAGE_TYPE_PROCESS_MEMORY && type != USAGE_TYPE_PROCESS_CPU) {
                //LCOV_EXCL_START : system error
                _E("INVALID_PARAMETER(0x%08x): invalid info parameter",
@@ -113,56 +101,58 @@ static DBusMessage *runtime_info_dbus_process_usage_info(int *pid, int size,
                //LCOV_EXCL_STOP
        }
 
+       /* Make argument for requesting */
        _D("Process %d: received query to get %s usage of %d processes",
                        getpid(), dbus_info[type].caption, size);
 
-       msg = dbus_message_new_method_call(RESOURCED_BUS_NAME,
-                               RESOURCED_USAGE_OBJECT_NAME,
-                               RESOURCED_USAGE_INTERFACE_NAME,
-                               dbus_info[type].method_name);
-       if (!msg) {
-               //LCOV_EXCL_START : system error
-               _E("DBUS_METHOD_CALL: not able to create method call (%s:%s-%s)",
-                               RESOURCED_USAGE_OBJECT_NAME, RESOURCED_USAGE_INTERFACE_NAME,
-                               dbus_info[type].method_name);
-               *error = RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
-               return NULL;
-               //LCOV_EXCL_STOP
-       }
-
-       ret = dbus_message_append_args(msg, DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &pid,
-                       size, DBUS_TYPE_INVALID);
-       if (!ret) {
+       args_in = runtime_info_append_args(pid, size);
+       if (!args_in) {
                //LCOV_EXCL_START : system error
                _E("DBUS_METHOD_CALL: not able to append pid array to message");
                *error = RUNTIME_INFO_ERROR_IO_ERROR;
-               dbus_message_unref(msg);
                return NULL;
                //LCOV_EXCL_STOP
        }
 
-       dbus_error_init(&err);
-       *error = 0;
+       /* Send message to resourced and receive reply */
+       if (!conn) {
+               conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+               if (!conn) {
+                       //LCOV_EXCL_START : system error
+                       _E("Failed to get dbus connection : %s", err->message);
+                       g_variant_unref(args_in);
+                       return NULL;
+                       //LCOV_EXCL_STOP
+               }
+       }
 
-       _D("Process %d: Sending dbus message to resourced for %s usage info",
+       _D("Process %d: Sending dbus message to resourced for %s info",
                        getpid(), dbus_info[type].caption);
 
-       reply = dbus_connection_send_with_reply_and_block(conn, msg,
-                       DBUS_REPLY_TIMEOUT, &err);
-       if (!reply)
-               _E("DBUS_METHOD_CALL: not able to send message");       //LCOV_EXCL_LINE : system error
-
-       if (dbus_error_is_set(&err)) {
+       args_out = g_dbus_connection_call_sync(conn,
+                               RESOURCED_BUS_NAME,
+                               RESOURCED_USAGE_OBJECT_NAME,
+                               RESOURCED_USAGE_INTERFACE_NAME,
+                               dbus_info[type].method_name,
+                               args_in, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &err);
+       if (!args_out) {
                //LCOV_EXCL_START : system error
-               _E("DBUS_METHOD_CALL: dbus_connection_send error(%s:%s)", err.name, err.message);
-               *error = runtime_info_get_dbus_error(err.name);
-               dbus_error_free(&err);
-               reply = NULL;
+               _E("DBUS_METHOD_CALL: not able to send message : %s", err->message);
+               if (err->code == G_FILE_ERROR_TXTBSY)
+                       *error = RUNTIME_INFO_ERROR_PERMISSION_DENIED;
+               else
+                       *error = RUNTIME_INFO_ERROR_REMOTE_IO;
+               g_variant_unref(args_in);
+               return NULL;
                //LCOV_EXCL_STOP
        }
 
-       dbus_message_unref(msg);
-       return reply;
+       g_variant_unref(args_in);
+
+       usage = g_variant_get_child_value(args_out, 0);
+       g_variant_unref(args_out);
+
+       return usage;
 }
 
 API int runtime_info_get_system_memory_info(runtime_memory_info_s *info)
@@ -210,11 +200,10 @@ API int runtime_info_get_system_memory_info(runtime_memory_info_s *info)
 
 API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_info_s **info)
 {
-       DBusMessageIter iter, iter_array, iter_struct;
-       int index, value;
+       int i;
        int error;
-       process_memory_info_s *proc_info;
-       DBusMessage *replymsg;
+       GVariant *usages;
+       GVariantIter iter;
 
        if (!pid || size <= 0) {
                _E("INVALID_PARAMETER(0x%08x) : invalid input param",
@@ -231,28 +220,19 @@ API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_
        *info = NULL;
 
        /* Get the needed information from resourced daemon using dbus */
-       replymsg = runtime_info_dbus_process_usage_info(pid, size, USAGE_TYPE_PROCESS_MEMORY, &error);
-       if (!replymsg) {
+       usages = runtime_info_dbus_request_usage_info(USAGE_TYPE_PROCESS_MEMORY, pid, size, &error);
+       if (!usages) {
                //LCOV_EXCL_START : system error
                _E("DBUS_METHOD_CALL: call to resourced not successful");
                return error;
                //LCOV_EXCL_STOP
        }
 
-       /* Check if the message is an error message or not in expected
-        * format and return error value */
-       dbus_message_iter_init(replymsg, &iter);
-       if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32) {
-               //LCOV_EXCL_START : system error
-               dbus_message_iter_get_basic(&iter, &error);
-               _E("DBUS_METHOD_CALL: call to resourced returned error message");
-               dbus_message_unref(replymsg);
-               return error;
-               //LCOV_EXCL_STOP
-       } else if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
+       /* Check whether the received usage has expected format or not */
+       if (g_strcmp0(g_variant_get_type_string(usages), "a(iiiiiii)")) {
                //LCOV_EXCL_START : system error
                _E("DBUS_METHOD_CALL: received dbus message is not in expected format");
-               dbus_message_unref(replymsg);
+               g_variant_unref(usages);
                return RUNTIME_INFO_ERROR_REMOTE_IO;
                //LCOV_EXCL_STOP
        }
@@ -261,41 +241,23 @@ API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_
        *info = (process_memory_info_s *)malloc(size * sizeof(process_memory_info_s));
        if (!(*info)) {
                _E("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
-               dbus_message_unref(replymsg);
+               g_variant_unref(usages);
                return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
        }
 
-       dbus_message_iter_recurse(&iter, &iter_array);
-       for (index = 0; index < size; ++index) {
-               proc_info = &(*info)[index];
-
-               dbus_message_iter_recurse(&iter_array, &iter_struct);
-
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_info->vsz = value;
-               dbus_message_iter_next(&iter_struct);
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_info->rss = value;
-               dbus_message_iter_next(&iter_struct);
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_info->pss = value;
-               dbus_message_iter_next(&iter_struct);
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_info->shared_clean = value;
-               dbus_message_iter_next(&iter_struct);
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_info->shared_dirty = value;
-               dbus_message_iter_next(&iter_struct);
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_info->private_clean = value;
-               dbus_message_iter_next(&iter_struct);
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_info->private_dirty = value;
-
-               dbus_message_iter_next(&iter_array);
-       }
-
-       dbus_message_unref(replymsg);
+       g_variant_iter_init(&iter, usages);
+       for (i = 0; i < size; i++)
+               g_variant_iter_next(&iter, "(iiiiiii)",
+                               &(info[i]->vsz),
+                               &(info[i]->rss),
+                               &(info[i]->pss),
+                               &(info[i]->shared_clean),
+                               &(info[i]->shared_dirty),
+                               &(info[i]->private_clean),
+                               &(info[i]->private_dirty));
+
+       g_variant_unref(usages);
+
        return RUNTIME_INFO_ERROR_NONE;
 }
 
@@ -346,11 +308,10 @@ API int runtime_info_get_cpu_usage(runtime_cpu_usage_s *usage)
 
 API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage_s **usage)
 {
-       DBusMessageIter iter, iter_array, iter_struct;
-       int index, value;
+       int i;
        int error;
-       process_cpu_usage_s *proc_usage;
-       DBusMessage *replymsg;
+       GVariant *usages;
+       GVariantIter iter;
 
        if (!pid || size <= 0) {
                _E("INVALID_PARAMETER(0x%08x) : invalid input param",
@@ -367,29 +328,19 @@ API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage
        *usage = NULL;
 
        /* Get the needed information from resourced daemon using dbus */
-       replymsg = runtime_info_dbus_process_usage_info(pid, size, USAGE_TYPE_PROCESS_CPU, &error);
-
-       if (!replymsg) {
+       usages = runtime_info_dbus_request_usage_info(USAGE_TYPE_PROCESS_CPU, pid, size, &error);
+       if (!usages) {
                //LCOV_EXCL_START : system error
                _E("DBUS_METHOD_CALL: call to resourced not successful");
                return error;
                //LCOV_EXCL_STOP
        }
 
-       /* Check if the message is an error message or not in expected format
-        * and return error value */
-       dbus_message_iter_init(replymsg, &iter);
-       if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32) {
-               //LCOV_EXCL_START : system error
-               dbus_message_iter_get_basic(&iter, &error);
-               _E("DBUS_METHOD_CALL: call to resourced returned error message");
-               dbus_message_unref(replymsg);
-               return error;
-               //LCOV_EXCL_STOP
-       } else if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
+       /* Check whether the received usage has expected format or not */
+       if (g_strcmp0(g_variant_get_type_string(usages), "a(ii)")) {
                //LCOV_EXCL_START : system error
                _E("DBUS_METHOD_CALL: received dbus message is not in expected format");
-               dbus_message_unref(replymsg);
+               g_variant_unref(usages);
                return RUNTIME_INFO_ERROR_REMOTE_IO;
                //LCOV_EXCL_STOP
        }
@@ -399,27 +350,17 @@ API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage
        if (!(*usage)) {
                //LCOV_EXCL_START : system error
                _E("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
-               dbus_message_unref(replymsg);
+               g_variant_unref(usages);
                return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
                //LCOV_EXCL_STOP
        }
 
-       dbus_message_iter_recurse(&iter, &iter_array);
-       for (index = 0; index < size; ++index) {
-               proc_usage = &(*usage)[index];
-
-               dbus_message_iter_recurse(&iter_array, &iter_struct);
+       g_variant_iter_init(&iter, usages);
+       for (i = 0; i < size; i++)
+               g_variant_iter_next(&iter, "(ii)", &(usage[i]->utime), &(usage[i]->stime));
 
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_usage->utime = value;
-               dbus_message_iter_next(&iter_struct);
-               dbus_message_iter_get_basic(&iter_struct, &value);
-               proc_usage->stime = value;
-
-               dbus_message_iter_next(&iter_array);
-       }
+       g_variant_unref(usages);
 
-       dbus_message_unref(replymsg);
        return RUNTIME_INFO_ERROR_NONE;
 }