blob: 1b5f217c7b929f18865a76c16dca4974f3ab0d6a [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>
21#include <fcntl.h>
22#include <pwd.h>
23#include <sys/mman.h>
24#include <sys/mount.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <time.h>
28#include <unistd.h>
29
30#include <regex>
31
32#include <android-base/file.h>
33#include <android-base/logging.h>
34#include <android-base/properties.h>
35#include <android-base/stringprintf.h>
36#include <android-base/unique_fd.h>
37#include <cgroup_map.h>
38#include <json/reader.h>
39#include <json/value.h>
40#include <processgroup/processgroup.h>
41
42using android::base::GetBoolProperty;
43using android::base::StringPrintf;
44using android::base::unique_fd;
45
46static constexpr const char* CGROUPS_DESC_FILE = "/etc/cgroups.json";
47
48static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs";
49static constexpr const char* CGROUP_TASKS_FILE = "/tasks";
50static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.tasks";
51
52static bool Mkdir(const std::string& path, mode_t mode, const std::string& uid,
53 const std::string& gid) {
54 if (mode == 0) {
55 mode = 0755;
56 }
57
58 if (mkdir(path.c_str(), mode) != 0) {
59 /* chmod in case the directory already exists */
60 if (errno == EEXIST) {
61 if (fchmodat(AT_FDCWD, path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
62 // /acct is a special case when the directory already exists
63 // TODO: check if file mode is already what we want instead of using EROFS
64 if (errno != EROFS) {
65 PLOG(ERROR) << "fchmodat() failed for " << path;
66 return false;
67 }
68 }
69 } else {
70 PLOG(ERROR) << "mkdir() failed for " << path;
71 return false;
72 }
73 }
74
75 passwd* uid_pwd = nullptr;
76 passwd* gid_pwd = nullptr;
77
78 if (!uid.empty()) {
79 uid_pwd = getpwnam(uid.c_str());
80 if (!uid_pwd) {
81 PLOG(ERROR) << "Unable to decode UID for '" << uid << "'";
82 return false;
83 }
84
85 if (!gid.empty()) {
86 gid_pwd = getpwnam(gid.c_str());
87 if (!gid_pwd) {
88 PLOG(ERROR) << "Unable to decode GID for '" << gid << "'";
89 return false;
90 }
91 }
92 }
93
94 if (uid_pwd && lchown(path.c_str(), uid_pwd->pw_uid, gid_pwd ? gid_pwd->pw_uid : -1) < 0) {
95 PLOG(ERROR) << "lchown() failed for " << path;
96 return false;
97 }
98
99 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
100 if (mode & (S_ISUID | S_ISGID)) {
101 if (fchmodat(AT_FDCWD, path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
102 PLOG(ERROR) << "fchmodat() failed for " << path;
103 return false;
104 }
105 }
106
107 return true;
108}
109
110static bool ReadDescriptors(std::map<std::string, CgroupDescriptor>* descriptors) {
111 std::vector<CgroupDescriptor> result;
112 std::string json_doc;
113
114 if (!android::base::ReadFileToString(CGROUPS_DESC_FILE, &json_doc)) {
115 LOG(ERROR) << "Failed to read task profiles from " << CGROUPS_DESC_FILE;
116 return false;
117 }
118
119 Json::Reader reader;
120 Json::Value root;
121 if (!reader.parse(json_doc, root)) {
122 LOG(ERROR) << "Failed to parse cgroups description: " << reader.getFormattedErrorMessages();
123 return false;
124 }
125
126 Json::Value cgroups = root["Cgroups"];
127 for (Json::Value::ArrayIndex i = 0; i < cgroups.size(); ++i) {
128 std::string name = cgroups[i]["Controller"].asString();
129 descriptors->emplace(std::make_pair(
130 name,
131 CgroupDescriptor(1, name, cgroups[i]["Path"].asString(), cgroups[i]["Mode"].asInt(),
132 cgroups[i]["UID"].asString(), cgroups[i]["GID"].asString())));
133 }
134
135 Json::Value cgroups2 = root["Cgroups2"];
136 descriptors->emplace(std::make_pair(
137 CGROUPV2_CONTROLLER_NAME,
138 CgroupDescriptor(2, CGROUPV2_CONTROLLER_NAME, cgroups2["Path"].asString(),
139 cgroups2["Mode"].asInt(), cgroups2["UID"].asString(),
140 cgroups2["GID"].asString())));
141
142 return true;
143}
144
145static bool SetupCgroup(const CgroupDescriptor& descriptor) {
146 const CgroupController* controller = descriptor.controller();
147
148 // mkdir <path> [mode] [owner] [group]
149 if (!Mkdir(controller->path(), descriptor.mode(), descriptor.uid(), descriptor.gid())) {
150 PLOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup";
151 return false;
152 }
153
154 int result;
155 if (controller->version() == 2) {
156 result = mount("none", controller->path(), "cgroup2", MS_NODEV | MS_NOEXEC | MS_NOSUID,
157 nullptr);
158 } else {
159 // Unfortunately historically cpuset controller was mounted using a mount command
160 // different from all other controllers. This results in controller attributes not
161 // to be prepended with controller name. For example this way instead of
162 // /dev/cpuset/cpuset.cpus the attribute becomes /dev/cpuset/cpus which is what
163 // the system currently expects.
164 if (!strcmp(controller->name(), "cpuset")) {
165 // mount cpuset none /dev/cpuset nodev noexec nosuid
166 result = mount("none", controller->path(), controller->name(),
167 MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr);
168 } else {
169 // mount cgroup none <path> nodev noexec nosuid <controller>
170 result = mount("none", controller->path(), "cgroup", MS_NODEV | MS_NOEXEC | MS_NOSUID,
171 controller->name());
172 }
173 }
174
175 if (result < 0) {
176 PLOG(ERROR) << "Failed to mount " << controller->name() << " cgroup";
177 return false;
178 }
179
180 return true;
181}
182
183static bool WriteRcFile(const std::map<std::string, CgroupDescriptor>& descriptors) {
184 std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CgroupMap::CGROUPS_RC_FILE);
185 unique_fd fd(TEMP_FAILURE_RETRY(open(cgroup_rc_path.c_str(),
186 O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
187 S_IRUSR | S_IRGRP | S_IROTH)));
188 if (fd < 0) {
189 PLOG(ERROR) << "open() failed for " << cgroup_rc_path;
190 return false;
191 }
192
193 CgroupFile fl;
194 fl.version_ = CgroupFile::FILE_CURR_VERSION;
195 fl.controller_count_ = descriptors.size();
196 int ret = TEMP_FAILURE_RETRY(write(fd, &fl, sizeof(fl)));
197 if (ret < 0) {
198 PLOG(ERROR) << "write() failed for " << cgroup_rc_path;
199 return false;
200 }
201
202 for (const auto& [name, descriptor] : descriptors) {
203 ret = TEMP_FAILURE_RETRY(write(fd, descriptor.controller(), sizeof(CgroupController)));
204 if (ret < 0) {
205 PLOG(ERROR) << "write() failed for " << cgroup_rc_path;
206 return false;
207 }
208 }
209
210 return true;
211}
212
213CgroupController::CgroupController(uint32_t version, const std::string& name,
214 const std::string& path) {
215 version_ = version;
216 strncpy(name_, name.c_str(), sizeof(name_) - 1);
217 name_[sizeof(name_) - 1] = '\0';
218 strncpy(path_, path.c_str(), sizeof(path_) - 1);
219 path_[sizeof(path_) - 1] = '\0';
220}
221
222std::string CgroupController::GetTasksFilePath(const std::string& path) const {
223 std::string tasks_path = path_;
224
225 if (!path.empty()) {
226 tasks_path += "/" + path;
227 }
228 return (version_ == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
229}
230
231std::string CgroupController::GetProcsFilePath(const std::string& path, uid_t uid,
232 pid_t pid) const {
233 std::string proc_path(path_);
234 proc_path.append("/").append(path);
235 proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid));
236 proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid));
237
238 return proc_path.append(CGROUP_PROCS_FILE);
239}
240
241bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
242 std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
243 std::string content;
244 if (!android::base::ReadFileToString(file_name, &content)) {
245 LOG(ERROR) << "Failed to read " << file_name;
246 return false;
247 }
248
249 // if group is null and tid exists return early because
250 // user is not interested in cgroup membership
251 if (group == nullptr) {
252 return true;
253 }
254
255 std::string cg_tag = StringPrintf(":%s:", name_);
256 size_t start_pos = content.find(cg_tag);
257 if (start_pos == std::string::npos) {
258 return false;
259 }
260
261 start_pos += cg_tag.length() + 1; // skip '/'
262 size_t end_pos = content.find('\n', start_pos);
263 if (end_pos == std::string::npos) {
264 *group = content.substr(start_pos, std::string::npos);
265 } else {
266 *group = content.substr(start_pos, end_pos - start_pos);
267 }
268
269 return true;
270}
271
272CgroupDescriptor::CgroupDescriptor(uint32_t version, const std::string& name,
273 const std::string& path, mode_t mode, const std::string& uid,
274 const std::string& gid)
275 : controller_(version, name, path), mode_(mode), uid_(uid), gid_(gid) {}
276
277CgroupMap::CgroupMap() : cg_file_data_(nullptr), cg_file_size_(0) {
278 if (!LoadRcFile()) {
279 PLOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed";
280 }
281}
282
283CgroupMap::~CgroupMap() {
284 if (cg_file_data_) {
285 munmap(cg_file_data_, cg_file_size_);
286 cg_file_data_ = nullptr;
287 cg_file_size_ = 0;
288 }
289}
290
291CgroupMap& CgroupMap::GetInstance() {
292 static CgroupMap instance;
293 return instance;
294}
295
296bool CgroupMap::LoadRcFile() {
297 struct stat sb;
298
299 if (cg_file_data_) {
300 // Data already initialized
301 return true;
302 }
303
304 std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CGROUPS_RC_FILE);
305 unique_fd fd(TEMP_FAILURE_RETRY(open(cgroup_rc_path.c_str(), O_RDONLY | O_CLOEXEC)));
306 if (fd < 0) {
307 PLOG(ERROR) << "open() failed for " << cgroup_rc_path;
308 return false;
309 }
310
311 if (fstat(fd, &sb) < 0) {
312 PLOG(ERROR) << "fstat() failed for " << cgroup_rc_path;
313 return false;
314 }
315
316 cg_file_size_ = sb.st_size;
317 if (cg_file_size_ < sizeof(CgroupFile)) {
318 PLOG(ERROR) << "Invalid file format " << cgroup_rc_path;
319 return false;
320 }
321
322 cg_file_data_ = (CgroupFile*)mmap(nullptr, cg_file_size_, PROT_READ, MAP_SHARED, fd, 0);
323 if (cg_file_data_ == MAP_FAILED) {
324 PLOG(ERROR) << "Failed to mmap " << cgroup_rc_path;
325 return false;
326 }
327
328 if (cg_file_data_->version_ != CgroupFile::FILE_CURR_VERSION) {
329 PLOG(ERROR) << cgroup_rc_path << " file version mismatch";
330 return false;
331 }
332
333 return true;
334}
335
336void CgroupMap::Print() {
337 LOG(INFO) << "File version = " << cg_file_data_->version_;
338 LOG(INFO) << "File controller count = " << cg_file_data_->controller_count_;
339
340 LOG(INFO) << "Mounted cgroups:";
341 CgroupController* controller = (CgroupController*)(cg_file_data_ + 1);
342 for (int i = 0; i < cg_file_data_->controller_count_; i++, controller++) {
343 LOG(INFO) << "\t" << controller->name() << " ver " << controller->version() << " path "
344 << controller->path();
345 }
346}
347
348bool CgroupMap::SetupCgroups() {
349 std::map<std::string, CgroupDescriptor> descriptors;
350
351 // load cgroups.json file
352 if (!ReadDescriptors(&descriptors)) {
353 PLOG(ERROR) << "Failed to load cgroup description file";
354 return false;
355 }
356
357 // setup cgroups
358 for (const auto& [name, descriptor] : descriptors) {
359 if (!SetupCgroup(descriptor)) {
360 // issue a warning and proceed with the next cgroup
361 // TODO: mark the descriptor as invalid and skip it in WriteRcFile()
362 LOG(WARNING) << "Failed to setup " << name << " cgroup";
363 }
364 }
365
366 // mkdir <CGROUPS_RC_DIR> 0711 system system
367 if (!Mkdir(CGROUPS_RC_DIR, 0711, "system", "system")) {
368 PLOG(ERROR) << "Failed to create directory for <CGROUPS_RC_FILE> file";
369 return false;
370 }
371
372 // Generate <CGROUPS_RC_FILE> file which can be directly mmapped into
373 // process memory. This optimizes performance, memory usage
374 // and limits infrormation shared with unprivileged processes
375 // to the minimum subset of information from cgroups.json
376 if (!WriteRcFile(descriptors)) {
377 LOG(ERROR) << "Failed to write " << CGROUPS_RC_FILE << " file";
378 return false;
379 }
380
381 std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CGROUPS_RC_FILE);
382 // chmod 0644 <cgroup_rc_path>
383 if (fchmodat(AT_FDCWD, cgroup_rc_path.c_str(), 0644, AT_SYMLINK_NOFOLLOW) < 0) {
384 LOG(ERROR) << "fchmodat() failed";
385 return false;
386 }
387
388 return true;
389}
390
391const CgroupController* CgroupMap::FindController(const std::string& name) const {
392 if (!cg_file_data_) {
393 return nullptr;
394 }
395
396 // skip the file header to get to the first controller
397 CgroupController* controller = (CgroupController*)(cg_file_data_ + 1);
398 for (int i = 0; i < cg_file_data_->controller_count_; i++, controller++) {
399 if (name == controller->name()) {
400 return controller;
401 }
402 }
403
404 return nullptr;
405}