From 8e13c9a76f28599a039e92b5779a55f743aec162 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Mon, 10 Apr 2017 20:25:32 +0900 Subject: [PATCH] common: Remove readdir - readdir makes no guarantee of thread safety - Use readdir_r instead Change-Id: I0a5c3e6bcabe1ef76291a58dd756a99a5a4a95d0 Signed-off-by: pr.jung --- src/block/block.c | 3 ++- src/block/ext4.c | 44 +++++++++++++++++++++----------------------- src/block/utils.c | 23 ++++++++++++++--------- src/core/modules.c | 11 ++++++----- src/shared/dbus.c | 13 +++---------- 5 files changed, 46 insertions(+), 48 deletions(-) diff --git a/src/block/block.c b/src/block/block.c index 895ae7b..838cc91 100644 --- a/src/block/block.c +++ b/src/block/block.c @@ -1967,6 +1967,7 @@ static int add_operation(struct block_device *bdev, static bool disk_is_partitioned_by_kernel(struct udev_device *dev) { DIR *dp; + struct dirent entry; struct dirent *dir; const char *syspath; bool ret = false; @@ -1982,7 +1983,7 @@ static bool disk_is_partitioned_by_kernel(struct udev_device *dev) } /* TODO compare devname and d_name */ - while ((dir = readdir(dp))) { + while (readdir_r(dp, &entry, &dir) == 0 && dir != NULL) { if (!fnmatch(MMC_PARTITION_PATH, dir->d_name, 0) || !fnmatch(SCSI_PARTITION_PATH, dir->d_name, 0)) { ret = true; diff --git a/src/block/ext4.c b/src/block/ext4.c index 3d1551a..2e79c34 100644 --- a/src/block/ext4.c +++ b/src/block/ext4.c @@ -34,31 +34,15 @@ #define FS_EXT4_NAME "ext4" -static const char *check_smack_arg[] = { - "/usr/bin/mmc-smack-label", - NULL, NULL, -}; - -static const char *ext4_arg[] = { - "/sbin/mkfs.ext4", - NULL, NULL, -}; - -static const char *ext4_check_arg[] = { - "/sbin/fsck.ext4", - "-f", "-y", NULL, NULL, -}; - -static struct fs_check ext4_info = { - FS_TYPE_EXT4, - "ext4", - 0x438, - 2, - {0x53, 0xef}, -}; - static bool ext4_match(const char *devpath) { + struct fs_check ext4_info = { + FS_TYPE_EXT4, + "ext4", + 0x438, + 2, + {0x53, 0xef}, + }; char buf[4]; int fd, r; @@ -93,7 +77,12 @@ error: static int ext4_check(const char *devpath) { + const char *ext4_check_arg[] = { + "/sbin/fsck.ext4", + "-f", "-y", NULL, NULL, + }; int argc; + argc = ARRAY_SIZE(ext4_check_arg); ext4_check_arg[argc - 2] = devpath; return run_child(argc, ext4_check_arg); @@ -101,6 +90,10 @@ static int ext4_check(const char *devpath) static int mmc_check_smack(const char *mount_point) { + const char *check_smack_arg[] = { + "/usr/bin/mmc-smack-label", + NULL, NULL, + }; char buf[NAME_MAX] = {0,}; int argc; int ret; @@ -148,7 +141,12 @@ static int ext4_mount(bool smack, const char *devpath, const char *mount_point) static int ext4_format(const char *devpath) { + const char *ext4_arg[] = { + "/sbin/mkfs.ext4", + NULL, NULL, + }; int argc; + argc = ARRAY_SIZE(ext4_arg); ext4_arg[argc - 2] = devpath; return run_child(argc, ext4_arg); diff --git a/src/block/utils.c b/src/block/utils.c index c12ece7..086f8e0 100644 --- a/src/block/utils.c +++ b/src/block/utils.c @@ -42,10 +42,11 @@ int print_open_files(const char *mount_point) { DIR *dp; - struct dirent *dentry; + struct dirent entry; + struct dirent *dir; DIR *dp_child; - struct dirent *dentry_child; + struct dirent *dir_child; int pid = -1, fd; int ret; @@ -64,14 +65,16 @@ int print_open_files(const char *mount_point) } while (1) { - dentry = readdir(dp); - if (dentry == NULL) + ret = readdir_r(dp, &entry, &dir); + if (dir == NULL) + break; + if (ret != 0) break; - if (!isdigit(dentry->d_name[0])) + if (!isdigit(dir->d_name[0])) continue; - pid = atoi(dentry->d_name); + pid = atoi(dir->d_name); snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid); fd = open(buf, O_RDONLY); @@ -89,11 +92,13 @@ int print_open_files(const char *mount_point) if (!dp_child) continue; while (1) { - dentry_child = readdir(dp_child); - if (dentry_child == NULL) + ret = readdir_r(dp, &entry, &dir_child); + if (dir_child == NULL) + break; + if (ret != 0) break; - snprintf(check_path, PATH_MAX, "%s/%s", buf, dentry_child->d_name); + snprintf(check_path, PATH_MAX, "%s/%s", buf, dir_child->d_name); if (readlink(check_path, buf2, PATH_MAX) < 0) continue; diff --git a/src/core/modules.c b/src/core/modules.c index 6e65246..5bcaa98 100644 --- a/src/core/modules.c +++ b/src/core/modules.c @@ -105,7 +105,8 @@ int modules_init(void *data) storaged_module_interface *plugin; struct storaged_module *module; DIR *dp; - struct dirent *dentry; + struct dirent entry; + struct dirent *dir; int ret; char path[256]; @@ -116,11 +117,11 @@ int modules_init(void *data) return ret; } - while ((dentry = readdir(dp))) { - if (dentry->d_type != DT_REG) + while (readdir_r(dp, &entry, &dir) == 0 && dir != NULL) { + if (dir->d_type != DT_REG) continue; - snprintf(path, sizeof(path), "%s/%s", PLUGIN_PATH, dentry->d_name); + snprintf(path, sizeof(path), "%s/%s", PLUGIN_PATH, dir->d_name); ret = storaged_open_module(path, &plugin); if (ret < 0) { _E("Failed to open module (%s: %d)", path, ret); @@ -132,7 +133,7 @@ int modules_init(void *data) module = calloc(1, sizeof(struct storaged_module)); module->plugin = plugin; module->name = strdup(plugin->name); - module->so_name = strdup(dentry->d_name); + module->so_name = strdup(dir->d_name); module_list = g_list_append(module_list, module); diff --git a/src/shared/dbus.c b/src/shared/dbus.c index 04b5cd4..e8502e4 100644 --- a/src/shared/dbus.c +++ b/src/shared/dbus.c @@ -451,6 +451,7 @@ void unregister_dbus_methods(dbus_handle_h handle, dbus_method_handle_h method_h struct dbus_method_handle_s *item; struct dbus_handle_s *h = handle; struct dbus_method_handle_s *mh = method_handle; + bool found = false; if (!h || !h->conn) return; @@ -461,11 +462,12 @@ void unregister_dbus_methods(dbus_handle_h handle, dbus_method_handle_h method_h l = g_list_next(l), item = NULL) { if (item == mh) { h->method_handle_list = g_list_remove(h->method_handle_list, item); + found = true; break; } } - if (!item) + if (!found) return; dbus_connection_unregister_object_path(h->conn, item->path); @@ -996,15 +998,6 @@ int call_dbus_method_async(const char *dest, const char *path, return -ECOMM; } - pdata = calloc(1, sizeof(struct pending_call_data)); - if (!pdata) { - _E("malloc error : %s-%s", interface, method); - return -ENOMEM; - } - - pdata->func = cb; - pdata->data = data; - msg = dbus_message_new_method_call(dest, path, interface, method); if (!msg) { _E("dbus_message_new_method_call(%s:%s-%s)", -- 2.7.4