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 | 53e0deb | 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 | 53e0deb | 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 | 53e0deb | 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 | |
| 70 | std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const { |
| 71 | std::string tasks_path = path(); |
| 72 | |
| 73 | if (!rel_path.empty()) { |
| 74 | tasks_path += "/" + rel_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 75 | } |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 76 | 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] | 77 | } |
| 78 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 79 | std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_t uid, |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 80 | pid_t pid) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 81 | std::string proc_path(path()); |
| 82 | proc_path.append("/").append(rel_path); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 83 | proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid)); |
| 84 | proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid)); |
| 85 | |
| 86 | return proc_path.append(CGROUP_PROCS_FILE); |
| 87 | } |
| 88 | |
| 89 | bool CgroupController::GetTaskGroup(int tid, std::string* group) const { |
| 90 | std::string file_name = StringPrintf("/proc/%d/cgroup", tid); |
| 91 | std::string content; |
| 92 | if (!android::base::ReadFileToString(file_name, &content)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 93 | PLOG(ERROR) << "Failed to read " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
| 97 | // if group is null and tid exists return early because |
| 98 | // user is not interested in cgroup membership |
| 99 | if (group == nullptr) { |
| 100 | return true; |
| 101 | } |
| 102 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 103 | std::string cg_tag = StringPrintf(":%s:", name()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 104 | size_t start_pos = content.find(cg_tag); |
| 105 | if (start_pos == std::string::npos) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | start_pos += cg_tag.length() + 1; // skip '/' |
| 110 | size_t end_pos = content.find('\n', start_pos); |
| 111 | if (end_pos == std::string::npos) { |
| 112 | *group = content.substr(start_pos, std::string::npos); |
| 113 | } else { |
| 114 | *group = content.substr(start_pos, end_pos - start_pos); |
| 115 | } |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 120 | CgroupMap::CgroupMap() { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 121 | if (!LoadRcFile()) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 122 | LOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 126 | CgroupMap& CgroupMap::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 127 | // Deliberately leak this object to avoid a race between destruction on |
| 128 | // process exit and concurrent access from another thread. |
| 129 | static auto* instance = new CgroupMap; |
| 130 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool CgroupMap::LoadRcFile() { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 134 | if (!loaded_) { |
| 135 | loaded_ = (ACgroupFile_getVersion() != 0); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 136 | } |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 137 | return loaded_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 140 | void CgroupMap::Print() const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 141 | if (!loaded_) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 142 | LOG(ERROR) << "CgroupMap::Print called for [" << getpid() |
| 143 | << "] failed, RC file was not initialized properly"; |
| 144 | return; |
| 145 | } |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 146 | LOG(INFO) << "File version = " << ACgroupFile_getVersion(); |
| 147 | LOG(INFO) << "File controller count = " << ACgroupFile_getControllerCount(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 148 | |
| 149 | LOG(INFO) << "Mounted cgroups:"; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 150 | |
| 151 | auto controller_count = ACgroupFile_getControllerCount(); |
| 152 | for (uint32_t i = 0; i < controller_count; ++i) { |
| 153 | const ACgroupController* controller = ACgroupFile_getController(i); |
| 154 | LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver " |
| 155 | << ACgroupController_getVersion(controller) << " path " |
| 156 | << ACgroupController_getPath(controller); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 160 | CgroupController CgroupMap::FindController(const std::string& name) const { |
| 161 | if (!loaded_) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 162 | LOG(ERROR) << "CgroupMap::FindController called for [" << getpid() |
| 163 | << "] failed, RC file was not initialized properly"; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 164 | return CgroupController(nullptr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 167 | auto controller_count = ACgroupFile_getControllerCount(); |
| 168 | for (uint32_t i = 0; i < controller_count; ++i) { |
| 169 | const ACgroupController* controller = ACgroupFile_getController(i); |
| 170 | if (name == ACgroupController_getName(controller)) { |
| 171 | return CgroupController(controller); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame^] | 175 | return CgroupController(nullptr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 176 | } |