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

Commit 96b0065e authored by T.J. Mercier's avatar T.J. Mercier Committed by Gerrit Code Review
Browse files

Merge changes I6e95870e,Ibcef6b73,I302ce3c2 into main

* changes:
  libprocessgroup: Use pid_t consistently for TIDs
  libprocessgroup: Use pid_t consistently for PIDs
  libprocessgroup: Use uid_t consistently for UIDs
parents b077fcce 1c007996
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_
    return proc_path.append(CGROUP_PROCS_FILE);
}

bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
bool CgroupController::GetTaskGroup(pid_t tid, std::string* group) const {
    std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
    std::string content;
    if (!android::base::ReadFileToString(file_name, &content)) {
+2 −1
Original line number Diff line number Diff line
@@ -43,7 +43,8 @@ class CgroupController {

    std::string GetTasksFilePath(const std::string& path) const;
    std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const;
    bool GetTaskGroup(int tid, std::string* group) const;
    bool GetTaskGroup(pid_t tid, std::string* group) const;

  private:
    enum ControllerState {
        UNKNOWN = 0,
+14 −13
Original line number Diff line number Diff line
@@ -33,19 +33,20 @@ bool CgroupsAvailable();
bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path);
bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name);
bool CgroupGetAttributePath(const std::string& attr_name, std::string* path);
bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path);
bool CgroupGetAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path);

bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache = false);
bool SetTaskProfiles(pid_t 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);
bool SetUserProfiles(uid_t uid, const std::vector<std::string>& profiles);

__END_DECLS

bool SetTaskProfiles(int tid, std::initializer_list<std::string_view> profiles,
bool SetTaskProfiles(pid_t 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 SetTaskProfiles(pid_t 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
@@ -67,35 +68,35 @@ void DropTaskProfilesResourceCaching();

// Return 0 if all processes were killed and the cgroup was successfully removed.
// Returns -1 in the case of an error occurring or if there are processes still running.
int killProcessGroup(uid_t uid, int initialPid, int signal);
int killProcessGroup(uid_t uid, pid_t initialPid, int signal);

// Returns the same as killProcessGroup(), however it does not retry, which means
// that it only returns 0 in the case that the cgroup exists and it contains no processes.
int killProcessGroupOnce(uid_t uid, int initialPid, int signal);
int killProcessGroupOnce(uid_t uid, pid_t initialPid, int signal);

// Sends the provided signal to all members of a process group, but does not wait for processes to
// exit, or for the cgroup to be removed. Callers should also ensure that killProcessGroup is called
// later to ensure the cgroup is fully removed, otherwise system resources will leak.
// Returns true if no errors are encountered sending signals, otherwise false.
bool sendSignalToProcessGroup(uid_t uid, int initialPid, int signal);
bool sendSignalToProcessGroup(uid_t uid, pid_t initialPid, int signal);

int createProcessGroup(uid_t uid, int initialPid, bool memControl = false);
int createProcessGroup(uid_t uid, pid_t initialPid, bool memControl = false);

// Set various properties of a process group. For these functions to work, the process group must
// have been created by passing memControl=true to createProcessGroup.
bool setProcessGroupSwappiness(uid_t uid, int initialPid, int swappiness);
bool setProcessGroupSoftLimit(uid_t uid, int initialPid, int64_t softLimitInBytes);
bool setProcessGroupLimit(uid_t uid, int initialPid, int64_t limitInBytes);
bool setProcessGroupSwappiness(uid_t uid, pid_t initialPid, int swappiness);
bool setProcessGroupSoftLimit(uid_t uid, pid_t initialPid, int64_t softLimitInBytes);
bool setProcessGroupLimit(uid_t uid, pid_t initialPid, int64_t limitInBytes);

void removeAllEmptyProcessGroups(void);

// Provides the path for an attribute in a specific process group
// Returns false in case of error, true in case of success
bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path);
bool getAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path);

// Check if a profile can be applied without failing.
// Returns true if it can be applied without failing, false otherwise
bool isProfileValidForProcess(const std::string& profile_name, int uid, int pid);
bool isProfileValidForProcess(const std::string& profile_name, uid_t uid, pid_t pid);

#endif // __ANDROID_VNDK__

+19 −18
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
    return StringPrintf("%s/uid_%u", cgroup, uid);
}

static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) {
static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, pid_t pid) {
    return StringPrintf("%s/uid_%u/pid_%d", cgroup, uid, pid);
}

@@ -147,7 +147,7 @@ bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
    return true;
}

bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
bool CgroupGetAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path) {
    const TaskProfiles& tp = TaskProfiles::GetInstance();
    const IProfileAttribute* attr = tp.GetAttribute(attr_name);

@@ -198,17 +198,18 @@ bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::strin
            uid, pid, std::span<const std::string>(profiles), true);
}

bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
bool SetTaskProfiles(pid_t 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) {
bool SetTaskProfiles(pid_t 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) {
bool SetTaskProfiles(pid_t tid, std::span<const std::string_view> profiles, bool use_fd_cache) {
    return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
}

@@ -232,7 +233,7 @@ bool SetUserProfiles(uid_t uid, const std::vector<std::string>& profiles) {
                                                       false);
}

static int RemoveCgroup(const char* cgroup, uid_t uid, int pid) {
static int RemoveCgroup(const char* cgroup, uid_t uid, pid_t pid) {
    auto path = ConvertUidPidToPath(cgroup, uid, pid);
    int ret = TEMP_FAILURE_RETRY(rmdir(path.c_str()));

@@ -370,7 +371,7 @@ err:
    return false;
}

bool sendSignalToProcessGroup(uid_t uid, int initialPid, int signal) {
bool sendSignalToProcessGroup(uid_t uid, pid_t initialPid, int signal) {
    std::set<pid_t> pgids, pids;

    if (CgroupsAvailable()) {
@@ -525,7 +526,7 @@ static populated_status cgroupIsPopulated(int events_fd) {
// implementation of this function. The default retry value was 40 for killing and 400 for cgroup
// removal with 5ms sleeps between each retry.
static int KillProcessGroup(
        uid_t uid, int initialPid, int signal, bool once = false,
        uid_t uid, pid_t initialPid, int signal, bool once = false,
        std::chrono::steady_clock::time_point until = std::chrono::steady_clock::now() + 2200ms) {
    CHECK_GE(uid, 0);
    CHECK_GT(initialPid, 0);
@@ -632,15 +633,15 @@ static int KillProcessGroup(
    return ret;
}

int killProcessGroup(uid_t uid, int initialPid, int signal) {
int killProcessGroup(uid_t uid, pid_t initialPid, int signal) {
    return KillProcessGroup(uid, initialPid, signal);
}

int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
int killProcessGroupOnce(uid_t uid, pid_t initialPid, int signal) {
    return KillProcessGroup(uid, initialPid, signal, true);
}

static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup,
static int createProcessGroupInternal(uid_t uid, pid_t initialPid, std::string cgroup,
                                      bool activate_controllers) {
    auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);

@@ -687,7 +688,7 @@ static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgr
    return ret;
}

int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
int createProcessGroup(uid_t uid, pid_t initialPid, bool memControl) {
    CHECK_GE(uid, 0);
    CHECK_GT(initialPid, 0);

@@ -712,7 +713,7 @@ int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
    return createProcessGroupInternal(uid, initialPid, cgroup, true);
}

static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) {
static bool SetProcessGroupValue(pid_t tid, const std::string& attr_name, int64_t value) {
    if (!isMemoryCgroupSupported()) {
        LOG(ERROR) << "Memcg is not mounted.";
        return false;
@@ -731,23 +732,23 @@ static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t
    return true;
}

bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) {
bool setProcessGroupSwappiness(uid_t, pid_t pid, int swappiness) {
    return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
}

bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) {
bool setProcessGroupSoftLimit(uid_t, pid_t pid, int64_t soft_limit_in_bytes) {
    return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
}

bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) {
bool setProcessGroupLimit(uid_t, pid_t pid, int64_t limit_in_bytes) {
    return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
}

bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
bool getAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path) {
    return CgroupGetAttributePathForTask(attr_name, tid, path);
}

bool isProfileValidForProcess(const std::string& profile_name, int uid, int pid) {
bool isProfileValidForProcess(const std::string& profile_name, uid_t uid, pid_t pid) {
    const TaskProfile* tp = TaskProfiles::GetInstance().GetProfile(profile_name);

    if (tp == nullptr) {
+4 −4
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ static inline SchedPolicy _policy(SchedPolicy p) {

#if defined(__ANDROID__)

int set_cpuset_policy(int tid, SchedPolicy policy) {
int set_cpuset_policy(pid_t tid, SchedPolicy policy) {
    if (tid == 0) {
        tid = GetThreadId();
    }
@@ -64,7 +64,7 @@ int set_cpuset_policy(int tid, SchedPolicy policy) {
    return 0;
}

int set_sched_policy(int tid, SchedPolicy policy) {
int set_sched_policy(pid_t tid, SchedPolicy policy) {
    if (tid == 0) {
        tid = GetThreadId();
    }
@@ -154,7 +154,7 @@ bool schedboost_enabled() {
    return enabled;
}

static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) {
static int getCGroupSubsys(pid_t tid, const char* subsys, std::string& subgroup) {
    auto controller = CgroupMap::GetInstance().FindController(subsys);

    if (!controller.IsUsable()) return -1;
@@ -185,7 +185,7 @@ static int get_sched_policy_from_group(const std::string& group, SchedPolicy* po
    return 0;
}

int get_sched_policy(int tid, SchedPolicy* policy) {
int get_sched_policy(pid_t tid, SchedPolicy* policy) {
    if (tid == 0) {
        tid = GetThreadId();
    }
Loading