blob: a80965fd053192433c5fbe76b8cea84baa91df95 [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>
31
32#include <log/log.h>
33#include <private/android_filesystem_config.h>
34
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070035#include <utils/SystemClock.h>
36
Colin Crosscf8d1c22014-06-03 13:24:21 -070037#include <processgroup/processgroup.h>
38#include "processgroup_priv.h"
39
40struct ctx {
41 bool initialized;
42 int fd;
43 char buf[128];
44 char *buf_ptr;
45 size_t buf_len;
46};
47
48static int convertUidToPath(char *path, size_t size, uid_t uid)
49{
50 return snprintf(path, size, "%s/%s%d",
51 PROCESSGROUP_CGROUP_PATH,
52 PROCESSGROUP_UID_PREFIX,
53 uid);
54}
55
56static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
57{
58 return snprintf(path, size, "%s/%s%d/%s%d",
59 PROCESSGROUP_CGROUP_PATH,
60 PROCESSGROUP_UID_PREFIX,
61 uid,
62 PROCESSGROUP_PID_PREFIX,
63 pid);
64}
65
66static int initCtx(uid_t uid, int pid, struct ctx *ctx)
67{
68 int ret;
69 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
70 convertUidPidToPath(path, sizeof(path), uid, pid);
71 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
72
73 int fd = open(path, O_RDONLY);
74 if (fd < 0) {
75 ret = -errno;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070076 SLOGW("failed to open %s: %s", path, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -070077 return ret;
78 }
79
80 ctx->fd = fd;
81 ctx->buf_ptr = ctx->buf;
82 ctx->buf_len = 0;
83 ctx->initialized = true;
84
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070085 SLOGV("Initialized context for %s", path);
86
Colin Crosscf8d1c22014-06-03 13:24:21 -070087 return 0;
88}
89
90static int refillBuffer(struct ctx *ctx)
91{
92 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
93 ctx->buf_ptr = ctx->buf;
94
95 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070096 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -070097 if (ret < 0) {
98 return -errno;
99 } else if (ret == 0) {
100 return 0;
101 }
102
103 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700104 ctx->buf[ctx->buf_len] = 0;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800105 SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700106
Colin Crosscf8d1c22014-06-03 13:24:21 -0700107 assert(ctx->buf_len <= sizeof(ctx->buf));
108
109 return ret;
110}
111
112static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
113{
114 if (!ctx->initialized) {
115 int ret = initCtx(uid, appProcessPid, ctx);
116 if (ret < 0) {
117 return ret;
118 }
119 }
120
121 char *eptr;
122 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
123 int ret = refillBuffer(ctx);
124 if (ret == 0) {
125 return -ERANGE;
126 }
127 if (ret < 0) {
128 return ret;
129 }
130 }
131
132 *eptr = '\0';
133 char *pid_eptr = NULL;
134 errno = 0;
135 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
136 if (errno != 0) {
137 return -errno;
138 }
139 if (pid_eptr != eptr) {
140 return -EINVAL;
141 }
142
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700143 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700144 ctx->buf_ptr = eptr + 1;
145
146 return (pid_t)pid;
147}
148
149static int removeProcessGroup(uid_t uid, int pid)
150{
151 int ret;
152 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
153
154 convertUidPidToPath(path, sizeof(path), uid, pid);
155 ret = rmdir(path);
156
157 convertUidToPath(path, sizeof(path), uid);
158 rmdir(path);
159
160 return ret;
161}
162
163static void removeUidProcessGroups(const char *uid_path)
164{
165 DIR *uid = opendir(uid_path);
166 if (uid != NULL) {
167 struct dirent cur;
168 struct dirent *dir;
169 while ((readdir_r(uid, &cur, &dir) == 0) && dir) {
170 char path[PROCESSGROUP_MAX_PATH_LEN];
171
172 if (dir->d_type != DT_DIR) {
173 continue;
174 }
175
176 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
177 continue;
178 }
179
180 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
181 SLOGV("removing %s\n", path);
182 rmdir(path);
183 }
Colin Crossc15dd042014-08-20 14:11:13 -0700184 closedir(uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700185 }
186}
187
188void removeAllProcessGroups()
189{
190 SLOGV("removeAllProcessGroups()");
191 DIR *root = opendir(PROCESSGROUP_CGROUP_PATH);
192 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;
197 while ((readdir_r(root, &cur, &dir) == 0) && dir) {
198 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 Crossc15dd042014-08-20 14:11:13 -0700212 closedir(root);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700213 }
214}
215
216static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
217{
218 int processes = 0;
219 struct ctx ctx;
220 pid_t pid;
221
222 ctx.initialized = false;
223
224 while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
225 processes++;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700226 if (pid == 0) {
227 // Should never happen... but if it does, trying to kill this
228 // will boomerang right back and kill us! Let's not let that happen.
229 SLOGW("Yikes, we've been told to kill pid 0! How about we don't do that.");
230 continue;
231 }
232 if (pid != initialPid) {
233 // We want to be noisy about killing processes so we can understand
234 // what is going on in the log; however, don't be noisy about the base
235 // process, since that it something we always kill, and we have already
236 // logged elsewhere about killing it.
237 SLOGI("Killing pid %d in uid %d as part of process group %d", pid, uid, initialPid);
238 }
Colin Crosscf8d1c22014-06-03 13:24:21 -0700239 int ret = kill(pid, signal);
240 if (ret == -1) {
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700241 SLOGW("failed to kill pid %d: %s", pid, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -0700242 }
243 }
244
245 if (ctx.initialized) {
246 close(ctx.fd);
247 }
248
249 return processes;
250}
251
252int killProcessGroup(uid_t uid, int initialPid, int signal)
253{
254 int processes;
255 int sleep_us = 100;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800256 int64_t startTime = android::uptimeMillis();
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);
260 if (sleep_us < 128000) {
261 usleep(sleep_us);
262 sleep_us *= 2;
263 } 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