blob: f160ac17d75a93bc0794da5aa1cc7ba779d1246f [file] [log] [blame]
Colin Crosscf8d1c22014-06-03 13:24:21 -07001/*
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 Hughes0badbd62014-12-29 12:24:25 -080022#include <errno.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070023#include <fcntl.h>
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -080024#include <inttypes.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080025#include <mutex>
Colin Crosscf8d1c22014-06-03 13:24:21 -070026#include <stdbool.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32
33#include <log/log.h>
34#include <private/android_filesystem_config.h>
35
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070036#include <utils/SystemClock.h>
37
Colin Crosscf8d1c22014-06-03 13:24:21 -070038#include <processgroup/processgroup.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080039
40#define MEM_CGROUP_PATH "/dev/memcg/apps"
41#define ACCT_CGROUP_PATH "/acct"
42
43#define PROCESSGROUP_UID_PREFIX "uid_"
44#define PROCESSGROUP_PID_PREFIX "pid_"
45#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
46#define PROCESSGROUP_MAX_UID_LEN 11
47#define PROCESSGROUP_MAX_PID_LEN 11
48#define PROCESSGROUP_MAX_PATH_LEN \
49 ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
50 sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
51 sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
52 PROCESSGROUP_MAX_UID_LEN + \
53 sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
54 PROCESSGROUP_MAX_PID_LEN + \
55 sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
56 1)
57
58std::once_flag init_path_flag;
Colin Crosscf8d1c22014-06-03 13:24:21 -070059
60struct ctx {
61 bool initialized;
62 int fd;
63 char buf[128];
64 char *buf_ptr;
65 size_t buf_len;
66};
67
Martijn Coenenb82bab62016-01-20 16:39:16 -080068static const char* getCgroupRootPath() {
69 static const char* cgroup_root_path = NULL;
70 std::call_once(init_path_flag, [&]() {
71 cgroup_root_path = access(MEM_CGROUP_PATH, W_OK) ? ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
72 });
73 return cgroup_root_path;
74}
75
Colin Crosscf8d1c22014-06-03 13:24:21 -070076static int convertUidToPath(char *path, size_t size, uid_t uid)
77{
78 return snprintf(path, size, "%s/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080079 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -070080 PROCESSGROUP_UID_PREFIX,
81 uid);
82}
83
84static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
85{
86 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080087 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -070088 PROCESSGROUP_UID_PREFIX,
89 uid,
90 PROCESSGROUP_PID_PREFIX,
91 pid);
92}
93
94static int initCtx(uid_t uid, int pid, struct ctx *ctx)
95{
96 int ret;
97 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
98 convertUidPidToPath(path, sizeof(path), uid, pid);
99 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
100
101 int fd = open(path, O_RDONLY);
102 if (fd < 0) {
103 ret = -errno;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700104 SLOGW("failed to open %s: %s", path, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700105 return ret;
106 }
107
108 ctx->fd = fd;
109 ctx->buf_ptr = ctx->buf;
110 ctx->buf_len = 0;
111 ctx->initialized = true;
112
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700113 SLOGV("Initialized context for %s", path);
114
Colin Crosscf8d1c22014-06-03 13:24:21 -0700115 return 0;
116}
117
118static int refillBuffer(struct ctx *ctx)
119{
120 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
121 ctx->buf_ptr = ctx->buf;
122
123 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700124 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700125 if (ret < 0) {
126 return -errno;
127 } else if (ret == 0) {
128 return 0;
129 }
130
131 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700132 ctx->buf[ctx->buf_len] = 0;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800133 SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700134
Colin Crosscf8d1c22014-06-03 13:24:21 -0700135 assert(ctx->buf_len <= sizeof(ctx->buf));
136
137 return ret;
138}
139
140static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
141{
142 if (!ctx->initialized) {
143 int ret = initCtx(uid, appProcessPid, ctx);
144 if (ret < 0) {
145 return ret;
146 }
147 }
148
149 char *eptr;
150 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
151 int ret = refillBuffer(ctx);
152 if (ret == 0) {
153 return -ERANGE;
154 }
155 if (ret < 0) {
156 return ret;
157 }
158 }
159
160 *eptr = '\0';
161 char *pid_eptr = NULL;
162 errno = 0;
163 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
164 if (errno != 0) {
165 return -errno;
166 }
167 if (pid_eptr != eptr) {
168 return -EINVAL;
169 }
170
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700171 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700172 ctx->buf_ptr = eptr + 1;
173
174 return (pid_t)pid;
175}
176
177static int removeProcessGroup(uid_t uid, int pid)
178{
179 int ret;
180 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
181
182 convertUidPidToPath(path, sizeof(path), uid, pid);
183 ret = rmdir(path);
184
185 convertUidToPath(path, sizeof(path), uid);
186 rmdir(path);
187
188 return ret;
189}
190
191static void removeUidProcessGroups(const char *uid_path)
192{
193 DIR *uid = opendir(uid_path);
194 if (uid != NULL) {
195 struct dirent cur;
196 struct dirent *dir;
197 while ((readdir_r(uid, &cur, &dir) == 0) && dir) {
198 char path[PROCESSGROUP_MAX_PATH_LEN];
199
200 if (dir->d_type != DT_DIR) {
201 continue;
202 }
203
204 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
205 continue;
206 }
207
208 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
209 SLOGV("removing %s\n", path);
210 rmdir(path);
211 }
Colin Crossc15dd042014-08-20 14:11:13 -0700212 closedir(uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700213 }
214}
215
216void removeAllProcessGroups()
217{
218 SLOGV("removeAllProcessGroups()");
Martijn Coenenb82bab62016-01-20 16:39:16 -0800219 const char *cgroup_root_path = getCgroupRootPath();
220 DIR *root = opendir(cgroup_root_path);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700221 if (root == NULL) {
Martijn Coenenb82bab62016-01-20 16:39:16 -0800222 SLOGE("failed to open %s: %s", cgroup_root_path, strerror(errno));
Colin Crossc15dd042014-08-20 14:11:13 -0700223 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700224 struct dirent cur;
225 struct dirent *dir;
226 while ((readdir_r(root, &cur, &dir) == 0) && dir) {
227 char path[PROCESSGROUP_MAX_PATH_LEN];
228
229 if (dir->d_type != DT_DIR) {
230 continue;
231 }
232 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
233 continue;
234 }
235
Martijn Coenenb82bab62016-01-20 16:39:16 -0800236 snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700237 removeUidProcessGroups(path);
238 SLOGV("removing %s\n", path);
239 rmdir(path);
240 }
Colin Crossc15dd042014-08-20 14:11:13 -0700241 closedir(root);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700242 }
243}
244
245static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
246{
247 int processes = 0;
248 struct ctx ctx;
249 pid_t pid;
250
251 ctx.initialized = false;
252
253 while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
254 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700255 if (pid == 0) {
256 // Should never happen... but if it does, trying to kill this
257 // will boomerang right back and kill us! Let's not let that happen.
258 SLOGW("Yikes, we've been told to kill pid 0! How about we don't do that.");
259 continue;
260 }
261 if (pid != initialPid) {
262 // We want to be noisy about killing processes so we can understand
263 // what is going on in the log; however, don't be noisy about the base
264 // process, since that it something we always kill, and we have already
265 // logged elsewhere about killing it.
266 SLOGI("Killing pid %d in uid %d as part of process group %d", pid, uid, initialPid);
267 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700268 int ret = kill(pid, signal);
269 if (ret == -1) {
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700270 SLOGW("failed to kill pid %d: %s", pid, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700271 }
272 }
273
274 if (ctx.initialized) {
275 close(ctx.fd);
276 }
277
278 return processes;
279}
280
281int killProcessGroup(uid_t uid, int initialPid, int signal)
282{
283 int processes;
Yusuke Satod5039302015-06-16 13:51:14 -0700284 const int sleep_us = 5 * 1000; // 5ms
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800285 int64_t startTime = android::uptimeMillis();
Yusuke Satod5039302015-06-16 13:51:14 -0700286 int retry = 40;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700287
288 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
289 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
Yusuke Satod5039302015-06-16 13:51:14 -0700290 if (retry > 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700291 usleep(sleep_us);
Yusuke Satod5039302015-06-16 13:51:14 -0700292 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700293 } else {
294 SLOGE("failed to kill %d processes for processgroup %d\n",
295 processes, initialPid);
296 break;
297 }
298 }
299
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800300 SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700301 android::uptimeMillis()-startTime, processes);
302
Colin Crosscf8d1c22014-06-03 13:24:21 -0700303 if (processes == 0) {
304 return removeProcessGroup(uid, initialPid);
305 } else {
306 return -1;
307 }
308}
309
310static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
311{
312 int ret;
313
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100314 ret = mkdir(path, mode);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700315 if (ret < 0 && errno != EEXIST) {
316 return -errno;
317 }
318
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100319 ret = chown(path, uid, gid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700320 if (ret < 0) {
321 ret = -errno;
322 rmdir(path);
323 return ret;
324 }
325
326 return 0;
327}
328
329int createProcessGroup(uid_t uid, int initialPid)
330{
331 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
332 int ret;
333
334 convertUidToPath(path, sizeof(path), uid);
335
336 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
337 if (ret < 0) {
338 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
339 return ret;
340 }
341
342 convertUidPidToPath(path, sizeof(path), uid, initialPid);
343
344 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
345 if (ret < 0) {
346 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
347 return ret;
348 }
349
350 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
351
352 int fd = open(path, O_WRONLY);
353 if (fd < 0) {
354 ret = -errno;
355 SLOGE("failed to open %s: %s", path, strerror(errno));
356 return ret;
357 }
358
359 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
360 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
361
362 ret = write(fd, pid, len);
363 if (ret < 0) {
364 ret = -errno;
365 SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno));
366 } else {
367 ret = 0;
368 }
369
370 close(fd);
371 return ret;
372}
373