From 8f9e3ea706c8c642788d316c19dc9bf7ea5211af Mon Sep 17 00:00:00 2001 From: SangYoun Kwak Date: Thu, 15 Sep 2022 13:31:17 +0900 Subject: [PATCH] block: fix return method of BLOCK_DEV_FORMAT operation Change-Id: Ic5a059e713b65a0a4521bb6de1483624ad8ebb1e Signed-off-by: SangYoun Kwak --- src/block/block.c | 58 +++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/src/block/block.c b/src/block/block.c index 6d0121b..f917551 100644 --- a/src/block/block.c +++ b/src/block/block.c @@ -3173,27 +3173,28 @@ static GVariant *request_private_unmount_block(GDBusConnection *conn, return request_unmount_block(conn, sender, path, iface, name, param, invocation, user_data, true); } -static int process_request_format_block(const gchar *sender, GDBusMethodInvocation *invocation, +static GVariant *process_request_format_block(const gchar *sender, GDBusMethodInvocation *invocation, struct block_device *bdev, int option, const char *type) { struct format_data *fdata; pid_t pid; int prev_state; - int ret = -EBADMSG; + int ret; pid = gdbus_connection_get_sender_pid(NULL, sender); if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) { _E("Failed to format on private state."); - return -EPERM; + return g_variant_new("(i)", -EPERM); } fdata = get_format_data(type, option); if (!fdata) { _E("Failed to get format data."); - return ret; + return g_variant_new("(i)", -EBADMSG); } prev_state = bdev->data->state; + /* unmount if mounted */ if (prev_state == BLOCK_MOUNT) { if (bdev->on_private_op == REQ_PRIVATE) { bdev->on_private_op = REQ_PRIVATE_FORMAT; @@ -3203,10 +3204,11 @@ static int process_request_format_block(const gchar *sender, GDBusMethodInvocati if (ret < 0) { _E("Failed to add operation(unmount, %s).", bdev->data->devnode); release_format_data(fdata); - return ret; + return g_variant_new("(i)", ret); } } + /* do format */ ret = add_operation(bdev, BLOCK_DEV_FORMAT, invocation, (void *)fdata); if (ret < 0) { _E("Failed to add operation(format, %s).", bdev->data->devnode); @@ -3217,11 +3219,13 @@ static int process_request_format_block(const gchar *sender, GDBusMethodInvocati if (prev_state == BLOCK_MOUNT) { if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) { _E("Failed to add operation(mount, %s).", bdev->data->devnode); - return ret; + /* mount was failed, but returns ret */ + return g_variant_new("(i)", ret); } } - return 0; + /* everything was successful */ + return NULL; } static GVariant *request_format_block(GDBusConnection *conn, @@ -3231,12 +3235,10 @@ static GVariant *request_format_block(GDBusConnection *conn, struct block_device *bdev; int id; int option; - int ret = -EBADMSG; if (!block_control) { _D("Block module is disabled."); - ret = -EPERM; - goto out; + return g_variant_new("(i)", -EPERM); } g_variant_get(param, "(ii)", &id, &option); @@ -3244,25 +3246,16 @@ static GVariant *request_format_block(GDBusConnection *conn, bdev = find_block_device_by_id(id); if (!bdev) { _E("Failed to find (%d) in the device list.", id); - goto out; + return g_variant_new("(i)", -EBADMSG); } if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV || !strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) { _D("Format dbus request for extended internal storage is blocked."); - ret = -EPERM; - goto out; + return g_variant_new("(i)", -EPERM); } - ret = process_request_format_block(sender, invocation, bdev, option, NULL); - if (ret < 0) { - goto out; - } - - return NULL; - -out: - return g_variant_new("(i)", ret); + return process_request_format_block(sender, invocation, bdev, option, NULL); } static GVariant *request_format_block_type(GDBusConnection *conn, @@ -3273,12 +3266,11 @@ static GVariant *request_format_block_type(GDBusConnection *conn, char *type = NULL; int id; int option; - int ret = -EBADMSG; + GVariant *ret = NULL; if (!block_control) { _D("Block module is disabled."); - ret = -EPERM; - goto out; + return g_variant_new("(i)", -EPERM); } g_variant_get(param, "(iis)", &id, &option, &type); @@ -3286,27 +3278,21 @@ static GVariant *request_format_block_type(GDBusConnection *conn, bdev = find_block_device_by_id(id); if (!bdev) { _E("Failed to find (%d) in the device list.", id); - goto out; + g_free(type); + return g_variant_new("(i)", -EBADMSG); } /* FormatwithType dbus call is needed when app proceeds extended internal -> portable storage */ if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV) { _D("FormatwithType dbus request for extended internal storage is blocked."); - ret = -EPERM; - goto out; + g_free(type); + return g_variant_new("(i)", -EPERM); } ret = process_request_format_block(sender, invocation, bdev, option, type); - if (ret < 0) { - goto out; - } g_free(type); - return NULL; - -out: - g_free(type); - return g_variant_new("(i)", ret); + return ret; } static GVariant *block_data_to_gvariant(struct block_data *data, int flags) -- 2.7.4