blob: f7bc2cd7d8b5d180f93ed962d3f970d6beb6d4a4 [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>
23#include <stdbool.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29
30#include <log/log.h>
31#include <private/android_filesystem_config.h>
32
33#include <processgroup/processgroup.h>
34#include "processgroup_priv.h"
35
36struct ctx {
37 bool initialized;
38 int fd;
39 char buf[128];
40 char *buf_ptr;
41 size_t buf_len;
42};
43
44static int convertUidToPath(char *path, size_t size, uid_t uid)
45{
46 return snprintf(path, size, "%s/%s%d",
47 PROCESSGROUP_CGROUP_PATH,
48 PROCESSGROUP_UID_PREFIX,
49 uid);
50}
51
52static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
53{
54 return snprintf(path, size, "%s/%s%d/%s%d",
55 PROCESSGROUP_CGROUP_PATH,
56 PROCESSGROUP_UID_PREFIX,
57 uid,
58 PROCESSGROUP_PID_PREFIX,
59 pid);
60}
61
62static int initCtx(uid_t uid, int pid, struct ctx *ctx)
63{
64 int ret;
65 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
66 convertUidPidToPath(path, sizeof(path), uid, pid);
67 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
68
69 int fd = open(path, O_RDONLY);
70 if (fd < 0) {
71 ret = -errno;
72 SLOGV("failed to open %s: %s", path, strerror(errno));
73 return ret;
74 }
75
76 ctx->fd = fd;
77 ctx->buf_ptr = ctx->buf;
78 ctx->buf_len = 0;
79 ctx->initialized = true;
80
81 return 0;
82}
83
84static int refillBuffer(struct ctx *ctx)
85{
86 memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
87 ctx->buf_ptr = ctx->buf;
88
89 ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
90 sizeof(ctx->buf) - ctx->buf_len);
91 if (ret < 0) {
92 return -errno;
93 } else if (ret == 0) {
94 return 0;
95 }
96
97 ctx->buf_len += ret;
98 assert(ctx->buf_len <= sizeof(ctx->buf));
99
100 return ret;
101}
102
103static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
104{
105 if (!ctx->initialized) {
106 int ret = initCtx(uid, appProcessPid, ctx);
107 if (ret < 0) {
108 return ret;
109 }
110 }
111
112 char *eptr;
113 while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
114 int ret = refillBuffer(ctx);
115 if (ret == 0) {
116 return -ERANGE;
117 }
118 if (ret < 0) {
119 return ret;
120 }
121 }
122
123 *eptr = '\0';
124 char *pid_eptr = NULL;
125 errno = 0;
126 long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
127 if (errno != 0) {
128 return -errno;
129 }
130 if (pid_eptr != eptr) {
131 return -EINVAL;
132 }
133
134 ctx->buf_ptr = eptr + 1;
135
136 return (pid_t)pid;
137}
138
139static int removeProcessGroup(uid_t uid, int pid)
140{
141 int ret;
142 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
143
144 convertUidPidToPath(path, sizeof(path), uid, pid);
145 ret = rmdir(path);
146
147 convertUidToPath(path, sizeof(path), uid);
148 rmdir(path);
149
150 return ret;
151}
152
153static void removeUidProcessGroups(const char *uid_path)
154{
155 DIR *uid = opendir(uid_path);
156 if (uid != NULL) {
157 struct dirent cur;
158 struct dirent *dir;
159 while ((readdir_r(uid, &cur, &dir) == 0) && dir) {
160 char path[PROCESSGROUP_MAX_PATH_LEN];
161
162 if (dir->d_type != DT_DIR) {
163 continue;
164 }
165
166 if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
167 continue;
168 }
169
170 snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
171 SLOGV("removing %s\n", path);
172 rmdir(path);
173 }
Colin Crossc15dd042014-08-20 14:11:13 -0700174 closedir(uid);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700175 }
176}
177
178void removeAllProcessGroups()
179{
180 SLOGV("removeAllProcessGroups()");
181 DIR *root = opendir(PROCESSGROUP_CGROUP_PATH);
182 if (root == NULL) {
183 SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno));
Colin Crossc15dd042014-08-20 14:11:13 -0700184 } else {
Colin Crosscf8d1c22014-06-03 13:24:21 -0700185 struct dirent cur;
186 struct dirent *dir;
187 while ((readdir_r(root, &cur, &dir) == 0) && dir) {
188 char path[PROCESSGROUP_MAX_PATH_LEN];
189
190 if (dir->d_type != DT_DIR) {
191 continue;
192 }
193 if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
194 continue;
195 }
196
197 snprintf(path, sizeof(path), "%s/%s", PROCESSGROUP_CGROUP_PATH, dir->d_name);
198 removeUidProcessGroups(path);
199 SLOGV("removing %s\n", path);
200 rmdir(path);
201 }
Colin Crossc15dd042014-08-20 14:11:13 -0700202 closedir(root);
Colin Crosscf8d1c22014-06-03 13:24:21 -0700203 }
204}
205
206static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
207{
208 int processes = 0;
209 struct ctx ctx;
210 pid_t pid;
211
212 ctx.initialized = false;
213
214 while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
215 processes++;
216 SLOGV("sending processgroup kill to pid %d\n", pid);
217 int ret = kill(pid, signal);
218 if (ret == -1) {
219 SLOGV("failed to kill pid %d: %s", pid, strerror(errno));
220 }
221 }
222
223 if (ctx.initialized) {
224 close(ctx.fd);
225 }
226
227 return processes;
228}
229
230int killProcessGroup(uid_t uid, int initialPid, int signal)
231{
232 int processes;
233 int sleep_us = 100;
234
235 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
236 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
237 if (sleep_us < 128000) {
238 usleep(sleep_us);
239 sleep_us *= 2;
240 } else {
241 SLOGE("failed to kill %d processes for processgroup %d\n",
242 processes, initialPid);
243 break;
244 }
245 }
246
247 if (processes == 0) {
248 return removeProcessGroup(uid, initialPid);
249 } else {
250 return -1;
251 }
252}
253
254static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
255{
256 int ret;
257
258 ret = mkdir(path, 0750);
259 if (ret < 0 && errno != EEXIST) {
260 return -errno;
261 }
262
263 ret = chown(path, AID_SYSTEM, AID_SYSTEM);
264 if (ret < 0) {
265 ret = -errno;
266 rmdir(path);
267 return ret;
268 }
269
270 return 0;
271}
272
273int createProcessGroup(uid_t uid, int initialPid)
274{
275 char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
276 int ret;
277
278 convertUidToPath(path, sizeof(path), uid);
279
280 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
281 if (ret < 0) {
282 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
283 return ret;
284 }
285
286 convertUidPidToPath(path, sizeof(path), uid, initialPid);
287
288 ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
289 if (ret < 0) {
290 SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
291 return ret;
292 }
293
294 strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
295
296 int fd = open(path, O_WRONLY);
297 if (fd < 0) {
298 ret = -errno;
299 SLOGE("failed to open %s: %s", path, strerror(errno));
300 return ret;
301 }
302
303 char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
304 int len = snprintf(pid, sizeof(pid), "%d", initialPid);
305
306 ret = write(fd, pid, len);
307 if (ret < 0) {
308 ret = -errno;
309 SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno));
310 } else {
311 ret = 0;
312 }
313
314 close(fd);
315 return ret;
316}
317