| 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 |  | 
| Nikita Ioffe | c2b1654 | 2022-10-20 14:14:39 +0100 | [diff] [blame] | 58 | bool CgroupsAvailable() { | 
|  | 59 | static bool cgroups_available = access("/proc/cgroups", F_OK) == 0; | 
|  | 60 | return cgroups_available; | 
|  | 61 | } | 
|  | 62 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 63 | bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 64 | auto controller = CgroupMap::GetInstance().FindController(cgroup_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 65 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 66 | if (!controller.HasValue()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 67 | return false; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | if (path) { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 71 | *path = controller.path(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | return true; | 
|  | 75 | } | 
|  | 76 |  | 
| Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 77 | static bool CgroupGetMemcgAppsPath(std::string* path) { | 
|  | 78 | CgroupController controller = CgroupMap::GetInstance().FindController("memory"); | 
|  | 79 |  | 
|  | 80 | if (!controller.HasValue()) { | 
|  | 81 | return false; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | if (path) { | 
|  | 85 | *path = controller.path(); | 
|  | 86 | if (controller.version() == 1) { | 
|  | 87 | *path += "/apps"; | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | return true; | 
|  | 92 | } | 
|  | 93 |  | 
| Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 94 | bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) { | 
|  | 95 | auto controller = CgroupMap::GetInstance().FindControllerByPath(path); | 
|  | 96 |  | 
|  | 97 | if (!controller.HasValue()) { | 
|  | 98 | return false; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | if (cgroup_name) { | 
|  | 102 | *cgroup_name = controller.name(); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | return true; | 
|  | 106 | } | 
|  | 107 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 108 | bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) { | 
|  | 109 | const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
| Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 110 | const IProfileAttribute* attr = tp.GetAttribute(attr_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 111 |  | 
|  | 112 | if (attr == nullptr) { | 
|  | 113 | return false; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if (path) { | 
|  | 117 | *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str()); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | return true; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { | 
|  | 124 | const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
| Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 125 | const IProfileAttribute* attr = tp.GetAttribute(attr_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 126 |  | 
|  | 127 | if (attr == nullptr) { | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | if (!attr->GetPathForTask(tid, path)) { | 
|  | 132 | PLOG(ERROR) << "Failed to find cgroup for tid " << tid; | 
|  | 133 | return false; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | return true; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | bool UsePerAppMemcg() { | 
|  | 140 | bool low_ram_device = GetBoolProperty("ro.config.low_ram", false); | 
|  | 141 | return GetBoolProperty("ro.config.per_app_memcg", low_ram_device); | 
|  | 142 | } | 
|  | 143 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 144 | static bool isMemoryCgroupSupported() { | 
| Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 145 | static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 146 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 147 | return memcg_supported; | 
| Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 150 | void DropTaskProfilesResourceCaching() { | 
| Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 151 | TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_TASK); | 
|  | 152 | TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_PROCESS); | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 155 | 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] | 156 | return TaskProfiles::GetInstance().SetProcessProfiles( | 
|  | 157 | uid, pid, std::span<const std::string>(profiles), false); | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | bool SetProcessProfiles(uid_t uid, pid_t pid, std::initializer_list<std::string_view> profiles) { | 
|  | 161 | return TaskProfiles::GetInstance().SetProcessProfiles( | 
|  | 162 | uid, pid, std::span<const std::string_view>(profiles), false); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | 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] | 166 | return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | 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] | 170 | return TaskProfiles::GetInstance().SetProcessProfiles( | 
|  | 171 | uid, pid, std::span<const std::string>(profiles), true); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 174 | 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] | 175 | return TaskProfiles::GetInstance().SetTaskProfiles(tid, std::span<const std::string>(profiles), | 
|  | 176 | use_fd_cache); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | bool SetTaskProfiles(int tid, std::initializer_list<std::string_view> profiles, bool use_fd_cache) { | 
|  | 180 | return TaskProfiles::GetInstance().SetTaskProfiles( | 
|  | 181 | tid, std::span<const std::string_view>(profiles), use_fd_cache); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | 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] | 185 | return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
| Jiyong Park | 8bf5940 | 2022-04-01 12:24:22 +0900 | [diff] [blame] | 188 | // C wrapper for SetProcessProfiles. | 
|  | 189 | // No need to have this in the header file because this function is specifically for crosvm. Crosvm | 
|  | 190 | // which is written in Rust has its own declaration of this foreign function and doesn't rely on the | 
|  | 191 | // header. See | 
|  | 192 | // https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3574427/5/src/linux/android.rs#12 | 
|  | 193 | extern "C" bool android_set_process_profiles(uid_t uid, pid_t pid, size_t num_profiles, | 
|  | 194 | const char* profiles[]) { | 
| Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 195 | std::vector<std::string_view> profiles_; | 
| Jiyong Park | cc9932b | 2022-04-20 17:11:42 +0900 | [diff] [blame] | 196 | profiles_.reserve(num_profiles); | 
| Jiyong Park | 8bf5940 | 2022-04-01 12:24:22 +0900 | [diff] [blame] | 197 | for (size_t i = 0; i < num_profiles; i++) { | 
|  | 198 | profiles_.emplace_back(profiles[i]); | 
|  | 199 | } | 
| Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 200 | return SetProcessProfiles(uid, pid, std::span<const std::string_view>(profiles_)); | 
| Jiyong Park | 8bf5940 | 2022-04-01 12:24:22 +0900 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
| T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 203 | bool SetUserProfiles(uid_t uid, const std::vector<std::string>& profiles) { | 
|  | 204 | return TaskProfiles::GetInstance().SetUserProfiles(uid, std::span<const std::string>(profiles), | 
|  | 205 | false); | 
|  | 206 | } | 
|  | 207 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 208 | static std::string ConvertUidToPath(const char* cgroup, uid_t uid) { | 
| T.J. Mercier | aa6158b | 2023-07-26 22:12:44 +0000 | [diff] [blame] | 209 | return StringPrintf("%s/uid_%u", cgroup, uid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 212 | static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) { | 
| T.J. Mercier | aa6158b | 2023-07-26 22:12:44 +0000 | [diff] [blame] | 213 | return StringPrintf("%s/uid_%u/pid_%d", cgroup, uid, pid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 214 | } | 
|  | 215 |  | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 216 | static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid, unsigned int retries) { | 
|  | 217 | int ret = 0; | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 218 | auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid); | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 219 |  | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 220 | while (retries--) { | 
|  | 221 | ret = rmdir(uid_pid_path.c_str()); | 
|  | 222 | if (!ret || errno != EBUSY) break; | 
|  | 223 | std::this_thread::sleep_for(5ms); | 
|  | 224 | } | 
|  | 225 |  | 
| T.J. Mercier | 9c8c748 | 2023-07-12 18:18:42 +0000 | [diff] [blame] | 226 | if (!ret && uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END) { | 
|  | 227 | // Isolated UIDs are unlikely to be reused soon after removal, | 
|  | 228 | // so free up the kernel resources for the UID level cgroup. | 
|  | 229 | const auto uid_path = ConvertUidToPath(cgroup, uid); | 
|  | 230 | ret = rmdir(uid_path.c_str()); | 
|  | 231 | } | 
|  | 232 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 233 | return ret; | 
|  | 234 | } | 
|  | 235 |  | 
| Suren Baghdasaryan | 4345f3f | 2022-04-28 13:24:28 -0700 | [diff] [blame] | 236 | static bool RemoveUidProcessGroups(const std::string& uid_path, bool empty_only) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 237 | 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] | 238 | bool empty = true; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 239 | if (uid != NULL) { | 
| Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 240 | dirent* dir; | 
|  | 241 | while ((dir = readdir(uid.get())) != nullptr) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 242 | if (dir->d_type != DT_DIR) { | 
|  | 243 | continue; | 
|  | 244 | } | 
|  | 245 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 246 | if (!StartsWith(dir->d_name, "pid_")) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 247 | continue; | 
|  | 248 | } | 
|  | 249 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 250 | auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name); | 
| Suren Baghdasaryan | 4345f3f | 2022-04-28 13:24:28 -0700 | [diff] [blame] | 251 | if (empty_only) { | 
|  | 252 | struct stat st; | 
|  | 253 | auto procs_file = StringPrintf("%s/%s", path.c_str(), | 
|  | 254 | PROCESSGROUP_CGROUP_PROCS_FILE); | 
|  | 255 | if (stat(procs_file.c_str(), &st) == -1) { | 
|  | 256 | PLOG(ERROR) << "Failed to get stats for " << procs_file; | 
|  | 257 | continue; | 
|  | 258 | } | 
|  | 259 | if (st.st_size > 0) { | 
|  | 260 | // skip non-empty groups | 
|  | 261 | LOG(VERBOSE) << "Skipping non-empty group " << path; | 
|  | 262 | empty = false; | 
|  | 263 | continue; | 
|  | 264 | } | 
|  | 265 | } | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 266 | LOG(VERBOSE) << "Removing " << path; | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 267 | if (rmdir(path.c_str()) == -1) { | 
|  | 268 | if (errno != EBUSY) { | 
|  | 269 | PLOG(WARNING) << "Failed to remove " << path; | 
|  | 270 | } | 
|  | 271 | empty = false; | 
|  | 272 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 273 | } | 
|  | 274 | } | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 275 | return empty; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
| Suren Baghdasaryan | 4345f3f | 2022-04-28 13:24:28 -0700 | [diff] [blame] | 278 | void removeAllProcessGroupsInternal(bool empty_only) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 279 | std::vector<std::string> cgroups; | 
| Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 280 | std::string path, memcg_apps_path; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 281 |  | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 282 | if (CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &path)) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 283 | cgroups.push_back(path); | 
|  | 284 | } | 
| Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 285 | if (CgroupGetMemcgAppsPath(&memcg_apps_path) && memcg_apps_path != path) { | 
|  | 286 | cgroups.push_back(memcg_apps_path); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 287 | } | 
|  | 288 |  | 
|  | 289 | for (std::string cgroup_root_path : cgroups) { | 
|  | 290 | 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] | 291 | if (root == NULL) { | 
| Bart Van Assche | 6e814b0 | 2022-02-03 19:24:42 +0000 | [diff] [blame] | 292 | PLOG(ERROR) << __func__ << " failed to open " << cgroup_root_path; | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 293 | } else { | 
|  | 294 | dirent* dir; | 
|  | 295 | while ((dir = readdir(root.get())) != nullptr) { | 
|  | 296 | if (dir->d_type != DT_DIR) { | 
|  | 297 | continue; | 
|  | 298 | } | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 299 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 300 | if (!StartsWith(dir->d_name, "uid_")) { | 
|  | 301 | continue; | 
|  | 302 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 303 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 304 | auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name); | 
| Suren Baghdasaryan | 4345f3f | 2022-04-28 13:24:28 -0700 | [diff] [blame] | 305 | if (!RemoveUidProcessGroups(path, empty_only)) { | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 306 | LOG(VERBOSE) << "Skip removing " << path; | 
|  | 307 | continue; | 
|  | 308 | } | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 309 | LOG(VERBOSE) << "Removing " << path; | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 310 | if (rmdir(path.c_str()) == -1 && errno != EBUSY) { | 
|  | 311 | PLOG(WARNING) << "Failed to remove " << path; | 
|  | 312 | } | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 313 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 314 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 315 | } | 
|  | 316 | } | 
|  | 317 |  | 
| Suren Baghdasaryan | 4345f3f | 2022-04-28 13:24:28 -0700 | [diff] [blame] | 318 | void removeAllProcessGroups() { | 
|  | 319 | LOG(VERBOSE) << "removeAllProcessGroups()"; | 
|  | 320 | removeAllProcessGroupsInternal(false); | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | void removeAllEmptyProcessGroups() { | 
|  | 324 | LOG(VERBOSE) << "removeAllEmptyProcessGroups()"; | 
|  | 325 | removeAllProcessGroupsInternal(true); | 
|  | 326 | } | 
|  | 327 |  | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 328 | /** | 
|  | 329 | * Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by | 
|  | 330 | * the user root. Ownership for the newly created cgroup and all of its files must thus be | 
|  | 331 | * transferred for the user/group passed as uid/gid before system_server can properly access them. | 
|  | 332 | */ | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 333 | 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] | 334 | if (mkdir(path.c_str(), mode) == -1) { | 
|  | 335 | if (errno == EEXIST) { | 
|  | 336 | // Directory already exists and permissions have been set at the time it was created | 
|  | 337 | return true; | 
|  | 338 | } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 339 | return false; | 
|  | 340 | } | 
|  | 341 |  | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 342 | auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir); | 
|  | 343 |  | 
|  | 344 | if (dir == NULL) { | 
|  | 345 | PLOG(ERROR) << "opendir failed for " << path; | 
|  | 346 | goto err; | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | struct dirent* dir_entry; | 
|  | 350 | while ((dir_entry = readdir(dir.get()))) { | 
|  | 351 | if (!strcmp("..", dir_entry->d_name)) { | 
|  | 352 | continue; | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | std::string file_path = path + "/" + dir_entry->d_name; | 
|  | 356 |  | 
|  | 357 | if (lchown(file_path.c_str(), uid, gid) < 0) { | 
|  | 358 | PLOG(ERROR) << "lchown failed for " << file_path; | 
|  | 359 | goto err; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) { | 
|  | 363 | PLOG(ERROR) << "fchmodat failed for " << file_path; | 
|  | 364 | goto err; | 
|  | 365 | } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
|  | 368 | return true; | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 369 | err: | 
|  | 370 | int saved_errno = errno; | 
|  | 371 | rmdir(path.c_str()); | 
|  | 372 | errno = saved_errno; | 
|  | 373 |  | 
|  | 374 | return false; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 377 | // Returns number of processes killed on success | 
|  | 378 | // 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] | 379 | // Returns -1 on error | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 380 | static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) { | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 381 | // We separate all of the pids in the cgroup into those pids that are also the leaders of | 
|  | 382 | // process groups (stored in the pgids set) and those that are not (stored in the pids set). | 
|  | 383 | std::set<pid_t> pgids; | 
|  | 384 | pgids.emplace(initialPid); | 
|  | 385 | std::set<pid_t> pids; | 
| Suren Baghdasaryan | 4f7cc8c | 2023-03-02 14:12:49 -0800 | [diff] [blame] | 386 | int processes = 0; | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 387 |  | 
| Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 388 | std::unique_ptr<FILE, decltype(&fclose)> fd(nullptr, fclose); | 
|  | 389 |  | 
|  | 390 | if (CgroupsAvailable()) { | 
|  | 391 | auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE; | 
|  | 392 | fd.reset(fopen(path.c_str(), "re")); | 
|  | 393 | if (!fd) { | 
|  | 394 | if (errno == ENOENT) { | 
|  | 395 | // This happens when process is already dead | 
|  | 396 | return 0; | 
|  | 397 | } | 
|  | 398 | PLOG(WARNING) << __func__ << " failed to open process cgroup uid " << uid << " pid " | 
|  | 399 | << initialPid; | 
|  | 400 | return -1; | 
|  | 401 | } | 
|  | 402 | pid_t pid; | 
|  | 403 | bool file_is_empty = true; | 
|  | 404 | while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) { | 
| Suren Baghdasaryan | 4f7cc8c | 2023-03-02 14:12:49 -0800 | [diff] [blame] | 405 | processes++; | 
| Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 406 | file_is_empty = false; | 
|  | 407 | if (pid == 0) { | 
|  | 408 | // Should never happen...  but if it does, trying to kill this | 
|  | 409 | // will boomerang right back and kill us!  Let's not let that happen. | 
|  | 410 | LOG(WARNING) | 
|  | 411 | << "Yikes, we've been told to kill pid 0!  How about we don't do that?"; | 
|  | 412 | continue; | 
|  | 413 | } | 
|  | 414 | pid_t pgid = getpgid(pid); | 
|  | 415 | if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed"; | 
|  | 416 | if (pgid == pid) { | 
|  | 417 | pgids.emplace(pid); | 
|  | 418 | } else { | 
|  | 419 | pids.emplace(pid); | 
|  | 420 | } | 
|  | 421 | } | 
| Jing Ji | 304c0f1 | 2023-02-27 21:58:19 -0800 | [diff] [blame] | 422 | if (!file_is_empty) { | 
|  | 423 | // Erase all pids that will be killed when we kill the process groups. | 
|  | 424 | for (auto it = pids.begin(); it != pids.end();) { | 
|  | 425 | pid_t pgid = getpgid(*it); | 
|  | 426 | if (pgids.count(pgid) == 1) { | 
|  | 427 | it = pids.erase(it); | 
|  | 428 | } else { | 
|  | 429 | ++it; | 
|  | 430 | } | 
| Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 431 | } | 
|  | 432 | } | 
|  | 433 | } | 
|  | 434 |  | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 435 | // Kill all process groups. | 
|  | 436 | for (const auto pgid : pgids) { | 
|  | 437 | LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid | 
|  | 438 | << " as part of process cgroup " << initialPid; | 
|  | 439 |  | 
| Suren Baghdasaryan | 4f7cc8c | 2023-03-02 14:12:49 -0800 | [diff] [blame] | 440 | if (kill(-pgid, signal) == -1 && errno != ESRCH) { | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 441 | PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed"; | 
|  | 442 | } | 
|  | 443 | } | 
|  | 444 |  | 
|  | 445 | // Kill remaining pids. | 
|  | 446 | for (const auto pid : pids) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 447 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup " | 
|  | 448 | << initialPid; | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 449 |  | 
| Suren Baghdasaryan | 4f7cc8c | 2023-03-02 14:12:49 -0800 | [diff] [blame] | 450 | if (kill(pid, signal) == -1 && errno != ESRCH) { | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 451 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
| Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 455 | return (!fd || feof(fd.get())) ? processes : -1; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
| Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 458 | static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries, | 
|  | 459 | int* max_processes) { | 
| Bart Van Assche | 5a3c3f7 | 2023-03-22 13:21:03 -0700 | [diff] [blame] | 460 | CHECK_GE(uid, 0); | 
|  | 461 | CHECK_GT(initialPid, 0); | 
|  | 462 |  | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 463 | std::string hierarchy_root_path; | 
| Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 464 | if (CgroupsAvailable()) { | 
|  | 465 | CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path); | 
|  | 466 | } | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 467 | const char* cgroup = hierarchy_root_path.c_str(); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 468 |  | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 469 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 470 |  | 
| Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 471 | if (max_processes != nullptr) { | 
|  | 472 | *max_processes = 0; | 
|  | 473 | } | 
|  | 474 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 475 | int retry = retries; | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 476 | int processes; | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 477 | while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) { | 
| Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 478 | if (max_processes != nullptr && processes > *max_processes) { | 
|  | 479 | *max_processes = processes; | 
|  | 480 | } | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 481 | LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid; | 
| Inseob Kim | 110cd77 | 2022-12-14 11:12:48 +0900 | [diff] [blame] | 482 | if (!CgroupsAvailable()) { | 
|  | 483 | // makes no sense to retry, because there are no cgroup_procs file | 
|  | 484 | processes = 0;  // no remaining processes | 
|  | 485 | break; | 
|  | 486 | } | 
| Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 487 | if (retry > 0) { | 
| Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 488 | std::this_thread::sleep_for(5ms); | 
| Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 489 | --retry; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 490 | } else { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 491 | break; | 
|  | 492 | } | 
|  | 493 | } | 
|  | 494 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 495 | if (processes < 0) { | 
|  | 496 | PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid " | 
|  | 497 | << initialPid; | 
|  | 498 | return -1; | 
|  | 499 | } | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 500 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 501 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 502 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 503 |  | 
|  | 504 | // We only calculate the number of 'processes' when killing the processes. | 
|  | 505 | // In the retries == 0 case, we only kill the processes once and therefore | 
|  | 506 | // will not have waited then recalculated how many processes are remaining | 
|  | 507 | // after the first signals have been sent. | 
|  | 508 | // Logging anything regarding the number of 'processes' here does not make sense. | 
| Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 509 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 510 | if (processes == 0) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 511 | if (retries > 0) { | 
|  | 512 | LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid | 
|  | 513 | << " in " << static_cast<int>(ms) << "ms"; | 
|  | 514 | } | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 515 |  | 
| Inseob Kim | a049a99 | 2022-11-17 23:42:12 +0900 | [diff] [blame] | 516 | if (!CgroupsAvailable()) { | 
|  | 517 | // nothing to do here, if cgroups isn't available | 
|  | 518 | return 0; | 
|  | 519 | } | 
|  | 520 |  | 
| Suren Baghdasaryan | fd93378 | 2022-05-26 17:40:25 -0700 | [diff] [blame] | 521 | // 400 retries correspond to 2 secs max timeout | 
|  | 522 | int err = RemoveProcessGroup(cgroup, uid, initialPid, 400); | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 523 |  | 
|  | 524 | if (isMemoryCgroupSupported() && UsePerAppMemcg()) { | 
| Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 525 | std::string memcg_apps_path; | 
|  | 526 | if (CgroupGetMemcgAppsPath(&memcg_apps_path) && | 
| Suren Baghdasaryan | fd93378 | 2022-05-26 17:40:25 -0700 | [diff] [blame] | 527 | RemoveProcessGroup(memcg_apps_path.c_str(), uid, initialPid, 400) < 0) { | 
| Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 528 | return -1; | 
|  | 529 | } | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
|  | 532 | return err; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 533 | } else { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 534 | if (retries > 0) { | 
|  | 535 | LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid | 
|  | 536 | << " in " << static_cast<int>(ms) << "ms, " << processes | 
|  | 537 | << " processes remain"; | 
|  | 538 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 539 | return -1; | 
|  | 540 | } | 
|  | 541 | } | 
|  | 542 |  | 
| Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 543 | int killProcessGroup(uid_t uid, int initialPid, int signal, int* max_processes) { | 
|  | 544 | return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/, max_processes); | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 545 | } | 
|  | 546 |  | 
| Tom Cherry | d89ed13 | 2019-11-19 14:19:40 -0800 | [diff] [blame] | 547 | int killProcessGroupOnce(uid_t uid, int initialPid, int signal, int* max_processes) { | 
|  | 548 | return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/, max_processes); | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 549 | } | 
|  | 550 |  | 
| T.J. Mercier | 22006bf | 2023-04-13 21:48:55 +0000 | [diff] [blame] | 551 | int sendSignalToProcessGroup(uid_t uid, int initialPid, int signal) { | 
|  | 552 | std::string hierarchy_root_path; | 
|  | 553 | if (CgroupsAvailable()) { | 
|  | 554 | CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path); | 
|  | 555 | } | 
|  | 556 | const char* cgroup = hierarchy_root_path.c_str(); | 
|  | 557 | return DoKillProcessGroupOnce(cgroup, uid, initialPid, signal); | 
|  | 558 | } | 
|  | 559 |  | 
| Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 560 | static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup, | 
|  | 561 | bool activate_controllers) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 562 | auto uid_path = ConvertUidToPath(cgroup.c_str(), uid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 563 |  | 
| Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 564 | struct stat cgroup_stat; | 
|  | 565 | mode_t cgroup_mode = 0750; | 
| Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 566 | uid_t cgroup_uid = AID_SYSTEM; | 
| Bart Van Assche | 32a9b1c | 2022-03-10 21:42:40 +0000 | [diff] [blame] | 567 | gid_t cgroup_gid = AID_SYSTEM; | 
| Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 568 | int ret = 0; | 
| Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 569 |  | 
| Bart Van Assche | 55a9b1e | 2022-03-23 10:13:18 -0700 | [diff] [blame] | 570 | if (stat(cgroup.c_str(), &cgroup_stat) < 0) { | 
| Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 571 | PLOG(ERROR) << "Failed to get stats for " << cgroup; | 
|  | 572 | } else { | 
|  | 573 | cgroup_mode = cgroup_stat.st_mode; | 
| Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 574 | cgroup_uid = cgroup_stat.st_uid; | 
| Marco Ballesio | 9e628a6 | 2021-02-11 14:44:53 -0800 | [diff] [blame] | 575 | cgroup_gid = cgroup_stat.st_gid; | 
|  | 576 | } | 
|  | 577 |  | 
| Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 578 | if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 579 | PLOG(ERROR) << "Failed to make and chown " << uid_path; | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 580 | return -errno; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 581 | } | 
| Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 582 | if (activate_controllers) { | 
|  | 583 | ret = CgroupMap::GetInstance().ActivateControllers(uid_path); | 
|  | 584 | if (ret) { | 
|  | 585 | LOG(ERROR) << "Failed to activate controllers in " << uid_path; | 
|  | 586 | return ret; | 
|  | 587 | } | 
|  | 588 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 589 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 590 | auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 591 |  | 
| Bart Van Assche | 8eb7a6e | 2022-03-29 23:47:08 +0000 | [diff] [blame] | 592 | if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 593 | PLOG(ERROR) << "Failed to make and chown " << uid_pid_path; | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 594 | return -errno; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 595 | } | 
|  | 596 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 597 | auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 598 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 599 | if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 600 | ret = -errno; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 601 | PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 602 | } | 
|  | 603 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 604 | return ret; | 
|  | 605 | } | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 606 |  | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 607 | int createProcessGroup(uid_t uid, int initialPid, bool memControl) { | 
| Bart Van Assche | 5a3c3f7 | 2023-03-22 13:21:03 -0700 | [diff] [blame] | 608 | CHECK_GE(uid, 0); | 
|  | 609 | CHECK_GT(initialPid, 0); | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 610 |  | 
|  | 611 | if (memControl && !UsePerAppMemcg()) { | 
|  | 612 | PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support"; | 
|  | 613 | return -EINVAL; | 
|  | 614 | } | 
|  | 615 |  | 
| Bart Van Assche | 4c95712 | 2022-02-04 18:19:45 +0000 | [diff] [blame] | 616 | if (std::string memcg_apps_path; | 
|  | 617 | isMemoryCgroupSupported() && UsePerAppMemcg() && CgroupGetMemcgAppsPath(&memcg_apps_path)) { | 
|  | 618 | // Note by bvanassche: passing 'false' as fourth argument below implies that the v1 | 
|  | 619 | // hierarchy is used. It is not clear to me whether the above conditions guarantee that the | 
|  | 620 | // v1 hierarchy is used. | 
|  | 621 | int ret = createProcessGroupInternal(uid, initialPid, memcg_apps_path, false); | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 622 | if (ret != 0) { | 
|  | 623 | return ret; | 
|  | 624 | } | 
|  | 625 | } | 
|  | 626 |  | 
| Bart Van Assche | 5a3c3f7 | 2023-03-22 13:21:03 -0700 | [diff] [blame] | 627 | std::string cgroup; | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 628 | CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup); | 
| Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 629 | return createProcessGroupInternal(uid, initialPid, cgroup, true); | 
| Marco Ballesio | 4dac816 | 2021-02-10 16:35:44 -0800 | [diff] [blame] | 630 | } | 
|  | 631 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 632 | 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] | 633 | if (!isMemoryCgroupSupported()) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 634 | PLOG(ERROR) << "Memcg is not mounted."; | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 635 | return false; | 
|  | 636 | } | 
|  | 637 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 638 | std::string path; | 
|  | 639 | if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) { | 
|  | 640 | PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'"; | 
|  | 641 | return false; | 
|  | 642 | } | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 643 |  | 
|  | 644 | if (!WriteStringToFile(std::to_string(value), path)) { | 
|  | 645 | PLOG(ERROR) << "Failed to write '" << value << "' to " << path; | 
|  | 646 | return false; | 
|  | 647 | } | 
|  | 648 | return true; | 
|  | 649 | } | 
|  | 650 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 651 | bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) { | 
|  | 652 | return SetProcessGroupValue(pid, "MemSwappiness", swappiness); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 653 | } | 
|  | 654 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 655 | bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) { | 
|  | 656 | return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 657 | } | 
|  | 658 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 659 | bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) { | 
|  | 660 | return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 661 | } | 
| Marco Ballesio | 4e644c4 | 2021-02-24 16:30:50 -0800 | [diff] [blame] | 662 |  | 
|  | 663 | bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { | 
|  | 664 | return CgroupGetAttributePathForTask(attr_name, tid, path); | 
|  | 665 | } | 
| Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 666 |  | 
|  | 667 | bool isProfileValidForProcess(const std::string& profile_name, int uid, int pid) { | 
|  | 668 | const TaskProfile* tp = TaskProfiles::GetInstance().GetProfile(profile_name); | 
|  | 669 |  | 
|  | 670 | if (tp == nullptr) { | 
|  | 671 | return false; | 
|  | 672 | } | 
|  | 673 |  | 
|  | 674 | return tp->IsValidForProcess(uid, pid); | 
|  | 675 | } |