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