blob: 00a0357f5b02e09d717de68a934319b6cf065a70 [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 <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/stat.h>
30#include <sys/types.h>
James Hawkins588a2ca2016-02-18 14:52:46 -080031#include <memory>
Colin Crosscf8d1c22014-06-03 13:24:21 -070032
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>
39#include "processgroup_priv.h"
40
41struct ctx {
42 bool initialized;
43 int fd;
44 char buf[128];
45 char *buf_ptr;
46 size_t buf_len;
47};
48
49static int convertUidToPath(char *path, size_t size, uid_t uid)
50{
51 return snprintf(path, size, "%s/%s%d",
52 PROCESSGROUP_CGROUP_PATH,
53 PROCESSGROUP_UID_PREFIX,
54 uid);
55}
56
57static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
58{
59 return snprintf(path, size, "%s/%s%d/%s%d",
60 PROCESSGROUP_CGROUP_PATH,
61 PROCESSGROUP_UID_PREFIX,
62 uid,
63 PROCESSGROUP_PID_PREFIX,
64 pid);
65}
66
67static int initCtx(uid_t uid, int pid, struct ctx *ctx)
68{
69 int ret;
70 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
71 convertUidPidToPath(path, sizeof(path), uid, pid);
72 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
73
74 int fd = open(path, O_RDONLY);
75 if (fd < 0) {
76 ret = -errno;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070077 SLOGW("failed to open %s: %s", path, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -070078 return ret;
79 }
80
81 ctx->fd = fd;
82 ctx->buf_ptr = ctx->buf;
83 ctx->buf_len = 0;
84 ctx->initialized = true;
85
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070086 SLOGV("Initialized context for %s", path);
87
Colin Crosscf8d1c22014-06-03 13:24:21 -070088 return 0;
89}
90
91static int refillBuffer(struct ctx *ctx)
92{
93 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
94 ctx->buf_ptr = ctx->buf;
95
96 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070097 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -070098 if (ret < 0) {
99 return -errno;
100 } else if (ret == 0) {
101 return 0;
102 }
103
104 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700105 ctx->buf[ctx->buf_len] = 0;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800106 SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700107
Colin Crosscf8d1c22014-06-03 13:24:21 -0700108 assert(ctx->buf_len <= sizeof(ctx->buf));
109
110 return ret;
111}
112
113static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
114{
115 if (!ctx->initialized) {
116 int ret = initCtx(uid, appProcessPid, ctx);
117 if (ret < 0) {
118 return ret;
119 }
120 }
121
122 char *eptr;
123 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
124 int ret = refillBuffer(ctx);
125 if (ret == 0) {
126 return -ERANGE;
127 }
128 if (ret < 0) {
129 return ret;
130 }
131 }
132
133 *eptr = '\0';
134 char *pid_eptr = NULL;
135 errno = 0;
136 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
137 if (errno != 0) {
138 return -errno;
139 }
140 if (pid_eptr != eptr) {
141 return -EINVAL;
142 }
143
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700144 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700145 ctx->buf_ptr = eptr + 1;
146
147 return (pid_t)pid;
148}
149
150static int removeProcessGroup(uid_t uid, int pid)
151{
152 int ret;
153 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
154
155 convertUidPidToPath(path, sizeof(path), uid, pid);
156 ret = rmdir(path);
157
158 convertUidToPath(path, sizeof(path), uid);
159 rmdir(path);
160
161 return ret;
162}
163
164static void removeUidProcessGroups(const char *uid_path)
165{
James Hawkins588a2ca2016-02-18 14:52:46 -0800166 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700167 if (uid != NULL) {
168 struct dirent cur;
169 struct dirent *dir;
James Hawkins588a2ca2016-02-18 14:52:46 -0800170 while ((readdir_r(uid.get(), &cur, &dir) == 0) && dir) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700171 char path[PROCESSGROUP_MAX_PATH_LEN];
172
173 if (dir->d_type != DT_DIR) {
174 continue;
175 }
176
177 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
178 continue;
179 }
180
181 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
182 SLOGV("removing %s\n", path);
183 rmdir(path);
184 }
185 }
186}
187
188void removeAllProcessGroups()
189{
190 SLOGV("removeAllProcessGroups()");
James Hawkins588a2ca2016-02-18 14:52:46 -0800191 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(PROCESSGROUP_CGROUP_PATH), closedir);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700192 if (root == NULL) {
193 SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno));
Colin Crossc15dd042014-08-20 14:11:13 -0700194 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700195 struct dirent cur;
196 struct dirent *dir;
James Hawkins588a2ca2016-02-18 14:52:46 -0800197 while ((readdir_r(root.get(), &cur, &dir) == 0) && dir) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700198 char path[PROCESSGROUP_MAX_PATH_LEN];
199
200 if (dir->d_type != DT_DIR) {
201 continue;
202 }
203 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
204 continue;
205 }
206
207 snprintf(path, sizeof(path), "%s/%s", PROCESSGROUP_CGROUP_PATH, dir->d_name);
208 removeUidProcessGroups(path);
209 SLOGV("removing %s\n", path);
210 rmdir(path);
211 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700212 }
213}
214
215static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
216{
217 int processes = 0;
218 struct ctx ctx;
219 pid_t pid;
220
221 ctx.initialized = false;
222
223 while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
224 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700225 if (pid == 0) {
226 // Should never happen... but if it does, trying to kill this
227 // will boomerang right back and kill us! Let's not let that happen.
228 SLOGW("Yikes, we've been told to kill pid 0! How about we don't do that.");
229 continue;
230 }
231 if (pid != initialPid) {
232 // We want to be noisy about killing processes so we can understand
233 // what is going on in the log; however, don't be noisy about the base
234 // process, since that it something we always kill, and we have already
235 // logged elsewhere about killing it.
236 SLOGI("Killing pid %d in uid %d as part of process group %d", pid, uid, initialPid);
237 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700238 int ret = kill(pid, signal);
239 if (ret == -1) {
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700240 SLOGW("failed to kill pid %d: %s", pid, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700241 }
242 }
243
244 if (ctx.initialized) {
245 close(ctx.fd);
246 }
247
248 return processes;
249}
250
251int killProcessGroup(uid_t uid, int initialPid, int signal)
252{
253 int processes;
Yusuke Satod5039302015-06-16 13:51:14 -0700254 const int sleep_us = 5 * 1000; // 5ms
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800255 int64_t startTime = android::uptimeMillis();
Yusuke Satod5039302015-06-16 13:51:14 -0700256 int retry = 40;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700257
258 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
259 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
Yusuke Satod5039302015-06-16 13:51:14 -0700260 if (retry > 0) {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700261 usleep(sleep_us);
Yusuke Satod5039302015-06-16 13:51:14 -0700262 --retry;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700263 } else {
264 SLOGE("failed to kill %d processes for processgroup %d\n",
265 processes, initialPid);
266 break;
267 }
268 }
269
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800270 SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700271 android::uptimeMillis()-startTime, processes);
272
Colin Crosscf8d1c22014-06-03 13:24:21 -0700273 if (processes == 0) {
274 return removeProcessGroup(uid, initialPid);
275 } else {
276 return -1;
277 }
278}
279
280static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
281{
282 int ret;
283
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100284 ret = mkdir(path, mode);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700285 if (ret < 0 && errno != EEXIST) {
286 return -errno;
287 }
288
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100289 ret = chown(path, uid, gid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700290 if (ret < 0) {
291 ret = -errno;
292 rmdir(path);
293 return ret;
294 }
295
296 return 0;
297}
298
299int createProcessGroup(uid_t uid, int initialPid)
300{
301 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
302 int ret;
303
304 convertUidToPath(path, sizeof(path), uid);
305
306 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
307 if (ret < 0) {
308 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
309 return ret;
310 }
311
312 convertUidPidToPath(path, sizeof(path), uid, initialPid);
313
314 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
315 if (ret < 0) {
316 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
317 return ret;
318 }
319
320 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
321
322 int fd = open(path, O_WRONLY);
323 if (fd < 0) {
324 ret = -errno;
325 SLOGE("failed to open %s: %s", path, strerror(errno));
326 return ret;
327 }
328
329 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
330 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
331
332 ret = write(fd, pid, len);
333 if (ret < 0) {
334 ret = -errno;
335 SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno));
336 } else {
337 ret = 0;
338 }
339
340 close(fd);
341 return ret;
342}
343