blob: ebc05994433585a091993073df2d43d0bbb7d5f8 [file] [log] [blame]
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -08001/*
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 Baghdasaryan82b72a52018-12-21 11:41:50 -080021#include <unistd.h>
22
23#include <regex>
24
25#include <android-base/file.h>
26#include <android-base/logging.h>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080027#include <android-base/stringprintf.h>
Suren Baghdasaryan9e3ace52021-06-16 17:01:19 -070028#include <android-base/strings.h>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080029#include <cgroup_map.h>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080030#include <processgroup/processgroup.h>
31
Suren Baghdasaryan9e3ace52021-06-16 17:01:19 -070032using android::base::StartsWith;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080033using android::base::StringPrintf;
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -070034using android::base::WriteStringToFile;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080035
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080036static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs";
37static constexpr const char* CGROUP_TASKS_FILE = "/tasks";
Bart Van Assche0e8e4f82023-01-06 14:03:37 -080038static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.threads";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080039
Yifan Hong53e0deb2019-03-22 17:01:08 -070040uint32_t CgroupController::version() const {
41 CHECK(HasValue());
42 return ACgroupController_getVersion(controller_);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080043}
44
Yifan Hong53e0deb2019-03-22 17:01:08 -070045const char* CgroupController::name() const {
46 CHECK(HasValue());
47 return ACgroupController_getName(controller_);
48}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080049
Yifan Hong53e0deb2019-03-22 17:01:08 -070050const char* CgroupController::path() const {
51 CHECK(HasValue());
52 return ACgroupController_getPath(controller_);
53}
54
55bool CgroupController::HasValue() const {
56 return controller_ != nullptr;
57}
58
Suren Baghdasaryan25eb1bf2019-06-26 11:08:50 -070059bool CgroupController::IsUsable() {
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -070060 if (!HasValue()) return false;
61
Suren Baghdasaryan25eb1bf2019-06-26 11:08:50 -070062 if (state_ == UNKNOWN) {
Jiyong Parkab8a7d22020-08-11 09:32:55 +090063 if (__builtin_available(android 30, *)) {
Suren Baghdasaryancee468f2020-01-23 16:18:13 -080064 uint32_t flags = ACgroupController_getFlags(controller_);
65 state_ = (flags & CGROUPRC_CONTROLLER_FLAG_MOUNTED) != 0 ? USABLE : MISSING;
66 } else {
67 state_ = access(GetProcsFilePath("", 0, 0).c_str(), F_OK) == 0 ? USABLE : MISSING;
68 }
Suren Baghdasaryan25eb1bf2019-06-26 11:08:50 -070069 }
70
71 return state_ == USABLE;
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -070072}
73
Yifan Hong53e0deb2019-03-22 17:01:08 -070074std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const {
75 std::string tasks_path = path();
76
77 if (!rel_path.empty()) {
78 tasks_path += "/" + rel_path;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080079 }
Yifan Hong53e0deb2019-03-22 17:01:08 -070080 return (version() == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080081}
82
Yifan Hong53e0deb2019-03-22 17:01:08 -070083std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_t uid,
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080084 pid_t pid) const {
Yifan Hong53e0deb2019-03-22 17:01:08 -070085 std::string proc_path(path());
86 proc_path.append("/").append(rel_path);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080087 proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid));
88 proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid));
89
90 return proc_path.append(CGROUP_PROCS_FILE);
91}
92
T.J. Mercier1c007992024-01-25 16:29:54 +000093bool CgroupController::GetTaskGroup(pid_t tid, std::string* group) const {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080094 std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
95 std::string content;
96 if (!android::base::ReadFileToString(file_name, &content)) {
Wei Wangd71d3012019-03-07 11:59:12 -080097 PLOG(ERROR) << "Failed to read " << file_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080098 return false;
99 }
100
101 // if group is null and tid exists return early because
102 // user is not interested in cgroup membership
103 if (group == nullptr) {
104 return true;
105 }
106
Marco Ballesiof16f9412020-06-02 15:21:13 -0700107 std::string cg_tag;
108
109 if (version() == 2) {
110 cg_tag = "0::";
111 } else {
112 cg_tag = StringPrintf(":%s:", name());
113 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800114 size_t start_pos = content.find(cg_tag);
115 if (start_pos == std::string::npos) {
116 return false;
117 }
118
119 start_pos += cg_tag.length() + 1; // skip '/'
120 size_t end_pos = content.find('\n', start_pos);
121 if (end_pos == std::string::npos) {
122 *group = content.substr(start_pos, std::string::npos);
123 } else {
124 *group = content.substr(start_pos, end_pos - start_pos);
125 }
126
127 return true;
128}
129
Yifan Hong53e0deb2019-03-22 17:01:08 -0700130CgroupMap::CgroupMap() {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800131 if (!LoadRcFile()) {
Wei Wangd71d3012019-03-07 11:59:12 -0800132 LOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800133 }
134}
135
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800136CgroupMap& CgroupMap::GetInstance() {
Peter Collingbournedba6d442019-03-20 21:09:46 -0700137 // Deliberately leak this object to avoid a race between destruction on
138 // process exit and concurrent access from another thread.
139 static auto* instance = new CgroupMap;
140 return *instance;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800141}
142
143bool CgroupMap::LoadRcFile() {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700144 if (!loaded_) {
145 loaded_ = (ACgroupFile_getVersion() != 0);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800146 }
Yifan Hong53e0deb2019-03-22 17:01:08 -0700147 return loaded_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800148}
149
Wei Wangd71d3012019-03-07 11:59:12 -0800150void CgroupMap::Print() const {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700151 if (!loaded_) {
Wei Wangd71d3012019-03-07 11:59:12 -0800152 LOG(ERROR) << "CgroupMap::Print called for [" << getpid()
153 << "] failed, RC file was not initialized properly";
154 return;
155 }
Yifan Hong53e0deb2019-03-22 17:01:08 -0700156 LOG(INFO) << "File version = " << ACgroupFile_getVersion();
157 LOG(INFO) << "File controller count = " << ACgroupFile_getControllerCount();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800158
159 LOG(INFO) << "Mounted cgroups:";
Yifan Hong53e0deb2019-03-22 17:01:08 -0700160
161 auto controller_count = ACgroupFile_getControllerCount();
162 for (uint32_t i = 0; i < controller_count; ++i) {
163 const ACgroupController* controller = ACgroupFile_getController(i);
Jiyong Parkab8a7d22020-08-11 09:32:55 +0900164 if (__builtin_available(android 30, *)) {
Suren Baghdasaryancee468f2020-01-23 16:18:13 -0800165 LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
166 << ACgroupController_getVersion(controller) << " path "
167 << ACgroupController_getPath(controller) << " flags "
168 << ACgroupController_getFlags(controller);
169 } else {
170 LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
171 << ACgroupController_getVersion(controller) << " path "
172 << ACgroupController_getPath(controller);
173 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800174 }
175}
176
Yifan Hong53e0deb2019-03-22 17:01:08 -0700177CgroupController CgroupMap::FindController(const std::string& name) const {
178 if (!loaded_) {
Wei Wangd71d3012019-03-07 11:59:12 -0800179 LOG(ERROR) << "CgroupMap::FindController called for [" << getpid()
180 << "] failed, RC file was not initialized properly";
Yifan Hong53e0deb2019-03-22 17:01:08 -0700181 return CgroupController(nullptr);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800182 }
183
Yifan Hong53e0deb2019-03-22 17:01:08 -0700184 auto controller_count = ACgroupFile_getControllerCount();
185 for (uint32_t i = 0; i < controller_count; ++i) {
186 const ACgroupController* controller = ACgroupFile_getController(i);
187 if (name == ACgroupController_getName(controller)) {
188 return CgroupController(controller);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800189 }
190 }
191
Yifan Hong53e0deb2019-03-22 17:01:08 -0700192 return CgroupController(nullptr);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800193}
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700194
Suren Baghdasaryan9e3ace52021-06-16 17:01:19 -0700195CgroupController CgroupMap::FindControllerByPath(const std::string& path) const {
196 if (!loaded_) {
197 LOG(ERROR) << "CgroupMap::FindControllerByPath called for [" << getpid()
198 << "] failed, RC file was not initialized properly";
199 return CgroupController(nullptr);
200 }
201
202 auto controller_count = ACgroupFile_getControllerCount();
203 for (uint32_t i = 0; i < controller_count; ++i) {
204 const ACgroupController* controller = ACgroupFile_getController(i);
205 if (StartsWith(path, ACgroupController_getPath(controller))) {
206 return CgroupController(controller);
207 }
208 }
209
210 return CgroupController(nullptr);
211}
212
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700213int CgroupMap::ActivateControllers(const std::string& path) const {
214 if (__builtin_available(android 30, *)) {
215 auto controller_count = ACgroupFile_getControllerCount();
216 for (uint32_t i = 0; i < controller_count; ++i) {
217 const ACgroupController* controller = ACgroupFile_getController(i);
Bart Van Assche17c84b92023-02-07 15:04:37 -0800218 const uint32_t flags = ACgroupController_getFlags(controller);
219 if (flags & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION) {
Bart Van Assche8923d722022-01-25 23:49:48 +0000220 std::string str("+");
221 str.append(ACgroupController_getName(controller));
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700222 if (!WriteStringToFile(str, path + "/cgroup.subtree_control")) {
Bart Van Asscheb0947e02023-02-07 14:46:15 -0800223 if (flags & CGROUPRC_CONTROLLER_FLAG_OPTIONAL) {
224 PLOG(WARNING) << "Activation of cgroup controller " << str
225 << " failed in path " << path;
226 } else {
227 return -errno;
228 }
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -0700229 }
230 }
231 }
232 return 0;
233 }
234 return -ENOSYS;
235}