Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d950242c authored by Suren Baghdasaryan's avatar Suren Baghdasaryan Committed by Gerrit Code Review
Browse files

Merge changes from topic "SetProcessProfilesCached"

* changes:
  init.rc: Set permissions to cgroup.procs files
  libprocessgroup: Add fd caching support for SetProcessProfiles
  libprocessgroup: Move fd caching code into FdCacheHelper
parents 0e54327f 2e7799a4
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -35,6 +35,8 @@ bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& pr


#ifndef __ANDROID_VNDK__
#ifndef __ANDROID_VNDK__


bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles);

static constexpr const char* CGROUPS_RC_PATH = "/dev/cgroup_info/cgroup.rc";
static constexpr const char* CGROUPS_RC_PATH = "/dev/cgroup_info/cgroup.rc";


bool UsePerAppMemcg();
bool UsePerAppMemcg();
+7 −2
Original line number Original line Diff line number Diff line
@@ -126,11 +126,16 @@ static bool isMemoryCgroupSupported() {
}
}


void DropTaskProfilesResourceCaching() {
void DropTaskProfilesResourceCaching() {
    TaskProfiles::GetInstance().DropResourceCaching();
    TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_TASK);
    TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_PROCESS);
}
}


bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
    return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles);
    return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false);
}

bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
    return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, true);
}
}


bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
+189 −107
Original line number Original line Diff line number Diff line
@@ -51,6 +51,67 @@ static constexpr const char* TASK_PROFILE_DB_VENDOR_FILE = "/vendor/etc/task_pro
static constexpr const char* TEMPLATE_TASK_PROFILE_API_FILE =
static constexpr const char* TEMPLATE_TASK_PROFILE_API_FILE =
        "/etc/task_profiles/task_profiles_%u.json";
        "/etc/task_profiles/task_profiles_%u.json";


class FdCacheHelper {
  public:
    enum FdState {
        FDS_INACCESSIBLE = -1,
        FDS_APP_DEPENDENT = -2,
        FDS_NOT_CACHED = -3,
    };

    static void Cache(const std::string& path, android::base::unique_fd& fd);
    static void Drop(android::base::unique_fd& fd);
    static void Init(const std::string& path, android::base::unique_fd& fd);
    static bool IsCached(const android::base::unique_fd& fd) { return fd > FDS_INACCESSIBLE; }

  private:
    static bool IsAppDependentPath(const std::string& path);
};

void FdCacheHelper::Init(const std::string& path, android::base::unique_fd& fd) {
    // file descriptors for app-dependent paths can't be cached
    if (IsAppDependentPath(path)) {
        // file descriptor is not cached
        fd.reset(FDS_APP_DEPENDENT);
        return;
    }
    // file descriptor can be cached later on request
    fd.reset(FDS_NOT_CACHED);
}

void FdCacheHelper::Cache(const std::string& path, android::base::unique_fd& fd) {
    if (fd != FDS_NOT_CACHED) {
        return;
    }

    if (access(path.c_str(), W_OK) != 0) {
        // file is not accessible
        fd.reset(FDS_INACCESSIBLE);
        return;
    }

    unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC)));
    if (tmp_fd < 0) {
        PLOG(ERROR) << "Failed to cache fd '" << path << "'";
        fd.reset(FDS_INACCESSIBLE);
        return;
    }

    fd = std::move(tmp_fd);
}

void FdCacheHelper::Drop(android::base::unique_fd& fd) {
    if (fd == FDS_NOT_CACHED) {
        return;
    }

    fd.reset(FDS_NOT_CACHED);
}

bool FdCacheHelper::IsAppDependentPath(const std::string& path) {
    return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
}

void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) {
void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) {
    controller_ = controller;
    controller_ = controller;
    file_name_ = file_name;
    file_name_ = file_name;
@@ -144,57 +205,11 @@ bool SetAttributeAction::ExecuteForTask(int tid) const {
    return true;
    return true;
}
}


void CachedFdProfileAction::EnableResourceCaching() {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    if (fd_ != FDS_NOT_CACHED) {
        return;
    }

    std::string tasks_path = GetPath();

    if (access(tasks_path.c_str(), W_OK) != 0) {
        // file is not accessible
        fd_.reset(FDS_INACCESSIBLE);
        return;
    }

    unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
    if (fd < 0) {
        PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'";
        fd_.reset(FDS_INACCESSIBLE);
        return;
    }

    fd_ = std::move(fd);
}

void CachedFdProfileAction::DropResourceCaching() {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    if (fd_ == FDS_NOT_CACHED) {
        return;
    }

    fd_.reset(FDS_NOT_CACHED);
}

bool CachedFdProfileAction::IsAppDependentPath(const std::string& path) {
    return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
}

void CachedFdProfileAction::InitFd(const std::string& path) {
    // file descriptors for app-dependent paths can't be cached
    if (IsAppDependentPath(path)) {
        // file descriptor is not cached
        fd_.reset(FDS_APP_DEPENDENT);
        return;
    }
    // file descriptor can be cached later on request
    fd_.reset(FDS_NOT_CACHED);
}

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) {
    InitFd(controller_.GetTasksFilePath(path_));
    FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]);
    // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them
    FdCacheHelper::Init(controller_.GetProcsFilePath(path_, 0, 0), fd_[ProfileAction::RCT_PROCESS]);
}
}


bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) {
bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) {
@@ -232,7 +247,40 @@ bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_nam
    return false;
    return false;
}
}


ProfileAction::CacheUseResult SetCgroupAction::UseCachedFd(ResourceCacheType cache_type,
                                                           int id) const {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    if (FdCacheHelper::IsCached(fd_[cache_type])) {
        // fd is cached, reuse it
        if (!AddTidToCgroup(id, fd_[cache_type], controller()->name())) {
            LOG(ERROR) << "Failed to add task into cgroup";
            return ProfileAction::FAIL;
        }
        return ProfileAction::SUCCESS;
    }

    if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) {
        // no permissions to access the file, ignore
        return ProfileAction::SUCCESS;
    }

    if (cache_type == ResourceCacheType::RCT_TASK &&
        fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) {
        // application-dependent path can't be used with tid
        PLOG(ERROR) << "Application profile can't be applied to a thread";
        return ProfileAction::FAIL;
    }

    return ProfileAction::UNUSED;
}

bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
    CacheUseResult result = UseCachedFd(ProfileAction::RCT_PROCESS, pid);
    if (result != ProfileAction::UNUSED) {
        return result == ProfileAction::SUCCESS;
    }

    // fd was not cached or cached fd can't be used
    std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
    std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
    unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
    unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
    if (tmp_fd < 0) {
    if (tmp_fd < 0) {
@@ -248,28 +296,12 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
}
}


bool SetCgroupAction::ExecuteForTask(int tid) const {
bool SetCgroupAction::ExecuteForTask(int tid) const {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, tid);
    if (IsFdValid()) {
    if (result != ProfileAction::UNUSED) {
        // fd is cached, reuse it
        return result == ProfileAction::SUCCESS;
        if (!AddTidToCgroup(tid, fd_, controller()->name())) {
            LOG(ERROR) << "Failed to add task into cgroup";
            return false;
        }
        return true;
    }

    if (fd_ == FDS_INACCESSIBLE) {
        // no permissions to access the file, ignore
        return true;
    }

    if (fd_ == FDS_APP_DEPENDENT) {
        // application-dependent path can't be used with tid
        PLOG(ERROR) << "Application profile can't be applied to a thread";
        return false;
    }
    }


    // fd was not cached because cached fd can't be used
    // fd was not cached or cached fd can't be used
    std::string tasks_path = controller()->GetTasksFilePath(path_);
    std::string tasks_path = controller()->GetTasksFilePath(path_);
    unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
    unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
    if (tmp_fd < 0) {
    if (tmp_fd < 0) {
@@ -284,10 +316,36 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
    return true;
    return true;
}
}


void SetCgroupAction::EnableResourceCaching(ResourceCacheType cache_type) {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    // Return early to prevent unnecessary calls to controller_.Get{Tasks|Procs}FilePath() which
    // include regex evaluations
    if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) {
        return;
    }
    switch (cache_type) {
        case (ProfileAction::RCT_TASK):
            FdCacheHelper::Cache(controller_.GetTasksFilePath(path_), fd_[cache_type]);
            break;
        case (ProfileAction::RCT_PROCESS):
            // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them
            FdCacheHelper::Cache(controller_.GetProcsFilePath(path_, 0, 0), fd_[cache_type]);
            break;
        default:
            LOG(ERROR) << "Invalid cache type is specified!";
            break;
    }
}

void SetCgroupAction::DropResourceCaching(ResourceCacheType cache_type) {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    FdCacheHelper::Drop(fd_[cache_type]);
}

WriteFileAction::WriteFileAction(const std::string& path, const std::string& value,
WriteFileAction::WriteFileAction(const std::string& path, const std::string& value,
                                 bool logfailures)
                                 bool logfailures)
    : path_(path), value_(value), logfailures_(logfailures) {
    : path_(path), value_(value), logfailures_(logfailures) {
    InitFd(path_);
    FdCacheHelper::Init(path_, fd_);
}
}


bool WriteFileAction::WriteValueToFile(const std::string& value, const std::string& path,
bool WriteFileAction::WriteValueToFile(const std::string& value, const std::string& path,
@@ -309,13 +367,43 @@ bool WriteFileAction::WriteValueToFile(const std::string& value, const std::stri
    return true;
    return true;
}
}


bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
ProfileAction::CacheUseResult WriteFileAction::UseCachedFd(ResourceCacheType cache_type,
                                                           const std::string& value) const {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    std::lock_guard<std::mutex> lock(fd_mutex_);
    if (FdCacheHelper::IsCached(fd_)) {
        // fd is cached, reuse it
        if (!WriteStringToFd(value, fd_)) {
            if (logfailures_) PLOG(ERROR) << "Failed to write '" << value << "' to " << path_;
            return ProfileAction::FAIL;
        }
        return ProfileAction::SUCCESS;
    }

    if (fd_ == FdCacheHelper::FDS_INACCESSIBLE) {
        // no permissions to access the file, ignore
        return ProfileAction::SUCCESS;
    }

    if (cache_type == ResourceCacheType::RCT_TASK && fd_ == FdCacheHelper::FDS_APP_DEPENDENT) {
        // application-dependent path can't be used with tid
        PLOG(ERROR) << "Application profile can't be applied to a thread";
        return ProfileAction::FAIL;
    }
    return ProfileAction::UNUSED;
}

bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
    std::string value(value_);
    std::string value(value_);
    std::string path(path_);


    value = StringReplace(value, "<uid>", std::to_string(uid), true);
    value = StringReplace(value, "<uid>", std::to_string(uid), true);
    value = StringReplace(value, "<pid>", std::to_string(pid), true);
    value = StringReplace(value, "<pid>", std::to_string(pid), true);

    CacheUseResult result = UseCachedFd(ProfileAction::RCT_PROCESS, value);
    if (result != ProfileAction::UNUSED) {
        return result == ProfileAction::SUCCESS;
    }

    std::string path(path_);
    path = StringReplace(path, "<uid>", std::to_string(uid), true);
    path = StringReplace(path, "<uid>", std::to_string(uid), true);
    path = StringReplace(path, "<pid>", std::to_string(pid), true);
    path = StringReplace(path, "<pid>", std::to_string(pid), true);


@@ -323,41 +411,33 @@ bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
}
}


bool WriteFileAction::ExecuteForTask(int tid) const {
bool WriteFileAction::ExecuteForTask(int tid) const {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    std::string value(value_);
    std::string value(value_);
    int uid = getuid();
    int uid = getuid();


    value = StringReplace(value, "<uid>", std::to_string(uid), true);
    value = StringReplace(value, "<uid>", std::to_string(uid), true);
    value = StringReplace(value, "<pid>", std::to_string(tid), true);
    value = StringReplace(value, "<pid>", std::to_string(tid), true);


    if (IsFdValid()) {
    CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, value);
        // fd is cached, reuse it
    if (result != ProfileAction::UNUSED) {
        if (!WriteStringToFd(value, fd_)) {
        return result == ProfileAction::SUCCESS;
            if (logfailures_) PLOG(ERROR) << "Failed to write '" << value << "' to " << path_;
            return false;
        }
        return true;
    }
    }


    if (fd_ == FDS_INACCESSIBLE) {
    return WriteValueToFile(value, path_, logfailures_);
        // no permissions to access the file, ignore
        return true;
}
}


    if (fd_ == FDS_APP_DEPENDENT) {
void WriteFileAction::EnableResourceCaching(ResourceCacheType) {
        // application-dependent path can't be used with tid
    std::lock_guard<std::mutex> lock(fd_mutex_);
        PLOG(ERROR) << "Application profile can't be applied to a thread";
    FdCacheHelper::Cache(path_, fd_);
        return false;
}
}


    return WriteValueToFile(value, path_, logfailures_);
void WriteFileAction::DropResourceCaching(ResourceCacheType) {
    std::lock_guard<std::mutex> lock(fd_mutex_);
    FdCacheHelper::Drop(fd_);
}
}


bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
    for (const auto& profile : profiles_) {
    for (const auto& profile : profiles_) {
        if (!profile->ExecuteForProcess(uid, pid)) {
        profile->ExecuteForProcess(uid, pid);
            PLOG(WARNING) << "ExecuteForProcess failed for aggregate profile";
        }
    }
    }
    return true;
    return true;
}
}
@@ -369,15 +449,15 @@ bool ApplyProfileAction::ExecuteForTask(int tid) const {
    return true;
    return true;
}
}


void ApplyProfileAction::EnableResourceCaching() {
void ApplyProfileAction::EnableResourceCaching(ResourceCacheType cache_type) {
    for (const auto& profile : profiles_) {
    for (const auto& profile : profiles_) {
        profile->EnableResourceCaching();
        profile->EnableResourceCaching(cache_type);
    }
    }
}
}


void ApplyProfileAction::DropResourceCaching() {
void ApplyProfileAction::DropResourceCaching(ResourceCacheType cache_type) {
    for (const auto& profile : profiles_) {
    for (const auto& profile : profiles_) {
        profile->DropResourceCaching();
        profile->DropResourceCaching(cache_type);
    }
    }
}
}


@@ -407,33 +487,33 @@ bool TaskProfile::ExecuteForTask(int tid) const {
    return true;
    return true;
}
}


void TaskProfile::EnableResourceCaching() {
void TaskProfile::EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) {
    if (res_cached_) {
    if (res_cached_) {
        return;
        return;
    }
    }


    for (auto& element : elements_) {
    for (auto& element : elements_) {
        element->EnableResourceCaching();
        element->EnableResourceCaching(cache_type);
    }
    }


    res_cached_ = true;
    res_cached_ = true;
}
}


void TaskProfile::DropResourceCaching() {
void TaskProfile::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) {
    if (!res_cached_) {
    if (!res_cached_) {
        return;
        return;
    }
    }


    for (auto& element : elements_) {
    for (auto& element : elements_) {
        element->DropResourceCaching();
        element->DropResourceCaching(cache_type);
    }
    }


    res_cached_ = false;
    res_cached_ = false;
}
}


void TaskProfiles::DropResourceCaching() const {
void TaskProfiles::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const {
    for (auto& iter : profiles_) {
    for (auto& iter : profiles_) {
        iter.second->DropResourceCaching();
        iter.second->DropResourceCaching(cache_type);
    }
    }
}
}


@@ -457,8 +537,7 @@ TaskProfiles::TaskProfiles() {
                android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level);
                android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level);
        if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) {
        if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) {
            if (!Load(CgroupMap::GetInstance(), api_profiles_path)) {
            if (!Load(CgroupMap::GetInstance(), api_profiles_path)) {
                LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid()
                LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() << "] failed";
                           << "] failed";
            }
            }
        }
        }
    }
    }
@@ -651,10 +730,13 @@ const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) cons
}
}


bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
                                      const std::vector<std::string>& profiles) {
                                      const std::vector<std::string>& profiles, bool use_fd_cache) {
    for (const auto& name : profiles) {
    for (const auto& name : profiles) {
        TaskProfile* profile = GetProfile(name);
        TaskProfile* profile = GetProfile(name);
        if (profile != nullptr) {
        if (profile != nullptr) {
            if (use_fd_cache) {
                profile->EnableResourceCaching(ProfileAction::RCT_PROCESS);
            }
            if (!profile->ExecuteForProcess(uid, pid)) {
            if (!profile->ExecuteForProcess(uid, pid)) {
                PLOG(WARNING) << "Failed to apply " << name << " process profile";
                PLOG(WARNING) << "Failed to apply " << name << " process profile";
            }
            }
@@ -671,7 +753,7 @@ bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& prof
        TaskProfile* profile = GetProfile(name);
        TaskProfile* profile = GetProfile(name);
        if (profile != nullptr) {
        if (profile != nullptr) {
            if (use_fd_cache) {
            if (use_fd_cache) {
                profile->EnableResourceCaching();
                profile->EnableResourceCaching(ProfileAction::RCT_TASK);
            }
            }
            if (!profile->ExecuteForTask(tid)) {
            if (!profile->ExecuteForTask(tid)) {
                PLOG(WARNING) << "Failed to apply " << name << " task profile";
                PLOG(WARNING) << "Failed to apply " << name << " task profile";
+26 −40
Original line number Original line Diff line number Diff line
@@ -45,14 +45,19 @@ class ProfileAttribute {
// Abstract profile element
// Abstract profile element
class ProfileAction {
class ProfileAction {
  public:
  public:
    enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };

    virtual ~ProfileAction() {}
    virtual ~ProfileAction() {}


    // Default implementations will fail
    // Default implementations will fail
    virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
    virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
    virtual bool ExecuteForTask(int) const { return false; };
    virtual bool ExecuteForTask(int) const { return false; };


    virtual void EnableResourceCaching() {}
    virtual void EnableResourceCaching(ResourceCacheType) {}
    virtual void DropResourceCaching() {}
    virtual void DropResourceCaching(ResourceCacheType) {}

  protected:
    enum CacheUseResult { SUCCESS, FAIL, UNUSED };
};
};


// Profile actions
// Profile actions
@@ -108,67 +113,47 @@ class SetAttributeAction : public ProfileAction {
    std::string value_;
    std::string value_;
};
};


// Abstract profile element for cached fd
class CachedFdProfileAction : public ProfileAction {
  public:
    virtual void EnableResourceCaching();
    virtual void DropResourceCaching();

  protected:
    enum FdState {
        FDS_INACCESSIBLE = -1,
        FDS_APP_DEPENDENT = -2,
        FDS_NOT_CACHED = -3,
    };

    android::base::unique_fd fd_;
    mutable std::mutex fd_mutex_;

    static bool IsAppDependentPath(const std::string& path);

    void InitFd(const std::string& path);
    bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; }

    virtual const std::string GetPath() const = 0;
};

// Set cgroup profile element
// Set cgroup profile element
class SetCgroupAction : public CachedFdProfileAction {
class SetCgroupAction : public ProfileAction {
  public:
  public:
    SetCgroupAction(const CgroupController& c, const std::string& p);
    SetCgroupAction(const CgroupController& c, const std::string& p);


    virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    virtual bool ExecuteForTask(int tid) const;
    virtual bool ExecuteForTask(int tid) const;
    virtual void EnableResourceCaching(ResourceCacheType cache_type);
    virtual void DropResourceCaching(ResourceCacheType cache_type);


    const CgroupController* controller() const { return &controller_; }
    const CgroupController* controller() const { return &controller_; }


  protected:
    const std::string GetPath() const override { return controller_.GetTasksFilePath(path_); }

  private:
  private:
    CgroupController controller_;
    CgroupController controller_;
    std::string path_;
    std::string path_;
    android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
    mutable std::mutex fd_mutex_;


    static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
    static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
    CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
};
};


// Write to file action
// Write to file action
class WriteFileAction : public CachedFdProfileAction {
class WriteFileAction : public ProfileAction {
  public:
  public:
    WriteFileAction(const std::string& path, const std::string& value, bool logfailures);
    WriteFileAction(const std::string& path, const std::string& value, bool logfailures);


    virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    virtual bool ExecuteForTask(int tid) const;
    virtual bool ExecuteForTask(int tid) const;

    virtual void EnableResourceCaching(ResourceCacheType cache_type);
  protected:
    virtual void DropResourceCaching(ResourceCacheType cache_type);
    const std::string GetPath() const override { return path_; }


  private:
  private:
    std::string path_, value_;
    std::string path_, value_;
    bool logfailures_;
    bool logfailures_;
    android::base::unique_fd fd_;
    mutable std::mutex fd_mutex_;


    static bool WriteValueToFile(const std::string& value, const std::string& path,
    static bool WriteValueToFile(const std::string& value, const std::string& path,
                                 bool logfailures);
                                 bool logfailures);
    CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
};
};


class TaskProfile {
class TaskProfile {
@@ -180,8 +165,8 @@ class TaskProfile {


    bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    bool ExecuteForTask(int tid) const;
    bool ExecuteForTask(int tid) const;
    void EnableResourceCaching();
    void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
    void DropResourceCaching();
    void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);


  private:
  private:
    bool res_cached_;
    bool res_cached_;
@@ -196,8 +181,8 @@ class ApplyProfileAction : public ProfileAction {


    virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
    virtual bool ExecuteForTask(int tid) const;
    virtual bool ExecuteForTask(int tid) const;
    virtual void EnableResourceCaching();
    virtual void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
    virtual void DropResourceCaching();
    virtual void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);


  private:
  private:
    std::vector<std::shared_ptr<TaskProfile>> profiles_;
    std::vector<std::shared_ptr<TaskProfile>> profiles_;
@@ -210,8 +195,9 @@ class TaskProfiles {


    TaskProfile* GetProfile(const std::string& name) const;
    TaskProfile* GetProfile(const std::string& name) const;
    const ProfileAttribute* GetAttribute(const std::string& name) const;
    const ProfileAttribute* GetAttribute(const std::string& name) const;
    void DropResourceCaching() const;
    void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
    bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles);
    bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
                            bool use_fd_cache);
    bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
    bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);


  private:
  private:
+52 −0

File changed.

Preview size limit exceeded, changes collapsed.