blob: 92fcd1e7172b908bf03a44dad69d9edc29c8b302 [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>
Suren Baghdasaryane3ad8882019-02-06 13:25:29 -080022#include <grp.h>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080023#include <pwd.h>
24#include <sys/mman.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
31#include <regex>
32
33#include <android-base/file.h>
34#include <android-base/logging.h>
35#include <android-base/properties.h>
36#include <android-base/stringprintf.h>
37#include <android-base/unique_fd.h>
38#include <cgroup_map.h>
39#include <json/reader.h>
40#include <json/value.h>
41#include <processgroup/processgroup.h>
42
43using android::base::GetBoolProperty;
44using android::base::StringPrintf;
45using android::base::unique_fd;
46
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080047static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs";
48static constexpr const char* CGROUP_TASKS_FILE = "/tasks";
49static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.tasks";
50
Yifan Hong53e0deb2019-03-22 17:01:08 -070051uint32_t CgroupController::version() const {
52 CHECK(HasValue());
53 return ACgroupController_getVersion(controller_);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054}
55
Yifan Hong53e0deb2019-03-22 17:01:08 -070056const char* CgroupController::name() const {
57 CHECK(HasValue());
58 return ACgroupController_getName(controller_);
59}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080060
Yifan Hong53e0deb2019-03-22 17:01:08 -070061const char* CgroupController::path() const {
62 CHECK(HasValue());
63 return ACgroupController_getPath(controller_);
64}
65
66bool CgroupController::HasValue() const {
67 return controller_ != nullptr;
68}
69
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -070070bool CgroupController::IsUsable() const {
71 if (!HasValue()) return false;
72
73 uint32_t flags = ACgroupController_getFlags(controller_);
74 return (flags & CGROUPRC_CONTROLLER_FLAG_MOUNTED) != 0;
75}
76
Yifan Hong53e0deb2019-03-22 17:01:08 -070077std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const {
78 std::string tasks_path = path();
79
80 if (!rel_path.empty()) {
81 tasks_path += "/" + rel_path;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080082 }
Yifan Hong53e0deb2019-03-22 17:01:08 -070083 return (version() == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080084}
85
Yifan Hong53e0deb2019-03-22 17:01:08 -070086std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_t uid,
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080087 pid_t pid) const {
Yifan Hong53e0deb2019-03-22 17:01:08 -070088 std::string proc_path(path());
89 proc_path.append("/").append(rel_path);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080090 proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid));
91 proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid));
92
93 return proc_path.append(CGROUP_PROCS_FILE);
94}
95
96bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
97 std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
98 std::string content;
99 if (!android::base::ReadFileToString(file_name, &content)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800100 PLOG(ERROR) << "Failed to read " << file_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800101 return false;
102 }
103
104 // if group is null and tid exists return early because
105 // user is not interested in cgroup membership
106 if (group == nullptr) {
107 return true;
108 }
109
Yifan Hong53e0deb2019-03-22 17:01:08 -0700110 std::string cg_tag = StringPrintf(":%s:", name());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800111 size_t start_pos = content.find(cg_tag);
112 if (start_pos == std::string::npos) {
113 return false;
114 }
115
116 start_pos += cg_tag.length() + 1; // skip '/'
117 size_t end_pos = content.find('\n', start_pos);
118 if (end_pos == std::string::npos) {
119 *group = content.substr(start_pos, std::string::npos);
120 } else {
121 *group = content.substr(start_pos, end_pos - start_pos);
122 }
123
124 return true;
125}
126
Yifan Hong53e0deb2019-03-22 17:01:08 -0700127CgroupMap::CgroupMap() {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800128 if (!LoadRcFile()) {
Wei Wangd71d3012019-03-07 11:59:12 -0800129 LOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800130 }
131}
132
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800133CgroupMap& CgroupMap::GetInstance() {
Peter Collingbournedba6d442019-03-20 21:09:46 -0700134 // Deliberately leak this object to avoid a race between destruction on
135 // process exit and concurrent access from another thread.
136 static auto* instance = new CgroupMap;
137 return *instance;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800138}
139
140bool CgroupMap::LoadRcFile() {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700141 if (!loaded_) {
142 loaded_ = (ACgroupFile_getVersion() != 0);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800143 }
Yifan Hong53e0deb2019-03-22 17:01:08 -0700144 return loaded_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800145}
146
Wei Wangd71d3012019-03-07 11:59:12 -0800147void CgroupMap::Print() const {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700148 if (!loaded_) {
Wei Wangd71d3012019-03-07 11:59:12 -0800149 LOG(ERROR) << "CgroupMap::Print called for [" << getpid()
150 << "] failed, RC file was not initialized properly";
151 return;
152 }
Yifan Hong53e0deb2019-03-22 17:01:08 -0700153 LOG(INFO) << "File version = " << ACgroupFile_getVersion();
154 LOG(INFO) << "File controller count = " << ACgroupFile_getControllerCount();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800155
156 LOG(INFO) << "Mounted cgroups:";
Yifan Hong53e0deb2019-03-22 17:01:08 -0700157
158 auto controller_count = ACgroupFile_getControllerCount();
159 for (uint32_t i = 0; i < controller_count; ++i) {
160 const ACgroupController* controller = ACgroupFile_getController(i);
161 LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
162 << ACgroupController_getVersion(controller) << " path "
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700163 << ACgroupController_getFlags(controller) << " flags "
Yifan Hong53e0deb2019-03-22 17:01:08 -0700164 << ACgroupController_getPath(controller);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800165 }
166}
167
Yifan Hong53e0deb2019-03-22 17:01:08 -0700168CgroupController CgroupMap::FindController(const std::string& name) const {
169 if (!loaded_) {
Wei Wangd71d3012019-03-07 11:59:12 -0800170 LOG(ERROR) << "CgroupMap::FindController called for [" << getpid()
171 << "] failed, RC file was not initialized properly";
Yifan Hong53e0deb2019-03-22 17:01:08 -0700172 return CgroupController(nullptr);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800173 }
174
Yifan Hong53e0deb2019-03-22 17:01:08 -0700175 auto controller_count = ACgroupFile_getControllerCount();
176 for (uint32_t i = 0; i < controller_count; ++i) {
177 const ACgroupController* controller = ACgroupFile_getController(i);
178 if (name == ACgroupController_getName(controller)) {
179 return CgroupController(controller);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800180 }
181 }
182
Yifan Hong53e0deb2019-03-22 17:01:08 -0700183 return CgroupController(nullptr);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800184}