blob: cfc9ae2dc0397c4e05d821493452b1dfca720006 [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>
James Hawkins588a2ca2016-02-18 14:52:46 -080032#include <memory>
Colin Crosscf8d1c22014-06-03 13:24:21 -070033
34#include <log/log.h>
35#include <private/android_filesystem_config.h>
36
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070037#include <utils/SystemClock.h>
38
Colin Crosscf8d1c22014-06-03 13:24:21 -070039#include <processgroup/processgroup.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080040
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010041// Uncomment line below use memory cgroups for keeping track of (forked) PIDs
42// #define USE_MEMCG 1
43
Martijn Coenenb82bab62016-01-20 16:39:16 -080044#define MEM_CGROUP_PATH "/dev/memcg/apps"
Martijn Coenen623b56a2016-02-08 11:42:25 +010045#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
Martijn Coenenb82bab62016-01-20 16:39:16 -080046#define ACCT_CGROUP_PATH "/acct"
47
48#define PROCESSGROUP_UID_PREFIX "uid_"
49#define PROCESSGROUP_PID_PREFIX "pid_"
50#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
51#define PROCESSGROUP_MAX_UID_LEN 11
52#define PROCESSGROUP_MAX_PID_LEN 11
53#define PROCESSGROUP_MAX_PATH_LEN \
54 ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
55 sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
56 sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
57 PROCESSGROUP_MAX_UID_LEN + \
58 sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
59 PROCESSGROUP_MAX_PID_LEN + \
60 sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
61 1)
62
63std::once_flag init_path_flag;
Colin Crosscf8d1c22014-06-03 13:24:21 -070064
65struct ctx {
66 bool initialized;
67 int fd;
68 char buf[128];
69 char *buf_ptr;
70 size_t buf_len;
71};
72
Martijn Coenenb82bab62016-01-20 16:39:16 -080073static const char* getCgroupRootPath() {
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010074#ifdef USE_MEMCG
Martijn Coenenb82bab62016-01-20 16:39:16 -080075 static const char* cgroup_root_path = NULL;
76 std::call_once(init_path_flag, [&]() {
Martijn Coenen623b56a2016-02-08 11:42:25 +010077 // Check if mem cgroup is mounted, only then check for write-access to avoid
78 // SELinux denials
79 cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
80 ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
Martijn Coenenb82bab62016-01-20 16:39:16 -080081 });
82 return cgroup_root_path;
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010083#else
84 return ACCT_CGROUP_PATH;
85#endif
Martijn Coenenb82bab62016-01-20 16:39:16 -080086}
87
Colin Crosscf8d1c22014-06-03 13:24:21 -070088static int convertUidToPath(char *path, size_t size, uid_t uid)
89{
90 return snprintf(path, size, "%s/%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}
95
96static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
97{
98 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080099 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700100 PROCESSGROUP_UID_PREFIX,
101 uid,
102 PROCESSGROUP_PID_PREFIX,
103 pid);
104}
105
106static int initCtx(uid_t uid, int pid, struct ctx *ctx)
107{
108 int ret;
109 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
110 convertUidPidToPath(path, sizeof(path), uid, pid);
111 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
112
113 int fd = open(path, O_RDONLY);
114 if (fd < 0) {
115 ret = -errno;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700116 SLOGW("failed to open %s: %s", path, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700117 return ret;
118 }
119
120 ctx->fd = fd;
121 ctx->buf_ptr = ctx->buf;
122 ctx->buf_len = 0;
123 ctx->initialized = true;
124
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700125 SLOGV("Initialized context for %s", path);
126
Colin Crosscf8d1c22014-06-03 13:24:21 -0700127 return 0;
128}
129
130static int refillBuffer(struct ctx *ctx)
131{
132 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
133 ctx->buf_ptr = ctx->buf;
134
135 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700136 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700137 if (ret < 0) {
138 return -errno;
139 } else if (ret == 0) {
140 return 0;
141 }
142
143 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700144 ctx->buf[ctx->buf_len] = 0;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800145 SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700146
Colin Crosscf8d1c22014-06-03 13:24:21 -0700147 assert(ctx->buf_len <= sizeof(ctx->buf));
148
149 return ret;
150}
151
152static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
153{
154 if (!ctx->initialized) {
155 int ret = initCtx(uid, appProcessPid, ctx);
156 if (ret < 0) {
157 return ret;
158 }
159 }
160
161 char *eptr;
162 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
163 int ret = refillBuffer(ctx);
164 if (ret == 0) {
165 return -ERANGE;
166 }
167 if (ret < 0) {
168 return ret;
169 }
170 }
171
172 *eptr = '\0';
173 char *pid_eptr = NULL;
174 errno = 0;
175 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
176 if (errno != 0) {
177 return -errno;
178 }
179 if (pid_eptr != eptr) {
180 return -EINVAL;
181 }
182
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700183 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700184 ctx->buf_ptr = eptr + 1;
185
186 return (pid_t)pid;
187}
188
189static int removeProcessGroup(uid_t uid, int pid)
190{
191 int ret;
192 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
193
194 convertUidPidToPath(path, sizeof(path), uid, pid);
195 ret = rmdir(path);
196
197 convertUidToPath(path, sizeof(path), uid);
198 rmdir(path);
199
200 return ret;
201}
202
203static void removeUidProcessGroups(const char *uid_path)
204{
James Hawkins588a2ca2016-02-18 14:52:46 -0800205 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700206 if (uid != NULL) {
207 struct dirent cur;
208 struct dirent *dir;
James Hawkins588a2ca2016-02-18 14:52:46 -0800209 while ((readdir_r(uid.get(), &cur, &dir) == 0) && dir) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700210 char path[PROCESSGROUP_MAX_PATH_LEN];
211
212 if (dir->d_type != DT_DIR) {
213 continue;
214 }
215
216 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
217 continue;
218 }
219
220 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
221 SLOGV("removing %s\n", path);
222 rmdir(path);
223 }
224 }
225}
226
227void removeAllProcessGroups()
228{
229 SLOGV("removeAllProcessGroups()");
Martijn Coenenb82bab62016-01-20 16:39:16 -0800230 const char *cgroup_root_path = getCgroupRootPath();
James Hawkins22b6f7a2016-02-19 11:10:30 -0800231 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700232 if (root == NULL) {
Martijn Coenenb82bab62016-01-20 16:39:16 -0800233 SLOGE("failed to open %s: %s", cgroup_root_path, strerror(errno));
Colin Crossc15dd042014-08-20 14:11:13 -0700234 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700235 struct dirent cur;
236 struct dirent *dir;
James Hawkins588a2ca2016-02-18 14:52:46 -0800237 while ((readdir_r(root.get(), &cur, &dir) == 0) && dir) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700238 char path[PROCESSGROUP_MAX_PATH_LEN];
239
240 if (dir->d_type != DT_DIR) {
241 continue;
242 }
243 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
244 continue;
245 }
246
Martijn Coenenb82bab62016-01-20 16:39:16 -0800247 snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700248 removeUidProcessGroups(path);
249 SLOGV("removing %s\n", path);
250 rmdir(path);
251 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700252 }
253}
254
255static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
256{
257 int processes = 0;
258 struct ctx ctx;
259 pid_t pid;
260
261 ctx.initialized = false;
262
263 while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
264 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700265 if (pid == 0) {
266 // Should never happen... but if it does, trying to kill this
267 // will boomerang right back and kill us! Let's not let that happen.
268 SLOGW("Yikes, we've been told to kill pid 0! How about we don't do that.");
269 continue;
270 }
271 if (pid != initialPid) {
272 // We want to be noisy about killing processes so we can understand
273 // what is going on in the log; however, don't be noisy about the base
274 // process, since that it something we always kill, and we have already
275 // logged elsewhere about killing it.
276 SLOGI("Killing pid %d in uid %d as part of process group %d", pid, uid, initialPid);
277 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700278 int ret = kill(pid, signal);
279 if (ret == -1) {
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700280 SLOGW("failed to kill pid %d: %s", pid, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700281 }
282 }
283
284 if (ctx.initialized) {
285 close(ctx.fd);
286 }
287
288 return processes;
289}
290
291int killProcessGroup(uid_t uid, int initialPid, int signal)
292{
293 int processes;
Yusuke Satod5039302015-06-16 13:51:14 -0700294 const int sleep_us = 5 * 1000; // 5ms
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800295 int64_t startTime = android::uptimeMillis();
Yusuke Satod5039302015-06-16 13:51:14 -0700296 int retry = 40;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700297
298 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
299 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
Yusuke Satod5039302015-06-16 13:51:14 -0700300 if (retry > 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700301 usleep(sleep_us);
Yusuke Satod5039302015-06-16 13:51:14 -0700302 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700303 } else {
304 SLOGE("failed to kill %d processes for processgroup %d\n",
305 processes, initialPid);
306 break;
307 }
308 }
309
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800310 SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700311 android::uptimeMillis()-startTime, processes);
312
Colin Crosscf8d1c22014-06-03 13:24:21 -0700313 if (processes == 0) {
314 return removeProcessGroup(uid, initialPid);
315 } else {
316 return -1;
317 }
318}
319
320static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
321{
322 int ret;
323
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100324 ret = mkdir(path, mode);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700325 if (ret < 0 && errno != EEXIST) {
326 return -errno;
327 }
328
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100329 ret = chown(path, uid, gid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700330 if (ret < 0) {
331 ret = -errno;
332 rmdir(path);
333 return ret;
334 }
335
336 return 0;
337}
338
339int createProcessGroup(uid_t uid, int initialPid)
340{
341 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
342 int ret;
343
344 convertUidToPath(path, sizeof(path), uid);
345
346 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
347 if (ret < 0) {
348 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
349 return ret;
350 }
351
352 convertUidPidToPath(path, sizeof(path), uid, initialPid);
353
354 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
355 if (ret < 0) {
356 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
357 return ret;
358 }
359
360 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
361
362 int fd = open(path, O_WRONLY);
363 if (fd < 0) {
364 ret = -errno;
365 SLOGE("failed to open %s: %s", path, strerror(errno));
366 return ret;
367 }
368
369 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
370 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
371
372 ret = write(fd, pid, len);
373 if (ret < 0) {
374 ret = -errno;
375 SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno));
376 } else {
377 ret = 0;
378 }
379
380 close(fd);
381 return ret;
382}
383