| /* | |
| * Copyright (C) 2024 The Android Open Source Project | |
| * | |
| * 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. | |
| */ | |
| #include <processgroup/util.h> | |
| #include <optional> | |
| #include <set> | |
| #include <string_view> | |
| #include <mntent.h> | |
| #include <android-base/file.h> | |
| #include <android-base/logging.h> | |
| #include <android-base/properties.h> | |
| #include <android-base/strings.h> | |
| #include <json/reader.h> | |
| #include <json/value.h> | |
| #include "../internal.h" | |
| using android::base::GetUintProperty; | |
| using android::base::Split; | |
| using android::base::StrCat; | |
| namespace { | |
| constexpr const char* CGROUPS_DESC_FILE = "/etc/cgroups.json"; | |
| constexpr const char* CGROUPS_DESC_VENDOR_FILE = "/vendor/etc/cgroups.json"; | |
| // This should match the publicly declared value in processgroup.h, | |
| // but we don't want this library to depend on libprocessgroup. | |
| constexpr std::string CGROUPV2_HIERARCHY_NAME_INTERNAL = "cgroup2"; | |
| void MergeCgroupToDescriptors(CgroupDescriptorMap* descriptors, const Json::Value& cgroup, | |
| const std::string& name, const std::string& root_path, | |
| int cgroups_version) { | |
| const std::string cgroup_path = cgroup["Path"].asString(); | |
| std::string path; | |
| if (!root_path.empty()) { | |
| path = root_path; | |
| if (cgroup_path != ".") { | |
| path += "/"; | |
| path += cgroup_path; | |
| } | |
| } else { | |
| path = cgroup_path; | |
| } | |
| uint32_t controller_flags = 0; | |
| if (cgroup["NeedsActivation"].isBool() && cgroup["NeedsActivation"].asBool()) { | |
| controller_flags |= CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION; | |
| } | |
| if (cgroup["Optional"].isBool() && cgroup["Optional"].asBool()) { | |
| controller_flags |= CGROUPRC_CONTROLLER_FLAG_OPTIONAL; | |
| } | |
| CgroupDescriptor descriptor( | |
| cgroups_version, name, path, std::strtoul(cgroup["Mode"].asString().c_str(), 0, 8), | |
| cgroup["UID"].asString(), cgroup["GID"].asString(), controller_flags); | |
| auto iter = descriptors->find(name); | |
| if (iter == descriptors->end()) { | |
| descriptors->emplace(name, descriptor); | |
| } else { | |
| iter->second = descriptor; | |
| } | |
| } | |
| bool ReadDescriptorsFromFile(const std::string& file_name, CgroupDescriptorMap* descriptors) { | |
| std::vector<CgroupDescriptor> result; | |
| std::string json_doc; | |
| if (!android::base::ReadFileToString(file_name, &json_doc)) { | |
| PLOG(ERROR) << "Failed to read task profiles from " << file_name; | |
| return false; | |
| } | |
| Json::CharReaderBuilder builder; | |
| std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); | |
| Json::Value root; | |
| std::string errorMessage; | |
| if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) { | |
| LOG(ERROR) << "Failed to parse cgroups description: " << errorMessage; | |
| return false; | |
| } | |
| if (root.isMember("Cgroups")) { | |
| const Json::Value& cgroups = root["Cgroups"]; | |
| for (Json::Value::ArrayIndex i = 0; i < cgroups.size(); ++i) { | |
| std::string name = cgroups[i]["Controller"].asString(); | |
| MergeCgroupToDescriptors(descriptors, cgroups[i], name, "", 1); | |
| } | |
| } | |
| std::string root_path; | |
| if (root.isMember("Cgroups2")) { | |
| const Json::Value& cgroups2 = root["Cgroups2"]; | |
| root_path = cgroups2["Path"].asString(); | |
| MergeCgroupToDescriptors(descriptors, cgroups2, CGROUPV2_HIERARCHY_NAME_INTERNAL, "", 2); | |
| const Json::Value& childGroups = cgroups2["Controllers"]; | |
| for (Json::Value::ArrayIndex i = 0; i < childGroups.size(); ++i) { | |
| std::string name = childGroups[i]["Controller"].asString(); | |
| MergeCgroupToDescriptors(descriptors, childGroups[i], name, root_path, 2); | |
| } | |
| } | |
| return true; | |
| } | |
| using MountDir = std::string; | |
| using MountOpts = std::string; | |
| struct CgroupMounts { | |
| std::map<MountDir, MountOpts> v1Mounts; | |
| std::set<MountDir> v2Mounts; | |
| }; | |
| static std::optional<CgroupMounts> ReadCgroupMounts() { | |
| FILE* fp = setmntent("/proc/mounts", "r"); | |
| if (fp == nullptr) { | |
| PLOG(ERROR) << "Failed to read mounts"; | |
| return std::nullopt; | |
| } | |
| CgroupMounts mounts; | |
| const std::string_view CGROUP_V1_TYPE = "cgroup"; | |
| const std::string_view CGROUP_V2_TYPE = "cgroup2"; | |
| for (mntent* mentry = getmntent(fp); mentry != nullptr; mentry = getmntent(fp)) { | |
| if (mentry->mnt_type && mentry->mnt_dir) { | |
| if (CGROUP_V1_TYPE == mentry->mnt_type && mentry->mnt_opts) { | |
| mounts.v1Mounts[mentry->mnt_dir] = mentry->mnt_opts; | |
| } else if (CGROUP_V2_TYPE == mentry->mnt_type) { | |
| mounts.v2Mounts.insert(mentry->mnt_dir); | |
| } | |
| } | |
| } | |
| endmntent(fp); | |
| return mounts; | |
| } | |
| } // anonymous namespace | |
| bool ReadDescriptors(CgroupDescriptorMap* descriptors) { | |
| // load system cgroup descriptors | |
| if (!ReadDescriptorsFromFile(CGROUPS_DESC_FILE, descriptors)) { | |
| return false; | |
| } | |
| // load API-level specific system cgroups descriptors if available | |
| unsigned int api_level = GetUintProperty<unsigned int>("ro.product.first_api_level", 0); | |
| if (api_level > 0) { | |
| std::string api_cgroups_path = | |
| android::base::StrCat("/etc/task_profiles/cgroups_", | |
| std::to_string(api_level), ".json"); | |
| if (!access(api_cgroups_path.c_str(), F_OK) || errno != ENOENT) { | |
| if (!ReadDescriptorsFromFile(api_cgroups_path, descriptors)) { | |
| return false; | |
| } | |
| } | |
| } | |
| // load vendor cgroup descriptors if the file exists | |
| if (!access(CGROUPS_DESC_VENDOR_FILE, F_OK) && | |
| !ReadDescriptorsFromFile(CGROUPS_DESC_VENDOR_FILE, descriptors)) { | |
| return false; | |
| } | |
| // check for v1 and v2 mount/usability status | |
| std::optional<CgroupMounts> mounts = ReadCgroupMounts(); | |
| if (!mounts) return false; | |
| for (auto& [name, descriptor] : *descriptors) { | |
| const CgroupController* const controller = descriptor.controller(); | |
| if (controller->version() == 1) { | |
| if (const auto it = mounts->v1Mounts.find(controller->path()); | |
| it != mounts->v1Mounts.end()) { | |
| if (it->second.contains(controller->name())) descriptor.set_mounted(true); | |
| } | |
| } | |
| } | |
| // check for v2 activation status | |
| const auto cgv2_it = descriptors->find(CGROUPV2_HIERARCHY_NAME_INTERNAL); | |
| if (cgv2_it != descriptors->end()) { | |
| const std::string& v2_path = cgv2_it->second.controller()->path(); | |
| if (mounts->v2Mounts.contains(v2_path)) { | |
| cgv2_it->second.set_mounted(true); | |
| std::string subtree_control; | |
| std::set<std::string> active_controllers; | |
| if (android::base::ReadFileToString( | |
| StrCat(v2_path, "/cgroup.subtree_control"), &subtree_control)) { | |
| for (const auto& token : Split(subtree_control, " \t\n\r")) { | |
| if (!token.empty()) { | |
| active_controllers.insert(token); | |
| } | |
| } | |
| } | |
| for (auto& [name, descriptor] : *descriptors) { | |
| const CgroupController* const controller = descriptor.controller(); | |
| if (controller->version() == 2 && name != CGROUPV2_HIERARCHY_NAME_INTERNAL) { | |
| if (!(controller->flags() & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION)) { | |
| descriptor.set_mounted(true); | |
| } else if (active_controllers.contains(controller->name())) { | |
| descriptor.set_mounted(true); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| bool ActivateControllers(const std::string& path, const CgroupDescriptorMap& descriptors) { | |
| for (const auto& [name, descriptor] : descriptors) { | |
| const uint32_t flags = descriptor.controller()->flags(); | |
| if (flags & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION) { | |
| std::string str = StrCat("+", descriptor.controller()->name()); | |
| if (!android::base::WriteStringToFile(str, StrCat(path, "/cgroup.subtree_control"))) { | |
| if (flags & CGROUPRC_CONTROLLER_FLAG_OPTIONAL) { | |
| PLOG(WARNING) << "Activation of cgroup controller " << str | |
| << " failed in path " << path; | |
| } else { | |
| return false; | |
| } | |
| } | |
| } | |
| } | |
| return true; | |
| } | |