From bdbefcfd9dc654860f7988d69753b5159e1bfb86 Mon Sep 17 00:00:00 2001 From: "sanghyeok.oh" Date: Fri, 10 May 2019 20:34:00 +0900 Subject: [PATCH] block: dbus: modified to reply error type message if failed gdbus method handler should reply g_variant with valid signature or error type message. Change-Id: I00398b6d0614c5117ac5f0ac5801d504126721dd Signed-off-by: sanghyeok.oh --- apps/extended-sd/src/es-portable-storage-page.c | 7 ++++ src/auto-test/block.c | 20 +++++++++-- src/block/block.c | 45 ++++++++++++++++++++++--- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/apps/extended-sd/src/es-portable-storage-page.c b/apps/extended-sd/src/es-portable-storage-page.c index 5826f58..e24eed0 100644 --- a/apps/extended-sd/src/es-portable-storage-page.c +++ b/apps/extended-sd/src/es-portable-storage-page.c @@ -119,6 +119,11 @@ mapper_device_cb(int mapper_id, storage_dev_e dev, storage_state_e state, } g_variant_get(output, "(a(issssssisibii))", &iter); + if (g_variant_iter_n_children(iter) == 0) { + DMSG("Failed to get DeviceList from gvariant."); + g_variant_iter_free(iter); + return; + } while (g_variant_iter_loop(iter, "(issssssisibii)", NULL, NULL, NULL, @@ -137,6 +142,8 @@ mapper_device_cb(int mapper_id, storage_dev_e dev, storage_state_e state, break; } + g_variant_iter_free(iter); + ret = dbus_handle_method_async_with_reply_var(STORAGED_BUS_NAME, STORAGED_PATH_BLOCK_MANAGER, STORAGED_INTERFACE_BLOCK_MANAGER, diff --git a/src/auto-test/block.c b/src/auto-test/block.c index 7098387..a9b8e95 100644 --- a/src/auto-test/block.c +++ b/src/auto-test/block.c @@ -137,11 +137,17 @@ static bool block_getdevicelist(char *type) g_variant_new("(s)", type)); if (!msg) { - _E("Failed to call dbus method(%s): no reply", METHOD_BLOCK_GETMMCPRIMARY); + _E("Failed to call dbus method(%s)", METHOD_BLOCK_GETMMCPRIMARY); return ret; } g_variant_get(msg, "(a(issssssisibii))", &iter); + if (g_variant_iter_n_children(iter) == 0) { + _E("Failed to get DeviceList from gvariant."); + g_variant_iter_free(iter); + return ret; + } + while (g_variant_iter_loop(iter, "(issssssisibii)", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &val)) { @@ -149,6 +155,8 @@ static bool block_getdevicelist(char *type) ret = TRUE; } + g_variant_iter_free(iter); + _I("Success. (%s): %d", METHOD_BLOCK_GETDEVICELIST, ret); return ret; } @@ -168,11 +176,17 @@ static bool block_getdevicelist2(char *type) g_variant_new("(s)", type)); if (!msg) { - _E("Failed to call dbus method(%s): no reply", METHOD_BLOCK_GETMMCPRIMARY); + _E("Failed to call dbus method(%s)", METHOD_BLOCK_GETMMCPRIMARY); return ret; } g_variant_get(msg, "(a(issssssisibi))", &iter); + if (g_variant_iter_n_children(iter) == 0) { + _E("Failed to get DeviceList2 from gvariant."); + g_variant_iter_free(iter); + return ret; + } + while (g_variant_iter_loop(iter, "(issssssisibi)", NULL, &val, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) { @@ -180,6 +194,8 @@ static bool block_getdevicelist2(char *type) ret = TRUE; } + g_variant_iter_free(iter); + _I("Success. %s: %d", METHOD_BLOCK_GETDEVICELIST2, ret); return ret; } diff --git a/src/block/block.c b/src/block/block.c index c8119ee..cd0d250 100644 --- a/src/block/block.c +++ b/src/block/block.c @@ -3253,12 +3253,14 @@ static GVariant *request_get_device_list(GDBusConnection *conn, char *type = NULL; int block_type; int i; + int item_cnt = 0; GVariantBuilder *builder = NULL; + const char *error = NULL; g_variant_get(param, "(s)", &type); - if (!type) { _E("Delivered type is NULL."); + error = "Delivered type is NULL"; goto out; } @@ -3272,10 +3274,12 @@ static GVariant *request_get_device_list(GDBusConnection *conn, block_type = -1; else { _E("Invalid type (%s) is requested.", type); + error = "Invalid type is requested"; goto out; } builder = g_variant_builder_new(G_VARIANT_TYPE("a(issssssisibii)")); + item_cnt = 0; for (i = 0; i < THREAD_MAX; i++) { pthread_mutex_lock(&(th_manager[i].mutex)); @@ -3310,16 +3314,29 @@ static GVariant *request_get_device_list(GDBusConnection *conn, data->primary, data->flags, data->id); + ++item_cnt; } pthread_mutex_unlock(&(th_manager[i].mutex)); } + /* if nothing is added, then gvariant(empty body) is generated */ + if (item_cnt == 0) { + _E("Not found matched device(%s).", type); + error = "Not found matched device"; + goto out; + } + reply = g_variant_new("(a(issssssisibii))", builder); - g_variant_builder_unref(builder); out: + if (builder) + g_variant_builder_unref(builder); + g_free(type); + if (!reply) - reply = dbus_handle_new_g_variant_tuple(); + g_dbus_method_invocation_return_error(invocation, + G_DBUS_ERROR, G_DBUS_ERROR_FAILED, + "%s", error); return reply; } @@ -3335,12 +3352,15 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn, char *type = NULL; int block_type; int i; + int item_cnt; GVariantBuilder *builder = NULL; + const char *error = NULL; g_variant_get(param, "(s)", &type); if (!type) { _E("Delivered type is NULL."); + error = "Delivered type is NULL"; goto out; } @@ -3354,10 +3374,12 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn, block_type = -1; else { _E("Invalid type (%s) is requested.", type); + error = "Invalid type is requested"; goto out; } builder = g_variant_builder_new(G_VARIANT_TYPE("a(issssssisibi)")); + item_cnt = 0; for (i = 0; i < THREAD_MAX; i++) { pthread_mutex_lock(&(th_manager[i].mutex)); @@ -3391,14 +3413,29 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn, data->state, data->primary, data->flags); + ++item_cnt; } pthread_mutex_unlock(&(th_manager[i].mutex)); } + /* if nothing is added, then gvariant(empty body) is generated */ + if (item_cnt == 0) { + _E("Not found matched device(%s).", type); + error = "Not found matched device"; + goto out; + } + reply = g_variant_new("(a(issssssisibi))", builder); - g_variant_builder_unref(builder); out: + if (builder) + g_variant_builder_unref(builder); + g_free(type); + + if (!reply) + g_dbus_method_invocation_return_error(invocation, + G_DBUS_ERROR, G_DBUS_ERROR_FAILED, + "%s", error); return reply; } -- 2.7.4