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> |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 29 | #include <cgroup_map.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 30 | #include <processgroup/processgroup.h> |
T.J. Mercier | 28b37f2 | 2024-06-07 18:34:19 +0000 | [diff] [blame] | 31 | #include <processgroup/util.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 32 | |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 33 | using android::base::StartsWith; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 34 | using android::base::StringPrintf; |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 35 | using android::base::WriteStringToFile; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 36 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 37 | static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs"; |
| 38 | static constexpr const char* CGROUP_TASKS_FILE = "/tasks"; |
Bart Van Assche | 0e8e4f8 | 2023-01-06 14:03:37 -0800 | [diff] [blame] | 39 | static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.threads"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 40 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 41 | uint32_t CgroupControllerWrapper::version() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 42 | CHECK(HasValue()); |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 43 | return ACgroupController_getVersion(controller_); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 44 | } |
| 45 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 46 | const char* CgroupControllerWrapper::name() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 47 | CHECK(HasValue()); |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 48 | return ACgroupController_getName(controller_); |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 49 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 50 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 51 | const char* CgroupControllerWrapper::path() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 52 | CHECK(HasValue()); |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 53 | return ACgroupController_getPath(controller_); |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 54 | } |
| 55 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 56 | bool CgroupControllerWrapper::HasValue() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 57 | return controller_ != nullptr; |
| 58 | } |
| 59 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 60 | bool CgroupControllerWrapper::IsUsable() { |
Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 61 | if (!HasValue()) return false; |
| 62 | |
Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 63 | if (state_ == UNKNOWN) { |
Jiyong Park | ab8a7d2 | 2020-08-11 09:32:55 +0900 | [diff] [blame] | 64 | if (__builtin_available(android 30, *)) { |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 65 | uint32_t flags = ACgroupController_getFlags(controller_); |
Suren Baghdasaryan | cee468f | 2020-01-23 16:18:13 -0800 | [diff] [blame] | 66 | state_ = (flags & CGROUPRC_CONTROLLER_FLAG_MOUNTED) != 0 ? USABLE : MISSING; |
| 67 | } else { |
| 68 | state_ = access(GetProcsFilePath("", 0, 0).c_str(), F_OK) == 0 ? USABLE : MISSING; |
| 69 | } |
Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return state_ == USABLE; |
Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 73 | } |
| 74 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 75 | std::string CgroupControllerWrapper::GetTasksFilePath(const std::string& rel_path) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 76 | std::string tasks_path = path(); |
| 77 | |
| 78 | if (!rel_path.empty()) { |
| 79 | tasks_path += "/" + rel_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 80 | } |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 81 | 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] | 82 | } |
| 83 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 84 | std::string CgroupControllerWrapper::GetProcsFilePath(const std::string& rel_path, uid_t uid, |
| 85 | pid_t pid) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 86 | std::string proc_path(path()); |
| 87 | proc_path.append("/").append(rel_path); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 88 | proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid)); |
| 89 | proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid)); |
| 90 | |
| 91 | return proc_path.append(CGROUP_PROCS_FILE); |
| 92 | } |
| 93 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 94 | bool CgroupControllerWrapper::GetTaskGroup(pid_t tid, std::string* group) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 95 | std::string file_name = StringPrintf("/proc/%d/cgroup", tid); |
| 96 | std::string content; |
| 97 | if (!android::base::ReadFileToString(file_name, &content)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 98 | PLOG(ERROR) << "Failed to read " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // if group is null and tid exists return early because |
| 103 | // user is not interested in cgroup membership |
| 104 | if (group == nullptr) { |
| 105 | return true; |
| 106 | } |
| 107 | |
Marco Ballesio | f16f941 | 2020-06-02 15:21:13 -0700 | [diff] [blame] | 108 | std::string cg_tag; |
| 109 | |
| 110 | if (version() == 2) { |
| 111 | cg_tag = "0::"; |
| 112 | } else { |
| 113 | cg_tag = StringPrintf(":%s:", name()); |
| 114 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 115 | size_t start_pos = content.find(cg_tag); |
| 116 | if (start_pos == std::string::npos) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | start_pos += cg_tag.length() + 1; // skip '/' |
| 121 | size_t end_pos = content.find('\n', start_pos); |
| 122 | if (end_pos == std::string::npos) { |
| 123 | *group = content.substr(start_pos, std::string::npos); |
| 124 | } else { |
| 125 | *group = content.substr(start_pos, end_pos - start_pos); |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 131 | CgroupMap::CgroupMap() { |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 132 | if (!LoadRcFile()) { |
| 133 | LOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 137 | CgroupMap& CgroupMap::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 138 | // Deliberately leak this object to avoid a race between destruction on |
| 139 | // process exit and concurrent access from another thread. |
| 140 | static auto* instance = new CgroupMap; |
| 141 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 144 | bool CgroupMap::LoadRcFile() { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 145 | if (!loaded_) { |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 146 | loaded_ = (ACgroupFile_getVersion() != 0); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 147 | } |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 148 | return loaded_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 151 | void CgroupMap::Print() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 152 | if (!loaded_) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 153 | LOG(ERROR) << "CgroupMap::Print called for [" << getpid() |
T.J. Mercier | a09ee8e | 2024-10-08 23:41:27 +0000 | [diff] [blame^] | 154 | << "] failed, cgroups were not initialized properly"; |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 157 | LOG(INFO) << "File version = " << ACgroupFile_getVersion(); |
| 158 | LOG(INFO) << "File controller count = " << ACgroupFile_getControllerCount(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 159 | |
| 160 | LOG(INFO) << "Mounted cgroups:"; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 161 | |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 162 | auto controller_count = ACgroupFile_getControllerCount(); |
| 163 | for (uint32_t i = 0; i < controller_count; ++i) { |
| 164 | const ACgroupController* controller = ACgroupFile_getController(i); |
| 165 | if (__builtin_available(android 30, *)) { |
| 166 | LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver " |
| 167 | << ACgroupController_getVersion(controller) << " path " |
| 168 | << ACgroupController_getPath(controller) << " flags " |
| 169 | << ACgroupController_getFlags(controller); |
| 170 | } else { |
| 171 | LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver " |
| 172 | << ACgroupController_getVersion(controller) << " path " |
| 173 | << ACgroupController_getPath(controller); |
| 174 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 178 | CgroupControllerWrapper CgroupMap::FindController(const std::string& name) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 179 | if (!loaded_) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 180 | LOG(ERROR) << "CgroupMap::FindController called for [" << getpid() |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 181 | << "] failed, RC file was not initialized properly"; |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 182 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 185 | auto controller_count = ACgroupFile_getControllerCount(); |
| 186 | for (uint32_t i = 0; i < controller_count; ++i) { |
| 187 | const ACgroupController* controller = ACgroupFile_getController(i); |
| 188 | if (name == ACgroupController_getName(controller)) { |
| 189 | return CgroupControllerWrapper(controller); |
| 190 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 191 | } |
| 192 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 193 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 194 | } |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 195 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 196 | CgroupControllerWrapper CgroupMap::FindControllerByPath(const std::string& path) const { |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 197 | if (!loaded_) { |
| 198 | LOG(ERROR) << "CgroupMap::FindControllerByPath called for [" << getpid() |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 199 | << "] failed, RC file was not initialized properly"; |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 200 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 203 | auto controller_count = ACgroupFile_getControllerCount(); |
| 204 | for (uint32_t i = 0; i < controller_count; ++i) { |
| 205 | const ACgroupController* controller = ACgroupFile_getController(i); |
| 206 | if (StartsWith(path, ACgroupController_getPath(controller))) { |
| 207 | return CgroupControllerWrapper(controller); |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 211 | return CgroupControllerWrapper(nullptr); |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Priyanka Advani (xWF) | 0fa4925 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 214 | int CgroupMap::ActivateControllers(const std::string& path) const { |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 215 | if (__builtin_available(android 30, *)) { |
| 216 | auto controller_count = ACgroupFile_getControllerCount(); |
| 217 | for (uint32_t i = 0; i < controller_count; ++i) { |
| 218 | const ACgroupController* controller = ACgroupFile_getController(i); |
| 219 | const uint32_t flags = ACgroupController_getFlags(controller); |
| 220 | uint32_t max_activation_depth = UINT32_MAX; |
| 221 | if (__builtin_available(android 36, *)) { |
| 222 | max_activation_depth = ACgroupController_getMaxActivationDepth(controller); |
| 223 | } |
T.J. Mercier | a09ee8e | 2024-10-08 23:41:27 +0000 | [diff] [blame^] | 224 | const int depth = GetCgroupDepth(ACgroupController_getPath(controller), path); |
Priyanka Advani (xWF) | 0fa4925 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 225 | |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 226 | if (flags & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION && depth < max_activation_depth) { |
| 227 | std::string str("+"); |
| 228 | str.append(ACgroupController_getName(controller)); |
| 229 | if (!WriteStringToFile(str, path + "/cgroup.subtree_control")) { |
| 230 | if (flags & CGROUPRC_CONTROLLER_FLAG_OPTIONAL) { |
| 231 | PLOG(WARNING) << "Activation of cgroup controller " << str |
| 232 | << " failed in path " << path; |
| 233 | } else { |
| 234 | return -errno; |
| 235 | } |
Priyanka Advani (xWF) | 0fa4925 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 239 | return 0; |
Priyanka Advani (xWF) | 0fa4925 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 240 | } |
Priyanka Advani (xWF) | 691ad73 | 2024-10-08 18:54:37 +0000 | [diff] [blame] | 241 | return -ENOSYS; |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 242 | } |