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> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #include <log/log.h> |
| 31 | #include <private/android_filesystem_config.h> |
| 32 | |
| 33 | #include <processgroup/processgroup.h> |
| 34 | #include "processgroup_priv.h" |
| 35 | |
| 36 | struct ctx { |
| 37 | bool initialized; |
| 38 | int fd; |
| 39 | char buf[128]; |
| 40 | char *buf_ptr; |
| 41 | size_t buf_len; |
| 42 | }; |
| 43 | |
| 44 | static int convertUidToPath(char *path, size_t size, uid_t uid) |
| 45 | { |
| 46 | return snprintf(path, size, "%s/%s%d", |
| 47 | PROCESSGROUP_CGROUP_PATH, |
| 48 | PROCESSGROUP_UID_PREFIX, |
| 49 | uid); |
| 50 | } |
| 51 | |
| 52 | static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid) |
| 53 | { |
| 54 | return snprintf(path, size, "%s/%s%d/%s%d", |
| 55 | PROCESSGROUP_CGROUP_PATH, |
| 56 | PROCESSGROUP_UID_PREFIX, |
| 57 | uid, |
| 58 | PROCESSGROUP_PID_PREFIX, |
| 59 | pid); |
| 60 | } |
| 61 | |
| 62 | static int initCtx(uid_t uid, int pid, struct ctx *ctx) |
| 63 | { |
| 64 | int ret; |
| 65 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 66 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 67 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 68 | |
| 69 | int fd = open(path, O_RDONLY); |
| 70 | if (fd < 0) { |
| 71 | ret = -errno; |
| 72 | SLOGV("failed to open %s: %s", path, strerror(errno)); |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | ctx->fd = fd; |
| 77 | ctx->buf_ptr = ctx->buf; |
| 78 | ctx->buf_len = 0; |
| 79 | ctx->initialized = true; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int refillBuffer(struct ctx *ctx) |
| 85 | { |
| 86 | memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len); |
| 87 | ctx->buf_ptr = ctx->buf; |
| 88 | |
| 89 | ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len, |
| 90 | sizeof(ctx->buf) - ctx->buf_len); |
| 91 | if (ret < 0) { |
| 92 | return -errno; |
| 93 | } else if (ret == 0) { |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | ctx->buf_len += ret; |
| 98 | assert(ctx->buf_len <= sizeof(ctx->buf)); |
| 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx) |
| 104 | { |
| 105 | if (!ctx->initialized) { |
| 106 | int ret = initCtx(uid, appProcessPid, ctx); |
| 107 | if (ret < 0) { |
| 108 | return ret; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | char *eptr; |
| 113 | while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) { |
| 114 | int ret = refillBuffer(ctx); |
| 115 | if (ret == 0) { |
| 116 | return -ERANGE; |
| 117 | } |
| 118 | if (ret < 0) { |
| 119 | return ret; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | *eptr = '\0'; |
| 124 | char *pid_eptr = NULL; |
| 125 | errno = 0; |
| 126 | long pid = strtol(ctx->buf_ptr, &pid_eptr, 10); |
| 127 | if (errno != 0) { |
| 128 | return -errno; |
| 129 | } |
| 130 | if (pid_eptr != eptr) { |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | |
| 134 | ctx->buf_ptr = eptr + 1; |
| 135 | |
| 136 | return (pid_t)pid; |
| 137 | } |
| 138 | |
| 139 | static int removeProcessGroup(uid_t uid, int pid) |
| 140 | { |
| 141 | int ret; |
| 142 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 143 | |
| 144 | convertUidPidToPath(path, sizeof(path), uid, pid); |
| 145 | ret = rmdir(path); |
| 146 | |
| 147 | convertUidToPath(path, sizeof(path), uid); |
| 148 | rmdir(path); |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static void removeUidProcessGroups(const char *uid_path) |
| 154 | { |
| 155 | DIR *uid = opendir(uid_path); |
| 156 | if (uid != NULL) { |
| 157 | struct dirent cur; |
| 158 | struct dirent *dir; |
| 159 | while ((readdir_r(uid, &cur, &dir) == 0) && dir) { |
| 160 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 161 | |
| 162 | if (dir->d_type != DT_DIR) { |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) { |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name); |
| 171 | SLOGV("removing %s\n", path); |
| 172 | rmdir(path); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void removeAllProcessGroups() |
| 178 | { |
| 179 | SLOGV("removeAllProcessGroups()"); |
| 180 | DIR *root = opendir(PROCESSGROUP_CGROUP_PATH); |
| 181 | if (root == NULL) { |
| 182 | SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno)); |
| 183 | } |
| 184 | if (root != NULL) { |
| 185 | struct dirent cur; |
| 186 | struct dirent *dir; |
| 187 | while ((readdir_r(root, &cur, &dir) == 0) && dir) { |
| 188 | char path[PROCESSGROUP_MAX_PATH_LEN]; |
| 189 | |
| 190 | if (dir->d_type != DT_DIR) { |
| 191 | continue; |
| 192 | } |
| 193 | if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) { |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | snprintf(path, sizeof(path), "%s/%s", PROCESSGROUP_CGROUP_PATH, dir->d_name); |
| 198 | removeUidProcessGroups(path); |
| 199 | SLOGV("removing %s\n", path); |
| 200 | rmdir(path); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static int killProcessGroupOnce(uid_t uid, int initialPid, int signal) |
| 206 | { |
| 207 | int processes = 0; |
| 208 | struct ctx ctx; |
| 209 | pid_t pid; |
| 210 | |
| 211 | ctx.initialized = false; |
| 212 | |
| 213 | while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) { |
| 214 | processes++; |
| 215 | SLOGV("sending processgroup kill to pid %d\n", pid); |
| 216 | int ret = kill(pid, signal); |
| 217 | if (ret == -1) { |
| 218 | SLOGV("failed to kill pid %d: %s", pid, strerror(errno)); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (ctx.initialized) { |
| 223 | close(ctx.fd); |
| 224 | } |
| 225 | |
| 226 | return processes; |
| 227 | } |
| 228 | |
| 229 | int killProcessGroup(uid_t uid, int initialPid, int signal) |
| 230 | { |
| 231 | int processes; |
| 232 | int sleep_us = 100; |
| 233 | |
| 234 | while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) { |
| 235 | SLOGV("killed %d processes for processgroup %d\n", processes, initialPid); |
| 236 | if (sleep_us < 128000) { |
| 237 | usleep(sleep_us); |
| 238 | sleep_us *= 2; |
| 239 | } else { |
| 240 | SLOGE("failed to kill %d processes for processgroup %d\n", |
| 241 | processes, initialPid); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (processes == 0) { |
| 247 | return removeProcessGroup(uid, initialPid); |
| 248 | } else { |
| 249 | return -1; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid) |
| 254 | { |
| 255 | int ret; |
| 256 | |
| 257 | ret = mkdir(path, 0750); |
| 258 | if (ret < 0 && errno != EEXIST) { |
| 259 | return -errno; |
| 260 | } |
| 261 | |
| 262 | ret = chown(path, AID_SYSTEM, AID_SYSTEM); |
| 263 | if (ret < 0) { |
| 264 | ret = -errno; |
| 265 | rmdir(path); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | int createProcessGroup(uid_t uid, int initialPid) |
| 273 | { |
| 274 | char path[PROCESSGROUP_MAX_PATH_LEN] = {0}; |
| 275 | int ret; |
| 276 | |
| 277 | convertUidToPath(path, sizeof(path), uid); |
| 278 | |
| 279 | ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM); |
| 280 | if (ret < 0) { |
| 281 | SLOGE("failed to make and chown %s: %s", path, strerror(-ret)); |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | convertUidPidToPath(path, sizeof(path), uid, initialPid); |
| 286 | |
| 287 | ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM); |
| 288 | if (ret < 0) { |
| 289 | SLOGE("failed to make and chown %s: %s", path, strerror(-ret)); |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path)); |
| 294 | |
| 295 | int fd = open(path, O_WRONLY); |
| 296 | if (fd < 0) { |
| 297 | ret = -errno; |
| 298 | SLOGE("failed to open %s: %s", path, strerror(errno)); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0}; |
| 303 | int len = snprintf(pid, sizeof(pid), "%d", initialPid); |
| 304 | |
| 305 | ret = write(fd, pid, len); |
| 306 | if (ret < 0) { |
| 307 | ret = -errno; |
| 308 | SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno)); |
| 309 | } else { |
| 310 | ret = 0; |
| 311 | } |
| 312 | |
| 313 | close(fd); |
| 314 | return ret; |
| 315 | } |
| 316 | |