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> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 34 | #include <android-base/logging.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
| 36 | |
| 37 | #include <processgroup/processgroup.h> |
| 38 | #include "processgroup_priv.h" |
| 39 | |
| 40 | struct ctx { |
| 41 | bool initialized; |
| 42 | int fd; |
| 43 | char buf[128]; |
| 44 | char *buf_ptr; |
| 45 | size_t buf_len; |
| 46 | }; |
| 47 | |
| 48 | static int convertUidToPath(char *path, size_t size, uid_t uid) |
| 49 | { |
| 50 | return snprintf(path, size, "%s/%s%d", |
| 51 | PROCESSGROUP_CGROUP_PATH, |
| 52 | PROCESSGROUP_UID_PREFIX, |
| 53 | uid); |
| 54 | } |
| 55 | |
| 56 | static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid) |
| 57 | { |
| 58 | return snprintf(path, size, "%s/%s%d/%s%d", |
| 59 | PROCESSGROUP_CGROUP_PATH, |
| 60 | PROCESSGROUP_UID_PREFIX, |
| 61 | uid, |
| 62 | PROCESSGROUP_PID_PREFIX, |
| 63 | pid); |
| 64 | } |
| 65 | |
| 66 | static int initCtx(uid_t uid, int pid, struct ctx *ctx) |
| 67 | { |
| 68 | int ret; |
| 69 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 70 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 71 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 72 | |
| 73 | int fd = open(path, O_RDONLY); |
| 74 | if (fd < 0) { |
| 75 | ret = -errno; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 76 | PLOG(WARNING) << "failed to open " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | ctx->fd = fd; |
| 81 | ctx->buf_ptr = ctx->buf; |
| 82 | ctx->buf_len = 0; |
| 83 | ctx->initialized = true; |
| 84 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 85 | LOG(VERBOSE) << "Initialized context for " << path; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 86 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int refillBuffer(struct ctx *ctx) |
| 91 | { |
| 92 | memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len); |
| 93 | ctx->buf_ptr = ctx->buf; |
| 94 | |
| 95 | ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len, |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 96 | sizeof(ctx->buf) - ctx->buf_len - 1); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 97 | if (ret < 0) { |
| 98 | return -errno; |
| 99 | } else if (ret == 0) { |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | ctx->buf_len += ret; |
Dianne Hackborn | 67f46cb | 2014-10-15 11:36:28 -0700 | [diff] [blame] | 104 | ctx->buf[ctx->buf_len] = 0; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 105 | LOG(VERBOSE) << "Read " << ret << " to buffer: " << ctx->buf; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 106 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 107 | assert(ctx->buf_len <= sizeof(ctx->buf)); |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx) |
| 113 | { |
| 114 | if (!ctx->initialized) { |
| 115 | int ret = initCtx(uid, appProcessPid, ctx); |
| 116 | if (ret < 0) { |
| 117 | return ret; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | char *eptr; |
| 122 | while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) { |
| 123 | int ret = refillBuffer(ctx); |
| 124 | if (ret == 0) { |
| 125 | return -ERANGE; |
| 126 | } |
| 127 | if (ret < 0) { |
| 128 | return ret; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | *eptr = '\0'; |
| 133 | char *pid_eptr = NULL; |
| 134 | errno = 0; |
| 135 | long pid = strtol(ctx->buf_ptr, &pid_eptr, 10); |
| 136 | if (errno != 0) { |
| 137 | return -errno; |
| 138 | } |
| 139 | if (pid_eptr != eptr) { |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 143 | ctx->buf_len -= (eptr - ctx->buf_ptr) + 1; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 144 | ctx->buf_ptr = eptr + 1; |
| 145 | |
| 146 | return (pid_t)pid; |
| 147 | } |
| 148 | |
| 149 | static int removeProcessGroup(uid_t uid, int pid) |
| 150 | { |
| 151 | int ret; |
| 152 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 153 | |
| 154 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 155 | ret = rmdir(path); |
| 156 | |
| 157 | convertUidToPath(path, sizeof(path), uid); |
| 158 | rmdir(path); |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static void removeUidProcessGroups(const char *uid_path) |
| 164 | { |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 165 | std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 166 | if (uid != NULL) { |
| 167 | struct dirent cur; |
| 168 | struct dirent *dir; |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 169 | while ((readdir_r(uid.get(), &cur, &dir) == 0) && dir) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 170 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 171 | |
| 172 | if (dir->d_type != DT_DIR) { |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 181 | LOG(VERBOSE) << "removing " << path; |
| 182 | if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void removeAllProcessGroups() |
| 188 | { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 189 | LOG(VERBOSE) << "removeAllProcessGroups()"; |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 190 | std::unique_ptr<DIR, decltype(&closedir)> root(opendir(PROCESSGROUP_CGROUP_PATH), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 191 | if (root == NULL) { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 192 | PLOG(ERROR) << "failed to open " << PROCESSGROUP_CGROUP_PATH; |
Colin Cross | c15dd04 | 2014-08-20 14:11:13 -0700 | [diff] [blame] | 193 | } else { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 194 | struct dirent cur; |
| 195 | struct dirent *dir; |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 196 | while ((readdir_r(root.get(), &cur, &dir) == 0) && dir) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 197 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 198 | |
| 199 | if (dir->d_type != DT_DIR) { |
| 200 | continue; |
| 201 | } |
| 202 | if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) { |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | snprintf(path, sizeof(path), "%s/%s", PROCESSGROUP_CGROUP_PATH, dir->d_name); |
| 207 | removeUidProcessGroups(path); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 208 | LOG(VERBOSE) << "removing " << path; |
| 209 | if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 210 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | static int killProcessGroupOnce(uid_t uid, int initialPid, int signal) |
| 215 | { |
| 216 | int processes = 0; |
| 217 | struct ctx ctx; |
| 218 | pid_t pid; |
| 219 | |
| 220 | ctx.initialized = false; |
| 221 | |
| 222 | while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) { |
| 223 | processes++; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 224 | if (pid == 0) { |
| 225 | // Should never happen... but if it does, trying to kill this |
| 226 | // 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^] | 227 | 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] | 228 | continue; |
| 229 | } |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 230 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid |
| 231 | << " as part of process group " << initialPid; |
| 232 | if (kill(pid, signal) == -1) { |
| 233 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | if (ctx.initialized) { |
| 238 | close(ctx.fd); |
| 239 | } |
| 240 | |
| 241 | return processes; |
| 242 | } |
| 243 | |
| 244 | int killProcessGroup(uid_t uid, int initialPid, int signal) |
| 245 | { |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 246 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 247 | |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 248 | int retry = 40; |
| 249 | int processes; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 250 | while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 251 | LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid; |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 252 | if (retry > 0) { |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 253 | usleep(5 * 1000); // 5ms |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 254 | --retry; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 255 | } else { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 256 | LOG(ERROR) << "failed to kill " << processes << " processes for processgroup " |
| 257 | << initialPid; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 262 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
| 263 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 264 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 265 | LOG(VERBOSE) << "Killed process group uid " << uid << " pid " << initialPid << " in " |
| 266 | << static_cast<int>(ms) << "ms, " << processes << " procs remain"; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 267 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 268 | if (processes == 0) { |
| 269 | return removeProcessGroup(uid, initialPid); |
| 270 | } else { |
| 271 | return -1; |
| 272 | } |
| 273 | } |
| 274 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 275 | 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] | 276 | { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 277 | if (mkdir(path, mode) == -1 && errno != EEXIST) { |
| 278 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 281 | if (chown(path, uid, gid) == -1) { |
| 282 | int saved_errno = errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 283 | rmdir(path); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 284 | errno = saved_errno; |
| 285 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 288 | return true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | int createProcessGroup(uid_t uid, int initialPid) |
| 292 | { |
| 293 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 294 | |
| 295 | convertUidToPath(path, sizeof(path), uid); |
| 296 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 297 | if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
| 298 | PLOG(ERROR) << "failed to make and chown " << path; |
| 299 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | convertUidPidToPath(path, sizeof(path), uid, initialPid); |
| 303 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 304 | if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
| 305 | PLOG(ERROR) << "failed to make and chown " << path; |
| 306 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 310 | |
| 311 | int fd = open(path, O_WRONLY); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 312 | if (fd == -1) { |
| 313 | int ret = -errno; |
| 314 | PLOG(ERROR) << "failed to open " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0}; |
| 319 | int len = snprintf(pid, sizeof(pid), "%d", initialPid); |
| 320 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 321 | int ret = 0; |
| 322 | if (write(fd, pid, len) < 0) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 323 | ret = -errno; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame^] | 324 | PLOG(ERROR) << "failed to write '" << pid << "' to " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | close(fd); |
| 328 | return ret; |
| 329 | } |