blob: 4b09f247c5d360da8dadd826da6d833baab6f3a0 [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>
22#include <fcntl.h>
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -080023#include <inttypes.h>
Colin Crosscf8d1c22014-06-03 13:24:21 -070024#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30
31#include <log/log.h>
32#include <private/android_filesystem_config.h>
33
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070034#include <utils/SystemClock.h>
35
Colin Crosscf8d1c22014-06-03 13:24:21 -070036#include <processgroup/processgroup.h>
37#include "processgroup_priv.h"
38
39struct ctx {
40 bool initialized;
41 int fd;
42 char buf[128];
43 char *buf_ptr;
44 size_t buf_len;
45};
46
47static int convertUidToPath(char *path, size_t size, uid_t uid)
48{
49 return snprintf(path, size, "%s/%s%d",
50 PROCESSGROUP_CGROUP_PATH,
51 PROCESSGROUP_UID_PREFIX,
52 uid);
53}
54
55static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
56{
57 return snprintf(path, size, "%s/%s%d/%s%d",
58 PROCESSGROUP_CGROUP_PATH,
59 PROCESSGROUP_UID_PREFIX,
60 uid,
61 PROCESSGROUP_PID_PREFIX,
62 pid);
63}
64
65static int initCtx(uid_t uid, int pid, struct ctx *ctx)
66{
67 int ret;
68 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
69 convertUidPidToPath(path, sizeof(path), uid, pid);
70 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
71
72 int fd = open(path, O_RDONLY);
73 if (fd < 0) {
74 ret = -errno;
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070075 SLOGW("failed to open %s: %s", path, strerror(errno));
Colin Crosscf8d1c22014-06-03 13:24:21 -070076 return ret;
77 }
78
79 ctx->fd = fd;
80 ctx->buf_ptr = ctx->buf;
81 ctx->buf_len = 0;
82 ctx->initialized = true;
83
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070084 SLOGV("Initialized context for %s", path);
85
Colin Crosscf8d1c22014-06-03 13:24:21 -070086 return 0;
87}
88
89static int refillBuffer(struct ctx *ctx)
90{
91 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
92 ctx->buf_ptr = ctx->buf;
93
94 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -070095 sizeof(ctx->buf) - ctx->buf_len - 1);
Colin Crosscf8d1c22014-06-03 13:24:21 -070096 if (ret < 0) {
97 return -errno;
98 } else if (ret == 0) {
99 return 0;
100 }
101
102 ctx->buf_len += ret;
Dianne Hackborn67f46cb2014-10-15 11:36:28 -0700103 ctx->buf[ctx->buf_len] = 0;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800104 SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700105
Colin Crosscf8d1c22014-06-03 13:24:21 -0700106 assert(ctx->buf_len <= sizeof(ctx->buf));
107
108 return ret;
109}
110
111static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
112{
113 if (!ctx->initialized) {
114 int ret = initCtx(uid, appProcessPid, ctx);
115 if (ret < 0) {
116 return ret;
117 }
118 }
119
120 char *eptr;
121 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
122 int ret = refillBuffer(ctx);
123 if (ret == 0) {
124 return -ERANGE;
125 }
126 if (ret < 0) {
127 return ret;
128 }
129 }
130
131 *eptr = '\0';
132 char *pid_eptr = NULL;
133 errno = 0;
134 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
135 if (errno != 0) {
136 return -errno;
137 }
138 if (pid_eptr != eptr) {
139 return -EINVAL;
140 }
141
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700142 ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
Colin Crosscf8d1c22014-06-03 13:24:21 -0700143 ctx->buf_ptr = eptr + 1;
144
145 return (pid_t)pid;
146}
147
148static int removeProcessGroup(uid_t uid, int pid)
149{
150 int ret;
151 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
152
153 convertUidPidToPath(path, sizeof(path), uid, pid);
154 ret = rmdir(path);
155
156 convertUidToPath(path, sizeof(path), uid);
157 rmdir(path);
158
159 return ret;
160}
161
162static void removeUidProcessGroups(const char *uid_path)
163{
164 DIR *uid = opendir(uid_path);
165 if (uid != NULL) {
166 struct dirent cur;
167 struct dirent *dir;
168 while ((readdir_r(uid, &cur, &dir) == 0) && dir) {
169 char path[PROCESSGROUP_MAX_PATH_LEN];
170
171 if (dir->d_type != DT_DIR) {
172 continue;
173 }
174
175 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
176 continue;
177 }
178
179 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
180 SLOGV("removing %s\n", path);
181 rmdir(path);
182 }
Colin Crossc15dd042014-08-20 14:11:13 -0700183 closedir(uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700184 }
185}
186
187void removeAllProcessGroups()
188{
189 SLOGV("removeAllProcessGroups()");
190 DIR *root = opendir(PROCESSGROUP_CGROUP_PATH);
191 if (root == NULL) {
192 SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno));
Colin Crossc15dd042014-08-20 14:11:13 -0700193 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700194 struct dirent cur;
195 struct dirent *dir;
196 while ((readdir_r(root, &cur, &dir) == 0) && dir) {
197 char path[PROCESSGROUP_MAX_PATH_LEN];
198
199 if (dir->d_type != DT_DIR) {
200 continue;
201 }
202 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
203 continue;
204 }
205
206 snprintf(path, sizeof(path), "%s/%s", PROCESSGROUP_CGROUP_PATH, dir->d_name);
207 removeUidProcessGroups(path);
208 SLOGV("removing %s\n", path);
209 rmdir(path);
210 }
Colin Crossc15dd042014-08-20 14:11:13 -0700211 closedir(root);
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;
254 int sleep_us = 100;
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800255 int64_t startTime = android::uptimeMillis();
Colin Crosscf8d1c22014-06-03 13:24:21 -0700256
257 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
258 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
259 if (sleep_us < 128000) {
260 usleep(sleep_us);
261 sleep_us *= 2;
262 } else {
263 SLOGE("failed to kill %d processes for processgroup %d\n",
264 processes, initialPid);
265 break;
266 }
267 }
268
Chih-Hung Hsiehfcc81152014-11-20 17:05:15 -0800269 SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
Dianne Hackborn2c5e7e12014-10-13 17:45:22 -0700270 android::uptimeMillis()-startTime, processes);
271
Colin Crosscf8d1c22014-06-03 13:24:21 -0700272 if (processes == 0) {
273 return removeProcessGroup(uid, initialPid);
274 } else {
275 return -1;
276 }
277}
278
279static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
280{
281 int ret;
282
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100283 ret = mkdir(path, mode);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700284 if (ret < 0 && errno != EEXIST) {
285 return -errno;
286 }
287
Bernhard Rosenkränzer758aeb72014-11-17 20:46:00 +0100288 ret = chown(path, uid, gid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700289 if (ret < 0) {
290 ret = -errno;
291 rmdir(path);
292 return ret;
293 }
294
295 return 0;
296}
297
298int createProcessGroup(uid_t uid, int initialPid)
299{
300 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
301 int ret;
302
303 convertUidToPath(path, sizeof(path), uid);
304
305 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
306 if (ret < 0) {
307 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
308 return ret;
309 }
310
311 convertUidPidToPath(path, sizeof(path), uid, initialPid);
312
313 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
314 if (ret < 0) {
315 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
316 return ret;
317 }
318
319 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
320
321 int fd = open(path, O_WRONLY);
322 if (fd < 0) {
323 ret = -errno;
324 SLOGE("failed to open %s: %s", path, strerror(errno));
325 return ret;
326 }
327
328 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
329 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
330
331 ret = write(fd, pid, len);
332 if (ret < 0) {
333 ret = -errno;
334 SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno));
335 } else {
336 ret = 0;
337 }
338
339 close(fd);
340 return ret;
341}
342