blob: 5ab957d860014e1192741ccda67e2117a43de6ec [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"
Martijn Coenen623b56a2016-02-08 11:42:25 +010041#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
Martijn Coenenb82bab62016-01-20 16:39:16 -080042#define ACCT_CGROUP_PATH "/acct"
43
44#define PROCESSGROUP_UID_PREFIX "uid_"
45#define PROCESSGROUP_PID_PREFIX "pid_"
46#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
47#define PROCESSGROUP_MAX_UID_LEN 11
48#define PROCESSGROUP_MAX_PID_LEN 11
49#define PROCESSGROUP_MAX_PATH_LEN \
50 ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
51 sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
52 sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
53 PROCESSGROUP_MAX_UID_LEN + \
54 sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
55 PROCESSGROUP_MAX_PID_LEN + \
56 sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
57 1)
58
59std::once_flag init_path_flag;
Colin Crosscf8d1c22014-06-03 13:24:21 -070060
61struct ctx {
62 bool initialized;
63 int fd;
64 char buf[128];
65 char *buf_ptr;
66 size_t buf_len;
67};
68
Martijn Coenenb82bab62016-01-20 16:39:16 -080069static const char* getCgroupRootPath() {
70 static const char* cgroup_root_path = NULL;
71 std::call_once(init_path_flag, [&]() {
Martijn Coenen623b56a2016-02-08 11:42:25 +010072 // Check if mem cgroup is mounted, only then check for write-access to avoid
73 // SELinux denials
74 cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
75 ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
Martijn Coenenb82bab62016-01-20 16:39:16 -080076 });
77 return cgroup_root_path;
78}
79
Colin Crosscf8d1c22014-06-03 13:24:21 -070080static int convertUidToPath(char *path, size_t size, uid_t uid)
81{
82 return snprintf(path, size, "%s/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080083 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -070084 PROCESSGROUP_UID_PREFIX,
85 uid);
86}
87
88static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
89{
90 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080091 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -070092 PROCESSGROUP_UID_PREFIX,
93 uid,
94 PROCESSGROUP_PID_PREFIX,
95 pid);
96}
97
98static int initCtx(uid_t uid, int pid, struct ctx *ctx)
99{
100 int ret;
101 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
102 convertUidPidToPath(path, sizeof(path), uid, pid);
103 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
104
105 int fd = open(path, O_RDONLY);
106 if (fd < 0) {
107 ret = -errno;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700108 SLOGW("failed to open %s: %s", path, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700109 return ret;
110 }
111
112 ctx->fd = fd;
113 ctx->buf_ptr = ctx->buf;
114 ctx->buf_len = 0;
115 ctx->initialized = true;
116
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700117 SLOGV("Initialized context for %s", path);
118
Colin Crosscf8d1c22014-06-03 13:24:21 -0700119 return 0;
120}
121
122static int refillBuffer(struct ctx *ctx)
123{
124 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
125 ctx->buf_ptr = ctx->buf;
126
127 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700128 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700129 if (ret < 0) {
130 return -errno;
131 } else if (ret == 0) {
132 return 0;
133 }
134
135 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700136 ctx->buf[ctx->buf_len] = 0;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800137 SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700138
Colin Crosscf8d1c22014-06-03 13:24:21 -0700139 assert(ctx->buf_len <= sizeof(ctx->buf));
140
141 return ret;
142}
143
144static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
145{
146 if (!ctx->initialized) {
147 int ret = initCtx(uid, appProcessPid, ctx);
148 if (ret < 0) {
149 return ret;
150 }
151 }
152
153 char *eptr;
154 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
155 int ret = refillBuffer(ctx);
156 if (ret == 0) {
157 return -ERANGE;
158 }
159 if (ret < 0) {
160 return ret;
161 }
162 }
163
164 *eptr = '\0';
165 char *pid_eptr = NULL;
166 errno = 0;
167 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
168 if (errno != 0) {
169 return -errno;
170 }
171 if (pid_eptr != eptr) {
172 return -EINVAL;
173 }
174
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700175 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700176 ctx->buf_ptr = eptr + 1;
177
178 return (pid_t)pid;
179}
180
181static int removeProcessGroup(uid_t uid, int pid)
182{
183 int ret;
184 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
185
186 convertUidPidToPath(path, sizeof(path), uid, pid);
187 ret = rmdir(path);
188
189 convertUidToPath(path, sizeof(path), uid);
190 rmdir(path);
191
192 return ret;
193}
194
195static void removeUidProcessGroups(const char *uid_path)
196{
197 DIR *uid = opendir(uid_path);
198 if (uid != NULL) {
199 struct dirent cur;
200 struct dirent *dir;
201 while ((readdir_r(uid, &cur, &dir) == 0) && dir) {
202 char path[PROCESSGROUP_MAX_PATH_LEN];
203
204 if (dir->d_type != DT_DIR) {
205 continue;
206 }
207
208 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
209 continue;
210 }
211
212 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
213 SLOGV("removing %s\n", path);
214 rmdir(path);
215 }
Colin Crossc15dd042014-08-20 14:11:13 -0700216 closedir(uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700217 }
218}
219
220void removeAllProcessGroups()
221{
222 SLOGV("removeAllProcessGroups()");
Martijn Coenenb82bab62016-01-20 16:39:16 -0800223 const char *cgroup_root_path = getCgroupRootPath();
224 DIR *root = opendir(cgroup_root_path);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700225 if (root == NULL) {
Martijn Coenenb82bab62016-01-20 16:39:16 -0800226 SLOGE("failed to open %s: %s", cgroup_root_path, strerror(errno));
Colin Crossc15dd042014-08-20 14:11:13 -0700227 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700228 struct dirent cur;
229 struct dirent *dir;
230 while ((readdir_r(root, &cur, &dir) == 0) && dir) {
231 char path[PROCESSGROUP_MAX_PATH_LEN];
232
233 if (dir->d_type != DT_DIR) {
234 continue;
235 }
236 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
237 continue;
238 }
239
Martijn Coenenb82bab62016-01-20 16:39:16 -0800240 snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700241 removeUidProcessGroups(path);
242 SLOGV("removing %s\n", path);
243 rmdir(path);
244 }
Colin Crossc15dd042014-08-20 14:11:13 -0700245 closedir(root);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700246 }
247}
248
249static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
250{
251 int processes = 0;
252 struct ctx ctx;
253 pid_t pid;
254
255 ctx.initialized = false;
256
257 while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
258 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700259 if (pid == 0) {
260 // Should never happen... but if it does, trying to kill this
261 // will boomerang right back and kill us! Let's not let that happen.
262 SLOGW("Yikes, we've been told to kill pid 0! How about we don't do that.");
263 continue;
264 }
265 if (pid != initialPid) {
266 // We want to be noisy about killing processes so we can understand
267 // what is going on in the log; however, don't be noisy about the base
268 // process, since that it something we always kill, and we have already
269 // logged elsewhere about killing it.
270 SLOGI("Killing pid %d in uid %d as part of process group %d", pid, uid, initialPid);
271 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700272 int ret = kill(pid, signal);
273 if (ret == -1) {
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700274 SLOGW("failed to kill pid %d: %s", pid, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700275 }
276 }
277
278 if (ctx.initialized) {
279 close(ctx.fd);
280 }
281
282 return processes;
283}
284
285int killProcessGroup(uid_t uid, int initialPid, int signal)
286{
287 int processes;
Yusuke Satod5039302015-06-16 13:51:14 -0700288 const int sleep_us = 5 * 1000; // 5ms
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800289 int64_t startTime = android::uptimeMillis();
Yusuke Satod5039302015-06-16 13:51:14 -0700290 int retry = 40;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700291
292 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
293 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
Yusuke Satod5039302015-06-16 13:51:14 -0700294 if (retry > 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700295 usleep(sleep_us);
Yusuke Satod5039302015-06-16 13:51:14 -0700296 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700297 } else {
298 SLOGE("failed to kill %d processes for processgroup %d\n",
299 processes, initialPid);
300 break;
301 }
302 }
303
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800304 SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700305 android::uptimeMillis()-startTime, processes);
306
Colin Crosscf8d1c22014-06-03 13:24:21 -0700307 if (processes == 0) {
308 return removeProcessGroup(uid, initialPid);
309 } else {
310 return -1;
311 }
312}
313
314static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
315{
316 int ret;
317
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100318 ret = mkdir(path, mode);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700319 if (ret < 0 && errno != EEXIST) {
320 return -errno;
321 }
322
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100323 ret = chown(path, uid, gid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700324 if (ret < 0) {
325 ret = -errno;
326 rmdir(path);
327 return ret;
328 }
329
330 return 0;
331}
332
333int createProcessGroup(uid_t uid, int initialPid)
334{
335 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
336 int ret;
337
338 convertUidToPath(path, sizeof(path), uid);
339
340 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
341 if (ret < 0) {
342 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
343 return ret;
344 }
345
346 convertUidPidToPath(path, sizeof(path), uid, initialPid);
347
348 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
349 if (ret < 0) {
350 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
351 return ret;
352 }
353
354 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
355
356 int fd = open(path, O_WRONLY);
357 if (fd < 0) {
358 ret = -errno;
359 SLOGE("failed to open %s: %s", path, strerror(errno));
360 return ret;
361 }
362
363 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
364 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
365
366 ret = write(fd, pid, len);
367 if (ret < 0) {
368 ret = -errno;
369 SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno));
370 } else {
371 ret = 0;
372 }
373
374 close(fd);
375 return ret;
376}
377