Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google, Inc |
| 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 <assert.h> |
| 21 | #include <dirent.h> |
Elliott Hughes | 0badbd6 | 2014-12-29 12:24:25 -0800 | [diff] [blame] | 22 | #include <errno.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Chih-Hung Hsieh | fcc8115 | 2014-11-20 17:05:15 -0800 | [diff] [blame] | 24 | #include <inttypes.h> |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 25 | #include <poll.h> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 26 | #include <signal.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 32 | |
| 33 | #include <chrono> |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 34 | #include <cstring> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 35 | #include <map> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 36 | #include <memory> |
Elliott Hughes | 8d532e4 | 2016-06-06 21:19:55 -0700 | [diff] [blame] | 37 | #include <mutex> |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 38 | #include <set> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 39 | #include <string> |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 40 | #include <thread> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 41 | |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 42 | #include <android-base/file.h> |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 43 | #include <android-base/logging.h> |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 44 | #include <android-base/properties.h> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 45 | #include <android-base/stringprintf.h> |
| 46 | #include <android-base/strings.h> |
Suren Baghdasaryan | 7bf4381 | 2019-01-25 05:29:55 +0000 | [diff] [blame] | 47 | #include <cutils/android_filesystem_config.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 48 | #include <processgroup/processgroup.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 49 | #include <task_profiles.h> |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 50 | |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 51 | using android::base::GetBoolProperty; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 52 | using android::base::StartsWith; |
| 53 | using android::base::StringPrintf; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 54 | using android::base::WriteStringToFile; |
| 55 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 56 | using namespace std::chrono_literals; |
| 57 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 58 | #define PROCESSGROUP_CGROUP_PROCS_FILE "cgroup.procs" |
T.J. Mercier | a103630 | 2023-11-07 14:36:46 +0000 | [diff] [blame] | 59 | #define PROCESSGROUP_CGROUP_KILL_FILE "cgroup.kill" |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 60 | #define PROCESSGROUP_CGROUP_EVENTS_FILE "cgroup.events" |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 61 | |
Nikita Ioffe | c2b1654 | 2022-10-20 14:14:39 +0100 | [diff] [blame] | 62 | bool CgroupsAvailable() { |
| 63 | static bool cgroups_available = access("/proc/cgroups", F_OK) == 0; |
| 64 | return cgroups_available; |
| 65 | } |
| 66 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 67 | bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 68 | auto controller = CgroupMap::GetInstance().FindController(cgroup_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 69 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 70 | if (!controller.HasValue()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (path) { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 75 | *path = controller.path(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
T.J. Mercier | a103630 | 2023-11-07 14:36:46 +0000 | [diff] [blame] | 81 | static std::string ConvertUidToPath(const char* cgroup, uid_t uid) { |
| 82 | return StringPrintf("%s/uid_%u", cgroup, uid); |
| 83 | } |
| 84 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 85 | static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, pid_t pid) { |
T.J. Mercier | a103630 | 2023-11-07 14:36:46 +0000 | [diff] [blame] | 86 | return StringPrintf("%s/uid_%u/pid_%d", cgroup, uid, pid); |
| 87 | } |
| 88 | |
| 89 | static bool CgroupKillAvailable() { |
| 90 | static std::once_flag f; |
| 91 | static bool cgroup_kill_available = false; |
| 92 | std::call_once(f, []() { |
| 93 | std::string cg_kill; |
| 94 | CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &cg_kill); |
| 95 | // cgroup.kill is not on the root cgroup, so check a non-root cgroup that should always |
| 96 | // exist |
| 97 | cg_kill = ConvertUidToPath(cg_kill.c_str(), AID_ROOT) + '/' + PROCESSGROUP_CGROUP_KILL_FILE; |
| 98 | cgroup_kill_available = access(cg_kill.c_str(), F_OK) == 0; |
| 99 | }); |
| 100 | |
| 101 | return cgroup_kill_available; |
| 102 | } |
| 103 | |
Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 104 | static bool CgroupGetMemcgAppsPath(std::string* path) { |
| 105 | CgroupController controller = CgroupMap::GetInstance().FindController("memory"); |
| 106 | |
| 107 | if (!controller.HasValue()) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (path) { |
| 112 | *path = controller.path(); |
| 113 | if (controller.version() == 1) { |
| 114 | *path += "/apps"; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 121 | bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) { |
| 122 | auto controller = CgroupMap::GetInstance().FindControllerByPath(path); |
| 123 | |
| 124 | if (!controller.HasValue()) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (cgroup_name) { |
| 129 | *cgroup_name = controller.name(); |
| 130 | } |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 135 | bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) { |
| 136 | const TaskProfiles& tp = TaskProfiles::GetInstance(); |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 137 | const IProfileAttribute* attr = tp.GetAttribute(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 138 | |
| 139 | if (attr == nullptr) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | if (path) { |
| 144 | *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str()); |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { |
| 151 | const TaskProfiles& tp = TaskProfiles::GetInstance(); |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 152 | const IProfileAttribute* attr = tp.GetAttribute(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 153 | |
| 154 | if (attr == nullptr) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | if (!attr->GetPathForTask(tid, path)) { |
T.J. Mercier | 4d0d285 | 2023-10-27 23:44:03 +0000 | [diff] [blame] | 159 | LOG(ERROR) << "Failed to find cgroup for tid " << tid; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | bool UsePerAppMemcg() { |
| 167 | bool low_ram_device = GetBoolProperty("ro.config.low_ram", false); |
| 168 | return GetBoolProperty("ro.config.per_app_memcg", low_ram_device); |
| 169 | } |
| 170 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 171 | static bool isMemoryCgroupSupported() { |
Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 172 | static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 173 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 174 | return memcg_supported; |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 177 | void DropTaskProfilesResourceCaching() { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 178 | TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_TASK); |
| 179 | TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_PROCESS); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 180 | } |
| 181 | |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 182 | bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) { |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 183 | return TaskProfiles::GetInstance().SetProcessProfiles( |
| 184 | uid, pid, std::span<const std::string>(profiles), false); |
| 185 | } |
| 186 | |
| 187 | bool SetProcessProfiles(uid_t uid, pid_t pid, std::initializer_list<std::string_view> profiles) { |
| 188 | return TaskProfiles::GetInstance().SetProcessProfiles( |
| 189 | uid, pid, std::span<const std::string_view>(profiles), false); |
| 190 | } |
| 191 | |
| 192 | bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const std::string_view> profiles) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 193 | return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false); |
| 194 | } |
| 195 | |
| 196 | bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) { |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 197 | return TaskProfiles::GetInstance().SetProcessProfiles( |
| 198 | uid, pid, std::span<const std::string>(profiles), true); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 201 | bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) { |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 202 | return TaskProfiles::GetInstance().SetTaskProfiles(tid, std::span<const std::string>(profiles), |
| 203 | use_fd_cache); |
| 204 | } |
| 205 | |
| 206 | bool SetTaskProfiles(int tid, std::initializer_list<std::string_view> profiles, bool use_fd_cache) { |
| 207 | return TaskProfiles::GetInstance().SetTaskProfiles( |
| 208 | tid, std::span<const std::string_view>(profiles), use_fd_cache); |
| 209 | } |
| 210 | |
| 211 | bool SetTaskProfiles(int tid, std::span<const std::string_view> profiles, bool use_fd_cache) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 212 | return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Jiyong Park | 8bf5940 | 2022-04-01 12:24:22 +0900 | [diff] [blame] | 215 | // C wrapper for SetProcessProfiles. |
| 216 | // No need to have this in the header file because this function is specifically for crosvm. Crosvm |
| 217 | // which is written in Rust has its own declaration of this foreign function and doesn't rely on the |
| 218 | // header. See |
| 219 | // https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3574427/5/src/linux/android.rs#12 |
| 220 | extern "C" bool android_set_process_profiles(uid_t uid, pid_t pid, size_t num_profiles, |
| 221 | const char* profiles[]) { |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 222 | std::vector<std::string_view> profiles_; |
Jiyong Park | cc9932b | 2022-04-20 17:11:42 +0900 | [diff] [blame] | 223 | profiles_.reserve(num_profiles); |
Jiyong Park | 8bf5940 | 2022-04-01 12:24:22 +0900 | [diff] [blame] | 224 | for (size_t i = 0; i < num_profiles; i++) { |
| 225 | profiles_.emplace_back(profiles[i]); |
| 226 | } |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 227 | return SetProcessProfiles(uid, pid, std::span<const std::string_view>(profiles_)); |
Jiyong Park | 8bf5940 | 2022-04-01 12:24:22 +0900 | [diff] [blame] | 228 | } |
| 229 | |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 230 | bool SetUserProfiles(uid_t uid, const std::vector<std::string>& profiles) { |
| 231 | return TaskProfiles::GetInstance().SetUserProfiles(uid, std::span<const std::string>(profiles), |
| 232 | false); |
| 233 | } |
| 234 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 235 | static int RemoveCgroup(const char* cgroup, uid_t uid, pid_t pid) { |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 236 | auto path = ConvertUidPidToPath(cgroup, uid, pid); |
| 237 | int ret = TEMP_FAILURE_RETRY(rmdir(path.c_str())); |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 238 | |
T.J. Mercier | 9c8c748 | 2023-07-12 18:18:42 +0000 | [diff] [blame] | 239 | if (!ret && uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END) { |
| 240 | // Isolated UIDs are unlikely to be reused soon after removal, |
| 241 | // so free up the kernel resources for the UID level cgroup. |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 242 | path = ConvertUidToPath(cgroup, uid); |
| 243 | ret = TEMP_FAILURE_RETRY(rmdir(path.c_str())); |
| 244 | } |
| 245 | |
| 246 | if (ret < 0 && errno == ENOENT) { |
| 247 | // This function is idempoetent, but still warn here. |
| 248 | LOG(WARNING) << "RemoveCgroup: " << path << " does not exist."; |
| 249 | ret = 0; |
T.J. Mercier | 9c8c748 | 2023-07-12 18:18:42 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 252 | return ret; |
| 253 | } |
| 254 | |
Bart Van Assche | 3048812 | 2023-11-15 09:11:20 -0800 | [diff] [blame] | 255 | static bool RemoveEmptyUidCgroups(const std::string& uid_path) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 256 | std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir); |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 257 | bool empty = true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 258 | if (uid != NULL) { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 259 | dirent* dir; |
| 260 | while ((dir = readdir(uid.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 261 | if (dir->d_type != DT_DIR) { |
| 262 | continue; |
| 263 | } |
| 264 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 265 | if (!StartsWith(dir->d_name, "pid_")) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 266 | continue; |
| 267 | } |
| 268 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 269 | auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 270 | LOG(VERBOSE) << "Removing " << path; |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 271 | if (rmdir(path.c_str()) == -1) { |
| 272 | if (errno != EBUSY) { |
| 273 | PLOG(WARNING) << "Failed to remove " << path; |
| 274 | } |
| 275 | empty = false; |
| 276 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 279 | return empty; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Bart Van Assche | 3048812 | 2023-11-15 09:11:20 -0800 | [diff] [blame] | 282 | void removeAllEmptyProcessGroups() { |
| 283 | LOG(VERBOSE) << "removeAllEmptyProcessGroups()"; |
| 284 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 285 | std::vector<std::string> cgroups; |
Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 286 | std::string path, memcg_apps_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 287 | |
T.J. Mercier | a99e7d8 | 2023-10-27 17:29:01 +0000 | [diff] [blame] | 288 | if (CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &path)) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 289 | cgroups.push_back(path); |
| 290 | } |
Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 291 | if (CgroupGetMemcgAppsPath(&memcg_apps_path) && memcg_apps_path != path) { |
| 292 | cgroups.push_back(memcg_apps_path); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | for (std::string cgroup_root_path : cgroups) { |
| 296 | std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir); |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 297 | if (root == NULL) { |
Bart Van Assche | 6e814b0 | 2022-02-03 19:24:42 +0000 | [diff] [blame] | 298 | PLOG(ERROR) << __func__ << " failed to open " << cgroup_root_path; |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 299 | } else { |
| 300 | dirent* dir; |
| 301 | while ((dir = readdir(root.get())) != nullptr) { |
| 302 | if (dir->d_type != DT_DIR) { |
| 303 | continue; |
| 304 | } |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 305 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 306 | if (!StartsWith(dir->d_name, "uid_")) { |
| 307 | continue; |
| 308 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 309 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 310 | auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name); |
Bart Van Assche | 3048812 | 2023-11-15 09:11:20 -0800 | [diff] [blame] | 311 | if (!RemoveEmptyUidCgroups(path)) { |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 312 | LOG(VERBOSE) << "Skip removing " << path; |
| 313 | continue; |
| 314 | } |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 315 | LOG(VERBOSE) << "Removing " << path; |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 316 | if (rmdir(path.c_str()) == -1 && errno != EBUSY) { |
| 317 | PLOG(WARNING) << "Failed to remove " << path; |
| 318 | } |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 319 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 320 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 324 | /** |
| 325 | * Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by |
| 326 | * the user root. Ownership for the newly created cgroup and all of its files must thus be |
| 327 | * transferred for the user/group passed as uid/gid before system_server can properly access them. |
| 328 | */ |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 329 | static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { |
Suren Baghdasaryan | 29c9e26 | 2021-07-07 10:59:59 -0700 | [diff] [blame] | 330 | if (mkdir(path.c_str(), mode) == -1) { |
| 331 | if (errno == EEXIST) { |
| 332 | // Directory already exists and permissions have been set at the time it was created |
| 333 | return true; |
| 334 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 338 | auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir); |
| 339 | |
| 340 | if (dir == NULL) { |
| 341 | PLOG(ERROR) << "opendir failed for " << path; |
| 342 | goto err; |
| 343 | } |
| 344 | |
| 345 | struct dirent* dir_entry; |
| 346 | while ((dir_entry = readdir(dir.get()))) { |
| 347 | if (!strcmp("..", dir_entry->d_name)) { |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | std::string file_path = path + "/" + dir_entry->d_name; |
| 352 | |
| 353 | if (lchown(file_path.c_str(), uid, gid) < 0) { |
| 354 | PLOG(ERROR) << "lchown failed for " << file_path; |
| 355 | goto err; |
| 356 | } |
| 357 | |
| 358 | if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) { |
| 359 | PLOG(ERROR) << "fchmodat failed for " << file_path; |
| 360 | goto err; |
| 361 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return true; |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 365 | err: |
| 366 | int saved_errno = errno; |
| 367 | rmdir(path.c_str()); |
| 368 | errno = saved_errno; |
| 369 | |
| 370 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 371 | } |
| 372 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 373 | bool sendSignalToProcessGroup(uid_t uid, pid_t initialPid, int signal) { |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 374 | std::set<pid_t> pgids, pids; |
Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 375 | |
| 376 | if (CgroupsAvailable()) { |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 377 | std::string hierarchy_root_path, cgroup_v2_path; |
| 378 | CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &hierarchy_root_path); |
| 379 | cgroup_v2_path = ConvertUidPidToPath(hierarchy_root_path.c_str(), uid, initialPid); |
| 380 | |
T.J. Mercier | a103630 | 2023-11-07 14:36:46 +0000 | [diff] [blame] | 381 | if (signal == SIGKILL && CgroupKillAvailable()) { |
| 382 | LOG(VERBOSE) << "Using " << PROCESSGROUP_CGROUP_KILL_FILE << " to SIGKILL " |
| 383 | << cgroup_v2_path; |
| 384 | |
| 385 | // We need to kill the process group in addition to the cgroup. For normal apps they |
| 386 | // should completely overlap, but system_server kills depend on process group kills to |
| 387 | // take down apps which are in their own cgroups and not individually targeted. |
| 388 | if (kill(-initialPid, signal) == -1 && errno != ESRCH) { |
| 389 | PLOG(WARNING) << "kill(" << -initialPid << ", " << signal << ") failed"; |
| 390 | } |
| 391 | |
| 392 | const std::string killfilepath = cgroup_v2_path + '/' + PROCESSGROUP_CGROUP_KILL_FILE; |
| 393 | if (WriteStringToFile("1", killfilepath)) { |
| 394 | return true; |
| 395 | } else { |
| 396 | PLOG(ERROR) << "Failed to write 1 to " << killfilepath; |
| 397 | // Fallback to cgroup.procs below |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Since cgroup.kill only sends SIGKILLs, we read cgroup.procs to find each process to |
| 402 | // signal individually. This is more costly than using cgroup.kill for SIGKILLs. |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 403 | LOG(VERBOSE) << "Using " << PROCESSGROUP_CGROUP_PROCS_FILE << " to signal (" << signal |
| 404 | << ") " << cgroup_v2_path; |
| 405 | |
| 406 | // We separate all of the pids in the cgroup into those pids that are also the leaders of |
| 407 | // process groups (stored in the pgids set) and those that are not (stored in the pids set). |
| 408 | const auto procsfilepath = cgroup_v2_path + '/' + PROCESSGROUP_CGROUP_PROCS_FILE; |
| 409 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(procsfilepath.c_str(), "re"), fclose); |
| 410 | if (!fp) { |
| 411 | // This should only happen if the cgroup has already been removed with a successful call |
| 412 | // to killProcessGroup. Callers should only retry sendSignalToProcessGroup or |
| 413 | // killProcessGroup calls if they fail without ENOENT. |
| 414 | PLOG(ERROR) << "Failed to open " << procsfilepath; |
| 415 | kill(-initialPid, signal); |
| 416 | return false; |
Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 417 | } |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 418 | |
Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 419 | pid_t pid; |
| 420 | bool file_is_empty = true; |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 421 | while (fscanf(fp.get(), "%d\n", &pid) == 1 && pid >= 0) { |
Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 422 | file_is_empty = false; |
| 423 | if (pid == 0) { |
| 424 | // Should never happen... but if it does, trying to kill this |
| 425 | // will boomerang right back and kill us! Let's not let that happen. |
| 426 | LOG(WARNING) |
| 427 | << "Yikes, we've been told to kill pid 0! How about we don't do that?"; |
| 428 | continue; |
| 429 | } |
| 430 | pid_t pgid = getpgid(pid); |
| 431 | if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed"; |
| 432 | if (pgid == pid) { |
| 433 | pgids.emplace(pid); |
| 434 | } else { |
| 435 | pids.emplace(pid); |
| 436 | } |
| 437 | } |
Jing Ji | 304c0f1 | 2023-02-27 21:58:19 -0800 | [diff] [blame] | 438 | if (!file_is_empty) { |
| 439 | // Erase all pids that will be killed when we kill the process groups. |
| 440 | for (auto it = pids.begin(); it != pids.end();) { |
| 441 | pid_t pgid = getpgid(*it); |
| 442 | if (pgids.count(pgid) == 1) { |
| 443 | it = pids.erase(it); |
| 444 | } else { |
| 445 | ++it; |
| 446 | } |
Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 451 | pgids.emplace(initialPid); |
| 452 | |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 453 | // Kill all process groups. |
| 454 | for (const auto pgid : pgids) { |
| 455 | LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid |
| 456 | << " as part of process cgroup " << initialPid; |
| 457 | |
Suren Baghdasaryan | 4f7cc8c | 2023-03-02 14:12:49 -0800 | [diff] [blame] | 458 | if (kill(-pgid, signal) == -1 && errno != ESRCH) { |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 459 | PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed"; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // Kill remaining pids. |
| 464 | for (const auto pid : pids) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 465 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup " |
| 466 | << initialPid; |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 467 | |
Suren Baghdasaryan | 4f7cc8c | 2023-03-02 14:12:49 -0800 | [diff] [blame] | 468 | if (kill(pid, signal) == -1 && errno != ESRCH) { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 469 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 473 | return true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 474 | } |
| 475 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 476 | template <typename T> |
| 477 | static std::chrono::milliseconds toMillisec(T&& duration) { |
| 478 | return std::chrono::duration_cast<std::chrono::milliseconds>(duration); |
| 479 | } |
| 480 | |
| 481 | enum class populated_status |
| 482 | { |
| 483 | populated, |
| 484 | not_populated, |
| 485 | error |
| 486 | }; |
| 487 | |
| 488 | static populated_status cgroupIsPopulated(int events_fd) { |
| 489 | const std::string POPULATED_KEY("populated "); |
| 490 | const std::string::size_type MAX_EVENTS_FILE_SIZE = 32; |
| 491 | |
| 492 | std::string buf; |
| 493 | buf.resize(MAX_EVENTS_FILE_SIZE); |
| 494 | ssize_t len = TEMP_FAILURE_RETRY(pread(events_fd, buf.data(), buf.size(), 0)); |
| 495 | if (len == -1) { |
| 496 | PLOG(ERROR) << "Could not read cgroup.events: "; |
| 497 | // Potentially ENODEV if the cgroup has been removed since we opened this file, but that |
| 498 | // shouldn't have happened yet. |
| 499 | return populated_status::error; |
| 500 | } |
| 501 | |
| 502 | if (len == 0) { |
| 503 | LOG(ERROR) << "cgroup.events EOF"; |
| 504 | return populated_status::error; |
| 505 | } |
| 506 | |
| 507 | buf.resize(len); |
| 508 | |
| 509 | const std::string::size_type pos = buf.find(POPULATED_KEY); |
| 510 | if (pos == std::string::npos) { |
| 511 | LOG(ERROR) << "Could not find populated key in cgroup.events"; |
| 512 | return populated_status::error; |
| 513 | } |
| 514 | |
| 515 | if (pos + POPULATED_KEY.size() + 1 > len) { |
| 516 | LOG(ERROR) << "Partial read of cgroup.events"; |
| 517 | return populated_status::error; |
| 518 | } |
| 519 | |
| 520 | return buf[pos + POPULATED_KEY.size()] == '1' ? |
| 521 | populated_status::populated : populated_status::not_populated; |
| 522 | } |
| 523 | |
| 524 | // The default timeout of 2200ms comes from the default number of retries in a previous |
| 525 | // implementation of this function. The default retry value was 40 for killing and 400 for cgroup |
| 526 | // removal with 5ms sleeps between each retry. |
| 527 | static int KillProcessGroup( |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 528 | uid_t uid, pid_t initialPid, int signal, bool once = false, |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 529 | std::chrono::steady_clock::time_point until = std::chrono::steady_clock::now() + 2200ms) { |
Bart Van Assche | 5a3c3f7 | 2023-03-22 13:21:03 -0700 | [diff] [blame] | 530 | CHECK_GE(uid, 0); |
| 531 | CHECK_GT(initialPid, 0); |
| 532 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 533 | // Always attempt to send a kill signal to at least the initialPid, at least once, regardless of |
| 534 | // whether its cgroup exists or not. This should only be necessary if a bug results in the |
| 535 | // migration of the targeted process out of its cgroup, which we will also attempt to kill. |
| 536 | const bool signal_ret = sendSignalToProcessGroup(uid, initialPid, signal); |
| 537 | |
| 538 | if (!CgroupsAvailable() || !signal_ret) return signal_ret ? 0 : -1; |
| 539 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 540 | std::string hierarchy_root_path; |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 541 | CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &hierarchy_root_path); |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 542 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 543 | const std::string cgroup_v2_path = |
| 544 | ConvertUidPidToPath(hierarchy_root_path.c_str(), uid, initialPid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 545 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 546 | const std::string eventsfile = cgroup_v2_path + '/' + PROCESSGROUP_CGROUP_EVENTS_FILE; |
| 547 | android::base::unique_fd events_fd(open(eventsfile.c_str(), O_RDONLY)); |
| 548 | if (events_fd.get() == -1) { |
| 549 | PLOG(WARNING) << "Error opening " << eventsfile << " for KillProcessGroup"; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 550 | return -1; |
| 551 | } |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 552 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 553 | struct pollfd fds = { |
| 554 | .fd = events_fd, |
| 555 | .events = POLLPRI, |
| 556 | }; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 557 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 558 | const std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 559 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 560 | // The primary reason to loop here is to capture any new forks or migrations that could occur |
| 561 | // after we send signals to the original set of processes, but before all of those processes |
| 562 | // exit and the cgroup becomes unpopulated, or before we remove the cgroup. We try hard to |
| 563 | // ensure this completes successfully to avoid permanent memory leaks, but we still place a |
| 564 | // large default upper bound on the amount of time we spend in this loop. The amount of CPU |
| 565 | // contention, and the amount of work that needs to be done in do_exit for each process |
| 566 | // determines how long this will take. |
| 567 | int ret; |
| 568 | do { |
| 569 | populated_status populated; |
| 570 | while ((populated = cgroupIsPopulated(events_fd.get())) == populated_status::populated && |
| 571 | std::chrono::steady_clock::now() < until) { |
| 572 | |
| 573 | sendSignalToProcessGroup(uid, initialPid, signal); |
| 574 | if (once) { |
| 575 | populated = cgroupIsPopulated(events_fd.get()); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | const std::chrono::steady_clock::time_point poll_start = |
| 580 | std::chrono::steady_clock::now(); |
| 581 | |
| 582 | if (poll_start < until) |
| 583 | ret = TEMP_FAILURE_RETRY(poll(&fds, 1, toMillisec(until - poll_start).count())); |
| 584 | |
| 585 | if (ret == -1) { |
| 586 | // Fallback to 5ms sleeps if poll fails |
| 587 | PLOG(ERROR) << "Poll on " << eventsfile << "failed"; |
| 588 | const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); |
| 589 | if (now < until) |
| 590 | std::this_thread::sleep_for(std::min(5ms, toMillisec(until - now))); |
| 591 | } |
| 592 | |
| 593 | LOG(VERBOSE) << "Waited " |
| 594 | << toMillisec(std::chrono::steady_clock::now() - poll_start).count() |
| 595 | << " ms for " << eventsfile << " poll"; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 596 | } |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 597 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 598 | const std::chrono::milliseconds kill_duration = |
| 599 | toMillisec(std::chrono::steady_clock::now() - start); |
| 600 | |
| 601 | if (populated == populated_status::populated) { |
| 602 | LOG(WARNING) << "Still waiting on process(es) to exit for cgroup " << cgroup_v2_path |
| 603 | << " after " << kill_duration.count() << " ms"; |
| 604 | // We'll still try the cgroup removal below which we expect to log an error. |
| 605 | } else if (populated == populated_status::not_populated) { |
| 606 | LOG(VERBOSE) << "Killed all processes under cgroup " << cgroup_v2_path |
| 607 | << " after " << kill_duration.count() << " ms"; |
Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 608 | } |
| 609 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 610 | ret = RemoveCgroup(hierarchy_root_path.c_str(), uid, initialPid); |
| 611 | if (ret) |
| 612 | PLOG(ERROR) << "Unable to remove cgroup " << cgroup_v2_path; |
| 613 | else |
| 614 | LOG(INFO) << "Removed cgroup " << cgroup_v2_path; |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 615 | |
| 616 | if (isMemoryCgroupSupported() && UsePerAppMemcg()) { |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 617 | // This per-application memcg v1 case should eventually be removed after migration to |
| 618 | // memcg v2. |
Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 619 | std::string memcg_apps_path; |
| 620 | if (CgroupGetMemcgAppsPath(&memcg_apps_path) && |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 621 | (ret = RemoveCgroup(memcg_apps_path.c_str(), uid, initialPid)) < 0) { |
| 622 | const auto memcg_v1_cgroup_path = |
| 623 | ConvertUidPidToPath(memcg_apps_path.c_str(), uid, initialPid); |
| 624 | PLOG(ERROR) << "Unable to remove memcg v1 cgroup " << memcg_v1_cgroup_path; |
Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 625 | } |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 626 | } |
| 627 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 628 | if (once) break; |
| 629 | if (std::chrono::steady_clock::now() >= until) break; |
| 630 | } while (ret && errno == EBUSY); |
| 631 | |
| 632 | return ret; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 633 | } |
| 634 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 635 | int killProcessGroup(uid_t uid, pid_t initialPid, int signal) { |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 636 | return KillProcessGroup(uid, initialPid, signal); |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 637 | } |
| 638 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 639 | int killProcessGroupOnce(uid_t uid, pid_t initialPid, int signal) { |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 640 | return KillProcessGroup(uid, initialPid, signal, true); |
T.J. Mercier | 22006bf | 2023-04-13 21:48:55 +0000 | [diff] [blame] | 641 | } |
| 642 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 643 | static int createProcessGroupInternal(uid_t uid, pid_t initialPid, std::string cgroup, |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 644 | bool activate_controllers) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 645 | auto uid_path = ConvertUidToPath(cgroup.c_str(), uid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 646 | |
Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 647 | struct stat cgroup_stat; |
| 648 | mode_t cgroup_mode = 0750; |
Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 649 | uid_t cgroup_uid = AID_SYSTEM; |
Bart Van Assche | 32a9b1c | 2022-03-10 21:42:40 +0000 | [diff] [blame] | 650 | gid_t cgroup_gid = AID_SYSTEM; |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 651 | int ret = 0; |
Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 652 | |
Bart Van Assche | 55a9b1e | 2022-03-23 10:13:18 -0700 | [diff] [blame] | 653 | if (stat(cgroup.c_str(), &cgroup_stat) < 0) { |
Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 654 | PLOG(ERROR) << "Failed to get stats for " << cgroup; |
| 655 | } else { |
| 656 | cgroup_mode = cgroup_stat.st_mode; |
Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 657 | cgroup_uid = cgroup_stat.st_uid; |
Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 658 | cgroup_gid = cgroup_stat.st_gid; |
| 659 | } |
| 660 | |
Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 661 | if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 662 | PLOG(ERROR) << "Failed to make and chown " << uid_path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 663 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 664 | } |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 665 | if (activate_controllers) { |
| 666 | ret = CgroupMap::GetInstance().ActivateControllers(uid_path); |
| 667 | if (ret) { |
| 668 | LOG(ERROR) << "Failed to activate controllers in " << uid_path; |
| 669 | return ret; |
| 670 | } |
| 671 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 672 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 673 | auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 674 | |
Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 675 | if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 676 | PLOG(ERROR) << "Failed to make and chown " << uid_pid_path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 677 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 678 | } |
| 679 | |
T.J. Mercier | 4928b6e | 2023-12-14 21:38:23 +0000 | [diff] [blame] | 680 | auto uid_pid_procs_file = uid_pid_path + '/' + PROCESSGROUP_CGROUP_PROCS_FILE; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 681 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 682 | if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 683 | ret = -errno; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 684 | PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 687 | return ret; |
| 688 | } |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 689 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 690 | int createProcessGroup(uid_t uid, pid_t initialPid, bool memControl) { |
Bart Van Assche | 5a3c3f7 | 2023-03-22 13:21:03 -0700 | [diff] [blame] | 691 | CHECK_GE(uid, 0); |
| 692 | CHECK_GT(initialPid, 0); |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 693 | |
| 694 | if (memControl && !UsePerAppMemcg()) { |
T.J. Mercier | 4d0d285 | 2023-10-27 23:44:03 +0000 | [diff] [blame] | 695 | LOG(ERROR) << "service memory controls are used without per-process memory cgroup support"; |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 696 | return -EINVAL; |
| 697 | } |
| 698 | |
Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 699 | if (std::string memcg_apps_path; |
| 700 | isMemoryCgroupSupported() && UsePerAppMemcg() && CgroupGetMemcgAppsPath(&memcg_apps_path)) { |
| 701 | // Note by bvanassche: passing 'false' as fourth argument below implies that the v1 |
| 702 | // hierarchy is used. It is not clear to me whether the above conditions guarantee that the |
| 703 | // v1 hierarchy is used. |
| 704 | int ret = createProcessGroupInternal(uid, initialPid, memcg_apps_path, false); |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 705 | if (ret != 0) { |
| 706 | return ret; |
| 707 | } |
| 708 | } |
| 709 | |
Bart Van Assche | 5a3c3f7 | 2023-03-22 13:21:03 -0700 | [diff] [blame] | 710 | std::string cgroup; |
T.J. Mercier | a99e7d8 | 2023-10-27 17:29:01 +0000 | [diff] [blame] | 711 | CgroupGetControllerPath(CGROUPV2_HIERARCHY_NAME, &cgroup); |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 712 | return createProcessGroupInternal(uid, initialPid, cgroup, true); |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 713 | } |
| 714 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 715 | static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) { |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 716 | if (!isMemoryCgroupSupported()) { |
T.J. Mercier | 4d0d285 | 2023-10-27 23:44:03 +0000 | [diff] [blame] | 717 | LOG(ERROR) << "Memcg is not mounted."; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 718 | return false; |
| 719 | } |
| 720 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 721 | std::string path; |
| 722 | if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) { |
T.J. Mercier | 4d0d285 | 2023-10-27 23:44:03 +0000 | [diff] [blame] | 723 | LOG(ERROR) << "Failed to find attribute '" << attr_name << "'"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 724 | return false; |
| 725 | } |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 726 | |
| 727 | if (!WriteStringToFile(std::to_string(value), path)) { |
| 728 | PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
| 729 | return false; |
| 730 | } |
| 731 | return true; |
| 732 | } |
| 733 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 734 | bool setProcessGroupSwappiness(uid_t, pid_t pid, int swappiness) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 735 | return SetProcessGroupValue(pid, "MemSwappiness", swappiness); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 736 | } |
| 737 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 738 | bool setProcessGroupSoftLimit(uid_t, pid_t pid, int64_t soft_limit_in_bytes) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 739 | return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 740 | } |
| 741 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 742 | bool setProcessGroupLimit(uid_t, pid_t pid, int64_t limit_in_bytes) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 743 | return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 744 | } |
Marco Ballesio | 4e644c4 | 2021-02-24 16:30:50 -0800 | [diff] [blame] | 745 | |
| 746 | bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { |
| 747 | return CgroupGetAttributePathForTask(attr_name, tid, path); |
| 748 | } |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 749 | |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 750 | bool isProfileValidForProcess(const std::string& profile_name, uid_t uid, pid_t pid) { |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 751 | const TaskProfile* tp = TaskProfiles::GetInstance().GetProfile(profile_name); |
| 752 | |
| 753 | if (tp == nullptr) { |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | return tp->IsValidForProcess(uid, pid); |
T.J. Mercier | a99e7d8 | 2023-10-27 17:29:01 +0000 | [diff] [blame] | 758 | } |