From 821ba553140387c31998977cbb490f3c7b96eadc Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 28 Sep 2016 13:29:54 -0700 Subject: Use readdir instead of readdir_r. http://elliotth.blogspot.com/2012/10/how-not-to-use-readdirr3.html Test: boots. Change-Id: If75532e24fe4d17743bf8e8c9590156dee378a63 --- libprocessgroup/processgroup.cpp | 10 ++++------ libutils/ProcessCallStack.cpp | 13 +++---------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index ad0500d..0fa835b 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -164,9 +164,8 @@ static void removeUidProcessGroups(const char *uid_path) { DIR *uid = opendir(uid_path); if (uid != NULL) { - struct dirent cur; - struct dirent *dir; - while ((readdir_r(uid, &cur, &dir) == 0) && dir) { + dirent* dir; + while ((dir = readdir(uid.get())) != nullptr) { char path[PROCESSGROUP_MAX_PATH_LEN]; if (dir->d_type != DT_DIR) { @@ -192,9 +191,8 @@ void removeAllProcessGroups() 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) { + dirent* dir; + while ((dir = readdir(root.get())) != nullptr) { char path[PROCESSGROUP_MAX_PATH_LEN]; if (dir->d_type != DT_DIR) { diff --git a/libutils/ProcessCallStack.cpp b/libutils/ProcessCallStack.cpp index db07e56..39e7488 100644 --- a/libutils/ProcessCallStack.cpp +++ b/libutils/ProcessCallStack.cpp @@ -18,6 +18,7 @@ // #define LOG_NDEBUG 0 #include +#include #include #include @@ -129,11 +130,9 @@ 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 (errno = %d, '%s')", __FUNCTION__, errno, strerror(errno)); @@ -158,8 +157,7 @@ void ProcessCallStack::update() { * Each tid is a directory inside of /proc/self/task * - Read every file in directory => get every tid */ - int code; - while ((code = readdir_r(dp, &entry, &ep)) == 0 && ep != NULL) { + while ((ep = readdir(dp.get())) != NULL) { pid_t tid = -1; sscanf(ep->d_name, "%d", &tid); @@ -194,13 +192,8 @@ void ProcessCallStack::update() { ALOGV("%s: Got call stack for tid %d (size %zu)", __FUNCTION__, tid, threadInfo.callStack.size()); } - if (code != 0) { // returns positive error value on error - ALOGE("%s: Failed to readdir from %s (errno = %d, '%s')", - __FUNCTION__, PATH_SELF_TASK, -code, strerror(code)); - } #endif - closedir(dp); } void ProcessCallStack::log(const char* logtag, android_LogPriority priority, -- cgit v1.1