2014-06-03 22:24:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google, Inc
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
#define LOG_TAG "libprocessgroup"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dirent.h>
|
2014-12-29 21:24:25 +01:00
|
|
|
#include <errno.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <fcntl.h>
|
2014-11-21 02:05:15 +01:00
|
|
|
#include <inttypes.h>
|
2018-02-23 22:04:40 +01:00
|
|
|
#include <signal.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2017-06-06 04:20:17 +02:00
|
|
|
#include <unistd.h>
|
2016-06-01 23:03:55 +02:00
|
|
|
|
|
|
|
#include <chrono>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <map>
|
2016-02-18 23:52:46 +01:00
|
|
|
#include <memory>
|
2016-06-07 06:19:55 +02:00
|
|
|
#include <mutex>
|
2017-06-06 04:20:17 +02:00
|
|
|
#include <set>
|
2018-02-23 22:04:40 +01:00
|
|
|
#include <string>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <thread>
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2017-07-17 04:38:11 +02:00
|
|
|
#include <android-base/file.h>
|
2016-08-02 22:30:30 +02:00
|
|
|
#include <android-base/logging.h>
|
2018-05-23 21:30:44 +02:00
|
|
|
#include <android-base/properties.h>
|
2018-02-23 22:04:40 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2019-01-25 06:29:55 +01:00
|
|
|
#include <cutils/android_filesystem_config.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <processgroup/processgroup.h>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <task_profiles.h>
|
2016-01-21 01:39:16 +01:00
|
|
|
|
2018-05-23 21:30:44 +02:00
|
|
|
using android::base::GetBoolProperty;
|
2018-02-23 22:04:40 +01:00
|
|
|
using android::base::StartsWith;
|
|
|
|
using android::base::StringPrintf;
|
2017-07-17 04:38:11 +02:00
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2016-01-21 01:39:16 +01:00
|
|
|
#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
|
|
|
|
|
2022-10-20 15:14:39 +02:00
|
|
|
bool CgroupsAvailable() {
|
|
|
|
static bool cgroups_available = access("/proc/cgroups", F_OK) == 0;
|
|
|
|
return cgroups_available;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
|
2019-03-23 01:01:08 +01:00
|
|
|
auto controller = CgroupMap::GetInstance().FindController(cgroup_name);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
if (!controller.HasValue()) {
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
2019-03-23 01:01:08 +01:00
|
|
|
*path = controller.path();
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-04 19:19:45 +01:00
|
|
|
static bool CgroupGetMemcgAppsPath(std::string* path) {
|
|
|
|
CgroupController controller = CgroupMap::GetInstance().FindController("memory");
|
|
|
|
|
|
|
|
if (!controller.HasValue()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
*path = controller.path();
|
|
|
|
if (controller.version() == 1) {
|
|
|
|
*path += "/apps";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-17 02:01:19 +02:00
|
|
|
bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) {
|
|
|
|
auto controller = CgroupMap::GetInstance().FindControllerByPath(path);
|
|
|
|
|
|
|
|
if (!controller.HasValue()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cgroup_name) {
|
|
|
|
*cgroup_name = controller.name();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
|
|
|
|
const TaskProfiles& tp = TaskProfiles::GetInstance();
|
2022-02-03 20:50:16 +01:00
|
|
|
const IProfileAttribute* attr = tp.GetAttribute(attr_name);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
if (attr == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
*path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
|
|
|
|
const TaskProfiles& tp = TaskProfiles::GetInstance();
|
2022-02-03 20:50:16 +01:00
|
|
|
const IProfileAttribute* attr = tp.GetAttribute(attr_name);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
if (attr == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!attr->GetPathForTask(tid, path)) {
|
|
|
|
PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UsePerAppMemcg() {
|
|
|
|
bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
|
|
|
|
return GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
|
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
static bool isMemoryCgroupSupported() {
|
2019-05-09 02:59:55 +02:00
|
|
|
static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable();
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
return memcg_supported;
|
2016-01-21 01:39:16 +01:00
|
|
|
}
|
|
|
|
|
2019-06-18 23:53:53 +02:00
|
|
|
void DropTaskProfilesResourceCaching() {
|
2022-01-21 00:41:28 +01:00
|
|
|
TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_TASK);
|
|
|
|
TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_PROCESS);
|
2019-06-18 23:53:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 02:28:00 +01:00
|
|
|
bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
|
2022-08-02 22:18:12 +02:00
|
|
|
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) {
|
2022-01-21 00:41:28 +01:00
|
|
|
return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
|
2022-08-02 22:18:12 +02:00
|
|
|
return TaskProfiles::GetInstance().SetProcessProfiles(
|
|
|
|
uid, pid, std::span<const std::string>(profiles), true);
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 23:40:41 +01:00
|
|
|
bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
|
2022-08-02 22:18:12 +02:00
|
|
|
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) {
|
2019-09-16 13:07:17 +02:00
|
|
|
return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 05:24:22 +02:00
|
|
|
// C wrapper for SetProcessProfiles.
|
|
|
|
// No need to have this in the header file because this function is specifically for crosvm. Crosvm
|
|
|
|
// which is written in Rust has its own declaration of this foreign function and doesn't rely on the
|
|
|
|
// header. See
|
|
|
|
// 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[]) {
|
2022-08-02 22:18:12 +02:00
|
|
|
std::vector<std::string_view> profiles_;
|
2022-04-20 10:11:42 +02:00
|
|
|
profiles_.reserve(num_profiles);
|
2022-04-01 05:24:22 +02:00
|
|
|
for (size_t i = 0; i < num_profiles; i++) {
|
|
|
|
profiles_.emplace_back(profiles[i]);
|
|
|
|
}
|
2022-08-02 22:18:12 +02:00
|
|
|
return SetProcessProfiles(uid, pid, std::span<const std::string_view>(profiles_));
|
2022-04-01 05:24:22 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 23:25:09 +02:00
|
|
|
bool SetUserProfiles(uid_t uid, const std::vector<std::string>& profiles) {
|
|
|
|
return TaskProfiles::GetInstance().SetUserProfiles(uid, std::span<const std::string>(profiles),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
|
2023-07-27 00:12:44 +02:00
|
|
|
return StringPrintf("%s/uid_%u", cgroup, uid);
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) {
|
2023-07-27 00:12:44 +02:00
|
|
|
return StringPrintf("%s/uid_%u/pid_%d", cgroup, uid, pid);
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid, unsigned int retries) {
|
|
|
|
int ret = 0;
|
2021-02-04 23:09:55 +01:00
|
|
|
auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid);
|
2021-02-11 01:35:44 +01:00
|
|
|
|
|
|
|
while (retries--) {
|
|
|
|
ret = rmdir(uid_pid_path.c_str());
|
2023-10-07 01:11:09 +02:00
|
|
|
if (!ret || errno != EBUSY || !retries) break;
|
2021-02-11 01:35:44 +01:00
|
|
|
std::this_thread::sleep_for(5ms);
|
|
|
|
}
|
|
|
|
|
2023-07-12 20:18:42 +02:00
|
|
|
if (!ret && uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END) {
|
|
|
|
// Isolated UIDs are unlikely to be reused soon after removal,
|
|
|
|
// so free up the kernel resources for the UID level cgroup.
|
|
|
|
const auto uid_path = ConvertUidToPath(cgroup, uid);
|
|
|
|
ret = rmdir(uid_path.c_str());
|
|
|
|
}
|
|
|
|
|
2014-06-03 22:24:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-28 22:24:28 +02:00
|
|
|
static bool RemoveUidProcessGroups(const std::string& uid_path, bool empty_only) {
|
2018-02-23 22:04:40 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir);
|
2019-02-21 20:25:16 +01:00
|
|
|
bool empty = true;
|
2014-06-03 22:24:21 +02:00
|
|
|
if (uid != NULL) {
|
2016-09-28 22:29:54 +02:00
|
|
|
dirent* dir;
|
|
|
|
while ((dir = readdir(uid.get())) != nullptr) {
|
2014-06-03 22:24:21 +02:00
|
|
|
if (dir->d_type != DT_DIR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
if (!StartsWith(dir->d_name, "pid_")) {
|
2014-06-03 22:24:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name);
|
2022-04-28 22:24:28 +02:00
|
|
|
if (empty_only) {
|
|
|
|
struct stat st;
|
|
|
|
auto procs_file = StringPrintf("%s/%s", path.c_str(),
|
|
|
|
PROCESSGROUP_CGROUP_PROCS_FILE);
|
|
|
|
if (stat(procs_file.c_str(), &st) == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to get stats for " << procs_file;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (st.st_size > 0) {
|
|
|
|
// skip non-empty groups
|
|
|
|
LOG(VERBOSE) << "Skipping non-empty group " << path;
|
|
|
|
empty = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-04-21 22:48:49 +02:00
|
|
|
LOG(VERBOSE) << "Removing " << path;
|
2019-02-21 20:25:16 +01:00
|
|
|
if (rmdir(path.c_str()) == -1) {
|
|
|
|
if (errno != EBUSY) {
|
|
|
|
PLOG(WARNING) << "Failed to remove " << path;
|
|
|
|
}
|
|
|
|
empty = false;
|
|
|
|
}
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 20:25:16 +01:00
|
|
|
return empty;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 22:24:28 +02:00
|
|
|
void removeAllProcessGroupsInternal(bool empty_only) {
|
2018-12-21 20:41:50 +01:00
|
|
|
std::vector<std::string> cgroups;
|
2022-02-04 19:19:45 +01:00
|
|
|
std::string path, memcg_apps_path;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
if (CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &path)) {
|
2018-12-21 20:41:50 +01:00
|
|
|
cgroups.push_back(path);
|
|
|
|
}
|
2022-02-04 19:19:45 +01:00
|
|
|
if (CgroupGetMemcgAppsPath(&memcg_apps_path) && memcg_apps_path != path) {
|
|
|
|
cgroups.push_back(memcg_apps_path);
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::string cgroup_root_path : cgroups) {
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir);
|
2018-10-30 23:49:33 +01:00
|
|
|
if (root == NULL) {
|
2022-02-03 20:24:42 +01:00
|
|
|
PLOG(ERROR) << __func__ << " failed to open " << cgroup_root_path;
|
2018-10-30 23:49:33 +01:00
|
|
|
} else {
|
|
|
|
dirent* dir;
|
|
|
|
while ((dir = readdir(root.get())) != nullptr) {
|
|
|
|
if (dir->d_type != DT_DIR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!StartsWith(dir->d_name, "uid_")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name);
|
2022-04-28 22:24:28 +02:00
|
|
|
if (!RemoveUidProcessGroups(path, empty_only)) {
|
2019-02-21 20:25:16 +01:00
|
|
|
LOG(VERBOSE) << "Skip removing " << path;
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-30 23:49:33 +01:00
|
|
|
LOG(VERBOSE) << "Removing " << path;
|
2019-02-21 20:25:16 +01:00
|
|
|
if (rmdir(path.c_str()) == -1 && errno != EBUSY) {
|
|
|
|
PLOG(WARNING) << "Failed to remove " << path;
|
|
|
|
}
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 22:24:28 +02:00
|
|
|
void removeAllProcessGroups() {
|
|
|
|
LOG(VERBOSE) << "removeAllProcessGroups()";
|
|
|
|
removeAllProcessGroupsInternal(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeAllEmptyProcessGroups() {
|
|
|
|
LOG(VERBOSE) << "removeAllEmptyProcessGroups()";
|
|
|
|
removeAllProcessGroupsInternal(true);
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
/**
|
|
|
|
* Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by
|
|
|
|
* the user root. Ownership for the newly created cgroup and all of its files must thus be
|
|
|
|
* transferred for the user/group passed as uid/gid before system_server can properly access them.
|
|
|
|
*/
|
2018-12-21 20:41:50 +01:00
|
|
|
static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
|
2021-07-07 19:59:59 +02:00
|
|
|
if (mkdir(path.c_str(), mode) == -1) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
// Directory already exists and permissions have been set at the time it was created
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
|
|
|
|
|
|
|
|
if (dir == NULL) {
|
|
|
|
PLOG(ERROR) << "opendir failed for " << path;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent* dir_entry;
|
|
|
|
while ((dir_entry = readdir(dir.get()))) {
|
|
|
|
if (!strcmp("..", dir_entry->d_name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_path = path + "/" + dir_entry->d_name;
|
|
|
|
|
|
|
|
if (lchown(file_path.c_str(), uid, gid) < 0) {
|
|
|
|
PLOG(ERROR) << "lchown failed for " << file_path;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
|
|
|
|
PLOG(ERROR) << "fchmodat failed for " << file_path;
|
|
|
|
goto err;
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-02-11 01:35:44 +01:00
|
|
|
err:
|
|
|
|
int saved_errno = errno;
|
|
|
|
rmdir(path.c_str());
|
|
|
|
errno = saved_errno;
|
|
|
|
|
|
|
|
return false;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
2017-04-21 22:48:49 +02:00
|
|
|
// Returns number of processes killed on success
|
|
|
|
// Returns 0 if there are no processes in the process cgroup left to kill
|
2018-02-23 22:04:40 +01:00
|
|
|
// Returns -1 on error
|
2018-10-30 23:49:33 +01:00
|
|
|
static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) {
|
2017-06-06 04:20:17 +02:00
|
|
|
// We separate all of the pids in the cgroup into those pids that are also the leaders of
|
|
|
|
// process groups (stored in the pgids set) and those that are not (stored in the pids set).
|
|
|
|
std::set<pid_t> pgids;
|
|
|
|
pgids.emplace(initialPid);
|
|
|
|
std::set<pid_t> pids;
|
2023-03-02 23:12:49 +01:00
|
|
|
int processes = 0;
|
2017-06-06 04:20:17 +02:00
|
|
|
|
2022-11-17 15:42:12 +01:00
|
|
|
std::unique_ptr<FILE, decltype(&fclose)> fd(nullptr, fclose);
|
|
|
|
|
|
|
|
if (CgroupsAvailable()) {
|
|
|
|
auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE;
|
|
|
|
fd.reset(fopen(path.c_str(), "re"));
|
|
|
|
if (!fd) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
// This happens when process is already dead
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PLOG(WARNING) << __func__ << " failed to open process cgroup uid " << uid << " pid "
|
|
|
|
<< initialPid;
|
|
|
|
return -1;
|
2014-10-14 02:45:22 +02:00
|
|
|
}
|
2022-11-17 15:42:12 +01:00
|
|
|
pid_t pid;
|
|
|
|
bool file_is_empty = true;
|
|
|
|
while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) {
|
2023-03-02 23:12:49 +01:00
|
|
|
processes++;
|
2022-11-17 15:42:12 +01:00
|
|
|
file_is_empty = false;
|
|
|
|
if (pid == 0) {
|
|
|
|
// Should never happen... but if it does, trying to kill this
|
|
|
|
// will boomerang right back and kill us! Let's not let that happen.
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "Yikes, we've been told to kill pid 0! How about we don't do that?";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pid_t pgid = getpgid(pid);
|
|
|
|
if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
|
|
|
|
if (pgid == pid) {
|
|
|
|
pgids.emplace(pid);
|
|
|
|
} else {
|
|
|
|
pids.emplace(pid);
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 06:58:19 +01:00
|
|
|
if (!file_is_empty) {
|
|
|
|
// Erase all pids that will be killed when we kill the process groups.
|
|
|
|
for (auto it = pids.begin(); it != pids.end();) {
|
|
|
|
pid_t pgid = getpgid(*it);
|
|
|
|
if (pgids.count(pgid) == 1) {
|
|
|
|
it = pids.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
2022-11-17 15:42:12 +01:00
|
|
|
}
|
2017-06-06 04:20:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill all process groups.
|
|
|
|
for (const auto pgid : pgids) {
|
|
|
|
LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
|
|
|
|
<< " as part of process cgroup " << initialPid;
|
|
|
|
|
2023-03-02 23:12:49 +01:00
|
|
|
if (kill(-pgid, signal) == -1 && errno != ESRCH) {
|
2017-06-06 04:20:17 +02:00
|
|
|
PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill remaining pids.
|
|
|
|
for (const auto pid : pids) {
|
2017-04-21 22:48:49 +02:00
|
|
|
LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
|
|
|
|
<< initialPid;
|
2017-06-06 04:20:17 +02:00
|
|
|
|
2023-03-02 23:12:49 +01:00
|
|
|
if (kill(pid, signal) == -1 && errno != ESRCH) {
|
2016-08-02 22:30:30 +02:00
|
|
|
PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 15:42:12 +01:00
|
|
|
return (!fd || feof(fd.get())) ? processes : -1;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 22:46:21 +02:00
|
|
|
static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries) {
|
2023-03-22 21:21:03 +01:00
|
|
|
CHECK_GE(uid, 0);
|
|
|
|
CHECK_GT(initialPid, 0);
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
std::string hierarchy_root_path;
|
2022-11-17 15:42:12 +01:00
|
|
|
if (CgroupsAvailable()) {
|
|
|
|
CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
|
|
|
|
}
|
2021-02-11 01:35:44 +01:00
|
|
|
const char* cgroup = hierarchy_root_path.c_str();
|
2018-10-30 23:49:33 +01:00
|
|
|
|
2016-06-01 23:03:55 +02:00
|
|
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2017-04-21 22:48:49 +02:00
|
|
|
int retry = retries;
|
2016-06-01 23:03:55 +02:00
|
|
|
int processes;
|
2018-10-30 23:49:33 +01:00
|
|
|
while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) {
|
2017-04-21 22:48:49 +02:00
|
|
|
LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
|
2022-12-14 03:12:48 +01:00
|
|
|
if (!CgroupsAvailable()) {
|
|
|
|
// makes no sense to retry, because there are no cgroup_procs file
|
|
|
|
processes = 0; // no remaining processes
|
|
|
|
break;
|
|
|
|
}
|
2015-06-16 22:51:14 +02:00
|
|
|
if (retry > 0) {
|
2016-11-15 02:08:47 +01:00
|
|
|
std::this_thread::sleep_for(5ms);
|
2015-06-16 22:51:14 +02:00
|
|
|
--retry;
|
2014-06-03 22:24:21 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:48:49 +02:00
|
|
|
if (processes < 0) {
|
|
|
|
PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
|
|
|
|
<< initialPid;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 23:03:55 +02:00
|
|
|
|
2017-04-21 22:48:49 +02:00
|
|
|
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
|
2016-08-02 22:30:30 +02:00
|
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
2017-04-21 22:48:49 +02:00
|
|
|
|
|
|
|
// We only calculate the number of 'processes' when killing the processes.
|
|
|
|
// In the retries == 0 case, we only kill the processes once and therefore
|
|
|
|
// will not have waited then recalculated how many processes are remaining
|
|
|
|
// after the first signals have been sent.
|
|
|
|
// Logging anything regarding the number of 'processes' here does not make sense.
|
2014-10-14 02:45:22 +02:00
|
|
|
|
2014-06-03 22:24:21 +02:00
|
|
|
if (processes == 0) {
|
2017-04-21 22:48:49 +02:00
|
|
|
if (retries > 0) {
|
|
|
|
LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
|
|
|
|
<< " in " << static_cast<int>(ms) << "ms";
|
|
|
|
}
|
2021-02-11 01:35:44 +01:00
|
|
|
|
2022-11-17 15:42:12 +01:00
|
|
|
if (!CgroupsAvailable()) {
|
|
|
|
// nothing to do here, if cgroups isn't available
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-27 02:40:25 +02:00
|
|
|
// 400 retries correspond to 2 secs max timeout
|
|
|
|
int err = RemoveProcessGroup(cgroup, uid, initialPid, 400);
|
2021-02-11 01:35:44 +01:00
|
|
|
|
|
|
|
if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
|
2022-02-04 19:19:45 +01:00
|
|
|
std::string memcg_apps_path;
|
|
|
|
if (CgroupGetMemcgAppsPath(&memcg_apps_path) &&
|
2022-05-27 02:40:25 +02:00
|
|
|
RemoveProcessGroup(memcg_apps_path.c_str(), uid, initialPid, 400) < 0) {
|
2022-02-04 19:19:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2021-02-11 01:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2014-06-03 22:24:21 +02:00
|
|
|
} else {
|
2017-04-21 22:48:49 +02:00
|
|
|
if (retries > 0) {
|
|
|
|
LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
|
|
|
|
<< " in " << static_cast<int>(ms) << "ms, " << processes
|
|
|
|
<< " processes remain";
|
|
|
|
}
|
2014-06-03 22:24:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 22:46:21 +02:00
|
|
|
int killProcessGroup(uid_t uid, int initialPid, int signal) {
|
|
|
|
return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/);
|
2017-03-29 02:25:24 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 22:46:21 +02:00
|
|
|
int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
|
|
|
|
return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/);
|
2017-03-29 02:25:24 +02:00
|
|
|
}
|
|
|
|
|
libprocessgroup: Add sendSignalToProcessGroup
Add a function which sends signals to all members of a process group,
but does not wait for the processes to exit, or for the associated
cgroup to be removed.
Bug: 274646058
Ignore-AOSP-First: Dependency of ActivityManager change which developed on interal git_master
Test: Force-stop of chrome with 15 tabs completes ~500ms faster
Test: Full Play store update causes no ANR
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:d87b6018d25cbbd33b345dc58c634718bf5d0def)
Merged-In: I37dbdecb3394101abbee8495e71f6912b3c031f5
Change-Id: I37dbdecb3394101abbee8495e71f6912b3c031f5
NOTE FOR REVIEWERS - original patch and result patch are not identical.
PLEASE REVIEW CAREFULLY.
Diffs between the patches:
37,6 +537,15 @@
return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/, max_processes);
}
+int sendSignalToProcessGroup(uid_t uid, int initialPid, int signal) {
+ std::string hierarchy_root_path;
+ if (CgroupsAvailable()) {
+ CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
+ }
+ const char* cgroup = hierarchy_root_path.c_str();
+ return DoKillProcessGroupOnce(cgroup, uid, initialPid, signal);
+}
+
static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup,
bool activate_controllers) {
auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
Original patch:
From d87b6018d25cbbd33b345dc58c634718bf5d0def Mon Sep 17 00:00:00 2001
From: T.J. Mercier <tjmercier@google.com>
Date: Tue, 04 Apr 2023 18:41:13 +0000
Subject: [PATCH] libprocessgroup: Add sendSignalToProcessGroup
Add a function which sends signals to all members of a process group,
but does not wait for the processes to exit, or for the associated
cgroup to be removed.
Bug: 274646058
Ignore-AOSP-First: Dependency of ActivityManager change which developed on interal git_master
Test: Force-stop of chrome with 15 tabs completes ~500ms faster
Test: Full Play store update causes no ANR
Change-Id: I37dbdecb3394101abbee8495e71f6912b3c031f5
---
diff --git a/libprocessgroup/include/processgroup/processgroup.h b/libprocessgroup/include/processgroup/processgroup.h
index 8fa9fd5..48bc0b7 100644
--- a/libprocessgroup/include/processgroup/processgroup.h
+++ b/libprocessgroup/include/processgroup/processgroup.h
@@ -76,6 +76,11 @@
// 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* max_processes = nullptr);
+// 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 may leak.
+int sendSignalToProcessGroup(uid_t uid, int initialPid, int signal);
+
int createProcessGroup(uid_t uid, int initialPid, bool memControl = false);
// Set various properties of a process group. For these functions to work, the process group must
Change-Id: Ie479348dee8e8092b1959927a1143009632d3914
2023-04-13 23:48:55 +02:00
|
|
|
int sendSignalToProcessGroup(uid_t uid, int initialPid, int signal) {
|
|
|
|
std::string hierarchy_root_path;
|
|
|
|
if (CgroupsAvailable()) {
|
|
|
|
CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
|
|
|
|
}
|
|
|
|
const char* cgroup = hierarchy_root_path.c_str();
|
|
|
|
return DoKillProcessGroupOnce(cgroup, uid, initialPid, signal);
|
|
|
|
}
|
|
|
|
|
2021-07-30 22:42:39 +02:00
|
|
|
static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup,
|
|
|
|
bool activate_controllers) {
|
2018-12-21 20:41:50 +01:00
|
|
|
auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2021-02-11 23:44:53 +01:00
|
|
|
struct stat cgroup_stat;
|
|
|
|
mode_t cgroup_mode = 0750;
|
2022-03-30 01:47:08 +02:00
|
|
|
uid_t cgroup_uid = AID_SYSTEM;
|
2022-03-10 22:42:40 +01:00
|
|
|
gid_t cgroup_gid = AID_SYSTEM;
|
2021-07-30 22:42:39 +02:00
|
|
|
int ret = 0;
|
2021-02-11 23:44:53 +01:00
|
|
|
|
2022-03-23 18:13:18 +01:00
|
|
|
if (stat(cgroup.c_str(), &cgroup_stat) < 0) {
|
2021-02-11 23:44:53 +01:00
|
|
|
PLOG(ERROR) << "Failed to get stats for " << cgroup;
|
|
|
|
} else {
|
|
|
|
cgroup_mode = cgroup_stat.st_mode;
|
2022-03-30 01:47:08 +02:00
|
|
|
cgroup_uid = cgroup_stat.st_uid;
|
2021-02-11 23:44:53 +01:00
|
|
|
cgroup_gid = cgroup_stat.st_gid;
|
|
|
|
}
|
|
|
|
|
2022-03-30 01:47:08 +02:00
|
|
|
if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
|
2018-02-23 22:04:40 +01:00
|
|
|
PLOG(ERROR) << "Failed to make and chown " << uid_path;
|
2016-08-02 22:30:30 +02:00
|
|
|
return -errno;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
2021-07-30 22:42:39 +02:00
|
|
|
if (activate_controllers) {
|
|
|
|
ret = CgroupMap::GetInstance().ActivateControllers(uid_path);
|
|
|
|
if (ret) {
|
|
|
|
LOG(ERROR) << "Failed to activate controllers in " << uid_path;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid);
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2022-03-30 01:47:08 +02:00
|
|
|
if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
|
2018-02-23 22:04:40 +01:00
|
|
|
PLOG(ERROR) << "Failed to make and chown " << uid_pid_path;
|
2016-08-02 22:30:30 +02:00
|
|
|
return -errno;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE;
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) {
|
2014-06-03 22:24:21 +02:00
|
|
|
ret = -errno;
|
2018-02-23 22:04:40 +01:00
|
|
|
PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-17 04:38:11 +02:00
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
|
2023-03-22 21:21:03 +01:00
|
|
|
CHECK_GE(uid, 0);
|
|
|
|
CHECK_GT(initialPid, 0);
|
2021-02-11 01:35:44 +01:00
|
|
|
|
|
|
|
if (memControl && !UsePerAppMemcg()) {
|
|
|
|
PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-02-04 19:19:45 +01:00
|
|
|
if (std::string memcg_apps_path;
|
|
|
|
isMemoryCgroupSupported() && UsePerAppMemcg() && CgroupGetMemcgAppsPath(&memcg_apps_path)) {
|
|
|
|
// Note by bvanassche: passing 'false' as fourth argument below implies that the v1
|
|
|
|
// hierarchy is used. It is not clear to me whether the above conditions guarantee that the
|
|
|
|
// v1 hierarchy is used.
|
|
|
|
int ret = createProcessGroupInternal(uid, initialPid, memcg_apps_path, false);
|
2021-02-11 01:35:44 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 21:21:03 +01:00
|
|
|
std::string cgroup;
|
2021-02-11 01:35:44 +01:00
|
|
|
CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup);
|
2021-07-30 22:42:39 +02:00
|
|
|
return createProcessGroupInternal(uid, initialPid, cgroup, true);
|
2021-02-11 01:35:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) {
|
2018-10-30 23:49:33 +01:00
|
|
|
if (!isMemoryCgroupSupported()) {
|
2018-02-23 22:04:40 +01:00
|
|
|
PLOG(ERROR) << "Memcg is not mounted.";
|
2017-07-17 04:38:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string path;
|
|
|
|
if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) {
|
|
|
|
PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'";
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-17 04:38:11 +02:00
|
|
|
|
|
|
|
if (!WriteStringToFile(std::to_string(value), path)) {
|
|
|
|
PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) {
|
|
|
|
return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) {
|
|
|
|
return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) {
|
|
|
|
return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
2021-02-25 01:30:50 +01:00
|
|
|
|
|
|
|
bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
|
|
|
|
return CgroupGetAttributePathForTask(attr_name, tid, path);
|
|
|
|
}
|
2023-04-12 03:24:23 +02:00
|
|
|
|
|
|
|
bool isProfileValidForProcess(const std::string& profile_name, int uid, int pid) {
|
|
|
|
const TaskProfile* tp = TaskProfiles::GetInstance().GetProfile(profile_name);
|
|
|
|
|
|
|
|
if (tp == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tp->IsValidForProcess(uid, pid);
|
|
|
|
}
|