Fix resource leak - mnt_new_table/mnt_free_table
[platform/core/system/storaged.git] / src / block / block.c
index 8d8e992..667c414 100644 (file)
 #include <app2ext_interface.h>
 #include <libmount.h>
 #include <blkid/blkid.h>
-#include <ode/luks.h>
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <gio/gio.h>
-#include <libsyscommon/dbus-system.h>
+#include <libsyscommon/libgdbus.h>
+#include <libsyscommon/ini-parser.h>
+#include <libsyscommon/list.h>
+#ifdef EXTENDED_STORAGE
+#include <ode/luks.h>
+#endif
 
 #include "log.h"
-#include "config-parser.h"
 #include "module-intf.h"
 #include "udev.h"
-#include "list.h"
 #include "block.h"
 #include "fd_handler.h"
 #include "utils.h"
 #define TIMEOUT_MAKE_OBJECT     500 /* milliseconds */
 
 #define SIGNAL_POWEROFF_STATE  "ChangeState"
+#define METHOD_ADD_POWEROFF_WAIT       "AddPowerOffWait"
+#define METHOD_REMOVE_POWEROFF_WAIT    "RemovePowerOffWait"
 
 #define BLOCK_DEVICE_ADDED      "DeviceAdded"
 #define BLOCK_DEVICE_REMOVED    "DeviceRemoved"
 #define BLOCK_DEVICE_BLOCKED    "DeviceBlocked"
 #define BLOCK_DEVICE_CHANGED    "DeviceChanged"
-#define BLOCK_DEVICE_CHANGED_2  "DeviceChanged2"
 
 #define BLOCK_TYPE_MMC          "mmc"
 #define BLOCK_TYPE_SCSI         "scsi"
 #define EXTENDEDSD_MOUNT_PATH  "/opt/extendedsd"
 
 #define VFAT_NAME              "vfat"
+#define EXFAT_NAME             "exfat"
 #define EXT4_NAME              "ext4"
 #define LUKS_NAME              "crypto_LUKS"
-#define EXTENDEDSD_NAME                "extendedsd"
+#define EXTENDEDSD_NAME        "extendedsd"
 
 /* Minimum value of block id */
 #define BLOCK_ID_MIN           10
@@ -158,7 +162,7 @@ struct operation_queue {
 
 struct block_device {
        struct block_data *data;
-       dd_list *op_queue;
+       GList *op_queue;
        int thread_id;          /* Current thread ID */
        bool removed;           /* True when device is physically removed but operation is not precessed yet */
        enum private_operation_state on_private_op;
@@ -184,8 +188,8 @@ static struct block_conf {
 } block_conf[BLOCK_EXTENDEDSD_DEV + 1];
 
 static struct manage_thread {
-       dd_list *th_node_list;  /* List of devnode which thread dealt with. Only main thread access */
-       dd_list *block_dev_list; /* Use thread mutex */
+       GList *th_node_list;    /* List of devnode which thread dealt with. Only main thread access */
+       GList *block_dev_list; /* Use thread mutex */
        pthread_t th;
        pthread_mutex_t mutex;
        pthread_cond_t cond;
@@ -204,14 +208,15 @@ char mmc_default_path[][FILE_NAME_LEN_MAX + 1] = {
 
 #define DIR_NUM ((int)(sizeof(mmc_default_path)/sizeof(mmc_default_path[0])))
 
-static dd_list *fs_head;
-static dd_list *block_ops_list;
+static GList *fs_head;
+static GList *block_ops_list;
 static bool smack;
 static int pfds[2];
 static fd_handler_h phandler;
 static bool block_control = false;
 static bool block_boot = false;
 static pthread_mutex_t pipe_mutex = PTHREAD_MUTEX_INITIALIZER;
+static bool add_poweroff_wait = false;
 
 /* Assume there is only one physical internal storage */
 static int dev_internal = -1;
@@ -225,13 +230,13 @@ static int add_operation(struct block_device *bdev,
                enum block_dev_operation operation,
                GDBusMethodInvocation *invocation, void *data);
 static void remove_operation(struct block_device *bdev);
-static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
-static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
+static void check_removed(struct block_device *bdev, GList **queue, struct operation_queue **op);
+static bool check_unmount(struct block_device *bdev, GList **queue, struct operation_queue **op);
 static int change_mount_point(struct block_device *bdev, const char *mount_point);
+static void terminate_threads(void);
 
 #define nullstr(x) (x ? x : "")
 static GVariant *block_data_to_gvariant(struct block_data *data, int flags);
-static GVariant *block_data_to_gvariant2(struct block_data *data, int flags);
 
 #define block_send_dbus_reply(invocation, result) if (invocation) {g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", result)); }
 
@@ -262,20 +267,20 @@ static void __CONSTRUCTOR__ smack_check(void)
 
 void add_fs(const struct block_fs_ops *fs)
 {
-       DD_LIST_APPEND(fs_head, (void *)fs);
+       SYS_G_LIST_APPEND(fs_head, (void *)fs);
 }
 
 void remove_fs(const struct block_fs_ops *fs)
 {
-       DD_LIST_REMOVE(fs_head, (void *)fs);
+       SYS_G_LIST_REMOVE(fs_head, (void *)fs);
 }
 
 const struct block_fs_ops *find_fs(enum block_fs_type type)
 {
        struct block_fs_ops *fs;
-       dd_list *elem;
+       GList *elem;
 
-       DD_LIST_FOREACH(fs_head, elem, fs) {
+       SYS_G_LIST_FOREACH(fs_head, elem, fs) {
                if (fs->type == type)
                        return fs;
        }
@@ -284,24 +289,24 @@ const struct block_fs_ops *find_fs(enum block_fs_type type)
 
 void add_block_dev(const struct block_dev_ops *ops)
 {
-       DD_LIST_APPEND(block_ops_list, (void *)ops);
+       SYS_G_LIST_APPEND(block_ops_list, (void *)ops);
 }
 
 void remove_block_dev(const struct block_dev_ops *ops)
 {
-       DD_LIST_REMOVE(block_ops_list, (void *)ops);
+       SYS_G_LIST_REMOVE(block_ops_list, (void *)ops);
 }
 
 static void broadcast_block_info(enum block_dev_operation op,
                struct block_data *data, int result)
 {
        struct block_dev_ops *ops;
-       dd_list *elem;
+       GList *elem;
 
        if (data->primary != true)
                return;
 
-       DD_LIST_FOREACH(block_ops_list, elem, ops) {
+       SYS_G_LIST_FOREACH(block_ops_list, elem, ops) {
                int data_block_type = (data->block_type == BLOCK_EXTENDEDSD_DEV)
                                      ? BLOCK_MMC_DEV : data->block_type;
 
@@ -326,7 +331,7 @@ static int block_get_new_id(void)
 {
        static int id = BLOCK_ID_MIN;
        struct block_device *bdev;
-       dd_list *elem;
+       GList *elem;
        bool found;
        int i, j;
 
@@ -334,7 +339,7 @@ static int block_get_new_id(void)
                found = false;
                for (j = 0; j < THREAD_MAX; j++) {
                        pthread_mutex_lock(&(th_manager[j].mutex));
-                       DD_LIST_FOREACH(th_manager[j].block_dev_list, elem, bdev) {
+                       SYS_G_LIST_FOREACH(th_manager[j].block_dev_list, elem, bdev) {
                                if (bdev->data->id == id) {
                                        found = true;
                                        break;
@@ -358,7 +363,7 @@ static int block_get_new_id(void)
 static void remove_file(int id, bool extendedsd)
 {
        char file_name[PATH_LEN];
-       int ret;
+       int ret_val;
 
        if (id < 0)
                return;
@@ -368,8 +373,8 @@ static void remove_file(int id, bool extendedsd)
        else
                snprintf(file_name, sizeof(file_name), EXTERNAL_STORAGE_PATH"/%d", id);
 
-       ret = remove(file_name);
-       if (ret < 0)
+       ret_val = remove(file_name);
+       if (ret_val < 0)
                _E("Failed to remove '%s': %d", file_name, errno);
 }
 
@@ -394,71 +399,79 @@ static void create_file(int id, char *mount_point, bool extendedsd)
                _E("Failed to open '%s'.", file_name);
 }
 
-static void signal_device_blocked(struct block_device *bdev)
+static void broadcast_device_blocked(struct block_device *bdev)
 {
        struct block_data *data;
+       int ret_dbus;
 
        if (!bdev || !bdev->data)
                return;
 
        data = bdev->data;
 
-       dbus_handle_broadcast_dbus_signal_var(STORAGED_PATH_BLOCK_MANAGER,
+       ret_dbus = gdbus_signal_emit(NULL,
+                       STORAGED_PATH_BLOCK_MANAGER,
                        STORAGED_INTERFACE_BLOCK_MANAGER,
                        BLOCK_DEVICE_BLOCKED,
                        block_data_to_gvariant(data, 0));
+       if (ret_dbus < 0)
+               _E("Failed to send dbus signal");
 }
 
-static void signal_device_changed(struct block_device *bdev,
+static void broadcast_device_changed(struct block_device *bdev,
                enum block_dev_operation op)
 {
        struct block_data *data;
-       GVariant *var = NULL;
-       int flags;
+       GVariant *param = NULL;
+       const char *signal_name = NULL;
+       int flags = 0;
+       int ret_dbus;
 
-       if (!bdev || !bdev->data)
+       if (!bdev || !bdev->data) {
+               _E("Failed to broadcast device changed signal. op(%d)", op);
                return;
+       }
 
        data = bdev->data;
 
+       /* set flags and signal name */
        switch (op) {
        case BLOCK_DEV_MOUNT:
                BLOCK_GET_MOUNT_FLAGS(data, flags);
+               signal_name = BLOCK_DEVICE_CHANGED;
                break;
        case BLOCK_DEV_UNMOUNT:
                BLOCK_GET_UNMOUNT_FLAGS(data, flags);
+               signal_name = BLOCK_DEVICE_CHANGED;
                break;
        case BLOCK_DEV_FORMAT:
                BLOCK_GET_FORMAT_FLAGS(data, flags);
+               signal_name = BLOCK_DEVICE_CHANGED;
                break;
-       default:
+       case BLOCK_DEV_INSERT:
                flags = 0;
+               signal_name = BLOCK_DEVICE_ADDED;
                break;
+       case BLOCK_DEV_REMOVE:
+               flags = 0;
+               signal_name = BLOCK_DEVICE_REMOVED;
+               break;
+       default:
+               /* unknown op */
+               _E("Failed to broadcast device changed signal. op(%d)", op);
+               return;
        }
 
        /* Broadcast outside with BlockManager iface */
-       var = block_data_to_gvariant(data, flags);
+       param = block_data_to_gvariant(data, flags);
 
-       if (op == BLOCK_DEV_INSERT)
-               dbus_handle_broadcast_dbus_signal_var(STORAGED_PATH_BLOCK_MANAGER,
-                               STORAGED_INTERFACE_BLOCK_MANAGER,
-                               BLOCK_DEVICE_ADDED,
-                               var);
-       else if (op == BLOCK_DEV_REMOVE)
-               dbus_handle_broadcast_dbus_signal_var(STORAGED_PATH_BLOCK_MANAGER,
+       ret_dbus = gdbus_signal_emit(NULL,
+                               STORAGED_PATH_BLOCK_MANAGER,
                                STORAGED_INTERFACE_BLOCK_MANAGER,
-                               BLOCK_DEVICE_REMOVED,
-                               var);
-       else {
-               dbus_handle_broadcast_dbus_signal_var(STORAGED_PATH_BLOCK_MANAGER,
-                               STORAGED_INTERFACE_BLOCK_MANAGER,
-                               BLOCK_DEVICE_CHANGED,
-                               var);
-               dbus_handle_broadcast_dbus_signal_var(STORAGED_PATH_BLOCK_MANAGER,
-                               STORAGED_INTERFACE_BLOCK_MANAGER,
-                               BLOCK_DEVICE_CHANGED_2,
-                               block_data_to_gvariant2(data, flags));
-       }
+                               signal_name,
+                               param);
+       if (ret_dbus < 0)
+               _E("Failed to broadcast device changed signal. op(%d)", op);
 }
 
 static int get_mmc_mount_node(char *devnode, char *node, size_t len)
@@ -518,7 +531,7 @@ static char *generate_mount_path(struct block_data *data)
 {
        const char *str;
        char *name, node[64];
-       int ret;
+       int ret_val;
 
        if (!data || !data->devnode || !data->fs_usage || (strcmp(data->fs_usage, FILESYSTEM_NAME) && strncmp(data->fs_usage, "crypto", strlen("crypto"))))
                return NULL;
@@ -530,10 +543,10 @@ static char *generate_mount_path(struct block_data *data)
 
        switch (data->block_type) {
        case BLOCK_MMC_DEV:
-               ret = get_mmc_mount_node(name, node, sizeof(node));
+               ret_val = get_mmc_mount_node(name, node, sizeof(node));
                break;
        case BLOCK_SCSI_DEV:
-               ret = get_scsi_mount_node(name, node, sizeof(node));
+               ret_val = get_scsi_mount_node(name, node, sizeof(node));
                break;
        case BLOCK_EXTENDEDSD_DEV:
                return strdup(EXTENDEDSD_MOUNT_PATH);
@@ -541,7 +554,7 @@ static char *generate_mount_path(struct block_data *data)
                _E("Invalid block type(%d).", data->block_type);
                return NULL;
        }
-       if (ret < 0)
+       if (ret_val < 0)
                goto out;
 
        str = tzplatform_mkpath(TZ_SYS_MEDIA, node);
@@ -558,14 +571,14 @@ static bool check_primary_partition(const char *devnode)
 {
        struct block_fs_ops *fs;
        blkid_probe probe;
-       dd_list *elem;
+       GList *elem;
        const char *filesystem = NULL;
        char *temp;
        char str[PATH_MAX];
        char str2[PATH_MAX];
        size_t fs_len;
        int len;
-       int ret;
+       int ret_val;
        int i;
        bool found = false;
 
@@ -592,7 +605,11 @@ static bool check_primary_partition(const char *devnode)
        str[len - 1] = '\0';
 
        for (i = 1; i <= 9; ++i) {
-               snprintf(str2, sizeof(str2), "%s%d", str, i);
+               if ((ret_val = snprintf(str2, sizeof(str2), "%s%d", str, i)) > sizeof(str2) - 1) {
+                       _E("Filename is longer than buffer. Need %d size of buffer.", ret_val + 1);
+                       continue;
+               }
+
                if (access(str2, R_OK) != 0)
                        continue;
 
@@ -602,12 +619,12 @@ static bool check_primary_partition(const char *devnode)
                if (blkid_do_probe(probe) != 0)
                        continue;
 
-               ret = blkid_probe_lookup_value(probe, "TYPE", &filesystem, &fs_len);
-               if (ret < 0) {
+               ret_val = blkid_probe_lookup_value(probe, "TYPE", &filesystem, &fs_len);
+               if (ret_val < 0) {
                        blkid_free_probe(probe);
                        continue;
                }
-               DD_LIST_FOREACH(fs_head, elem, fs) {
+               SYS_G_LIST_FOREACH(fs_head, elem, fs) {
                        if (!strncmp(fs->name, filesystem, fs_len)) {
                                found = true;
                                break;
@@ -775,7 +792,7 @@ static struct block_device *make_block_device(struct block_data *data)
 // Called by MainThread - Remove DevNode
 static void free_block_device(struct block_device *bdev)
 {
-       dd_list *l, *next;
+       GList *l, *next;
        struct operation_queue *op;
        int thread_id;
 
@@ -789,13 +806,13 @@ static void free_block_device(struct block_device *bdev)
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
 
        th_manager[thread_id].num_dev--;
-       DD_LIST_REMOVE(th_manager[thread_id].block_dev_list, bdev);
+       SYS_G_LIST_REMOVE(th_manager[thread_id].block_dev_list, bdev);
        free_block_data(bdev->data);
 
-       DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
+       SYS_G_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
                if (!op->done)
                        th_manager[thread_id].op_len--;
-               DD_LIST_REMOVE(bdev->op_queue, op);
+               SYS_G_LIST_REMOVE(bdev->op_queue, op);
                free(op);
        }
        pthread_mutex_unlock(&(th_manager[thread_id].mutex));
@@ -807,14 +824,14 @@ static void free_block_device(struct block_device *bdev)
 static struct block_device *find_block_device(const char *devnode)
 {
        struct block_device *bdev;
-       dd_list *elem;
+       GList *elem;
        int len;
        int i;
 
        len = strlen(devnode) + 1;
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        if (bdev->data && !bdev->removed &&
                            !strncmp(bdev->data->devnode, devnode, len)) {
                                pthread_mutex_unlock(&(th_manager[i].mutex));
@@ -831,14 +848,14 @@ static struct block_device *find_block_device(const char *devnode)
 static struct block_device *find_block_device_path(const char *mount_point)
 {
        struct block_device *bdev;
-       dd_list *elem;
+       GList *elem;
        int len;
        int i;
 
        len = strlen(mount_point) + 1;
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        if (bdev->data && !bdev->removed &&
                           (bdev->data->mount_point != NULL && !strncmp(bdev->data->mount_point, mount_point, len))) {
                                pthread_mutex_unlock(&(th_manager[i].mutex));
@@ -855,12 +872,12 @@ static struct block_device *find_block_device_path(const char *mount_point)
 static struct block_device *find_block_device_by_id(int id)
 {
        struct block_device *bdev;
-       dd_list *elem;
+       GList *elem;
        int i;
 
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        if (!bdev->data)
                                continue;
                        if (bdev->removed)
@@ -905,25 +922,29 @@ static const char *get_operation_char(enum block_dev_operation op)
 void mmc_make_default_path(const char *mount_path)
 {
        int i = 0;
-       int ret = 0;
+       int ret_val = 0;
        char mmc_path[FILE_NAME_LEN_MAX + 1] = {0, };
 
        for (i = 0; i < DIR_NUM; ++i) {
-               snprintf(mmc_path, sizeof(mmc_path), "%s/%s", mount_path, mmc_default_path[i]);
+               if ((ret_val = snprintf(mmc_path, sizeof(mmc_path), "%s/%s", mount_path, mmc_default_path[i])) > sizeof(mmc_path) - 1) {
+                       _E("Path is longer than buffer. Need %d size of buffer.", ret_val + 1);
+                       continue;
+               }
+
                if (!g_file_test(mmc_path, G_FILE_TEST_IS_DIR)) {
                        _D("Path(%s) did not exist.", mmc_path);
-                       ret = mkdir(mmc_path, 0777);
-                       if (ret != 0)
+                       ret_val = mkdir(mmc_path, 0777);
+                       if (ret_val != 0)
                                _E("Failed to mkdir: %d", errno);
 
                        /*this fuction for emulator*/
                        /*at the first time, the directroies are made permission 755*/
-                       ret = chmod(mmc_path, 0777);
-                       if (ret != 0)
+                       ret_val = chmod(mmc_path, 0777);
+                       if (ret_val != 0)
                                _E("Failed to chmod: %d", errno);
 
-                       ret = chown(mmc_path, 0, 10001);
-                       if (ret != 0)
+                       ret_val = chown(mmc_path, 0, 10001);
+                       if (ret_val != 0)
                                _E("Failed to chown: %d", errno);
                }
        }
@@ -931,11 +952,11 @@ void mmc_make_default_path(const char *mount_path)
 
 static void create_external_apps_directory(void)
 {
-       int ret;
+       int ret_dbus;
 
-       ret = dbus_handle_method_async(PKGDIR_BUS_NAME, PKGDIR_PATH,
-                       PKGDIR_INTERFACE, "CreateExternalDirsForAllPkgs", NULL, NULL);
-       if (ret)
+       ret_dbus = gdbus_call_async(PKGDIR_BUS_NAME, PKGDIR_PATH,
+                       PKGDIR_INTERFACE, "CreateExternalDirsForAllPkgs", NULL);
+       if (ret_dbus)
                _E("Failed to create external directory.");
 }
 
@@ -962,7 +983,7 @@ static bool pipe_cb(int fd, void *data)
        struct pipe_data pdata = {0,};
        int n;
        int thread_id;
-       int ret;
+       int ret_val;
 
        n = read(fd, &pdata, sizeof(pdata));
        if (n != sizeof(pdata) || !pdata.bdev) {
@@ -979,9 +1000,9 @@ static bool pipe_cb(int fd, void *data)
 
        if (pdata.op == BLOCK_DEV_MOUNT && pdata.result < 0) {
                if (pdata.bdev->data->state == BLOCK_UNMOUNT) {
-                       ret = change_mount_point(pdata.bdev, "");
+                       ret_val = change_mount_point(pdata.bdev, "");
                        /* Modify /run/external-storage/id file */
-                       if (ret == 0) {
+                       if (ret_val == 0) {
                                if (pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV)
                                        create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, true);
                                else
@@ -990,6 +1011,12 @@ static bool pipe_cb(int fd, void *data)
                }
                goto out;
        }
+
+       if (pdata.op == BLOCK_DEV_MOUNT && (pdata.result == 0)) {
+               /* Create file for block device /run/external-storage/id */
+               create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
+       }
+
        if (pdata.op == BLOCK_DEV_MOUNT &&
                pdata.bdev->data->state == BLOCK_MOUNT &&
                pdata.bdev->data->block_type == BLOCK_MMC_DEV &&
@@ -997,14 +1024,16 @@ static bool pipe_cb(int fd, void *data)
                create_external_apps_directory();
                mmc_make_default_path(pdata.bdev->data->mount_point);
 
-               ret = dbus_handle_method_sync_var(POPUP_BUS_NAME,
+               ret_val = gdbus_call_sync_with_reply_int(POPUP_BUS_NAME,
                        POPUP_PATH_NOTI,
                        POPUP_INTERFACE_NOTI,
                        MMC_POPUP_NOTI,
-                       g_variant_new("(s)", MMC_INSERTED));
-               if (ret != 0)
-                       _E("Failed to popup: %d", ret);
+                       g_variant_new("(s)", MMC_INSERTED),
+                       NULL);
+               if (ret_val != 0)
+                       _E("Failed to popup: %d", ret_val);
        }
+
        if (pdata.op == BLOCK_DEV_UNMOUNT) {
                /* Remove file for block device /run/xxxxxx/id */
                remove_file(pdata.bdev->data->id, pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
@@ -1013,13 +1042,14 @@ static bool pipe_cb(int fd, void *data)
                        pdata.bdev->data->primary &&
                        BLOCK_IS_FLAG_SET(pdata.bdev->data, UNMOUNT_UNSAFE)) {
 
-                       ret = dbus_handle_method_sync_var(POPUP_BUS_NAME,
+                       ret_val = gdbus_call_sync_with_reply_int(POPUP_BUS_NAME,
                                POPUP_PATH_NOTI,
                                POPUP_INTERFACE_NOTI,
                                MMC_POPUP_NOTI,
-                               g_variant_new("(s)", MMC_REMOVED));
-                       if (ret != 0)
-                               _E("Failed to popup: %d", ret);
+                               g_variant_new("(s)", MMC_REMOVED),
+                               NULL);
+                       if (ret_val != 0)
+                               _E("Failed to popup: %d", ret_val);
                }
        }
 
@@ -1028,7 +1058,7 @@ static bool pipe_cb(int fd, void *data)
 
        /* Broadcast outside with Block iface */
        if (pdata.bdev->on_private_op == REQ_NORMAL)
-               signal_device_changed(pdata.bdev, pdata.op);
+               broadcast_device_changed(pdata.bdev, pdata.op);
        else if (pdata.bdev->on_private_op == REQ_PRIVATE) {
                if (pdata.op == BLOCK_DEV_UNMOUNT) {
                        pdata.bdev->on_private_op = REQ_NORMAL;
@@ -1054,17 +1084,17 @@ out:
 
 static int pipe_init(void)
 {
-       int ret;
+       int ret_val;
 
-       ret = pipe2(pfds, O_CLOEXEC);
-       if (ret == -1)
+       ret_val = pipe2(pfds, O_CLOEXEC);
+       if (ret_val == -1)
                return -errno;
 
-       ret = add_fd_read_handler(pfds[0], pipe_cb,
+       ret_val = add_fd_read_handler(pfds[0], pipe_cb,
                        NULL, NULL, &phandler);
-       if (ret < 0) {
-               _E("Failed to add pipe handler: %d", ret);
-               return ret;
+       if (ret_val < 0) {
+               _E("Failed to add pipe handler: %d", ret_val);
+               return ret_val;
        }
 
        return 0;
@@ -1171,7 +1201,7 @@ static int retrieve_udev_device(struct block_data *data, bool mount_point_update
 static int block_mount(struct block_data *data)
 {
        struct block_fs_ops *fs;
-       dd_list *elem;
+       GList *elem;
        int r;
        int len;
 
@@ -1205,7 +1235,7 @@ static int block_mount(struct block_data *data)
 
        fs = NULL;
        len = strlen(data->fs_type) + 1;
-       DD_LIST_FOREACH(fs_head, elem, fs) {
+       SYS_G_LIST_FOREACH(fs_head, elem, fs) {
                if (!strncmp(fs->name, data->fs_type, len))
                        break;
        }
@@ -1242,7 +1272,7 @@ out:
 static int mount_start(struct block_device *bdev)
 {
        struct block_data *data;
-       int ret;
+       int ret_val;
        int r;
 
        assert(bdev);
@@ -1268,8 +1298,8 @@ static int mount_start(struct block_device *bdev)
 
        if (data->block_type == BLOCK_MMC_DEV) {
                /* app2ext_migrate_legacy_all has dbus method call to deviced */
-               ret = app2ext_migrate_legacy_all();
-               if (ret < 0)
+               ret_val = app2ext_migrate_legacy_all();
+               if (ret_val < 0)
                        _E("Failed to app2ext.");
        }
 
@@ -1343,7 +1373,6 @@ static int block_unmount(struct block_device *bdev,
 {
        struct block_data *data;
        int r, retry = 0;
-       struct timespec time = {0,};
 
        if (!bdev || !bdev->data || !bdev->data->mount_point)
                return -EINVAL;
@@ -1351,7 +1380,7 @@ static int block_unmount(struct block_device *bdev,
        data = bdev->data;
 
        if (bdev->on_private_op == REQ_NORMAL)
-               signal_device_blocked(bdev);
+               broadcast_device_blocked(bdev);
 
        /* it must called before unmounting mmc */
        r = mmc_check_and_unmount(data->mount_point);
@@ -1364,53 +1393,46 @@ static int block_unmount(struct block_device *bdev,
 
        _I("Execute force unmount.");
        /* Force Unmount Scenario */
-       while (1) {
-               switch (retry++) {
-               case 0:
-                       /* Mobile specific:
-                        * should unmount the below vconf key. */
-                       if ((data->block_type == BLOCK_MMC_DEV ||
-                               data->block_type == BLOCK_EXTENDEDSD_DEV) &&
-                               data->primary) {
-                               /* At first, notify to other app
-                                * who already access sdcard */
-                               _I("Notify to other app who already access sdcard.");
-                               vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
-                                               VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED);
-                       }
-                       break;
-               case 1:
-                       /* Second, kill app with SIGTERM */
-                       _I("Kill app with SIGTERM.");
-                       terminate_process(data->mount_point, false);
-                       break;
-               case 2:
-                       /* Last time, kill app with SIGKILL */
-                       _I("Kill app with SIGKILL.");
-                       terminate_process(data->mount_point, true);
-                       break;
-               default:
-                       if (umount2(data->mount_point, MNT_DETACH) != 0) {
-                               _I("Failed to unmount with lazy option: %d",
-                                               errno);
-                               return -errno;
-                       }
-                       goto out;
-               }
 
-               /* it takes some seconds til other app completely clean up */
-               time.tv_nsec = 500 * NANO_SECOND_MULTIPLIER;
-               nanosleep(&time, NULL);
+       /* Mobile specific:
+        * should unmount the below vconf key. */
+       if ((data->block_type == BLOCK_MMC_DEV ||
+               data->block_type == BLOCK_EXTENDEDSD_DEV) &&
+               data->primary) {
+               /* At first, notify to other app who already access sdcard */
+               _I("Notify to other app who already access sdcard.");
+               vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
+                               VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED);
 
-               print_open_files(data->mount_point);
+               /* Wait for 700 msec to release open files */
+               usleep(700 * 1000);
+       }
 
-               r = mmc_check_and_unmount(data->mount_point);
-               if (!r) {
-                       _D("Success to unmount '%s'.", data->mount_point);
+       sync();
+
+       if (umount2(data->mount_point, MNT_DETACH) != 0) {
+               _I("Failed to unmount with lazy option: %m");
+               return -errno;
+       }
+
+       while (retry++ < UNMOUNT_RETRY) {
+               _I("Kill app with SIGTERM.");
+               terminate_process(data->devnode, false);
+               usleep(500 * 1000);
+
+               if (!is_in_use_partition(data->devnode))
+                       break;
+
+               _I("Kill app with SIGKILL.");
+               terminate_process(data->devnode, true);
+               usleep(500 * 1000);
+
+               if (!is_in_use_partition(data->devnode))
                        break;
-               }
        }
 
+       sync();
+
 out:
        data->state = BLOCK_UNMOUNT;
 
@@ -1463,7 +1485,7 @@ static int block_format(struct block_data *data,
                const char *fs_type, bool mount_point_updated)
 {
        const struct block_fs_ops *fs;
-       dd_list *elem;
+       GList *elem;
        const char *fstype;
        int len;
        int r;
@@ -1481,22 +1503,23 @@ static int block_format(struct block_data *data,
                } else
                        fstype = fs_type;
        }
+       if (!strcmp(fstype, EXFAT_NAME))
+               fstype = VFAT_NAME;
 
        fs = NULL;
        len = strlen(fstype);
-       DD_LIST_FOREACH(fs_head, elem, fs) {
+       SYS_G_LIST_FOREACH(fs_head, elem, fs) {
                if (!strncmp(fs->name, fstype, len))
                        break;
        }
 
-       if (!fs) {
+       if (!fs || !fs->format) {
                BLOCK_FLAG_SET(data, FS_NOT_SUPPORTED);
                _E("Not supported file system(%s).", fstype);
                return -ENOTSUP;
        }
 
        _I("Format path=%s", data->devnode);
-       fs->check(data->devnode);
        r = fs->format(data->devnode);
        if (r < 0) {
                _E("Failed to format block data for %s.", data->devnode);
@@ -1579,7 +1602,7 @@ static void release_format_data(struct format_data *data)
 // Called by BlockThread - Real Mount Op
 static int block_mount_device(struct block_device *bdev, void *data)
 {
-       dd_list *l;
+       GList *l;
        int ret;
        int thread_id;
 
@@ -1590,7 +1613,7 @@ static int block_mount_device(struct block_device *bdev, void *data)
        if (thread_id < 0 || thread_id >= THREAD_MAX)
                return -EINVAL;
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
-       l = DD_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
+       l = SYS_G_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
        pthread_mutex_unlock(&(th_manager[thread_id].mutex));
        if (!l) {
                _E("'%s' does not exist in the device list.", bdev->data->devnode);
@@ -1608,7 +1631,7 @@ static int block_mount_device(struct block_device *bdev, void *data)
 // Called by BlockThread - Real Format Op
 static int block_format_device(struct block_device *bdev, void *data)
 {
-       dd_list *l;
+       GList *l;
        int ret;
        int thread_id;
        struct format_data *fdata = (struct format_data *)data;
@@ -1622,7 +1645,7 @@ static int block_format_device(struct block_device *bdev, void *data)
        if (thread_id < 0 || thread_id >= THREAD_MAX)
                return -EINVAL;
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
-       l = DD_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
+       l = SYS_G_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
        pthread_mutex_unlock(&(th_manager[thread_id].mutex));
        if (!l) {
                _E("'%s' does not exist in the device list.", bdev->data->devnode);
@@ -1666,7 +1689,7 @@ static int block_unmount_device(struct block_device *bdev, void *data)
 static void remove_operation(struct block_device *bdev)
 {
        struct operation_queue *op;
-       dd_list *l, *next;
+       GList *l, *next;
        int thread_id;
 
        assert(bdev);
@@ -1675,23 +1698,23 @@ static void remove_operation(struct block_device *bdev)
        if (thread_id < 0 || thread_id >= THREAD_MAX)
                return;
 
-       DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
+       SYS_G_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
                if (op->done) {
                        _D("Remove operation(%s, %s).",
                                        get_operation_char(op->op),
                                        bdev->data->devnode);
 
-                       DD_LIST_REMOVE(bdev->op_queue, op);
+                       SYS_G_LIST_REMOVE(bdev->op_queue, op);
                        free(op);
                }
        }
 }
 
 // Called by BlockThread
-static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
+static void check_removed(struct block_device *bdev, GList **queue, struct operation_queue **op)
 {
        struct operation_queue *temp;
-       dd_list *l;
+       GList *l;
        int thread_id;
 
        if (!bdev)
@@ -1709,7 +1732,7 @@ static void check_removed(struct block_device *bdev, dd_list **queue, struct ope
 
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
 
-       DD_LIST_FOREACH(*queue, l, temp) {
+       SYS_G_LIST_FOREACH(*queue, l, temp) {
                if (temp->op == BLOCK_DEV_REMOVE) {
                        *op = temp;
                        break;
@@ -1724,10 +1747,10 @@ static void check_removed(struct block_device *bdev, dd_list **queue, struct ope
 }
 
 // Called by BlockThread
-static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
+static bool check_unmount(struct block_device *bdev, GList **queue, struct operation_queue **op)
 {
        struct operation_queue *temp;
-       dd_list *l;
+       GList *l;
        int thread_id;
        bool unmounted = false;
 
@@ -1745,7 +1768,7 @@ static bool check_unmount(struct block_device *bdev, dd_list **queue, struct ope
                return false;
 
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
-       DD_LIST_FOREACH(*queue, l, temp) {
+       SYS_G_LIST_FOREACH(*queue, l, temp) {
                if (temp->op == BLOCK_DEV_UNMOUNT) {
                        unmounted = true;
                        _D("Operation queue has unmount operation.");
@@ -1759,7 +1782,7 @@ static bool check_unmount(struct block_device *bdev, dd_list **queue, struct ope
 
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
 
-       DD_LIST_FOREACH(*queue, l, temp) {
+       SYS_G_LIST_FOREACH(*queue, l, temp) {
                if (temp->op == BLOCK_DEV_UNMOUNT) {
                        *op = temp;
                        break;
@@ -1776,9 +1799,9 @@ static bool check_unmount(struct block_device *bdev, dd_list **queue, struct ope
 }
 
 // Called by BlockThread
-static void trigger_operation(struct block_device *bdev, dd_list *queue, struct operation_queue *op)
+static void trigger_operation(struct block_device *bdev, GList *queue, struct operation_queue *op)
 {
-       int ret = 0;
+       int ret_val = 0;
        int thread_id;
        char devnode[PATH_MAX];
        enum block_dev_operation operation;
@@ -1826,28 +1849,30 @@ static void trigger_operation(struct block_device *bdev, dd_list *queue, struct
                case BLOCK_DEV_INSERT:
                        break;
                case BLOCK_DEV_MOUNT:
-                       ret = block_mount_device(bdev, op->data);
-                       _D("Mount '%s': %d", devnode, ret);
+                       ret_val = block_mount_device(bdev, op->data);
+                       _D("Mount '%s': %d", devnode, ret_val);
                        break;
                case BLOCK_DEV_FORMAT:
-                       ret = block_format_device(bdev, op->data);
-                       _D("Format '%s': %d", devnode, ret);
+                       ret_val = block_format_device(bdev, op->data);
+                       _D("Format '%s': %d", devnode, ret_val);
                        break;
                case BLOCK_DEV_UNMOUNT:
-                       ret = block_unmount_device(bdev, op->data);
-                       _D("Unmount '%s': %d", devnode, ret);
+                       ret_val = block_unmount_device(bdev, op->data);
+                       _D("Unmount '%s': %d", devnode, ret_val);
                        break;
                case BLOCK_DEV_REMOVE:
                        /* Do nothing */
                        break;
                case BLOCK_LUKS_CLOSE:
-                       ret = ode_luks_close_sync(EXTENDEDSD_NAME);
-                       if (ret < 0)
+#ifdef EXTENDED_STORAGE
+                       ret_val = ode_luks_close_sync(EXTENDEDSD_NAME);
+                       if (ret_val < 0)
                                _E("Failed on ode_luks_close(%s).", EXTENDEDSD_NAME);
+#endif
                        break;
                default:
                        _E("Operation type(%d) is invalid.", op->op);
-                       ret = -EINVAL;
+                       ret_val = -EINVAL;
                        break;
                }
 
@@ -1858,13 +1883,13 @@ static void trigger_operation(struct block_device *bdev, dd_list *queue, struct
                op->done = true;
                th_manager[thread_id].op_len--;
 
-               block_send_dbus_reply(op->invocation, ret);
+               block_send_dbus_reply(op->invocation, ret_val);
 
                queue = bdev->op_queue;
                if (queue != NULL) {
-                       queue = DD_LIST_NEXT(queue);
+                       queue = SYS_G_LIST_NEXT(queue);
                        if (queue != NULL)
-                               op = DD_LIST_NTH(queue, 0);
+                               op = SYS_G_LIST_NTH(queue, 0);
                        else
                                op = NULL;
                } else
@@ -1890,8 +1915,8 @@ static void *block_th_start(void *arg)
        struct block_device *temp;
        struct manage_thread *th = (struct manage_thread *)arg;
        struct operation_queue *op = NULL;
-       dd_list *elem;
-       dd_list *queue = NULL;
+       GList *elem;
+       GList *queue = NULL;
        int thread_id;
 
        assert(th);
@@ -1910,16 +1935,16 @@ static void *block_th_start(void *arg)
                        _D("Wake up thread=%d.", thread_id);
                }
 
-               DD_LIST_FOREACH(th_manager[thread_id].block_dev_list, elem, temp) {
+               SYS_G_LIST_FOREACH(th_manager[thread_id].block_dev_list, elem, temp) {
                        queue = temp->op_queue;
                        do {
-                               op = DD_LIST_NTH(queue, 0);
+                               op = SYS_G_LIST_NTH(queue, 0);
                                if (!op) {
                                        _D("Operation queue for device %s is Empty.", temp->data->devnode);
                                        break;
                                }
                                if (op->done) {
-                                       queue = DD_LIST_NEXT(queue);
+                                       queue = SYS_G_LIST_NEXT(queue);
                                        continue;
                                }
                                break;
@@ -1939,7 +1964,7 @@ static void *block_th_start(void *arg)
 // Especially, we don't need to keep th_node_list.
 static int find_thread(char *devnode)
 {
-       dd_list *elem;
+       GList *elem;
        char str[PATH_MAX];
        char *th_node;
        char *temp;
@@ -1969,7 +1994,7 @@ static int find_thread(char *devnode)
        min_num = 1000;
        min = -1;
        for (i = 0; i < THREAD_MAX; i++) {
-               DD_LIST_FOREACH(th_manager[i].th_node_list, elem, temp) {
+               SYS_G_LIST_FOREACH(th_manager[i].th_node_list, elem, temp) {
                        if (!strncmp(temp, th_node, len)) {
                                free(th_node);
                                return i;
@@ -1982,12 +2007,12 @@ static int find_thread(char *devnode)
        }
 
        if (min >= 0 && min < THREAD_MAX) {
-               DD_LIST_APPEND(th_manager[min].th_node_list, th_node);
+               SYS_G_LIST_APPEND(th_manager[min].th_node_list, th_node);
                return min;
        }
 
        _E("Failed to find thread.");
-       DD_LIST_APPEND(th_manager[0].th_node_list, th_node);
+       SYS_G_LIST_APPEND(th_manager[0].th_node_list, th_node);
        return 0;
 }
 
@@ -1998,7 +2023,7 @@ static int add_operation(struct block_device *bdev,
                GDBusMethodInvocation *invocation, void *data)
 {
        struct operation_queue *op;
-       int ret;
+       int ret_val;
        int thread_id;
        bool start_th;
 
@@ -2043,7 +2068,7 @@ static int add_operation(struct block_device *bdev,
        op->done = false;
 
        start_th = th_manager[thread_id].start_th;
-       DD_LIST_APPEND(bdev->op_queue, op);
+       SYS_G_LIST_APPEND(bdev->op_queue, op);
        th_manager[thread_id].op_len++;
 
        if (th_manager[thread_id].op_len == 1 && start_th)
@@ -2055,8 +2080,8 @@ static int add_operation(struct block_device *bdev,
        if (!start_th) {
                _D("Start new thread for block device.");
                th_manager[thread_id].start_th = true;
-               ret = pthread_create(&(th_manager[thread_id].th), NULL, block_th_start, &th_manager[thread_id]);
-               if (ret != 0) {
+               ret_val = pthread_create(&(th_manager[thread_id].th), NULL, block_th_start, &th_manager[thread_id]);
+               if (ret_val != 0) {
                        _E("Failed to create thread for %s.", bdev->data->devnode);
                        return -EPERM;
                }
@@ -2222,7 +2247,7 @@ static int add_block_device(struct udev_device *dev, const char *devnode, bool m
 
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
        th_manager[thread_id].num_dev++;
-       DD_LIST_APPEND(th_manager[thread_id].block_dev_list, bdev);
+       SYS_G_LIST_APPEND(th_manager[thread_id].block_dev_list, bdev);
        pthread_mutex_unlock(&(th_manager[thread_id].mutex));
 
        if (need_format) {
@@ -2294,8 +2319,6 @@ static int add_block_device(struct udev_device *dev, const char *devnode, bool m
                return 0;
        }
 
-       /* Create file for block device /run/external-storage/id */
-       create_file(bdev->data->id, bdev->data->mount_point, bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
        ret = add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL);
        if (ret < 0) {
                _E("Failed to add operation(mount, %s).", devnode);
@@ -2401,13 +2424,18 @@ static int get_internal_storage_number(void)
                return -EPERM;
        }
        temp = mnt_fs_get_srcpath(fs);
-       if (!temp)
+       if (!temp) {
+               mnt_free_table(t);
                return -EPERM;
+       }
 
        name = strrchr(temp, '/');
-       if (!name)
+       if (!name) {
+               mnt_free_table(t);
                return -EPERM;
+       }
        name++;
+
        /* Boot from USB is not handled */
        if (!is_emulator()) {
                if (!fnmatch(MMC_PATH, temp, 0))
@@ -2471,12 +2499,12 @@ static int check_already_handled(const char* devnode)
 {
        struct block_device *bdev;
        struct block_data *data;
-       dd_list *elem;
+       GList *elem;
        int i;
 
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        data = bdev->data;
                        if (!data)
                                continue;
@@ -2589,12 +2617,12 @@ static void show_block_device_list(void)
 {
        struct block_device *bdev;
        struct block_data *data;
-       dd_list *elem;
+       GList *elem;
        int i;
 
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        data = bdev->data;
                        if (!data)
                                continue;
@@ -2625,15 +2653,15 @@ static void show_block_device_list(void)
 static void remove_whole_block_device(void)
 {
        struct block_device *bdev;
-       dd_list *elem;
-       dd_list *next;
+       GList *elem;
+       GList *next;
        int r;
        int i;
 
        for (i = 0; i < THREAD_MAX; i++) {
                do {
                        pthread_mutex_lock(&(th_manager[i].mutex));
-                       DD_LIST_FOREACH_SAFE(th_manager[i].block_dev_list, elem, next, bdev) {
+                       SYS_G_LIST_FOREACH_SAFE(th_manager[i].block_dev_list, elem, next, bdev) {
                                if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV ||
                                    !strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME)))
                                        continue;
@@ -2657,10 +2685,26 @@ static void remove_whole_block_device(void)
        }
 }
 
+static void add_poweroff_wait_cb(GVariant *var, void *user_data, GError *err)
+{
+       int ret_val = -ENOMSG;
+
+       if (!var)
+               return ;
+
+       if (!g_variant_get_safe(var, "(i)", &ret_val))
+               _E("Failed to get(%s): no message", g_variant_get_type_string(var));
+
+       if (ret_val >= 0)
+               add_poweroff_wait = true;
+
+       g_variant_unref(var);
+}
+
 static void booting_done(void)
 {
        static int done = 0;
-       int ret;
+       int ret_val;
 
        if (done > 0)
                return;
@@ -2668,14 +2712,24 @@ static void booting_done(void)
        _I("Booting done.");
 
        /* register mmc uevent control routine */
-       ret = register_udev_uevent_control(&uh);
-       if (ret < 0)
-               _E("Failed to register block uevent: %d", ret);
+       ret_val = register_udev_uevent_control(&uh);
+       if (ret_val < 0)
+               _E("Failed to register block uevent: %d", ret_val);
 
        block_control = true;
        /* if there is the attached device, try to mount */
        block_init_from_udev_enumerate();
 
+       ret_val = gdbus_call_async_with_reply(DEVICED_BUS_NAME,
+               DEVICED_PATH_POWEROFF,
+               DEVICED_INTERFACE_POWEROFF,
+               METHOD_ADD_POWEROFF_WAIT,
+               NULL,
+               add_poweroff_wait_cb,
+               -1, NULL);
+       if (ret_val < 0)
+               _E("Failed to call "METHOD_ADD_POWEROFF_WAIT" method.");
+
        block_boot = true;
 }
 
@@ -2688,6 +2742,8 @@ static void block_poweroff(GDBusConnection  *conn,
                gpointer data)
 {
        static int status = 0;
+       int ret_dbus;
+
        if (status > 0)
                return;
        status = 1;
@@ -2695,6 +2751,19 @@ static void block_poweroff(GDBusConnection  *conn,
        /* unregister mmc uevent control routine */
        unregister_udev_uevent_control(&uh);
        remove_whole_block_device();
+       terminate_threads();
+
+       if (add_poweroff_wait) {
+               ret_dbus = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
+                       DEVICED_PATH_POWEROFF,
+                       DEVICED_INTERFACE_POWEROFF,
+                       METHOD_REMOVE_POWEROFF_WAIT,
+                       NULL, NULL);
+               if (ret_dbus < 0)
+                       _E("Failed to call "METHOD_REMOVE_POWEROFF_WAIT" method.");
+               else
+                       add_poweroff_wait = false;
+       }
 }
 
 static void uevent_block_handler(struct udev_device *dev)
@@ -2818,14 +2887,8 @@ static GVariant *request_mount_block(GDBusConnection *conn,
 
        if (onprivate) {
                bdev->on_private_op = REQ_PRIVATE;
-               bdev->private_pid = dbus_handle_get_sender_pid(NULL, sender);
+               bdev->private_pid = gdbus_connection_get_sender_pid(NULL, sender);
                _D("Private operation state(%d). pid=%d.", bdev->on_private_op, bdev->private_pid);
-       } else {
-               if (bdev->on_private_op != REQ_NORMAL) {
-                       _E("Failed to process mount operation.");
-                       ret = -EPERM;
-                       goto out;
-               }
        }
 
        /* if requester want to use a specific mount point */
@@ -2835,12 +2898,6 @@ static GVariant *request_mount_block(GDBusConnection *conn,
                        ret = -EPERM;
                        goto out;
                }
-
-               /* Create /run/external-storage/id file */
-               create_file(bdev->data->id, bdev->data->mount_point, false);
-       } else {
-               /* Create file for block device /run/external-storage/id */
-               create_file(bdev->data->id, bdev->data->mount_point, false);
        }
 
        ret = add_operation(bdev, BLOCK_DEV_MOUNT, invocation, NULL);
@@ -2904,7 +2961,7 @@ static GVariant *request_unmount_block(GDBusConnection *conn,
        }
 
        if (onprivate) {
-               pid = dbus_handle_get_sender_pid(NULL, sender);
+               pid = gdbus_connection_get_sender_pid(NULL, sender);
                if (bdev->on_private_op == REQ_NORMAL || (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid)) {
                        _E("Failed to process private unmount operation pid=%d private_pid=%d.", pid, bdev->private_pid);
                        ret = -EPERM;
@@ -2983,7 +3040,7 @@ static GVariant *request_format_block(GDBusConnection *conn,
                goto out;
        }
 
-       pid = dbus_handle_get_sender_pid(NULL, sender);
+       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.");
                ret = -EPERM;
@@ -3064,7 +3121,7 @@ static GVariant *request_format_block_type(GDBusConnection *conn,
                goto out;
        }
 
-       pid = dbus_handle_get_sender_pid(NULL, sender);
+       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.");
                ret = -EPERM;
@@ -3116,7 +3173,7 @@ out:
 static GVariant *block_data_to_gvariant(struct block_data *data, int flags)
 {
        if (!data)
-               return dbus_handle_new_g_variant_tuple();
+               return gdbus_new_g_variant_tuple();
 
        return g_variant_new("(issssssisibii)",
                        data->block_type,
@@ -3134,27 +3191,6 @@ static GVariant *block_data_to_gvariant(struct block_data *data, int flags)
                        data->id);
 }
 
-static GVariant *block_data_to_gvariant2(struct block_data *data, int flags)
-{
-       if (!data)
-               return dbus_handle_new_g_variant_tuple();
-
-       return g_variant_new("(issssssisibi)",
-                       data->block_type,
-                       nullstr(data->devnode),
-                       nullstr(data->syspath),
-                       nullstr(data->fs_usage),
-                       nullstr(data->fs_type),
-                       nullstr(data->fs_version),
-                       nullstr(data->fs_uuid_enc),
-                       data->readonly,
-                       nullstr(data->mount_point),
-                       data->state,
-                       data->primary,
-                       flags >= 0 ? flags : data->flags);
-}
-
-
 //static int add_device_to_struct_iter(struct block_data *data, DBusMessageIter *iter)
 //{
 //     //DBusMessageIter piter;
@@ -3239,7 +3275,7 @@ static GVariant *request_show_device_list(GDBusConnection *conn,
                GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
 {
        show_block_device_list();
-       return dbus_handle_new_g_variant_tuple();
+       return gdbus_new_g_variant_tuple();
 }
 
 static enum block_device_type get_bdev_type_from_type_string(const char *type_str)
@@ -3265,24 +3301,21 @@ static GVariant *request_get_device_list(GDBusConnection *conn,
        GVariant *reply = NULL;
        struct block_device *bdev;
        struct block_data *data;
-       dd_list *elem;
+       GList *elem;
        char *type = NULL;
        enum block_device_type block_type;
        int i;
        GVariantBuilder *builder = NULL;
+       const char *error = NULL;
 
        g_variant_get(param, "(s)", &type);
 
-       if (!type) {
-               _E("Delivered type is NULL.");
-               goto out;
-       }
-
        _D("Block (%s) device list is requested.", type);
 
        block_type = get_bdev_type_from_type_string(type);
        if (block_type == BLOCK_UNKNOWN_DEV) {
                _E("Invalid type (%s) is requested.", type);
+               error = "Invalid type is requested";
                goto out;
        }
 
@@ -3290,7 +3323,7 @@ static GVariant *request_get_device_list(GDBusConnection *conn,
 
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        if (!bdev->data || bdev->removed)
                                continue;
 
@@ -3318,13 +3351,18 @@ static GVariant *request_get_device_list(GDBusConnection *conn,
                }
                pthread_mutex_unlock(&(th_manager[i].mutex));
        }
+
        reply = g_variant_new("(a(issssssisibii))", builder);
+
        g_variant_builder_unref(builder);
 
 out:
        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;
 }
 
@@ -3336,15 +3374,17 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn,
        GVariant *reply = NULL;
        struct block_device *bdev;
        struct block_data *data;
-       dd_list *elem;
+       GList *elem;
        char *type = NULL;
        enum block_device_type block_type;
        int i;
        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;
        }
 
@@ -3353,6 +3393,7 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn,
        block_type = get_bdev_type_from_type_string(type);
        if (block_type == BLOCK_UNKNOWN_DEV) {
                _E("Invalid type (%s) is requested.", type);
+               error = "Invalid type is requested";
                goto out;
        }
 
@@ -3360,7 +3401,7 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn,
 
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        if (!bdev->data || bdev->removed)
                                continue;
 
@@ -3387,11 +3428,18 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn,
                }
                pthread_mutex_unlock(&(th_manager[i].mutex));
        }
+
        reply = g_variant_new("(a(issssssisibi))", builder);
+
        g_variant_builder_unref(builder);
 
 out:
        g_free(type);
+
+       if (!reply)
+               g_dbus_method_invocation_return_error(invocation,
+                               G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
+                               "%s", error);
        return reply;
 }
 
@@ -3400,14 +3448,14 @@ static GVariant *request_get_mmc_primary(GDBusConnection *conn,
                GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
 {
        struct block_device *bdev;
-       struct block_data *data, nodata = {0,};
-       dd_list *elem;
+       struct block_data *data = NULL, nodata = {0,};
+       GList *elem;
        bool found = false;
        int i;
 
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
-               DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
+               SYS_G_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
                        data = bdev->data;
                        if (!data)
                                continue;
@@ -3445,7 +3493,7 @@ static GVariant *request_check_speed(GDBusConnection *conn,
        struct block_device *bdev;
        struct block_data *data;
        char *buf = NULL;
-       int ret;
+       int ret_val;
        int result = 0;
        int id;
        int fd;
@@ -3471,8 +3519,8 @@ static GVariant *request_check_speed(GDBusConnection *conn,
                result = -1;
                goto out;
        }
-       ret = posix_memalign((void**)&buf, 4096, SPEEDCHECK_SIZE << 20);
-       if (ret) {
+       ret_val = posix_memalign((void**)&buf, 4096, SPEEDCHECK_SIZE << 20);
+       if (ret_val) {
                _E("Failed to posix_memalign().");
                result = -1;
                goto out_close;
@@ -3480,13 +3528,13 @@ static GVariant *request_check_speed(GDBusConnection *conn,
 
        clock_gettime(CLOCK_REALTIME, &start_time);
        _I("Start time=%lu.%lu", start_time.tv_sec, start_time.tv_nsec);
-       ret = read(fd, buf, SPEEDCHECK_SIZE << 20);
+       ret_val = read(fd, buf, SPEEDCHECK_SIZE << 20);
        clock_gettime(CLOCK_REALTIME, &end_time);
        _I("End time=%lu.%lu", end_time.tv_sec, end_time.tv_nsec);
 
        free(buf);
 
-       if (ret < 0) {
+       if (ret_val < 0) {
                _E("Failed to read(): %d", errno);
                result = -1;
                goto out_close;
@@ -3613,7 +3661,7 @@ static int load_config(struct parse_result *result, void *user_data)
 #ifdef BLOCK_TMPFS
 static int mount_root_path_tmpfs(void)
 {
-       int ret;
+       int ret_val;
        const char *root;
 
        root = tzplatform_getenv(TZ_SYS_MEDIA);
@@ -3626,11 +3674,11 @@ static int mount_root_path_tmpfs(void)
        if (mount_check(root))
                return 0;
 
-       ret = mount("tmpfs", root, "tmpfs", 0, "smackfsroot=System::Shared");
-       if (ret < 0) {
-               ret = -errno;
-               _E("Failed to mount tmpfs: %d", ret);
-               return ret;
+       ret_val = mount("tmpfs", root, "tmpfs", 0, "smackfsroot=System::Shared");
+       if (ret_val < 0) {
+               ret_val = -errno;
+               _E("Failed to mount tmpfs: %d", ret_val);
+               return ret_val;
        }
 
        return 0;
@@ -3643,28 +3691,28 @@ static guint id_block_poweroff;
 
 static void block_init(void *data)
 {
-       int ret;
+       int ret_val;
        int i;
 
        udev_init(NULL);
 
        /* load config */
-       ret = config_parse(BLOCK_CONF_FILE, load_config, NULL);
-       if (ret < 0)
+       ret_val = config_parse(BLOCK_CONF_FILE, load_config, NULL);
+       if (ret_val < 0)
                _E("Failed to load '%s'. Use default value.", BLOCK_CONF_FILE);
 
-       ret = mount_root_path_tmpfs();
-       if (ret < 0)
-               _E("Failed to mount tmpfs to root mount path: %d", ret);
+       ret_val = mount_root_path_tmpfs();
+       if (ret_val < 0)
+               _E("Failed to mount tmpfs to root mount path: %d", ret_val);
 
        /* register block manager object and interface */
-       ret = dbus_handle_register_dbus_object(NULL, STORAGED_PATH_BLOCK_MANAGER, &block_interface);
-       if (ret < 0)
-               _E("Failed to register block interface and methods: %d", ret);
+       ret_val = gdbus_register_object(NULL, STORAGED_PATH_BLOCK_MANAGER, &block_interface);
+       if (ret_val < 0)
+               _E("Failed to register block interface and methods: %d", ret_val);
 
        /* init pipe */
-       ret = pipe_init();
-       if (ret < 0)
+       ret_val = pipe_init();
+       if (ret_val < 0)
                _E("Failed to init pipe.");
 
        for (i = 0; i < THREAD_MAX; i++) {
@@ -3676,25 +3724,25 @@ static void block_init(void *data)
                pthread_cond_init(&(th_manager[i].cond), NULL);
        }
 
-       ret = remove_directory(EXTERNAL_STORAGE_PATH);
-       if (ret < 0)
+       ret_val = remove_directory(EXTERNAL_STORAGE_PATH);
+       if (ret_val < 0)
                _E("Failed to remove directory.");
-       ret = mkdir(EXTERNAL_STORAGE_PATH, 0755);
-       if (ret < 0)
+       ret_val = mkdir(EXTERNAL_STORAGE_PATH, 0755);
+       if (ret_val < 0)
                _E("Failed to make directory: %d", errno);
 
-       ret = remove_directory(EXTENDED_INTERNAL_PATH);
-       if (ret < 0)
+       ret_val = remove_directory(EXTENDED_INTERNAL_PATH);
+       if (ret_val < 0)
                _E("Failed to remove directory.");
-       ret = mkdir(EXTENDED_INTERNAL_PATH, 0755);
-       if (ret < 0)
+       ret_val = mkdir(EXTENDED_INTERNAL_PATH, 0755);
+       if (ret_val < 0)
                _E("Failed to make directory: %d", errno);
 
-       ret = get_internal_storage_number();
-       if (ret < 0)
+       ret_val = get_internal_storage_number();
+       if (ret_val < 0)
                _E("Failed to get internal storage number.");
 
-       id_block_poweroff = subscribe_dbus_signal(NULL, DEVICED_PATH_POWEROFF,
+       id_block_poweroff = gdbus_signal_subscribe(NULL, DEVICED_PATH_POWEROFF,
                        DEVICED_INTERFACE_POWEROFF,
                        SIGNAL_POWEROFF_STATE,
                        block_poweroff, NULL, NULL);
@@ -3703,7 +3751,7 @@ static void block_init(void *data)
 }
 static void terminate_threads(void)
 {
-       dd_list *elem, *elem_next;
+       GList *elem, *elem_next;
        char *temp;
        int i, count;
        const int WAIT_TIME = 10;
@@ -3719,8 +3767,8 @@ static void terminate_threads(void)
                        pthread_cancel(th_manager[i].th);
                        pthread_join(th_manager[i].th, NULL);
                }
-               DD_LIST_FOREACH_SAFE(th_manager[i].th_node_list, elem, elem_next, temp) {
-                       DD_LIST_REMOVE(th_manager[i].th_node_list, temp);
+               SYS_G_LIST_FOREACH_SAFE(th_manager[i].th_node_list, elem, elem_next, temp) {
+                       SYS_G_LIST_REMOVE(th_manager[i].th_node_list, temp);
                        free(temp);
                }
        }
@@ -3728,17 +3776,17 @@ static void terminate_threads(void)
 
 static void block_exit(void *data)
 {
-       int ret;
+       int ret_val;
 
        udev_exit(NULL);
 
        /* unregister notifier for below each event */
-       unsubscribe_dbus_signal(NULL, id_block_poweroff);
+       gdbus_signal_unsubscribe(NULL, id_block_poweroff);
 
        /* unregister mmc uevent control routine */
-       ret = unregister_udev_uevent_control(&uh);
-       if (ret < 0)
-               _E("Failed to unregister block uevent: %d", ret);
+       ret_val = unregister_udev_uevent_control(&uh);
+       if (ret_val < 0)
+               _E("Failed to unregister block uevent: %d", ret_val);
 
        /* remove remaining blocks */
        remove_whole_block_device();
@@ -3748,6 +3796,18 @@ static void block_exit(void *data)
        /* exit pipe */
        pipe_exit();
 
+       if (add_poweroff_wait) {
+               ret_val = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
+                       DEVICED_PATH_POWEROFF,
+                       DEVICED_INTERFACE_POWEROFF,
+                       METHOD_REMOVE_POWEROFF_WAIT,
+                       NULL, NULL);
+               if (ret_val < 0)
+                       _E("Failed to call "METHOD_REMOVE_POWEROFF_WAIT" method.");
+               else
+                       add_poweroff_wait = false;
+       }
+
        block_control = false;
 }