blob: 1572cb3123ec36600f1df1f2d380c79800dfd3a2 [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
Keun-young Parkfac4b632017-03-28 17:25:24 -0700255static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700256 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
Keun-young Parkfac4b632017-03-28 17:25:24 -0700284static int killProcessGroup(uid_t uid, int initialPid, int signal, int retry) {
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000285 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700286
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000287 int processes;
Keun-young Parkfac4b632017-03-28 17:25:24 -0700288 while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700289 LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid;
Yusuke Satod5039302015-06-16 13:51:14 -0700290 if (retry > 0) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800291 std::this_thread::sleep_for(5ms);
Yusuke Satod5039302015-06-16 13:51:14 -0700292 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700293 } else {
Elliott Hughes171df0a2016-08-02 13:30:30 -0700294 LOG(ERROR) << "failed to kill " << processes << " processes for processgroup "
295 << initialPid;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700296 break;
297 }
298 }
299
Collin Mullinerf7e79b92016-06-01 21:03:55 +0000300 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
301
Elliott Hughes171df0a2016-08-02 13:30:30 -0700302 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
303 LOG(VERBOSE) << "Killed process group uid " << uid << " pid " << initialPid << " in "
304 << static_cast<int>(ms) << "ms, " << processes << " procs remain";
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700305
Colin Crosscf8d1c22014-06-03 13:24:21 -0700306 if (processes == 0) {
307 return removeProcessGroup(uid, initialPid);
308 } else {
309 return -1;
310 }
311}
312
Keun-young Parkfac4b632017-03-28 17:25:24 -0700313int killProcessGroup(uid_t uid, int initialPid, int signal) {
314 return killProcessGroup(uid, initialPid, signal, 40 /*maxRetry*/);
315}
316
317int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
318 return killProcessGroup(uid, initialPid, signal, 0 /*maxRetry*/);
319}
320
Elliott Hughes171df0a2016-08-02 13:30:30 -0700321static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
Colin Crosscf8d1c22014-06-03 13:24:21 -0700322{
Elliott Hughes171df0a2016-08-02 13:30:30 -0700323 if (mkdir(path, mode) == -1 && errno != EEXIST) {
324 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700325 }
326
Elliott Hughes171df0a2016-08-02 13:30:30 -0700327 if (chown(path, uid, gid) == -1) {
328 int saved_errno = errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700329 rmdir(path);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700330 errno = saved_errno;
331 return false;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700332 }
333
Elliott Hughes171df0a2016-08-02 13:30:30 -0700334 return true;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700335}
336
337int createProcessGroup(uid_t uid, int initialPid)
338{
339 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
Colin Crosscf8d1c22014-06-03 13:24:21 -0700340
341 convertUidToPath(path, sizeof(path), uid);
342
Elliott Hughes171df0a2016-08-02 13:30:30 -0700343 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
344 PLOG(ERROR) << "failed to make and chown " << path;
345 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700346 }
347
348 convertUidPidToPath(path, sizeof(path), uid, initialPid);
349
Elliott Hughes171df0a2016-08-02 13:30:30 -0700350 if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
351 PLOG(ERROR) << "failed to make and chown " << path;
352 return -errno;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700353 }
354
355 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
356
357 int fd = open(path, O_WRONLY);
Elliott Hughes171df0a2016-08-02 13:30:30 -0700358 if (fd == -1) {
359 int ret = -errno;
360 PLOG(ERROR) << "failed to open " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700361 return ret;
362 }
363
364 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
365 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
366
Elliott Hughes171df0a2016-08-02 13:30:30 -0700367 int ret = 0;
368 if (write(fd, pid, len) < 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700369 ret = -errno;
Elliott Hughes171df0a2016-08-02 13:30:30 -0700370 PLOG(ERROR) << "failed to write '" << pid << "' to " << path;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700371 }
372
373 close(fd);
374 return ret;
375}