| 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> | 
|  | 21 | #include <fcntl.h> | 
| Suren Baghdasaryan | e3ad888 | 2019-02-06 13:25:29 -0800 | [diff] [blame] | 22 | #include <grp.h> | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 23 | #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 |  | 
|  | 43 | using android::base::GetBoolProperty; | 
|  | 44 | using android::base::StringPrintf; | 
|  | 45 | using android::base::unique_fd; | 
|  | 46 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 47 | static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs"; | 
|  | 48 | static constexpr const char* CGROUP_TASKS_FILE = "/tasks"; | 
|  | 49 | static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.tasks"; | 
|  | 50 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 51 | uint32_t CgroupController::version() const { | 
|  | 52 | CHECK(HasValue()); | 
|  | 53 | return ACgroupController_getVersion(controller_); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 56 | const char* CgroupController::name() const { | 
|  | 57 | CHECK(HasValue()); | 
|  | 58 | return ACgroupController_getName(controller_); | 
|  | 59 | } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 60 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 61 | const char* CgroupController::path() const { | 
|  | 62 | CHECK(HasValue()); | 
|  | 63 | return ACgroupController_getPath(controller_); | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | bool CgroupController::HasValue() const { | 
|  | 67 | return controller_ != nullptr; | 
|  | 68 | } | 
|  | 69 |  | 
| Suren Baghdasaryan | cb06c2b | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 70 | bool CgroupController::IsUsable() { | 
| Suren Baghdasaryan | fd285d2 | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 71 | if (!HasValue()) return false; | 
|  | 72 |  | 
| Suren Baghdasaryan | cb06c2b | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 73 | if (state_ == UNKNOWN) { | 
| Suren Baghdasaryan | ea696c4 | 2020-01-23 16:18:13 -0800 | [diff] [blame] | 74 | if (ACgroupController_getFlags != nullptr) { | 
|  | 75 | uint32_t flags = ACgroupController_getFlags(controller_); | 
|  | 76 | state_ = (flags & CGROUPRC_CONTROLLER_FLAG_MOUNTED) != 0 ? USABLE : MISSING; | 
|  | 77 | } else { | 
|  | 78 | state_ = access(GetProcsFilePath("", 0, 0).c_str(), F_OK) == 0 ? USABLE : MISSING; | 
|  | 79 | } | 
| Suren Baghdasaryan | cb06c2b | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
|  | 82 | return state_ == USABLE; | 
| Suren Baghdasaryan | fd285d2 | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 85 | std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const { | 
|  | 86 | std::string tasks_path = path(); | 
|  | 87 |  | 
|  | 88 | if (!rel_path.empty()) { | 
|  | 89 | tasks_path += "/" + rel_path; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 90 | } | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 91 | 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] | 92 | } | 
|  | 93 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 94 | std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_t uid, | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 95 | pid_t pid) const { | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 96 | std::string proc_path(path()); | 
|  | 97 | proc_path.append("/").append(rel_path); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 98 | proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid)); | 
|  | 99 | proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid)); | 
|  | 100 |  | 
|  | 101 | return proc_path.append(CGROUP_PROCS_FILE); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | bool CgroupController::GetTaskGroup(int tid, std::string* group) const { | 
|  | 105 | std::string file_name = StringPrintf("/proc/%d/cgroup", tid); | 
|  | 106 | std::string content; | 
|  | 107 | if (!android::base::ReadFileToString(file_name, &content)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 108 | PLOG(ERROR) << "Failed to read " << file_name; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 109 | return false; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | // if group is null and tid exists return early because | 
|  | 113 | // user is not interested in cgroup membership | 
|  | 114 | if (group == nullptr) { | 
|  | 115 | return true; | 
|  | 116 | } | 
|  | 117 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 118 | std::string cg_tag = StringPrintf(":%s:", name()); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 119 | size_t start_pos = content.find(cg_tag); | 
|  | 120 | if (start_pos == std::string::npos) { | 
|  | 121 | return false; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | start_pos += cg_tag.length() + 1;  // skip '/' | 
|  | 125 | size_t end_pos = content.find('\n', start_pos); | 
|  | 126 | if (end_pos == std::string::npos) { | 
|  | 127 | *group = content.substr(start_pos, std::string::npos); | 
|  | 128 | } else { | 
|  | 129 | *group = content.substr(start_pos, end_pos - start_pos); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | return true; | 
|  | 133 | } | 
|  | 134 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 135 | CgroupMap::CgroupMap() { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 136 | if (!LoadRcFile()) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 137 | LOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 141 | CgroupMap& CgroupMap::GetInstance() { | 
| Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 142 | // Deliberately leak this object to avoid a race between destruction on | 
|  | 143 | // process exit and concurrent access from another thread. | 
|  | 144 | static auto* instance = new CgroupMap; | 
|  | 145 | return *instance; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
|  | 148 | bool CgroupMap::LoadRcFile() { | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 149 | if (!loaded_) { | 
|  | 150 | loaded_ = (ACgroupFile_getVersion() != 0); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 151 | } | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 152 | return loaded_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 155 | void CgroupMap::Print() const { | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 156 | if (!loaded_) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 157 | LOG(ERROR) << "CgroupMap::Print called for [" << getpid() | 
|  | 158 | << "] failed, RC file was not initialized properly"; | 
|  | 159 | return; | 
|  | 160 | } | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 161 | LOG(INFO) << "File version = " << ACgroupFile_getVersion(); | 
|  | 162 | LOG(INFO) << "File controller count = " << ACgroupFile_getControllerCount(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 163 |  | 
|  | 164 | LOG(INFO) << "Mounted cgroups:"; | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 165 |  | 
|  | 166 | auto controller_count = ACgroupFile_getControllerCount(); | 
|  | 167 | for (uint32_t i = 0; i < controller_count; ++i) { | 
|  | 168 | const ACgroupController* controller = ACgroupFile_getController(i); | 
| Suren Baghdasaryan | ea696c4 | 2020-01-23 16:18:13 -0800 | [diff] [blame] | 169 | if (ACgroupController_getFlags != nullptr) { | 
|  | 170 | LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver " | 
|  | 171 | << ACgroupController_getVersion(controller) << " path " | 
|  | 172 | << ACgroupController_getPath(controller) << " flags " | 
|  | 173 | << ACgroupController_getFlags(controller); | 
|  | 174 | } else { | 
|  | 175 | LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver " | 
|  | 176 | << ACgroupController_getVersion(controller) << " path " | 
|  | 177 | << ACgroupController_getPath(controller); | 
|  | 178 | } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 179 | } | 
|  | 180 | } | 
|  | 181 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 182 | CgroupController CgroupMap::FindController(const std::string& name) const { | 
|  | 183 | if (!loaded_) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 184 | LOG(ERROR) << "CgroupMap::FindController called for [" << getpid() | 
|  | 185 | << "] failed, RC file was not initialized properly"; | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 186 | return CgroupController(nullptr); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 189 | auto controller_count = ACgroupFile_getControllerCount(); | 
|  | 190 | for (uint32_t i = 0; i < controller_count; ++i) { | 
|  | 191 | const ACgroupController* controller = ACgroupFile_getController(i); | 
|  | 192 | if (name == ACgroupController_getName(controller)) { | 
|  | 193 | return CgroupController(controller); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 194 | } | 
|  | 195 | } | 
|  | 196 |  | 
| Yifan Hong | fad638c | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 197 | return CgroupController(nullptr); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 198 | } |