Merge "system/core: Cleanup direct calls to opendir by containing in a std::unique_ptr."

This commit is contained in:
James Hawkins 2016-02-19 17:49:13 +00:00 committed by Gerrit Code Review
commit bd04bb0d29
8 changed files with 27 additions and 33 deletions

View file

@ -26,6 +26,7 @@
#include <sys/poll.h> #include <sys/poll.h>
#include <linux/input.h> #include <linux/input.h>
#include <errno.h> #include <errno.h>
#include <memory>
#include <cutils/log.h> #include <cutils/log.h>
static struct pollfd* ufds; static struct pollfd* ufds;
@ -143,22 +144,20 @@ static int read_notify(const char* dirname, int nfd) {
static int scan_dir(const char* dirname) { static int scan_dir(const char* dirname) {
char devname[PATH_MAX]; char devname[PATH_MAX];
char* filename; char* filename;
DIR* dir;
struct dirent* de; struct dirent* de;
dir = opendir(dirname); std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
if (dir == NULL) if (dir == NULL)
return -1; return -1;
strcpy(devname, dirname); strcpy(devname, dirname);
filename = devname + strlen(devname); filename = devname + strlen(devname);
*filename++ = '/'; *filename++ = '/';
while ((de = readdir(dir))) { while ((de = readdir(dir.get()))) {
if ((de->d_name[0] == '.' && de->d_name[1] == '\0') || if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
(de->d_name[1] == '.' && de->d_name[2] == '\0')) (de->d_name[1] == '.' && de->d_name[2] == '\0'))
continue; continue;
strcpy(filename, de->d_name); strcpy(filename, de->d_name);
open_device(devname); open_device(devname);
} }
closedir(dir);
return 0; return 0;
} }

View file

@ -145,7 +145,7 @@ static int filter_usb_device(char* sysfs_name,
int in, out; int in, out;
unsigned i; unsigned i;
unsigned e; unsigned e;
if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE)) if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
return -1; return -1;
dev = (struct usb_device_descriptor *)ptr; dev = (struct usb_device_descriptor *)ptr;
@ -333,15 +333,14 @@ static std::unique_ptr<usb_handle> find_usb_device(const char* base, ifc_match_f
char desc[1024]; char desc[1024];
int n, in, out, ifc; int n, in, out, ifc;
DIR *busdir;
struct dirent *de; struct dirent *de;
int fd; int fd;
int writable; int writable;
busdir = opendir(base); std::unique_ptr<DIR, decltype(&closedir)> busdir(opendir(base), closedir);
if (busdir == 0) return 0; 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 (badname(de->d_name)) continue;
if (!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) { if (!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) {
@ -377,7 +376,6 @@ static std::unique_ptr<usb_handle> find_usb_device(const char* base, ifc_match_f
} }
} }
} }
closedir(busdir);
return usb; return usb;
} }

View file

@ -26,6 +26,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <memory>
#include <batteryservice/BatteryService.h> #include <batteryservice/BatteryService.h>
#include <cutils/klog.h> #include <cutils/klog.h>
@ -456,13 +457,13 @@ void BatteryMonitor::init(struct healthd_config *hc) {
char pval[PROPERTY_VALUE_MAX]; char pval[PROPERTY_VALUE_MAX];
mHealthdConfig = hc; mHealthdConfig = hc;
DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH); std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
if (dir == NULL) { if (dir == NULL) {
KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
} else { } else {
struct dirent* entry; struct dirent* entry;
while ((entry = readdir(dir))) { while ((entry = readdir(dir.get()))) {
const char* name = entry->d_name; const char* name = entry->d_name;
if (!strcmp(name, ".") || !strcmp(name, "..")) if (!strcmp(name, ".") || !strcmp(name, ".."))
@ -600,7 +601,6 @@ void BatteryMonitor::init(struct healthd_config *hc) {
break; break;
} }
} }
closedir(dir);
} }
// This indicates that there is no charger driver registered. // This indicates that there is no charger driver registered.

View file

@ -31,6 +31,8 @@
#include <sys/un.h> #include <sys/un.h>
#include <linux/netlink.h> #include <linux/netlink.h>
#include <memory>
#include <selinux/selinux.h> #include <selinux/selinux.h>
#include <selinux/label.h> #include <selinux/label.h>
#include <selinux/android.h> #include <selinux/android.h>
@ -957,10 +959,9 @@ static void do_coldboot(DIR *d)
static void coldboot(const char *path) static void coldboot(const char *path)
{ {
DIR *d = opendir(path); std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
if(d) { if(d) {
do_coldboot(d); do_coldboot(d.get());
closedir(d);
} }
} }

View file

@ -404,17 +404,16 @@ void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
char task_path[128]; char task_path[128];
snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
DIR* tasks_dir = opendir(task_path); std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir);
ASSERT_TRUE(tasks_dir != nullptr); ASSERT_TRUE(tasks_dir != nullptr);
struct dirent* entry; struct dirent* entry;
while ((entry = readdir(tasks_dir)) != nullptr) { while ((entry = readdir(tasks_dir.get())) != nullptr) {
char* end; char* end;
pid_t tid = strtoul(entry->d_name, &end, 10); pid_t tid = strtoul(entry->d_name, &end, 10);
if (*end == '\0') { if (*end == '\0') {
threads->push_back(tid); threads->push_back(tid);
} }
} }
closedir(tasks_dir);
} }
TEST(libbacktrace, ptrace_threads) { TEST(libbacktrace, ptrace_threads) {

View file

@ -28,6 +28,7 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <memory>
#include <log/log.h> #include <log/log.h>
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>
@ -162,11 +163,11 @@ static int removeProcessGroup(uid_t uid, int pid)
static void removeUidProcessGroups(const char *uid_path) static void removeUidProcessGroups(const char *uid_path)
{ {
DIR *uid = opendir(uid_path); std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
if (uid != NULL) { if (uid != NULL) {
struct dirent cur; struct dirent cur;
struct dirent *dir; 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]; char path[PROCESSGROUP_MAX_PATH_LEN];
if (dir->d_type != DT_DIR) { if (dir->d_type != DT_DIR) {
@ -181,20 +182,19 @@ static void removeUidProcessGroups(const char *uid_path)
SLOGV("removing %s\n", path); SLOGV("removing %s\n", path);
rmdir(path); rmdir(path);
} }
closedir(uid);
} }
} }
void removeAllProcessGroups() void removeAllProcessGroups()
{ {
SLOGV("removeAllProcessGroups()"); SLOGV("removeAllProcessGroups()");
DIR *root = opendir(PROCESSGROUP_CGROUP_PATH); std::unique_ptr<DIR, decltype(&closedir)> root(opendir(PROCESSGROUP_CGROUP_PATH), closedir);
if (root == NULL) { if (root == NULL) {
SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno)); SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno));
} else { } else {
struct dirent cur; struct dirent cur;
struct dirent *dir; 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]; char path[PROCESSGROUP_MAX_PATH_LEN];
if (dir->d_type != DT_DIR) { if (dir->d_type != DT_DIR) {
@ -209,7 +209,6 @@ void removeAllProcessGroups()
SLOGV("removing %s\n", path); SLOGV("removing %s\n", path);
rmdir(path); rmdir(path);
} }
closedir(root);
} }
} }

View file

@ -21,6 +21,7 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <memory>
#include <utils/Log.h> #include <utils/Log.h>
#include <utils/Errors.h> #include <utils/Errors.h>
@ -130,11 +131,10 @@ void ProcessCallStack::clear() {
} }
void ProcessCallStack::update() { void ProcessCallStack::update() {
DIR *dp;
struct dirent *ep; struct dirent *ep;
struct dirent entry; struct dirent entry;
dp = opendir(PATH_SELF_TASK); std::unique_ptr<DIR, decltype(&closedir)> dp(opendir(PATH_SELF_TASK), closedir);
if (dp == NULL) { if (dp == NULL) {
ALOGE("%s: Failed to update the process's call stacks: %s", ALOGE("%s: Failed to update the process's call stacks: %s",
__FUNCTION__, strerror(errno)); __FUNCTION__, strerror(errno));
@ -159,7 +159,7 @@ void ProcessCallStack::update() {
* - Read every file in directory => get every tid * - Read every file in directory => get every tid
*/ */
int code; 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; pid_t tid = -1;
sscanf(ep->d_name, "%d", &tid); sscanf(ep->d_name, "%d", &tid);
@ -198,8 +198,6 @@ void ProcessCallStack::update() {
ALOGE("%s: Failed to readdir from %s: %s", ALOGE("%s: Failed to readdir from %s: %s",
__FUNCTION__, PATH_SELF_TASK, strerror(code)); __FUNCTION__, PATH_SELF_TASK, strerror(code));
} }
closedir(dp);
} }
void ProcessCallStack::log(const char* logtag, android_LogPriority priority, void ProcessCallStack::log(const char* logtag, android_LogPriority priority,

View file

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <memory>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <log/log.h> #include <log/log.h>
@ -734,8 +735,8 @@ TEST(logcat, logrotate_continue) {
EXPECT_FALSE(system(command)); EXPECT_FALSE(system(command));
return; return;
} }
DIR *dir; std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(tmp_out_dir), closedir);
EXPECT_TRUE(NULL != (dir = opendir(tmp_out_dir))); EXPECT_NE(nullptr, dir);
if (!dir) { if (!dir) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir); snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(system(command)); EXPECT_FALSE(system(command));
@ -743,7 +744,7 @@ TEST(logcat, logrotate_continue) {
} }
struct dirent *entry; struct dirent *entry;
unsigned count = 0; unsigned count = 0;
while ((entry = readdir(dir))) { while ((entry = readdir(dir.get()))) {
if (strncmp(entry->d_name, log_filename, sizeof(log_filename) - 1)) { if (strncmp(entry->d_name, log_filename, sizeof(log_filename) - 1)) {
continue; continue;
} }
@ -766,7 +767,6 @@ TEST(logcat, logrotate_continue) {
free(line); free(line);
unlink(command); unlink(command);
} }
closedir(dir);
if (count > 1) { if (count > 1) {
char *brk = strpbrk(second_last_line, "\r\n"); char *brk = strpbrk(second_last_line, "\r\n");
if (!brk) { if (!brk) {