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

Commit f32c4ece authored by Bart Van Assche's avatar Bart Van Assche
Browse files

libprocessgroup: Provide SetProcessProfiles() and SetTaskProfiles() alternatives



Provide alternative versions that do not force callers to create
std::string objects. This patch has the intended side-effect that all
callers that pass a {string} initializer list to the 'profiles' argument
now call an std::initializer_list<> overload instead of the const
std::vector<std::string>& overload.

Additionally, add std::function<> arguments instead of calling
ExecuteForProcess() or ExecuteForTask() directly to make it easier to
write unit tests for SetTaskProfiles() and SetProcessProfiles().

Bug: 213617178
Change-Id: Ica61e944a66a17178ee43a113b8ca082f7eb834b
Signed-off-by: default avatarBart Van Assche <bvanassche@google.com>
parent d0b8ce21
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@

#include <sys/cdefs.h>
#include <sys/types.h>
#include <initializer_list>
#include <span>
#include <string>
#include <string_view>
#include <vector>

__BEGIN_DECLS
@@ -33,6 +36,19 @@ bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::s
bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache = false);
bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles);

__END_DECLS

bool SetTaskProfiles(int tid, std::initializer_list<std::string_view> profiles,
                     bool use_fd_cache = false);
bool SetProcessProfiles(uid_t uid, pid_t pid, std::initializer_list<std::string_view> profiles);
#if _LIBCPP_STD_VER > 17
bool SetTaskProfiles(int tid, std::span<const std::string_view> profiles,
                     bool use_fd_cache = false);
bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const std::string_view> profiles);
#endif

__BEGIN_DECLS

#ifndef __ANDROID_VNDK__

bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles);
+24 −3
Original line number Diff line number Diff line
@@ -148,14 +148,35 @@ void DropTaskProfilesResourceCaching() {
}

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

bool SetProcessProfiles(uid_t uid, pid_t pid, std::initializer_list<std::string_view> profiles) {
    return TaskProfiles::GetInstance().SetProcessProfiles(
            uid, pid, std::span<const std::string_view>(profiles), false);
}

bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const std::string_view> 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);
    return TaskProfiles::GetInstance().SetProcessProfiles(
            uid, pid, std::span<const std::string>(profiles), true);
}

bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
    return TaskProfiles::GetInstance().SetTaskProfiles(tid, std::span<const std::string>(profiles),
                                                       use_fd_cache);
}

bool SetTaskProfiles(int tid, std::initializer_list<std::string_view> profiles, bool use_fd_cache) {
    return TaskProfiles::GetInstance().SetTaskProfiles(
            tid, std::span<const std::string_view>(profiles), use_fd_cache);
}

bool SetTaskProfiles(int tid, std::span<const std::string_view> profiles, bool use_fd_cache) {
    return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
}

@@ -166,12 +187,12 @@ bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use
// https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3574427/5/src/linux/android.rs#12
extern "C" bool android_set_process_profiles(uid_t uid, pid_t pid, size_t num_profiles,
                                             const char* profiles[]) {
    std::vector<std::string> profiles_;
    std::vector<std::string_view> profiles_;
    profiles_.reserve(num_profiles);
    for (size_t i = 0; i < num_profiles; i++) {
        profiles_.emplace_back(profiles[i]);
    }
    return SetProcessProfiles(uid, pid, profiles_);
    return SetProcessProfiles(uid, pid, std::span<const std::string_view>(profiles_));
}

static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
+16 −4
Original line number Diff line number Diff line
@@ -804,8 +804,9 @@ const IProfileAttribute* TaskProfiles::GetAttribute(std::string_view name) const
    return nullptr;
}

bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
                                      const std::vector<std::string>& profiles, bool use_fd_cache) {
template <typename T>
bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles,
                                      bool use_fd_cache) {
    bool success = true;
    for (const auto& name : profiles) {
        TaskProfile* profile = GetProfile(name);
@@ -825,8 +826,8 @@ bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
    return success;
}

bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles,
                                   bool use_fd_cache) {
template <typename T>
bool TaskProfiles::SetTaskProfiles(int tid, std::span<const T> profiles, bool use_fd_cache) {
    bool success = true;
    for (const auto& name : profiles) {
        TaskProfile* profile = GetProfile(name);
@@ -845,3 +846,14 @@ bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& prof
    }
    return success;
}

template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
                                               std::span<const std::string> profiles,
                                               bool use_fd_cache);
template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
                                               std::span<const std::string_view> profiles,
                                               bool use_fd_cache);
template bool TaskProfiles::SetTaskProfiles(int tid, std::span<const std::string> profiles,
                                            bool use_fd_cache);
template bool TaskProfiles::SetTaskProfiles(int tid, std::span<const std::string_view> profiles,
                                            bool use_fd_cache);
+6 −3
Original line number Diff line number Diff line
@@ -18,8 +18,10 @@

#include <sys/cdefs.h>
#include <sys/types.h>
#include <functional>
#include <map>
#include <mutex>
#include <span>
#include <string>
#include <string_view>
#include <vector>
@@ -210,9 +212,10 @@ class TaskProfiles {
    TaskProfile* GetProfile(std::string_view name) const;
    const IProfileAttribute* GetAttribute(std::string_view name) const;
    void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
    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);
    template <typename T>
    bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
    template <typename T>
    bool SetTaskProfiles(int tid, std::span<const T> profiles, bool use_fd_cache);

  private:
    TaskProfiles();