diff options
-rw-r--r-- | libprocessgroup/processgroup.cpp | 25 |
1 files 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 <sys/types.h> #include <log/log.h> +#include <memory> #include <private/android_filesystem_config.h> #include <utils/SystemClock.h> @@ -37,6 +38,8 @@ #include <processgroup/processgroup.h> #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<DIR, decltype(&closedir)> 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<DIR, decltype(&closedir)> 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); } } |