Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "libprocessgroup" |
| 19 | |
| 20 | #include <errno.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <regex> |
| 24 | |
| 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 28 | #include <cgroup_map.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 29 | #include <processgroup/processgroup.h> |
T.J. Mercier | 28b37f2 | 2024-06-07 18:34:19 +0000 | [diff] [blame] | 30 | #include <processgroup/util.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 31 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 32 | using android::base::StringPrintf; |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 33 | using android::base::WriteStringToFile; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 34 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 35 | static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs"; |
| 36 | static constexpr const char* CGROUP_TASKS_FILE = "/tasks"; |
Bart Van Assche | 0e8e4f8 | 2023-01-06 14:03:37 -0800 | [diff] [blame] | 37 | static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.threads"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 38 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 39 | uint32_t CgroupControllerWrapper::version() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 40 | CHECK(HasValue()); |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 41 | return controller_->version(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 42 | } |
| 43 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 44 | const char* CgroupControllerWrapper::name() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 45 | CHECK(HasValue()); |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 46 | return controller_->name(); |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 47 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 48 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 49 | const char* CgroupControllerWrapper::path() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 50 | CHECK(HasValue()); |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 51 | return controller_->path(); |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 52 | } |
| 53 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 54 | bool CgroupControllerWrapper::HasValue() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 55 | return controller_ != nullptr; |
| 56 | } |
| 57 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 58 | bool CgroupControllerWrapper::IsUsable() { |
Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 59 | if (!HasValue()) return false; |
| 60 | |
Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 61 | if (state_ == UNKNOWN) { |
Jiyong Park | ab8a7d2 | 2020-08-11 09:32:55 +0900 | [diff] [blame] | 62 | if (__builtin_available(android 30, *)) { |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 63 | uint32_t flags = controller_->flags(); |
Suren Baghdasaryan | cee468f | 2020-01-23 16:18:13 -0800 | [diff] [blame] | 64 | state_ = (flags & CGROUPRC_CONTROLLER_FLAG_MOUNTED) != 0 ? USABLE : MISSING; |
| 65 | } else { |
| 66 | state_ = access(GetProcsFilePath("", 0, 0).c_str(), F_OK) == 0 ? USABLE : MISSING; |
| 67 | } |
Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return state_ == USABLE; |
Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 71 | } |
| 72 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 73 | std::string CgroupControllerWrapper::GetTasksFilePath(const std::string& rel_path) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 74 | std::string tasks_path = path(); |
| 75 | |
| 76 | if (!rel_path.empty()) { |
| 77 | tasks_path += "/" + rel_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 78 | } |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 79 | return (version() == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 80 | } |
| 81 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 82 | std::string CgroupControllerWrapper::GetProcsFilePath(const std::string& rel_path, uid_t uid, |
| 83 | pid_t pid) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 84 | std::string proc_path(path()); |
| 85 | proc_path.append("/").append(rel_path); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 86 | proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid)); |
| 87 | proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid)); |
| 88 | |
| 89 | return proc_path.append(CGROUP_PROCS_FILE); |
| 90 | } |
| 91 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 92 | bool CgroupControllerWrapper::GetTaskGroup(pid_t tid, std::string* group) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 93 | std::string file_name = StringPrintf("/proc/%d/cgroup", tid); |
| 94 | std::string content; |
| 95 | if (!android::base::ReadFileToString(file_name, &content)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 96 | PLOG(ERROR) << "Failed to read " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // if group is null and tid exists return early because |
| 101 | // user is not interested in cgroup membership |
| 102 | if (group == nullptr) { |
| 103 | return true; |
| 104 | } |
| 105 | |
Marco Ballesio | f16f941 | 2020-06-02 15:21:13 -0700 | [diff] [blame] | 106 | std::string cg_tag; |
| 107 | |
| 108 | if (version() == 2) { |
| 109 | cg_tag = "0::"; |
| 110 | } else { |
| 111 | cg_tag = StringPrintf(":%s:", name()); |
| 112 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 113 | size_t start_pos = content.find(cg_tag); |
| 114 | if (start_pos == std::string::npos) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | start_pos += cg_tag.length() + 1; // skip '/' |
| 119 | size_t end_pos = content.find('\n', start_pos); |
| 120 | if (end_pos == std::string::npos) { |
| 121 | *group = content.substr(start_pos, std::string::npos); |
| 122 | } else { |
| 123 | *group = content.substr(start_pos, end_pos - start_pos); |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 129 | CgroupMap::CgroupMap() { |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 130 | if (!LoadDescriptors()) { |
| 131 | LOG(ERROR) << "CgroupMap::LoadDescriptors called for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 135 | CgroupMap& CgroupMap::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 136 | // Deliberately leak this object to avoid a race between destruction on |
| 137 | // process exit and concurrent access from another thread. |
| 138 | static auto* instance = new CgroupMap; |
| 139 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 140 | } |
| 141 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 142 | bool CgroupMap::LoadDescriptors() { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 143 | if (!loaded_) { |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 144 | loaded_ = ReadDescriptors(&descriptors_); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 145 | } |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 146 | return loaded_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 149 | void CgroupMap::Print() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 150 | if (!loaded_) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 151 | LOG(ERROR) << "CgroupMap::Print called for [" << getpid() |
T.J. Mercier | a09ee8e | 2024-10-08 23:41:27 +0000 | [diff] [blame] | 152 | << "] failed, cgroups were not initialized properly"; |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 153 | return; |
| 154 | } |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 155 | LOG(INFO) << "Controller count = " << descriptors_.size(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 156 | |
| 157 | LOG(INFO) << "Mounted cgroups:"; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 158 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 159 | for (const auto& [name, descriptor] : descriptors_) { |
| 160 | LOG(INFO) << "\t" << descriptor.controller()->name() << " ver " |
| 161 | << descriptor.controller()->version() << " path " |
| 162 | << descriptor.controller()->path() << " flags " |
| 163 | << descriptor.controller()->flags(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 167 | CgroupControllerWrapper CgroupMap::FindController(const std::string& name) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 168 | if (!loaded_) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 169 | LOG(ERROR) << "CgroupMap::FindController called for [" << getpid() |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 170 | << "] failed, cgroups were not initialized properly"; |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 171 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 172 | } |
| 173 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 174 | if (const auto it = descriptors_.find(name); it != descriptors_.end()) { |
| 175 | return CgroupControllerWrapper(it->second.controller()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 176 | } |
| 177 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 178 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 179 | } |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 180 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 181 | CgroupControllerWrapper CgroupMap::FindControllerByPath(const std::string& path) const { |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 182 | if (!loaded_) { |
| 183 | LOG(ERROR) << "CgroupMap::FindControllerByPath called for [" << getpid() |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 184 | << "] failed, cgroups were not initialized properly"; |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 185 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 186 | } |
| 187 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 188 | for (const auto& [name, descriptor] : descriptors_) { |
| 189 | if (path.starts_with(descriptor.controller()->path())) { |
| 190 | return CgroupControllerWrapper(descriptor.controller()); |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 194 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 195 | } |
| 196 | |
T.J. Mercier | 787ddbc | 2024-10-08 23:41:34 +0000 | [diff] [blame] | 197 | bool CgroupMap::ActivateControllers(const std::string& path) const { |
| 198 | return ::ActivateControllers(path, descriptors_); |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 199 | } |