From 588a2cad7faee54bfb16050d0c7398709f304fea Mon Sep 17 00:00:00 2001 From: James Hawkins Date: Thu, 18 Feb 2016 14:52:46 -0800 Subject: [PATCH] system/core: Cleanup direct calls to opendir by containing in a std::unique_ptr. Bug: 26643633 Change-Id: Ia3491fdbff086558da694ae949cf08e4c89d0307 --- debuggerd/getevent.cpp | 7 +++---- fastboot/usb_linux.cpp | 8 +++----- healthd/BatteryMonitor.cpp | 6 +++--- init/devices.cpp | 7 ++++--- libbacktrace/backtrace_test.cpp | 5 ++--- libprocessgroup/processgroup.cpp | 11 +++++------ libutils/ProcessCallStack.cpp | 8 +++----- logcat/tests/logcat_test.cpp | 8 ++++---- 8 files changed, 27 insertions(+), 33 deletions(-) diff --git a/debuggerd/getevent.cpp b/debuggerd/getevent.cpp index 751c4fb6139b..e5acd1744f09 100644 --- a/debuggerd/getevent.cpp +++ b/debuggerd/getevent.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include static struct pollfd* ufds; @@ -143,22 +144,20 @@ static int read_notify(const char* dirname, int nfd) { static int scan_dir(const char* dirname) { char devname[PATH_MAX]; char* filename; - DIR* dir; struct dirent* de; - dir = opendir(dirname); + std::unique_ptr dir(opendir(dirname), closedir); if (dir == NULL) return -1; strcpy(devname, dirname); filename = devname + strlen(devname); *filename++ = '/'; - while ((de = readdir(dir))) { + while ((de = readdir(dir.get()))) { if ((de->d_name[0] == '.' && de->d_name[1] == '\0') || (de->d_name[1] == '.' && de->d_name[2] == '\0')) continue; strcpy(filename, de->d_name); open_device(devname); } - closedir(dir); return 0; } diff --git a/fastboot/usb_linux.cpp b/fastboot/usb_linux.cpp index 02ffcd978f41..d4824fb2ae40 100644 --- a/fastboot/usb_linux.cpp +++ b/fastboot/usb_linux.cpp @@ -145,7 +145,7 @@ static int filter_usb_device(char* sysfs_name, int in, out; unsigned i; unsigned e; - + if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE)) return -1; dev = (struct usb_device_descriptor *)ptr; @@ -333,15 +333,14 @@ static std::unique_ptr find_usb_device(const char* base, ifc_match_f char desc[1024]; int n, in, out, ifc; - DIR *busdir; struct dirent *de; int fd; int writable; - busdir = opendir(base); + std::unique_ptr busdir(opendir(base), closedir); if (busdir == 0) return 0; - while ((de = readdir(busdir)) && (usb == nullptr)) { + while ((de = readdir(busdir.get())) && (usb == nullptr)) { if (badname(de->d_name)) continue; if (!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) { @@ -377,7 +376,6 @@ static std::unique_ptr find_usb_device(const char* base, ifc_match_f } } } - closedir(busdir); return usb; } diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp index 0558fd594eb2..c78bab16048a 100644 --- a/healthd/BatteryMonitor.cpp +++ b/healthd/BatteryMonitor.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -456,13 +457,13 @@ void BatteryMonitor::init(struct healthd_config *hc) { char pval[PROPERTY_VALUE_MAX]; mHealthdConfig = hc; - DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH); + std::unique_ptr dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir); if (dir == NULL) { KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); } else { struct dirent* entry; - while ((entry = readdir(dir))) { + while ((entry = readdir(dir.get()))) { const char* name = entry->d_name; if (!strcmp(name, ".") || !strcmp(name, "..")) @@ -600,7 +601,6 @@ void BatteryMonitor::init(struct healthd_config *hc) { break; } } - closedir(dir); } // This indicates that there is no charger driver registered. diff --git a/init/devices.cpp b/init/devices.cpp index 39cd70656921..557a6acd5a5e 100644 --- a/init/devices.cpp +++ b/init/devices.cpp @@ -31,6 +31,8 @@ #include #include +#include + #include #include #include @@ -957,10 +959,9 @@ static void do_coldboot(DIR *d) static void coldboot(const char *path) { - DIR *d = opendir(path); + std::unique_ptr d(opendir(path), closedir); if(d) { - do_coldboot(d); - closedir(d); + do_coldboot(d.get()); } } diff --git a/libbacktrace/backtrace_test.cpp b/libbacktrace/backtrace_test.cpp index 7d829fe74974..b975db955826 100644 --- a/libbacktrace/backtrace_test.cpp +++ b/libbacktrace/backtrace_test.cpp @@ -404,17 +404,16 @@ void GetThreads(pid_t pid, std::vector* threads) { char task_path[128]; snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); - DIR* tasks_dir = opendir(task_path); + std::unique_ptr tasks_dir(opendir(task_path), closedir); ASSERT_TRUE(tasks_dir != nullptr); struct dirent* entry; - while ((entry = readdir(tasks_dir)) != nullptr) { + while ((entry = readdir(tasks_dir.get())) != nullptr) { char* end; pid_t tid = strtoul(entry->d_name, &end, 10); if (*end == '\0') { threads->push_back(tid); } } - closedir(tasks_dir); } TEST(libbacktrace, ptrace_threads) { diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index ad0500daba80..00a0357f5b02 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -162,11 +163,11 @@ static int removeProcessGroup(uid_t uid, int pid) static void removeUidProcessGroups(const char *uid_path) { - DIR *uid = opendir(uid_path); + std::unique_ptr uid(opendir(uid_path), closedir); if (uid != NULL) { struct dirent cur; struct dirent *dir; - while ((readdir_r(uid, &cur, &dir) == 0) && dir) { + while ((readdir_r(uid.get(), &cur, &dir) == 0) && dir) { char path[PROCESSGROUP_MAX_PATH_LEN]; if (dir->d_type != DT_DIR) { @@ -181,20 +182,19 @@ static void removeUidProcessGroups(const char *uid_path) SLOGV("removing %s\n", path); rmdir(path); } - closedir(uid); } } void removeAllProcessGroups() { SLOGV("removeAllProcessGroups()"); - DIR *root = opendir(PROCESSGROUP_CGROUP_PATH); + std::unique_ptr root(opendir(PROCESSGROUP_CGROUP_PATH), closedir); if (root == NULL) { SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno)); } else { struct dirent cur; struct dirent *dir; - while ((readdir_r(root, &cur, &dir) == 0) && dir) { + while ((readdir_r(root.get(), &cur, &dir) == 0) && dir) { char path[PROCESSGROUP_MAX_PATH_LEN]; if (dir->d_type != DT_DIR) { @@ -209,7 +209,6 @@ void removeAllProcessGroups() SLOGV("removing %s\n", path); rmdir(path); } - closedir(root); } } diff --git a/libutils/ProcessCallStack.cpp b/libutils/ProcessCallStack.cpp index cdb586d98b26..4e87a9875a2c 100644 --- a/libutils/ProcessCallStack.cpp +++ b/libutils/ProcessCallStack.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -130,11 +131,10 @@ void ProcessCallStack::clear() { } void ProcessCallStack::update() { - DIR *dp; struct dirent *ep; struct dirent entry; - dp = opendir(PATH_SELF_TASK); + std::unique_ptr dp(opendir(PATH_SELF_TASK), closedir); if (dp == NULL) { ALOGE("%s: Failed to update the process's call stacks: %s", __FUNCTION__, strerror(errno)); @@ -159,7 +159,7 @@ void ProcessCallStack::update() { * - Read every file in directory => get every tid */ int code; - while ((code = readdir_r(dp, &entry, &ep)) == 0 && ep != NULL) { + while ((code = readdir_r(dp.get(), &entry, &ep)) == 0 && ep != NULL) { pid_t tid = -1; sscanf(ep->d_name, "%d", &tid); @@ -198,8 +198,6 @@ void ProcessCallStack::update() { ALOGE("%s: Failed to readdir from %s: %s", __FUNCTION__, PATH_SELF_TASK, strerror(code)); } - - closedir(dp); } void ProcessCallStack::log(const char* logtag, android_LogPriority priority, diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp index 4f517bb184de..8459bd3d0f6f 100644 --- a/logcat/tests/logcat_test.cpp +++ b/logcat/tests/logcat_test.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -734,8 +735,8 @@ TEST(logcat, logrotate_continue) { EXPECT_FALSE(system(command)); return; } - DIR *dir; - EXPECT_TRUE(NULL != (dir = opendir(tmp_out_dir))); + std::unique_ptr dir(opendir(tmp_out_dir), closedir); + EXPECT_NE(nullptr, dir); if (!dir) { snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); EXPECT_FALSE(system(command)); @@ -743,7 +744,7 @@ TEST(logcat, logrotate_continue) { } struct dirent *entry; unsigned count = 0; - while ((entry = readdir(dir))) { + while ((entry = readdir(dir.get()))) { if (strncmp(entry->d_name, log_filename, sizeof(log_filename) - 1)) { continue; } @@ -766,7 +767,6 @@ TEST(logcat, logrotate_continue) { free(line); unlink(command); } - closedir(dir); if (count > 1) { char *brk = strpbrk(second_last_line, "\r\n"); if (!brk) {