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> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 25 | #include <signal.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 31 | |
| 32 | #include <chrono> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 33 | #include <map> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 34 | #include <memory> |
Elliott Hughes | 8d532e4 | 2016-06-06 21:19:55 -0700 | [diff] [blame] | 35 | #include <mutex> |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 36 | #include <set> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 37 | #include <string> |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 38 | #include <thread> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 39 | |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 40 | #include <android-base/file.h> |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
| 44 | #include <android-base/strings.h> |
Suren Baghdasaryan | 7bf4381 | 2019-01-25 05:29:55 +0000 | [diff] [blame] | 45 | #include <cutils/android_filesystem_config.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 46 | #include <processgroup/processgroup.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 47 | #include <task_profiles.h> |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 48 | |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 49 | using android::base::GetBoolProperty; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 50 | using android::base::StartsWith; |
| 51 | using android::base::StringPrintf; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 52 | using android::base::WriteStringToFile; |
| 53 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 54 | using namespace std::chrono_literals; |
| 55 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 56 | #define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs" |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 57 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 58 | bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 59 | auto controller = CgroupMap::GetInstance().FindController(cgroup_name); |
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 | if (!controller.HasValue()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (path) { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 66 | *path = controller.path(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 72 | bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) { |
| 73 | auto controller = CgroupMap::GetInstance().FindControllerByPath(path); |
| 74 | |
| 75 | if (!controller.HasValue()) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if (cgroup_name) { |
| 80 | *cgroup_name = controller.name(); |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 86 | bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) { |
| 87 | const TaskProfiles& tp = TaskProfiles::GetInstance(); |
| 88 | const ProfileAttribute* attr = tp.GetAttribute(attr_name); |
| 89 | |
| 90 | if (attr == nullptr) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | if (path) { |
| 95 | *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str()); |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { |
| 102 | const TaskProfiles& tp = TaskProfiles::GetInstance(); |
| 103 | const ProfileAttribute* attr = tp.GetAttribute(attr_name); |
| 104 | |
| 105 | if (attr == nullptr) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (!attr->GetPathForTask(tid, path)) { |
| 110 | PLOG(ERROR) << "Failed to find cgroup for tid " << tid; |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool UsePerAppMemcg() { |
| 118 | bool low_ram_device = GetBoolProperty("ro.config.low_ram", false); |
| 119 | return GetBoolProperty("ro.config.per_app_memcg", low_ram_device); |
| 120 | } |
| 121 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 122 | static bool isMemoryCgroupSupported() { |
Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 123 | static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 124 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 125 | return memcg_supported; |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 128 | void DropTaskProfilesResourceCaching() { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 129 | TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_TASK); |
| 130 | TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_PROCESS); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 131 | } |
| 132 | |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 133 | bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 134 | return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false); |
| 135 | } |
| 136 | |
| 137 | bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) { |
| 138 | return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, true); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 141 | bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 142 | return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 145 | static std::string ConvertUidToPath(const char* cgroup, uid_t uid) { |
| 146 | return StringPrintf("%s/uid_%d", cgroup, uid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 149 | static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) { |
| 150 | return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 153 | static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid, unsigned int retries) { |
| 154 | int ret = 0; |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 155 | auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid); |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 156 | auto uid_path = ConvertUidToPath(cgroup, uid); |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 157 | |
| 158 | if (retries == 0) { |
| 159 | retries = 1; |
| 160 | } |
| 161 | |
| 162 | while (retries--) { |
| 163 | ret = rmdir(uid_pid_path.c_str()); |
| 164 | if (!ret || errno != EBUSY) break; |
| 165 | std::this_thread::sleep_for(5ms); |
| 166 | } |
| 167 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 171 | static bool RemoveUidProcessGroups(const std::string& uid_path) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 172 | 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] | 173 | bool empty = true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 174 | if (uid != NULL) { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 175 | dirent* dir; |
| 176 | while ((dir = readdir(uid.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 177 | if (dir->d_type != DT_DIR) { |
| 178 | continue; |
| 179 | } |
| 180 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 181 | if (!StartsWith(dir->d_name, "pid_")) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 182 | continue; |
| 183 | } |
| 184 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 185 | auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 186 | LOG(VERBOSE) << "Removing " << path; |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 187 | if (rmdir(path.c_str()) == -1) { |
| 188 | if (errno != EBUSY) { |
| 189 | PLOG(WARNING) << "Failed to remove " << path; |
| 190 | } |
| 191 | empty = false; |
| 192 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 195 | return empty; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 198 | void removeAllProcessGroups() { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 199 | LOG(VERBOSE) << "removeAllProcessGroups()"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 200 | |
| 201 | std::vector<std::string> cgroups; |
| 202 | std::string path; |
| 203 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 204 | if (CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &path)) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 205 | cgroups.push_back(path); |
| 206 | } |
| 207 | if (CgroupGetControllerPath("memory", &path)) { |
Vic Yang | ab8d6ab | 2019-02-19 14:09:00 -0800 | [diff] [blame] | 208 | cgroups.push_back(path + "/apps"); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | for (std::string cgroup_root_path : cgroups) { |
| 212 | 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] | 213 | if (root == NULL) { |
Bart Van Assche | 6e814b0 | 2022-02-03 19:24:42 +0000 | [diff] [blame^] | 214 | PLOG(ERROR) << __func__ << " failed to open " << cgroup_root_path; |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 215 | } else { |
| 216 | dirent* dir; |
| 217 | while ((dir = readdir(root.get())) != nullptr) { |
| 218 | if (dir->d_type != DT_DIR) { |
| 219 | continue; |
| 220 | } |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 221 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 222 | if (!StartsWith(dir->d_name, "uid_")) { |
| 223 | continue; |
| 224 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 225 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 226 | auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name); |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 227 | if (!RemoveUidProcessGroups(path)) { |
| 228 | LOG(VERBOSE) << "Skip removing " << path; |
| 229 | continue; |
| 230 | } |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 231 | LOG(VERBOSE) << "Removing " << path; |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 232 | if (rmdir(path.c_str()) == -1 && errno != EBUSY) { |
| 233 | PLOG(WARNING) << "Failed to remove " << path; |
| 234 | } |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 235 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 236 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 240 | /** |
| 241 | * Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by |
| 242 | * the user root. Ownership for the newly created cgroup and all of its files must thus be |
| 243 | * transferred for the user/group passed as uid/gid before system_server can properly access them. |
| 244 | */ |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 245 | 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] | 246 | if (mkdir(path.c_str(), mode) == -1) { |
| 247 | if (errno == EEXIST) { |
| 248 | // Directory already exists and permissions have been set at the time it was created |
| 249 | return true; |
| 250 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 254 | auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir); |
| 255 | |
| 256 | if (dir == NULL) { |
| 257 | PLOG(ERROR) << "opendir failed for " << path; |
| 258 | goto err; |
| 259 | } |
| 260 | |
| 261 | struct dirent* dir_entry; |
| 262 | while ((dir_entry = readdir(dir.get()))) { |
| 263 | if (!strcmp("..", dir_entry->d_name)) { |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | std::string file_path = path + "/" + dir_entry->d_name; |
| 268 | |
| 269 | if (lchown(file_path.c_str(), uid, gid) < 0) { |
| 270 | PLOG(ERROR) << "lchown failed for " << file_path; |
| 271 | goto err; |
| 272 | } |
| 273 | |
| 274 | if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) { |
| 275 | PLOG(ERROR) << "fchmodat failed for " << file_path; |
| 276 | goto err; |
| 277 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return true; |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 281 | err: |
| 282 | int saved_errno = errno; |
| 283 | rmdir(path.c_str()); |
| 284 | errno = saved_errno; |
| 285 | |
| 286 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 289 | // Returns number of processes killed on success |
| 290 | // Returns 0 if there are no processes in the process cgroup left to kill |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 291 | // Returns -1 on error |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 292 | static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) { |
| 293 | auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 294 | std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose); |
| 295 | if (!fd) { |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 296 | if (errno == ENOENT) { |
| 297 | // This happens when process is already dead |
| 298 | return 0; |
| 299 | } |
Bart Van Assche | 6e814b0 | 2022-02-03 19:24:42 +0000 | [diff] [blame^] | 300 | PLOG(WARNING) << __func__ << " failed to open process cgroup uid " << uid << " pid " |
| 301 | << initialPid; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 302 | return -1; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 305 | // We separate all of the pids in the cgroup into those pids that are also the leaders of |
| 306 | // process groups (stored in the pgids set) and those that are not (stored in the pids set). |
| 307 | std::set<pid_t> pgids; |
| 308 | pgids.emplace(initialPid); |
| 309 | std::set<pid_t> pids; |
| 310 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 311 | pid_t pid; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 312 | int processes = 0; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 313 | while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 314 | processes++; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 315 | if (pid == 0) { |
| 316 | // Should never happen... but if it does, trying to kill this |
| 317 | // will boomerang right back and kill us! Let's not let that happen. |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 318 | LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?"; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 319 | continue; |
| 320 | } |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 321 | pid_t pgid = getpgid(pid); |
| 322 | if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed"; |
| 323 | if (pgid == pid) { |
| 324 | pgids.emplace(pid); |
| 325 | } else { |
| 326 | pids.emplace(pid); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Erase all pids that will be killed when we kill the process groups. |
| 331 | for (auto it = pids.begin(); it != pids.end();) { |
fengshaobo | 950f6f3 | 2018-09-13 14:57:07 +0800 | [diff] [blame] | 332 | pid_t pgid = getpgid(*it); |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 333 | if (pgids.count(pgid) == 1) { |
| 334 | it = pids.erase(it); |
| 335 | } else { |
| 336 | ++it; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Kill all process groups. |
| 341 | for (const auto pgid : pgids) { |
| 342 | LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid |
| 343 | << " as part of process cgroup " << initialPid; |
| 344 | |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 345 | if (kill(-pgid, signal) == -1 && errno != ESRCH) { |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 346 | PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed"; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // Kill remaining pids. |
| 351 | for (const auto pid : pids) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 352 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup " |
| 353 | << initialPid; |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 354 | |
Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 355 | if (kill(pid, signal) == -1 && errno != ESRCH) { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 356 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 360 | return feof(fd.get()) ? processes : -1; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 363 | static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries, |
| 364 | int* max_processes) { |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 365 | std::string hierarchy_root_path; |
| 366 | CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path); |
| 367 | const char* cgroup = hierarchy_root_path.c_str(); |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 368 | |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 369 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 370 | |
Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 371 | if (max_processes != nullptr) { |
| 372 | *max_processes = 0; |
| 373 | } |
| 374 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 375 | int retry = retries; |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 376 | int processes; |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 377 | while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) { |
Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 378 | if (max_processes != nullptr && processes > *max_processes) { |
| 379 | *max_processes = processes; |
| 380 | } |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 381 | LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid; |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 382 | if (retry > 0) { |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 383 | std::this_thread::sleep_for(5ms); |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 384 | --retry; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 385 | } else { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 390 | if (processes < 0) { |
| 391 | PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid " |
| 392 | << initialPid; |
| 393 | return -1; |
| 394 | } |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 395 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 396 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 397 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 398 | |
| 399 | // We only calculate the number of 'processes' when killing the processes. |
| 400 | // In the retries == 0 case, we only kill the processes once and therefore |
| 401 | // will not have waited then recalculated how many processes are remaining |
| 402 | // after the first signals have been sent. |
| 403 | // Logging anything regarding the number of 'processes' here does not make sense. |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 404 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 405 | if (processes == 0) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 406 | if (retries > 0) { |
| 407 | LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid |
| 408 | << " in " << static_cast<int>(ms) << "ms"; |
| 409 | } |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 410 | |
| 411 | int err = RemoveProcessGroup(cgroup, uid, initialPid, retries); |
| 412 | |
| 413 | if (isMemoryCgroupSupported() && UsePerAppMemcg()) { |
| 414 | std::string memory_path; |
| 415 | CgroupGetControllerPath("memory", &memory_path); |
| 416 | memory_path += "/apps"; |
| 417 | if (RemoveProcessGroup(memory_path.c_str(), uid, initialPid, retries)) return -1; |
| 418 | } |
| 419 | |
| 420 | return err; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 421 | } else { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 422 | if (retries > 0) { |
| 423 | LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid |
| 424 | << " in " << static_cast<int>(ms) << "ms, " << processes |
| 425 | << " processes remain"; |
| 426 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 427 | return -1; |
| 428 | } |
| 429 | } |
| 430 | |
Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 431 | int killProcessGroup(uid_t uid, int initialPid, int signal, int* max_processes) { |
| 432 | return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/, max_processes); |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 435 | int killProcessGroupOnce(uid_t uid, int initialPid, int signal, int* max_processes) { |
| 436 | return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/, max_processes); |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 439 | static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup, |
| 440 | bool activate_controllers) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 441 | auto uid_path = ConvertUidToPath(cgroup.c_str(), uid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 442 | |
Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 443 | struct stat cgroup_stat; |
| 444 | mode_t cgroup_mode = 0750; |
| 445 | gid_t cgroup_uid = AID_SYSTEM; |
| 446 | uid_t cgroup_gid = AID_SYSTEM; |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 447 | int ret = 0; |
Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 448 | |
| 449 | if (stat(cgroup.c_str(), &cgroup_stat) == 1) { |
| 450 | PLOG(ERROR) << "Failed to get stats for " << cgroup; |
| 451 | } else { |
| 452 | cgroup_mode = cgroup_stat.st_mode; |
| 453 | cgroup_uid = cgroup_stat.st_uid; |
| 454 | cgroup_gid = cgroup_stat.st_gid; |
| 455 | } |
| 456 | |
| 457 | if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 458 | PLOG(ERROR) << "Failed to make and chown " << uid_path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 459 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 460 | } |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 461 | if (activate_controllers) { |
| 462 | ret = CgroupMap::GetInstance().ActivateControllers(uid_path); |
| 463 | if (ret) { |
| 464 | LOG(ERROR) << "Failed to activate controllers in " << uid_path; |
| 465 | return ret; |
| 466 | } |
| 467 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 468 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 469 | auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 470 | |
Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 471 | if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 472 | PLOG(ERROR) << "Failed to make and chown " << uid_pid_path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 473 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 476 | auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 477 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 478 | if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 479 | ret = -errno; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 480 | PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 483 | return ret; |
| 484 | } |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 485 | |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 486 | int createProcessGroup(uid_t uid, int initialPid, bool memControl) { |
| 487 | std::string cgroup; |
| 488 | |
| 489 | if (memControl && !UsePerAppMemcg()) { |
| 490 | PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support"; |
| 491 | return -EINVAL; |
| 492 | } |
| 493 | |
| 494 | if (isMemoryCgroupSupported() && UsePerAppMemcg()) { |
| 495 | CgroupGetControllerPath("memory", &cgroup); |
| 496 | cgroup += "/apps"; |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 497 | int ret = createProcessGroupInternal(uid, initialPid, cgroup, false); |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 498 | if (ret != 0) { |
| 499 | return ret; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup); |
Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 504 | return createProcessGroupInternal(uid, initialPid, cgroup, true); |
Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 507 | 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] | 508 | if (!isMemoryCgroupSupported()) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 509 | PLOG(ERROR) << "Memcg is not mounted."; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 513 | std::string path; |
| 514 | if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) { |
| 515 | PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'"; |
| 516 | return false; |
| 517 | } |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 518 | |
| 519 | if (!WriteStringToFile(std::to_string(value), path)) { |
| 520 | PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
| 521 | return false; |
| 522 | } |
| 523 | return true; |
| 524 | } |
| 525 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 526 | bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) { |
| 527 | return SetProcessGroupValue(pid, "MemSwappiness", swappiness); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 530 | bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) { |
| 531 | return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 534 | bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) { |
| 535 | return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 536 | } |
Marco Ballesio | 4e644c4 | 2021-02-24 16:30:50 -0800 | [diff] [blame] | 537 | |
| 538 | bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { |
| 539 | return CgroupGetAttributePathForTask(attr_name, tid, path); |
| 540 | } |