| 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 CgroupSetupCgroups() { | 
|  | 59 | return CgroupMap::SetupCgroups(); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) { | 
|  | 63 | const CgroupController* controller = CgroupMap::GetInstance().FindController(cgroup_name); | 
|  | 64 |  | 
|  | 65 | if (controller == nullptr) { | 
|  | 66 | return false; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | if (path) { | 
|  | 70 | *path = controller->path(); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | return true; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) { | 
|  | 77 | const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
|  | 78 | const ProfileAttribute* attr = tp.GetAttribute(attr_name); | 
|  | 79 |  | 
|  | 80 | if (attr == nullptr) { | 
|  | 81 | return false; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | if (path) { | 
|  | 85 | *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str()); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | return true; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { | 
|  | 92 | const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
|  | 93 | const ProfileAttribute* attr = tp.GetAttribute(attr_name); | 
|  | 94 |  | 
|  | 95 | if (attr == nullptr) { | 
|  | 96 | return false; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | if (!attr->GetPathForTask(tid, path)) { | 
|  | 100 | PLOG(ERROR) << "Failed to find cgroup for tid " << tid; | 
|  | 101 | return false; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | return true; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | bool UsePerAppMemcg() { | 
|  | 108 | bool low_ram_device = GetBoolProperty("ro.config.low_ram", false); | 
|  | 109 | return GetBoolProperty("ro.config.per_app_memcg", low_ram_device); | 
|  | 110 | } | 
|  | 111 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 112 | static bool isMemoryCgroupSupported() { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 113 | std::string cgroup_name; | 
|  | 114 | static bool memcg_supported = (CgroupMap::GetInstance().FindController("memory") != nullptr); | 
|  | 115 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 116 | return memcg_supported; | 
| Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 119 | bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) { | 
|  | 120 | const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
|  | 121 |  | 
|  | 122 | for (const auto& name : profiles) { | 
|  | 123 | const TaskProfile* profile = tp.GetProfile(name); | 
|  | 124 | if (profile != nullptr) { | 
|  | 125 | if (!profile->ExecuteForProcess(uid, pid)) { | 
|  | 126 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; | 
|  | 127 | } | 
|  | 128 | } else { | 
|  | 129 | PLOG(WARNING) << "Failed to find " << name << "process profile"; | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | return true; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles) { | 
|  | 137 | const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
|  | 138 |  | 
|  | 139 | for (const auto& name : profiles) { | 
|  | 140 | const TaskProfile* profile = tp.GetProfile(name); | 
|  | 141 | if (profile != nullptr) { | 
|  | 142 | if (!profile->ExecuteForTask(tid)) { | 
|  | 143 | PLOG(WARNING) << "Failed to apply " << name << " task profile"; | 
|  | 144 | } | 
|  | 145 | } else { | 
|  | 146 | PLOG(WARNING) << "Failed to find " << name << "task profile"; | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | return true; | 
|  | 151 | } | 
|  | 152 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 153 | static std::string ConvertUidToPath(const char* cgroup, uid_t uid) { | 
|  | 154 | return StringPrintf("%s/uid_%d", cgroup, uid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 157 | static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) { | 
|  | 158 | return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 161 | static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 162 | int ret; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 163 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 164 | auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid); | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 165 | ret = rmdir(uid_pid_path.c_str()); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 166 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 167 | auto uid_path = ConvertUidToPath(cgroup, uid); | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 168 | rmdir(uid_path.c_str()); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 169 |  | 
|  | 170 | return ret; | 
|  | 171 | } | 
|  | 172 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 173 | static void RemoveUidProcessGroups(const std::string& uid_path) { | 
|  | 174 | std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 175 | if (uid != NULL) { | 
| Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 176 | dirent* dir; | 
|  | 177 | while ((dir = readdir(uid.get())) != nullptr) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 178 | if (dir->d_type != DT_DIR) { | 
|  | 179 | continue; | 
|  | 180 | } | 
|  | 181 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 182 | if (!StartsWith(dir->d_name, "pid_")) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 183 | continue; | 
|  | 184 | } | 
|  | 185 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 186 | auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name); | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 187 | LOG(VERBOSE) << "Removing " << path; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 188 | if (rmdir(path.c_str()) == -1) PLOG(WARNING) << "Failed to remove " << path; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 189 | } | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 193 | void removeAllProcessGroups() { | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 194 | LOG(VERBOSE) << "removeAllProcessGroups()"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 195 |  | 
|  | 196 | std::vector<std::string> cgroups; | 
|  | 197 | std::string path; | 
|  | 198 |  | 
|  | 199 | if (CgroupGetControllerPath("cpuacct", &path)) { | 
|  | 200 | cgroups.push_back(path); | 
|  | 201 | } | 
|  | 202 | if (CgroupGetControllerPath("memory", &path)) { | 
|  | 203 | cgroups.push_back(path); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | for (std::string cgroup_root_path : cgroups) { | 
|  | 207 | 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] | 208 | if (root == NULL) { | 
|  | 209 | PLOG(ERROR) << "Failed to open " << cgroup_root_path; | 
|  | 210 | } else { | 
|  | 211 | dirent* dir; | 
|  | 212 | while ((dir = readdir(root.get())) != nullptr) { | 
|  | 213 | if (dir->d_type != DT_DIR) { | 
|  | 214 | continue; | 
|  | 215 | } | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 216 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 217 | if (!StartsWith(dir->d_name, "uid_")) { | 
|  | 218 | continue; | 
|  | 219 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 220 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 221 | auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 222 | RemoveUidProcessGroups(path); | 
|  | 223 | LOG(VERBOSE) << "Removing " << path; | 
|  | 224 | if (rmdir(path.c_str()) == -1) PLOG(WARNING) << "Failed to remove " << path; | 
|  | 225 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 226 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 227 | } | 
|  | 228 | } | 
|  | 229 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 230 | static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { | 
|  | 231 | if (mkdir(path.c_str(), mode) == -1 && errno != EEXIST) { | 
|  | 232 | return false; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | if (chown(path.c_str(), uid, gid) == -1) { | 
|  | 236 | int saved_errno = errno; | 
|  | 237 | rmdir(path.c_str()); | 
|  | 238 | errno = saved_errno; | 
|  | 239 | return false; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | return true; | 
|  | 243 | } | 
|  | 244 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 245 | // Returns number of processes killed on success | 
|  | 246 | // 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] | 247 | // Returns -1 on error | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 248 | static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) { | 
|  | 249 | auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 250 | std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose); | 
|  | 251 | if (!fd) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 252 | PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 253 | return -1; | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 254 | } | 
|  | 255 |  | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 256 | // We separate all of the pids in the cgroup into those pids that are also the leaders of | 
|  | 257 | // process groups (stored in the pgids set) and those that are not (stored in the pids set). | 
|  | 258 | std::set<pid_t> pgids; | 
|  | 259 | pgids.emplace(initialPid); | 
|  | 260 | std::set<pid_t> pids; | 
|  | 261 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 262 | pid_t pid; | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 263 | int processes = 0; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 264 | while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 265 | processes++; | 
| Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 266 | if (pid == 0) { | 
|  | 267 | // Should never happen...  but if it does, trying to kill this | 
|  | 268 | // 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] | 269 | 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] | 270 | continue; | 
|  | 271 | } | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 272 | pid_t pgid = getpgid(pid); | 
|  | 273 | if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed"; | 
|  | 274 | if (pgid == pid) { | 
|  | 275 | pgids.emplace(pid); | 
|  | 276 | } else { | 
|  | 277 | pids.emplace(pid); | 
|  | 278 | } | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | // Erase all pids that will be killed when we kill the process groups. | 
|  | 282 | for (auto it = pids.begin(); it != pids.end();) { | 
| fengshaobo | 950f6f3 | 2018-09-13 14:57:07 +0800 | [diff] [blame] | 283 | pid_t pgid = getpgid(*it); | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 284 | if (pgids.count(pgid) == 1) { | 
|  | 285 | it = pids.erase(it); | 
|  | 286 | } else { | 
|  | 287 | ++it; | 
|  | 288 | } | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | // Kill all process groups. | 
|  | 292 | for (const auto pgid : pgids) { | 
|  | 293 | LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid | 
|  | 294 | << " as part of process cgroup " << initialPid; | 
|  | 295 |  | 
|  | 296 | if (kill(-pgid, signal) == -1) { | 
|  | 297 | PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed"; | 
|  | 298 | } | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | // Kill remaining pids. | 
|  | 302 | for (const auto pid : pids) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 303 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup " | 
|  | 304 | << initialPid; | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 305 |  | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 306 | if (kill(pid, signal) == -1) { | 
|  | 307 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 308 | } | 
|  | 309 | } | 
|  | 310 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 311 | return feof(fd.get()) ? processes : -1; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 312 | } | 
|  | 313 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 314 | static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 315 | std::string cpuacct_path; | 
|  | 316 | std::string memory_path; | 
|  | 317 |  | 
|  | 318 | CgroupGetControllerPath("cpuacct", &cpuacct_path); | 
|  | 319 | CgroupGetControllerPath("memory", &memory_path); | 
|  | 320 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 321 | const char* cgroup = | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 322 | (!access(ConvertUidPidToPath(cpuacct_path.c_str(), uid, initialPid).c_str(), F_OK)) | 
|  | 323 | ? cpuacct_path.c_str() | 
|  | 324 | : memory_path.c_str(); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 325 |  | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 326 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 327 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 328 | int retry = retries; | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 329 | int processes; | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 330 | while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 331 | LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid; | 
| Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 332 | if (retry > 0) { | 
| Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 333 | std::this_thread::sleep_for(5ms); | 
| Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 334 | --retry; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 335 | } else { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 336 | break; | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 340 | if (processes < 0) { | 
|  | 341 | PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid " | 
|  | 342 | << initialPid; | 
|  | 343 | return -1; | 
|  | 344 | } | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 345 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 346 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 347 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 348 |  | 
|  | 349 | // We only calculate the number of 'processes' when killing the processes. | 
|  | 350 | // In the retries == 0 case, we only kill the processes once and therefore | 
|  | 351 | // will not have waited then recalculated how many processes are remaining | 
|  | 352 | // after the first signals have been sent. | 
|  | 353 | // Logging anything regarding the number of 'processes' here does not make sense. | 
| Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 354 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 355 | if (processes == 0) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 356 | if (retries > 0) { | 
|  | 357 | LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid | 
|  | 358 | << " in " << static_cast<int>(ms) << "ms"; | 
|  | 359 | } | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 360 | return RemoveProcessGroup(cgroup, uid, initialPid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 361 | } else { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 362 | if (retries > 0) { | 
|  | 363 | LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid | 
|  | 364 | << " in " << static_cast<int>(ms) << "ms, " << processes | 
|  | 365 | << " processes remain"; | 
|  | 366 | } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 367 | return -1; | 
|  | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 371 | int killProcessGroup(uid_t uid, int initialPid, int signal) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 372 | return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/); | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 373 | } | 
|  | 374 |  | 
|  | 375 | int killProcessGroupOnce(uid_t uid, int initialPid, int signal) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 376 | return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/); | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 377 | } | 
|  | 378 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 379 | int createProcessGroup(uid_t uid, int initialPid, bool memControl) { | 
|  | 380 | std::string cgroup; | 
|  | 381 | if (isMemoryCgroupSupported() && (memControl || UsePerAppMemcg())) { | 
|  | 382 | CgroupGetControllerPath("memory", &cgroup); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 383 | } else { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 384 | CgroupGetControllerPath("cpuacct", &cgroup); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 385 | } | 
|  | 386 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 387 | auto uid_path = ConvertUidToPath(cgroup.c_str(), uid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 388 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 389 | if (!MkdirAndChown(uid_path, 0750, AID_SYSTEM, AID_SYSTEM)) { | 
|  | 390 | PLOG(ERROR) << "Failed to make and chown " << uid_path; | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 391 | return -errno; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 394 | auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 395 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 396 | if (!MkdirAndChown(uid_pid_path, 0750, AID_SYSTEM, AID_SYSTEM)) { | 
|  | 397 | PLOG(ERROR) << "Failed to make and chown " << uid_pid_path; | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 398 | return -errno; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 399 | } | 
|  | 400 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 401 | auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 402 |  | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 403 | int ret = 0; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 404 | if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 405 | ret = -errno; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 406 | PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 407 | } | 
|  | 408 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 409 | return ret; | 
|  | 410 | } | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 411 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 412 | 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] | 413 | if (!isMemoryCgroupSupported()) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 414 | PLOG(ERROR) << "Memcg is not mounted."; | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 415 | return false; | 
|  | 416 | } | 
|  | 417 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 418 | std::string path; | 
|  | 419 | if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) { | 
|  | 420 | PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'"; | 
|  | 421 | return false; | 
|  | 422 | } | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 423 |  | 
|  | 424 | if (!WriteStringToFile(std::to_string(value), path)) { | 
|  | 425 | PLOG(ERROR) << "Failed to write '" << value << "' to " << path; | 
|  | 426 | return false; | 
|  | 427 | } | 
|  | 428 | return true; | 
|  | 429 | } | 
|  | 430 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 431 | bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) { | 
|  | 432 | return SetProcessGroupValue(pid, "MemSwappiness", swappiness); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 433 | } | 
|  | 434 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 435 | bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) { | 
|  | 436 | return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 439 | bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) { | 
|  | 440 | return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 441 | } |