From 870d7cfb19f49ced89117b403972eef4a05faf08 Mon Sep 17 00:00:00 2001 From: Wolfgang Wiedmeyer Date: Fri, 9 Dec 2016 22:33:58 +0100 Subject: Fix backport of readdir patch Backport changes from Google's master to fix build errors when using readdir_r. Signed-off-by: Wolfgang Wiedmeyer --- libprocessgroup/processgroup.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index 0fa835b..2ac73b1 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -37,6 +38,8 @@ #include #include "processgroup_priv.h" +#define ACCT_CGROUP_PATH "/acct" + struct ctx { bool initialized; int fd; @@ -45,6 +48,21 @@ struct ctx { size_t buf_len; }; +static const char* getCgroupRootPath() { +#ifdef USE_MEMCG + static const char* cgroup_root_path = NULL; + std::call_once(init_path_flag, [&]() { + // Check if mem cgroup is mounted, only then check for write-access to avoid + // SELinux denials + cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ? + ACCT_CGROUP_PATH : MEM_CGROUP_PATH; + }); + return cgroup_root_path; +#else + return ACCT_CGROUP_PATH; +#endif +} + static int convertUidToPath(char *path, size_t size, uid_t uid) { return snprintf(path, size, "%s/%s%d", @@ -162,7 +180,7 @@ 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) { dirent* dir; while ((dir = readdir(uid.get())) != nullptr) { @@ -180,14 +198,14 @@ 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); + const char* cgroup_root_path = getCgroupRootPath(); + std::unique_ptr root(opendir(cgroup_root_path), closedir); if (root == NULL) { SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno)); } else { @@ -207,7 +225,6 @@ void removeAllProcessGroups() SLOGV("removing %s\n", path); rmdir(path); } - closedir(root); } } -- cgit v1.1