blob: 65bbc1e4274ede26ed6db96c0e40f227e1b655e9 [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>
Colin Crosscf8d1c22014-06-03 13:24:21 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/types.h>
Collin Mullinerf7e79b92016-06-01 21:03:55 +000030
31#include <chrono>
James Hawkins588a2ca2016-02-18 14:52:46 -080032#include <memory>
Elliott Hughes8d532e42016-06-06 21:19:55 -070033#include <mutex>
Colin Crosscf8d1c22014-06-03 13:24:21 -070034
Elliott Hughes171df0a2016-08-02 13:30:30 -070035#include <android-base/logging.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070036#include <private/android_filesystem_config.h>
37
38#include <processgroup/processgroup.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080039
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010040// Uncomment line below use memory cgroups for keeping track of (forked) PIDs
41// #define USE_MEMCG 1
42
Martijn Coenenb82bab62016-01-20 16:39:16 -080043#define MEM_CGROUP_PATH "/dev/memcg/apps"
Martijn Coenen623b56a2016-02-08 11:42:25 +010044#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
Martijn Coenenb82bab62016-01-20 16:39:16 -080045#define ACCT_CGROUP_PATH "/acct"
46
47#define PROCESSGROUP_UID_PREFIX "uid_"
48#define PROCESSGROUP_PID_PREFIX "pid_"
49#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
50#define PROCESSGROUP_MAX_UID_LEN 11
51#define PROCESSGROUP_MAX_PID_LEN 11
52#define PROCESSGROUP_MAX_PATH_LEN \
53 ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
54 sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
55 sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
56 PROCESSGROUP_MAX_UID_LEN + \
57 sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
58 PROCESSGROUP_MAX_PID_LEN + \
59 sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
60 1)
61
62std::once_flag init_path_flag;
Colin Crosscf8d1c22014-06-03 13:24:21 -070063
64struct ctx {
65 bool initialized;
66 int fd;
67 char buf[128];
68 char *buf_ptr;
69 size_t buf_len;
70};
71
Martijn Coenenb82bab62016-01-20 16:39:16 -080072static const char* getCgroupRootPath() {
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010073#ifdef USE_MEMCG
Martijn Coenenb82bab62016-01-20 16:39:16 -080074 static const char* cgroup_root_path = NULL;
75 std::call_once(init_path_flag, [&]() {
Martijn Coenen623b56a2016-02-08 11:42:25 +010076 // Check if mem cgroup is mounted, only then check for write-access to avoid
77 // SELinux denials
78 cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
79 ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
Martijn Coenenb82bab62016-01-20 16:39:16 -080080 });
81 return cgroup_root_path;
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010082#else
83 return ACCT_CGROUP_PATH;
84#endif
Martijn Coenenb82bab62016-01-20 16:39:16 -080085}
86
Colin Crosscf8d1c22014-06-03 13:24:21 -070087static int convertUidToPath(char *path, size_t size, uid_t uid)
88{
89 return snprintf(path, size, "%s/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080090 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -070091 PROCESSGROUP_UID_PREFIX,
92 uid);
93}
94
95static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
96{
97 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080098 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -070099 PROCESSGROUP_UID_PREFIX,
100 uid,
101 PROCESSGROUP_PID_PREFIX,
102 pid);
103}
104
105static int initCtx(uid_t uid, int pid, struct ctx *ctx)
106{
107 int ret;
108 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
109 convertUidPidToPath(path, sizeof(path), uid, pid);
110 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
111
112 int fd = open(path, O_RDONLY);
113 if (fd < 0) {
114 ret = -errno;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700115 PLOG(WARNING) << "failed to open " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700116 return ret;
117 }
118
119 ctx->fd = fd;
120 ctx->buf_ptr = ctx->buf;
121 ctx->buf_len = 0;
122 ctx->initialized = true;
123
Elliott Hughes171df0a2016-08-02 13:30:30 -0700124 LOG(VERBOSE) << "Initialized context for " << path;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700125
Colin Crosscf8d1c22014-06-03 13:24:21 -0700126 return 0;
127}
128
129static int refillBuffer(struct ctx *ctx)
130{
131 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
132 ctx->buf_ptr = ctx->buf;
133
134 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700135 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700136 if (ret < 0) {
137 return -errno;
138 } else if (ret == 0) {
139 return 0;
140 }
141
142 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700143 ctx->buf[ctx->buf_len] = 0;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700144 LOG(VERBOSE) << "Read " << ret << " to buffer: " << ctx->buf;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700145
Colin Crosscf8d1c22014-06-03 13:24:21 -0700146 assert(ctx->buf_len <= sizeof(ctx->buf));
147
148 return ret;
149}
150
151static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
152{
153 if (!ctx->initialized) {
154 int ret = initCtx(uid, appProcessPid, ctx);
155 if (ret < 0) {
156 return ret;
157 }
158 }
159
160 char *eptr;
161 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
162 int ret = refillBuffer(ctx);
163 if (ret == 0) {
164 return -ERANGE;
165 }
166 if (ret < 0) {
167 return ret;
168 }
169 }
170
171 *eptr = '\0';
172 char *pid_eptr = NULL;
173 errno = 0;
174 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
175 if (errno != 0) {
176 return -errno;
177 }
178 if (pid_eptr != eptr) {
179 return -EINVAL;
180 }
181
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700182 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700183 ctx->buf_ptr = eptr + 1;
184
185 return (pid_t)pid;
186}
187
188static int removeProcessGroup(uid_t uid, int pid)
189{
190 int ret;
191 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
192
193 convertUidPidToPath(path, sizeof(path), uid, pid);
194 ret = rmdir(path);
195
196 convertUidToPath(path, sizeof(path), uid);
197 rmdir(path);
198
199 return ret;
200}
201
202static void removeUidProcessGroups(const char *uid_path)
203{
James Hawkins588a2ca2016-02-18 14:52:46 -0800204 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700205 if (uid != NULL) {
206 struct dirent cur;
207 struct dirent *dir;
James Hawkins588a2ca2016-02-18 14:52:46 -0800208 while ((readdir_r(uid.get(), &cur, &dir) == 0) && dir) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700209 char path[PROCESSGROUP_MAX_PATH_LEN];
210
211 if (dir->d_type != DT_DIR) {
212 continue;
213 }
214
215 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
216 continue;
217 }
218
219 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700220 LOG(VERBOSE) << "removing " << path;
221 if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700222 }
223 }
224}
225
226void removeAllProcessGroups()
227{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700228 LOG(VERBOSE) << "removeAllProcessGroups()";
Elliott Hughesb6e1d152016-08-03 13:29:04 -0700229 const char* cgroup_root_path = getCgroupRootPath();
James Hawkins22b6f7a2016-02-19 11:10:30 -0800230 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700231 if (root == NULL) {
Elliott Hughesb6e1d152016-08-03 13:29:04 -0700232 PLOG(ERROR) << "failed to open " << cgroup_root_path;
Colin Crossc15dd042014-08-20 14:11:13 -0700233 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700234 struct dirent cur;
235 struct dirent *dir;
James Hawkins588a2ca2016-02-18 14:52:46 -0800236 while ((readdir_r(root.get(), &cur, &dir) == 0) && dir) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700237 char path[PROCESSGROUP_MAX_PATH_LEN];
238
239 if (dir->d_type != DT_DIR) {
240 continue;
241 }
242 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
243 continue;
244 }
245
Martijn Coenenb82bab62016-01-20 16:39:16 -0800246 snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700247 removeUidProcessGroups(path);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700248 LOG(VERBOSE) << "removing " << path;
249 if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700250 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700251 }
252}
253
254static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
255{
256 int processes = 0;
257 struct ctx ctx;
258 pid_t pid;
259
260 ctx.initialized = false;
261
262 while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
263 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700264 if (pid == 0) {
265 // Should never happen... but if it does, trying to kill this
266 // will boomerang right back and kill us! Let's not let that happen.
Elliott Hughes171df0a2016-08-02 13:30:30 -0700267 LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?";
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700268 continue;
269 }
Elliott Hughes171df0a2016-08-02 13:30:30 -0700270 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid
271 << " as part of process group " << initialPid;
272 if (kill(pid, signal) == -1) {
273 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
Colin Crosscf8d1c22014-06-03 13:24:21 -0700274 }
275 }
276
277 if (ctx.initialized) {
278 close(ctx.fd);
279 }
280
281 return processes;
282}
283
284int killProcessGroup(uid_t uid, int initialPid, int signal)
285{
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000286 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700287
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000288 int retry = 40;
289 int processes;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700290 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700291 LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700292 if (retry > 0) {
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000293 usleep(5 * 1000); // 5ms
Yusuke Satod5039302015-06-16 13:51:14 -0700294 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700295 } else {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700296 LOG(ERROR) << "failed to kill " << processes << " processes for processgroup "
297 << initialPid;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700298 break;
299 }
300 }
301
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000302 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
303
Elliott Hughes171df0a2016-08-02 13:30:30 -0700304 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
305 LOG(VERBOSE) << "Killed process group uid " << uid << " pid " << initialPid << " in "
306 << static_cast<int>(ms) << "ms, " << processes << " procs remain";
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700307
Colin Crosscf8d1c22014-06-03 13:24:21 -0700308 if (processes == 0) {
309 return removeProcessGroup(uid, initialPid);
310 } else {
311 return -1;
312 }
313}
314
Elliott Hughes171df0a2016-08-02 13:30:30 -0700315static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
Colin Crosscf8d1c22014-06-03 13:24:21 -0700316{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700317 if (mkdir(path, mode) == -1 && errno != EEXIST) {
318 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700319 }
320
Elliott Hughes171df0a2016-08-02 13:30:30 -0700321 if (chown(path, uid, gid) == -1) {
322 int saved_errno = errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700323 rmdir(path);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700324 errno = saved_errno;
325 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700326 }
327
Elliott Hughes171df0a2016-08-02 13:30:30 -0700328 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700329}
330
331int createProcessGroup(uid_t uid, int initialPid)
332{
333 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
Colin Crosscf8d1c22014-06-03 13:24:21 -0700334
335 convertUidToPath(path, sizeof(path), uid);
336
Elliott Hughes171df0a2016-08-02 13:30:30 -0700337 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
338 PLOG(ERROR) << "failed to make and chown " << path;
339 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700340 }
341
342 convertUidPidToPath(path, sizeof(path), uid, initialPid);
343
Elliott Hughes171df0a2016-08-02 13:30:30 -0700344 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
345 PLOG(ERROR) << "failed to make and chown " << path;
346 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700347 }
348
349 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
350
351 int fd = open(path, O_WRONLY);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700352 if (fd == -1) {
353 int ret = -errno;
354 PLOG(ERROR) << "failed to open " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700355 return ret;
356 }
357
358 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
359 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
360
Elliott Hughes171df0a2016-08-02 13:30:30 -0700361 int ret = 0;
362 if (write(fd, pid, len) < 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700363 ret = -errno;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700364 PLOG(ERROR) << "failed to write '" << pid << "' to " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700365 }
366
367 close(fd);
368 return ret;
369}