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> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 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> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 33 | #include <memory> |
Elliott Hughes | 8d532e4 | 2016-06-06 21:19:55 -0700 | [diff] [blame] | 34 | #include <mutex> |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 35 | #include <set> |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 36 | #include <thread> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 37 | |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 38 | #include <android-base/file.h> |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 39 | #include <android-base/logging.h> |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 40 | #include <android-base/unique_fd.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 41 | #include <private/android_filesystem_config.h> |
| 42 | |
| 43 | #include <processgroup/processgroup.h> |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 44 | |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 45 | using android::base::WriteStringToFile; |
| 46 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 47 | using namespace std::chrono_literals; |
| 48 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 49 | #define MEM_CGROUP_PATH "/dev/memcg/apps" |
Martijn Coenen | 623b56a | 2016-02-08 11:42:25 +0100 | [diff] [blame] | 50 | #define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks" |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 51 | #define ACCT_CGROUP_PATH "/acct" |
| 52 | |
| 53 | #define PROCESSGROUP_UID_PREFIX "uid_" |
| 54 | #define PROCESSGROUP_PID_PREFIX "pid_" |
| 55 | #define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs" |
| 56 | #define PROCESSGROUP_MAX_UID_LEN 11 |
| 57 | #define PROCESSGROUP_MAX_PID_LEN 11 |
| 58 | #define PROCESSGROUP_MAX_PATH_LEN \ |
| 59 | ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \ |
| 60 | sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \ |
| 61 | sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \ |
| 62 | PROCESSGROUP_MAX_UID_LEN + \ |
| 63 | sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \ |
| 64 | PROCESSGROUP_MAX_PID_LEN + \ |
| 65 | sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \ |
| 66 | 1) |
| 67 | |
| 68 | std::once_flag init_path_flag; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 69 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 70 | class ProcessGroup { |
| 71 | public: |
| 72 | ProcessGroup() : buf_ptr_(buf_), buf_len_(0) {} |
| 73 | |
| 74 | bool Open(uid_t uid, int pid); |
| 75 | |
| 76 | // Return positive number and sets *pid = next pid in process cgroup on success |
| 77 | // Returns 0 if there are no pids left in the process cgroup |
| 78 | // Returns -errno if an error was encountered |
| 79 | int GetOneAppProcess(pid_t* pid); |
| 80 | |
| 81 | private: |
| 82 | // Returns positive number of bytes filled on success |
| 83 | // Returns 0 if there was nothing to read |
| 84 | // Returns -errno if an error was encountered |
| 85 | int RefillBuffer(); |
| 86 | |
| 87 | android::base::unique_fd fd_; |
| 88 | char buf_[128]; |
| 89 | char* buf_ptr_; |
| 90 | size_t buf_len_; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 93 | static const char* getCgroupRootPath() { |
| 94 | static const char* cgroup_root_path = NULL; |
| 95 | std::call_once(init_path_flag, [&]() { |
Martijn Coenen | 623b56a | 2016-02-08 11:42:25 +0100 | [diff] [blame] | 96 | // Check if mem cgroup is mounted, only then check for write-access to avoid |
| 97 | // SELinux denials |
| 98 | cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ? |
| 99 | ACCT_CGROUP_PATH : MEM_CGROUP_PATH; |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 100 | }); |
| 101 | return cgroup_root_path; |
| 102 | } |
| 103 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 104 | static int convertUidToPath(char *path, size_t size, uid_t uid) |
| 105 | { |
| 106 | return snprintf(path, size, "%s/%s%d", |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 107 | getCgroupRootPath(), |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 108 | PROCESSGROUP_UID_PREFIX, |
| 109 | uid); |
| 110 | } |
| 111 | |
| 112 | static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid) |
| 113 | { |
| 114 | return snprintf(path, size, "%s/%s%d/%s%d", |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 115 | getCgroupRootPath(), |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 116 | PROCESSGROUP_UID_PREFIX, |
| 117 | uid, |
| 118 | PROCESSGROUP_PID_PREFIX, |
| 119 | pid); |
| 120 | } |
| 121 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 122 | bool ProcessGroup::Open(uid_t uid, int pid) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 123 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 124 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 125 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 126 | |
| 127 | int fd = open(path, O_RDONLY); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 128 | if (fd < 0) return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 129 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 130 | fd_.reset(fd); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 131 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 132 | LOG(VERBOSE) << "Initialized context for " << path; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 133 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 134 | return true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 137 | int ProcessGroup::RefillBuffer() { |
| 138 | memmove(buf_, buf_ptr_, buf_len_); |
| 139 | buf_ptr_ = buf_; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 140 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 141 | ssize_t ret = read(fd_, buf_ptr_ + buf_len_, sizeof(buf_) - buf_len_ - 1); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 142 | if (ret < 0) { |
| 143 | return -errno; |
| 144 | } else if (ret == 0) { |
| 145 | return 0; |
| 146 | } |
| 147 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 148 | buf_len_ += ret; |
| 149 | buf_[buf_len_] = 0; |
| 150 | LOG(VERBOSE) << "Read " << ret << " to buffer: " << buf_; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 151 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 152 | assert(buf_len_ <= sizeof(buf_)); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 157 | int ProcessGroup::GetOneAppProcess(pid_t* out_pid) { |
| 158 | *out_pid = 0; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 159 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 160 | char* eptr; |
| 161 | while ((eptr = static_cast<char*>(memchr(buf_ptr_, '\n', buf_len_))) == nullptr) { |
| 162 | int ret = RefillBuffer(); |
| 163 | if (ret <= 0) return ret; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | *eptr = '\0'; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 167 | char* pid_eptr = nullptr; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 168 | errno = 0; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 169 | long pid = strtol(buf_ptr_, &pid_eptr, 10); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 170 | if (errno != 0) { |
| 171 | return -errno; |
| 172 | } |
| 173 | if (pid_eptr != eptr) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 174 | errno = EINVAL; |
| 175 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 178 | buf_len_ -= (eptr - buf_ptr_) + 1; |
| 179 | buf_ptr_ = eptr + 1; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 180 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 181 | *out_pid = static_cast<pid_t>(pid); |
| 182 | return 1; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static int removeProcessGroup(uid_t uid, int pid) |
| 186 | { |
| 187 | int ret; |
| 188 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 189 | |
| 190 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 191 | ret = rmdir(path); |
| 192 | |
| 193 | convertUidToPath(path, sizeof(path), uid); |
| 194 | rmdir(path); |
| 195 | |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static void removeUidProcessGroups(const char *uid_path) |
| 200 | { |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 201 | std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 202 | if (uid != NULL) { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 203 | dirent* dir; |
| 204 | while ((dir = readdir(uid.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 205 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 206 | |
| 207 | if (dir->d_type != DT_DIR) { |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 216 | LOG(VERBOSE) << "Removing " << path; |
| 217 | if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void removeAllProcessGroups() |
| 223 | { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 224 | LOG(VERBOSE) << "removeAllProcessGroups()"; |
Elliott Hughes | b6e1d15 | 2016-08-03 13:29:04 -0700 | [diff] [blame] | 225 | const char* cgroup_root_path = getCgroupRootPath(); |
James Hawkins | 22b6f7a | 2016-02-19 11:10:30 -0800 | [diff] [blame] | 226 | std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 227 | if (root == NULL) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 228 | PLOG(ERROR) << "Failed to open " << cgroup_root_path; |
Colin Cross | c15dd04 | 2014-08-20 14:11:13 -0700 | [diff] [blame] | 229 | } else { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 230 | dirent* dir; |
| 231 | while ((dir = readdir(root.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 232 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 233 | |
| 234 | if (dir->d_type != DT_DIR) { |
| 235 | continue; |
| 236 | } |
| 237 | if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) { |
| 238 | continue; |
| 239 | } |
| 240 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 241 | snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 242 | removeUidProcessGroups(path); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 243 | LOG(VERBOSE) << "Removing " << path; |
| 244 | if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 245 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 249 | // Returns number of processes killed on success |
| 250 | // Returns 0 if there are no processes in the process cgroup left to kill |
| 251 | // Returns -errno on error |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 252 | static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 253 | ProcessGroup process_group; |
| 254 | if (!process_group.Open(uid, initialPid)) { |
| 255 | PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid; |
| 256 | return -errno; |
| 257 | } |
| 258 | |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 259 | // We separate all of the pids in the cgroup into those pids that are also the leaders of |
| 260 | // process groups (stored in the pgids set) and those that are not (stored in the pids set). |
| 261 | std::set<pid_t> pgids; |
| 262 | pgids.emplace(initialPid); |
| 263 | std::set<pid_t> pids; |
| 264 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 265 | int ret; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 266 | pid_t pid; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 267 | int processes = 0; |
| 268 | while ((ret = process_group.GetOneAppProcess(&pid)) > 0 && pid >= 0) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 269 | processes++; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 270 | if (pid == 0) { |
| 271 | // Should never happen... but if it does, trying to kill this |
| 272 | // 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] | 273 | 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] | 274 | continue; |
| 275 | } |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 276 | pid_t pgid = getpgid(pid); |
| 277 | if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed"; |
| 278 | if (pgid == pid) { |
| 279 | pgids.emplace(pid); |
| 280 | } else { |
| 281 | pids.emplace(pid); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Erase all pids that will be killed when we kill the process groups. |
| 286 | for (auto it = pids.begin(); it != pids.end();) { |
| 287 | pid_t pgid = getpgid(pid); |
| 288 | if (pgids.count(pgid) == 1) { |
| 289 | it = pids.erase(it); |
| 290 | } else { |
| 291 | ++it; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Kill all process groups. |
| 296 | for (const auto pgid : pgids) { |
| 297 | LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid |
| 298 | << " as part of process cgroup " << initialPid; |
| 299 | |
| 300 | if (kill(-pgid, signal) == -1) { |
| 301 | PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed"; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Kill remaining pids. |
| 306 | for (const auto pid : pids) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 307 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup " |
| 308 | << initialPid; |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 309 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 310 | if (kill(pid, signal) == -1) { |
| 311 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 315 | return ret >= 0 ? processes : ret; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 318 | static int killProcessGroup(uid_t uid, int initialPid, int signal, int retries) { |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 319 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 320 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 321 | int retry = retries; |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 322 | int processes; |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 323 | while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 324 | LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid; |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 325 | if (retry > 0) { |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 326 | std::this_thread::sleep_for(5ms); |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 327 | --retry; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 328 | } else { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 333 | if (processes < 0) { |
| 334 | PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid " |
| 335 | << initialPid; |
| 336 | return -1; |
| 337 | } |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 338 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 339 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 340 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 341 | |
| 342 | // We only calculate the number of 'processes' when killing the processes. |
| 343 | // In the retries == 0 case, we only kill the processes once and therefore |
| 344 | // will not have waited then recalculated how many processes are remaining |
| 345 | // after the first signals have been sent. |
| 346 | // Logging anything regarding the number of 'processes' here does not make sense. |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 347 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 348 | if (processes == 0) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 349 | if (retries > 0) { |
| 350 | LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid |
| 351 | << " in " << static_cast<int>(ms) << "ms"; |
| 352 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 353 | return removeProcessGroup(uid, initialPid); |
| 354 | } else { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 355 | if (retries > 0) { |
| 356 | LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid |
| 357 | << " in " << static_cast<int>(ms) << "ms, " << processes |
| 358 | << " processes remain"; |
| 359 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 360 | return -1; |
| 361 | } |
| 362 | } |
| 363 | |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 364 | int killProcessGroup(uid_t uid, int initialPid, int signal) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 365 | return killProcessGroup(uid, initialPid, signal, 40 /*retries*/); |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | int killProcessGroupOnce(uid_t uid, int initialPid, int signal) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 369 | return killProcessGroup(uid, initialPid, signal, 0 /*retries*/); |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 372 | static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid) |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 373 | { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 374 | if (mkdir(path, mode) == -1 && errno != EEXIST) { |
| 375 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 378 | if (chown(path, uid, gid) == -1) { |
| 379 | int saved_errno = errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 380 | rmdir(path); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 381 | errno = saved_errno; |
| 382 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 385 | return true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | int createProcessGroup(uid_t uid, int initialPid) |
| 389 | { |
| 390 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 391 | |
| 392 | convertUidToPath(path, sizeof(path), uid); |
| 393 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 394 | if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 395 | PLOG(ERROR) << "Failed to make and chown " << path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 396 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | convertUidPidToPath(path, sizeof(path), uid, initialPid); |
| 400 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 401 | if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 402 | PLOG(ERROR) << "Failed to make and chown " << path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 403 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 407 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 408 | int ret = 0; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 409 | if (!WriteStringToFile(std::to_string(initialPid), path)) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 410 | ret = -errno; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 411 | PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 414 | return ret; |
| 415 | } |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 416 | |
| 417 | static bool setProcessGroupValue(uid_t uid, int pid, const char* fileName, int64_t value) { |
| 418 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 419 | if (strcmp(getCgroupRootPath(), MEM_CGROUP_PATH)) { |
| 420 | PLOG(ERROR) << "Memcg is not mounted." << path; |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 425 | strlcat(path, fileName, sizeof(path)); |
| 426 | |
| 427 | if (!WriteStringToFile(std::to_string(value), path)) { |
| 428 | PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
| 429 | return false; |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | bool setProcessGroupSwappiness(uid_t uid, int pid, int swappiness) { |
| 435 | return setProcessGroupValue(uid, pid, "/memory.swappiness", swappiness); |
| 436 | } |
| 437 | |
| 438 | bool setProcessGroupSoftLimit(uid_t uid, int pid, int64_t soft_limit_in_bytes) { |
| 439 | return setProcessGroupValue(uid, pid, "/memory.soft_limit_in_bytes", soft_limit_in_bytes); |
| 440 | } |
| 441 | |
| 442 | bool setProcessGroupLimit(uid_t uid, int pid, int64_t limit_in_bytes) { |
| 443 | return setProcessGroupValue(uid, pid, "/memory.limit_in_bytes", limit_in_bytes); |
| 444 | } |