Merge "system/core: Cleanup direct calls to opendir by containing in a std::unique_ptr."
am: bd04bb0d29
* commit 'bd04bb0d297c1a7cd5aabdc258b1829905fb067d':
system/core: Cleanup direct calls to opendir by containing in a std::unique_ptr.
This commit is contained in:
commit
0f5d443d0c
|
@ -26,6 +26,7 @@
|
|||
#include <sys/poll.h>
|
||||
#include <linux/input.h>
|
||||
#include <errno.h>
|
||||
#include <memory>
|
||||
#include <cutils/log.h>
|
||||
|
||||
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, decltype(&closedir)> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<usb_handle> 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<DIR, decltype(&closedir)> 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<usb_handle> find_usb_device(const char* base, ifc_match_f
|
|||
}
|
||||
}
|
||||
}
|
||||
closedir(busdir);
|
||||
|
||||
return usb;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <memory>
|
||||
|
||||
#include <batteryservice/BatteryService.h>
|
||||
#include <cutils/klog.h>
|
||||
|
@ -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, decltype(&closedir)> 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.
|
||||
|
|
|
@ -31,6 +31,8 @@
|
|||
#include <sys/un.h>
|
||||
#include <linux/netlink.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <selinux/selinux.h>
|
||||
#include <selinux/label.h>
|
||||
#include <selinux/android.h>
|
||||
|
@ -957,10 +959,9 @@ static void do_coldboot(DIR *d)
|
|||
|
||||
static void coldboot(const char *path)
|
||||
{
|
||||
DIR *d = opendir(path);
|
||||
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
|
||||
if(d) {
|
||||
do_coldboot(d);
|
||||
closedir(d);
|
||||
do_coldboot(d.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -404,17 +404,16 @@ void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
|
|||
char task_path[128];
|
||||
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);
|
||||
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) {
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory>
|
||||
|
||||
#include <log/log.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)
|
||||
{
|
||||
DIR *uid = opendir(uid_path);
|
||||
std::unique_ptr<DIR, decltype(&closedir)> 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<DIR, decltype(&closedir)> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <memory>
|
||||
|
||||
#include <utils/Log.h>
|
||||
#include <utils/Errors.h>
|
||||
|
@ -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<DIR, decltype(&closedir)> 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,
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <log/log.h>
|
||||
|
@ -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, decltype(&closedir)> 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) {
|
||||
|
|
Loading…
Reference in New Issue