blob: eb6672762dce50bd2b8833a9d46a49fd7076cf4c [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>
Elliott Hughes290a2282016-11-14 17:08:47 -080034#include <thread>
Colin Crosscf8d1c22014-06-03 13:24:21 -070035
Elliott Hughes171df0a2016-08-02 13:30:30 -070036#include <android-base/logging.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070037#include <private/android_filesystem_config.h>
38
39#include <processgroup/processgroup.h>
Martijn Coenenb82bab62016-01-20 16:39:16 -080040
Elliott Hughes290a2282016-11-14 17:08:47 -080041using namespace std::chrono_literals;
42
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010043// Uncomment line below use memory cgroups for keeping track of (forked) PIDs
44// #define USE_MEMCG 1
45
Martijn Coenenb82bab62016-01-20 16:39:16 -080046#define MEM_CGROUP_PATH "/dev/memcg/apps"
Martijn Coenen623b56a2016-02-08 11:42:25 +010047#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
Martijn Coenenb82bab62016-01-20 16:39:16 -080048#define ACCT_CGROUP_PATH "/acct"
49
50#define PROCESSGROUP_UID_PREFIX "uid_"
51#define PROCESSGROUP_PID_PREFIX "pid_"
52#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
53#define PROCESSGROUP_MAX_UID_LEN 11
54#define PROCESSGROUP_MAX_PID_LEN 11
55#define PROCESSGROUP_MAX_PATH_LEN \
56 ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
57 sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
58 sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
59 PROCESSGROUP_MAX_UID_LEN + \
60 sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
61 PROCESSGROUP_MAX_PID_LEN + \
62 sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
63 1)
64
65std::once_flag init_path_flag;
Colin Crosscf8d1c22014-06-03 13:24:21 -070066
67struct ctx {
68 bool initialized;
69 int fd;
70 char buf[128];
71 char *buf_ptr;
72 size_t buf_len;
73};
74
Martijn Coenenb82bab62016-01-20 16:39:16 -080075static const char* getCgroupRootPath() {
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010076#ifdef USE_MEMCG
Martijn Coenenb82bab62016-01-20 16:39:16 -080077 static const char* cgroup_root_path = NULL;
78 std::call_once(init_path_flag, [&]() {
Martijn Coenen623b56a2016-02-08 11:42:25 +010079 // Check if mem cgroup is mounted, only then check for write-access to avoid
80 // SELinux denials
81 cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
82 ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
Martijn Coenenb82bab62016-01-20 16:39:16 -080083 });
84 return cgroup_root_path;
Martijn Coenen5bb91ab2016-03-18 15:28:31 +010085#else
86 return ACCT_CGROUP_PATH;
87#endif
Martijn Coenenb82bab62016-01-20 16:39:16 -080088}
89
Colin Crosscf8d1c22014-06-03 13:24:21 -070090static int convertUidToPath(char *path, size_t size, uid_t uid)
91{
92 return snprintf(path, size, "%s/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -080093 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -070094 PROCESSGROUP_UID_PREFIX,
95 uid);
96}
97
98static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
99{
100 return snprintf(path, size, "%s/%s%d/%s%d",
Martijn Coenenb82bab62016-01-20 16:39:16 -0800101 getCgroupRootPath(),
Colin Crosscf8d1c22014-06-03 13:24:21 -0700102 PROCESSGROUP_UID_PREFIX,
103 uid,
104 PROCESSGROUP_PID_PREFIX,
105 pid);
106}
107
108static int initCtx(uid_t uid, int pid, struct ctx *ctx)
109{
110 int ret;
111 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
112 convertUidPidToPath(path, sizeof(path), uid, pid);
113 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
114
115 int fd = open(path, O_RDONLY);
116 if (fd < 0) {
117 ret = -errno;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700118 PLOG(WARNING) << "failed to open " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700119 return ret;
120 }
121
122 ctx->fd = fd;
123 ctx->buf_ptr = ctx->buf;
124 ctx->buf_len = 0;
125 ctx->initialized = true;
126
Elliott Hughes171df0a2016-08-02 13:30:30 -0700127 LOG(VERBOSE) << "Initialized context for " << path;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700128
Colin Crosscf8d1c22014-06-03 13:24:21 -0700129 return 0;
130}
131
132static int refillBuffer(struct ctx *ctx)
133{
134 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
135 ctx->buf_ptr = ctx->buf;
136
137 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700138 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700139 if (ret < 0) {
140 return -errno;
141 } else if (ret == 0) {
142 return 0;
143 }
144
145 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700146 ctx->buf[ctx->buf_len] = 0;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700147 LOG(VERBOSE) << "Read " << ret << " to buffer: " << ctx->buf;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700148
Colin Crosscf8d1c22014-06-03 13:24:21 -0700149 assert(ctx->buf_len <= sizeof(ctx->buf));
150
151 return ret;
152}
153
154static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
155{
156 if (!ctx->initialized) {
157 int ret = initCtx(uid, appProcessPid, ctx);
158 if (ret < 0) {
159 return ret;
160 }
161 }
162
163 char *eptr;
164 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
165 int ret = refillBuffer(ctx);
166 if (ret == 0) {
167 return -ERANGE;
168 }
169 if (ret < 0) {
170 return ret;
171 }
172 }
173
174 *eptr = '\0';
175 char *pid_eptr = NULL;
176 errno = 0;
177 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
178 if (errno != 0) {
179 return -errno;
180 }
181 if (pid_eptr != eptr) {
182 return -EINVAL;
183 }
184
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700185 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700186 ctx->buf_ptr = eptr + 1;
187
188 return (pid_t)pid;
189}
190
191static int removeProcessGroup(uid_t uid, int pid)
192{
193 int ret;
194 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
195
196 convertUidPidToPath(path, sizeof(path), uid, pid);
197 ret = rmdir(path);
198
199 convertUidToPath(path, sizeof(path), uid);
200 rmdir(path);
201
202 return ret;
203}
204
205static void removeUidProcessGroups(const char *uid_path)
206{
James Hawkins588a2ca2016-02-18 14:52:46 -0800207 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700208 if (uid != NULL) {
Elliott Hughes9f206932016-09-28 13:29:54 -0700209 dirent* dir;
210 while ((dir = readdir(uid.get())) != nullptr) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700211 char path[PROCESSGROUP_MAX_PATH_LEN];
212
213 if (dir->d_type != DT_DIR) {
214 continue;
215 }
216
217 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
218 continue;
219 }
220
221 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700222 LOG(VERBOSE) << "removing " << path;
223 if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700224 }
225 }
226}
227
228void removeAllProcessGroups()
229{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700230 LOG(VERBOSE) << "removeAllProcessGroups()";
Elliott Hughesb6e1d152016-08-03 13:29:04 -0700231 const char* cgroup_root_path = getCgroupRootPath();
James Hawkins22b6f7a2016-02-19 11:10:30 -0800232 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700233 if (root == NULL) {
Elliott Hughesb6e1d152016-08-03 13:29:04 -0700234 PLOG(ERROR) << "failed to open " << cgroup_root_path;
Colin Crossc15dd042014-08-20 14:11:13 -0700235 } else {
Elliott Hughes9f206932016-09-28 13:29:54 -0700236 dirent* dir;
237 while ((dir = readdir(root.get())) != nullptr) {
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);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700249 LOG(VERBOSE) << "removing " << path;
250 if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700251 }
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.
Elliott Hughes171df0a2016-08-02 13:30:30 -0700268 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 -0700269 continue;
270 }
Elliott Hughes171df0a2016-08-02 13:30:30 -0700271 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid
272 << " as part of process group " << initialPid;
273 if (kill(pid, signal) == -1) {
274 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
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{
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000287 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700288
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000289 int retry = 40;
290 int processes;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700291 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700292 LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700293 if (retry > 0) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800294 std::this_thread::sleep_for(5ms);
Yusuke Satod5039302015-06-16 13:51:14 -0700295 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700296 } else {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700297 LOG(ERROR) << "failed to kill " << processes << " processes for processgroup "
298 << initialPid;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700299 break;
300 }
301 }
302
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000303 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
304
Elliott Hughes171df0a2016-08-02 13:30:30 -0700305 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
306 LOG(VERBOSE) << "Killed process group uid " << uid << " pid " << initialPid << " in "
307 << static_cast<int>(ms) << "ms, " << processes << " procs remain";
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700308
Colin Crosscf8d1c22014-06-03 13:24:21 -0700309 if (processes == 0) {
310 return removeProcessGroup(uid, initialPid);
311 } else {
312 return -1;
313 }
314}
315
Elliott Hughes171df0a2016-08-02 13:30:30 -0700316static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
Colin Crosscf8d1c22014-06-03 13:24:21 -0700317{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700318 if (mkdir(path, mode) == -1 && errno != EEXIST) {
319 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700320 }
321
Elliott Hughes171df0a2016-08-02 13:30:30 -0700322 if (chown(path, uid, gid) == -1) {
323 int saved_errno = errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700324 rmdir(path);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700325 errno = saved_errno;
326 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700327 }
328
Elliott Hughes171df0a2016-08-02 13:30:30 -0700329 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700330}
331
332int createProcessGroup(uid_t uid, int initialPid)
333{
334 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
Colin Crosscf8d1c22014-06-03 13:24:21 -0700335
336 convertUidToPath(path, sizeof(path), uid);
337
Elliott Hughes171df0a2016-08-02 13:30:30 -0700338 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
339 PLOG(ERROR) << "failed to make and chown " << path;
340 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700341 }
342
343 convertUidPidToPath(path, sizeof(path), uid, initialPid);
344
Elliott Hughes171df0a2016-08-02 13:30:30 -0700345 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
346 PLOG(ERROR) << "failed to make and chown " << path;
347 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700348 }
349
350 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
351
352 int fd = open(path, O_WRONLY);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700353 if (fd == -1) {
354 int ret = -errno;
355 PLOG(ERROR) << "failed to open " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700356 return ret;
357 }
358
359 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
360 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
361
Elliott Hughes171df0a2016-08-02 13:30:30 -0700362 int ret = 0;
363 if (write(fd, pid, len) < 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700364 ret = -errno;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700365 PLOG(ERROR) << "failed to write '" << pid << "' to " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700366 }
367
368 close(fd);
369 return ret;
370}