Loading libprocessgroup/task_profiles.cpp +32 −0 Original line number Original line Diff line number Diff line Loading @@ -132,6 +132,7 @@ bool SetCgroupAction::IsAppDependentPath(const std::string& path) { SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p) SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p) : controller_(c), path_(p) { : controller_(c), path_(p) { #ifdef CACHE_FILE_DESCRIPTORS // cache file descriptor only if path is app independent // cache file descriptor only if path is app independent if (IsAppDependentPath(path_)) { if (IsAppDependentPath(path_)) { // file descriptor is not cached // file descriptor is not cached Loading @@ -155,6 +156,7 @@ SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p } } fd_ = std::move(fd); fd_ = std::move(fd); #endif } } bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { Loading @@ -176,6 +178,7 @@ bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { } } bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { #ifdef CACHE_FILE_DESCRIPTORS if (fd_ >= 0) { if (fd_ >= 0) { // fd is cached, reuse it // fd is cached, reuse it if (!AddTidToCgroup(pid, fd_)) { if (!AddTidToCgroup(pid, fd_)) { Loading Loading @@ -203,9 +206,24 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { } } return true; return true; #else std::string procs_path = controller_->GetProcsFilePath(path_.c_str(), uid, pid); unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); if (tmp_fd < 0) { // no permissions to access the file, ignore return true; } if (!AddTidToCgroup(pid, tmp_fd)) { PLOG(ERROR) << "Failed to add task into cgroup"; return false; } return true; #endif } } bool SetCgroupAction::ExecuteForTask(int tid) const { bool SetCgroupAction::ExecuteForTask(int tid) const { #ifdef CACHE_FILE_DESCRIPTORS if (fd_ >= 0) { if (fd_ >= 0) { // fd is cached, reuse it // fd is cached, reuse it if (!AddTidToCgroup(tid, fd_)) { if (!AddTidToCgroup(tid, fd_)) { Loading @@ -223,6 +241,20 @@ bool SetCgroupAction::ExecuteForTask(int tid) const { // application-dependent path can't be used with tid // application-dependent path can't be used with tid PLOG(ERROR) << "Application profile can't be applied to a thread"; PLOG(ERROR) << "Application profile can't be applied to a thread"; return false; return false; #else std::string tasks_path = controller_->GetTasksFilePath(path_.c_str()); unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); if (tmp_fd < 0) { // no permissions to access the file, ignore return true; } if (!AddTidToCgroup(tid, tmp_fd)) { PLOG(ERROR) << "Failed to add task into cgroup"; return false; } return true; #endif } } bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { Loading libprocessgroup/task_profiles.h +2 −0 Original line number Original line Diff line number Diff line Loading @@ -117,7 +117,9 @@ class SetCgroupAction : public ProfileAction { private: private: const CgroupController* controller_; const CgroupController* controller_; std::string path_; std::string path_; #ifdef CACHE_FILE_DESCRIPTORS android::base::unique_fd fd_; android::base::unique_fd fd_; #endif static bool IsAppDependentPath(const std::string& path); static bool IsAppDependentPath(const std::string& path); static bool AddTidToCgroup(int tid, int fd); static bool AddTidToCgroup(int tid, int fd); Loading Loading
libprocessgroup/task_profiles.cpp +32 −0 Original line number Original line Diff line number Diff line Loading @@ -132,6 +132,7 @@ bool SetCgroupAction::IsAppDependentPath(const std::string& path) { SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p) SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p) : controller_(c), path_(p) { : controller_(c), path_(p) { #ifdef CACHE_FILE_DESCRIPTORS // cache file descriptor only if path is app independent // cache file descriptor only if path is app independent if (IsAppDependentPath(path_)) { if (IsAppDependentPath(path_)) { // file descriptor is not cached // file descriptor is not cached Loading @@ -155,6 +156,7 @@ SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p } } fd_ = std::move(fd); fd_ = std::move(fd); #endif } } bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { Loading @@ -176,6 +178,7 @@ bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { } } bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { #ifdef CACHE_FILE_DESCRIPTORS if (fd_ >= 0) { if (fd_ >= 0) { // fd is cached, reuse it // fd is cached, reuse it if (!AddTidToCgroup(pid, fd_)) { if (!AddTidToCgroup(pid, fd_)) { Loading Loading @@ -203,9 +206,24 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { } } return true; return true; #else std::string procs_path = controller_->GetProcsFilePath(path_.c_str(), uid, pid); unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); if (tmp_fd < 0) { // no permissions to access the file, ignore return true; } if (!AddTidToCgroup(pid, tmp_fd)) { PLOG(ERROR) << "Failed to add task into cgroup"; return false; } return true; #endif } } bool SetCgroupAction::ExecuteForTask(int tid) const { bool SetCgroupAction::ExecuteForTask(int tid) const { #ifdef CACHE_FILE_DESCRIPTORS if (fd_ >= 0) { if (fd_ >= 0) { // fd is cached, reuse it // fd is cached, reuse it if (!AddTidToCgroup(tid, fd_)) { if (!AddTidToCgroup(tid, fd_)) { Loading @@ -223,6 +241,20 @@ bool SetCgroupAction::ExecuteForTask(int tid) const { // application-dependent path can't be used with tid // application-dependent path can't be used with tid PLOG(ERROR) << "Application profile can't be applied to a thread"; PLOG(ERROR) << "Application profile can't be applied to a thread"; return false; return false; #else std::string tasks_path = controller_->GetTasksFilePath(path_.c_str()); unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); if (tmp_fd < 0) { // no permissions to access the file, ignore return true; } if (!AddTidToCgroup(tid, tmp_fd)) { PLOG(ERROR) << "Failed to add task into cgroup"; return false; } return true; #endif } } bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { Loading
libprocessgroup/task_profiles.h +2 −0 Original line number Original line Diff line number Diff line Loading @@ -117,7 +117,9 @@ class SetCgroupAction : public ProfileAction { private: private: const CgroupController* controller_; const CgroupController* controller_; std::string path_; std::string path_; #ifdef CACHE_FILE_DESCRIPTORS android::base::unique_fd fd_; android::base::unique_fd fd_; #endif static bool IsAppDependentPath(const std::string& path); static bool IsAppDependentPath(const std::string& path); static bool AddTidToCgroup(int tid, int fd); static bool AddTidToCgroup(int tid, int fd); Loading