2014-06-03 22:24:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google, Inc
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
#define LOG_TAG "libprocessgroup"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dirent.h>
|
2014-12-29 21:24:25 +01:00
|
|
|
#include <errno.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <fcntl.h>
|
2014-11-21 02:05:15 +01:00
|
|
|
#include <inttypes.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-06-01 23:03:55 +02:00
|
|
|
|
|
|
|
#include <chrono>
|
2016-02-18 23:52:46 +01:00
|
|
|
#include <memory>
|
2016-06-07 06:19:55 +02:00
|
|
|
#include <mutex>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <thread>
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
#include <android-base/logging.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
|
|
|
#include <processgroup/processgroup.h>
|
2016-01-21 01:39:16 +01:00
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2016-03-18 15:28:31 +01:00
|
|
|
// Uncomment line below use memory cgroups for keeping track of (forked) PIDs
|
|
|
|
// #define USE_MEMCG 1
|
|
|
|
|
2016-01-21 01:39:16 +01:00
|
|
|
#define MEM_CGROUP_PATH "/dev/memcg/apps"
|
2016-02-08 11:42:25 +01:00
|
|
|
#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
|
2016-01-21 01:39:16 +01:00
|
|
|
#define ACCT_CGROUP_PATH "/acct"
|
|
|
|
|
|
|
|
#define PROCESSGROUP_UID_PREFIX "uid_"
|
|
|
|
#define PROCESSGROUP_PID_PREFIX "pid_"
|
|
|
|
#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
|
|
|
|
#define PROCESSGROUP_MAX_UID_LEN 11
|
|
|
|
#define PROCESSGROUP_MAX_PID_LEN 11
|
|
|
|
#define PROCESSGROUP_MAX_PATH_LEN \
|
|
|
|
((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \
|
|
|
|
sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \
|
|
|
|
sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
|
|
|
|
PROCESSGROUP_MAX_UID_LEN + \
|
|
|
|
sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
|
|
|
|
PROCESSGROUP_MAX_PID_LEN + \
|
|
|
|
sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
|
|
|
|
1)
|
|
|
|
|
|
|
|
std::once_flag init_path_flag;
|
2014-06-03 22:24:21 +02:00
|
|
|
|
|
|
|
struct ctx {
|
|
|
|
bool initialized;
|
|
|
|
int fd;
|
|
|
|
char buf[128];
|
|
|
|
char *buf_ptr;
|
|
|
|
size_t buf_len;
|
|
|
|
};
|
|
|
|
|
2016-01-21 01:39:16 +01:00
|
|
|
static const char* getCgroupRootPath() {
|
2016-03-18 15:28:31 +01:00
|
|
|
#ifdef USE_MEMCG
|
2016-01-21 01:39:16 +01:00
|
|
|
static const char* cgroup_root_path = NULL;
|
|
|
|
std::call_once(init_path_flag, [&]() {
|
2016-02-08 11:42:25 +01:00
|
|
|
// Check if mem cgroup is mounted, only then check for write-access to avoid
|
|
|
|
// SELinux denials
|
|
|
|
cgroup_root_path = access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
|
|
|
|
ACCT_CGROUP_PATH : MEM_CGROUP_PATH;
|
2016-01-21 01:39:16 +01:00
|
|
|
});
|
|
|
|
return cgroup_root_path;
|
2016-03-18 15:28:31 +01:00
|
|
|
#else
|
|
|
|
return ACCT_CGROUP_PATH;
|
|
|
|
#endif
|
2016-01-21 01:39:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-03 22:24:21 +02:00
|
|
|
static int convertUidToPath(char *path, size_t size, uid_t uid)
|
|
|
|
{
|
|
|
|
return snprintf(path, size, "%s/%s%d",
|
2016-01-21 01:39:16 +01:00
|
|
|
getCgroupRootPath(),
|
2014-06-03 22:24:21 +02:00
|
|
|
PROCESSGROUP_UID_PREFIX,
|
|
|
|
uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
|
|
|
|
{
|
|
|
|
return snprintf(path, size, "%s/%s%d/%s%d",
|
2016-01-21 01:39:16 +01:00
|
|
|
getCgroupRootPath(),
|
2014-06-03 22:24:21 +02:00
|
|
|
PROCESSGROUP_UID_PREFIX,
|
|
|
|
uid,
|
|
|
|
PROCESSGROUP_PID_PREFIX,
|
|
|
|
pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int initCtx(uid_t uid, int pid, struct ctx *ctx)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
|
|
|
|
convertUidPidToPath(path, sizeof(path), uid, pid);
|
|
|
|
strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
|
|
|
|
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = -errno;
|
2016-08-02 22:30:30 +02:00
|
|
|
PLOG(WARNING) << "failed to open " << path;
|
2014-06-03 22:24:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->fd = fd;
|
|
|
|
ctx->buf_ptr = ctx->buf;
|
|
|
|
ctx->buf_len = 0;
|
|
|
|
ctx->initialized = true;
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "Initialized context for " << path;
|
2014-10-14 02:45:22 +02:00
|
|
|
|
2014-06-03 22:24:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int refillBuffer(struct ctx *ctx)
|
|
|
|
{
|
|
|
|
memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
|
|
|
|
ctx->buf_ptr = ctx->buf;
|
|
|
|
|
|
|
|
ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
|
2014-10-14 02:45:22 +02:00
|
|
|
sizeof(ctx->buf) - ctx->buf_len - 1);
|
2014-06-03 22:24:21 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
return -errno;
|
|
|
|
} else if (ret == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->buf_len += ret;
|
2014-10-15 20:36:28 +02:00
|
|
|
ctx->buf[ctx->buf_len] = 0;
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "Read " << ret << " to buffer: " << ctx->buf;
|
2014-10-14 02:45:22 +02:00
|
|
|
|
2014-06-03 22:24:21 +02:00
|
|
|
assert(ctx->buf_len <= sizeof(ctx->buf));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx->initialized) {
|
|
|
|
int ret = initCtx(uid, appProcessPid, ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *eptr;
|
|
|
|
while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
|
|
|
|
int ret = refillBuffer(ctx);
|
|
|
|
if (ret == 0) {
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*eptr = '\0';
|
|
|
|
char *pid_eptr = NULL;
|
|
|
|
errno = 0;
|
|
|
|
long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
|
|
|
|
if (errno != 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if (pid_eptr != eptr) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-10-14 02:45:22 +02:00
|
|
|
ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
|
2014-06-03 22:24:21 +02:00
|
|
|
ctx->buf_ptr = eptr + 1;
|
|
|
|
|
|
|
|
return (pid_t)pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int removeProcessGroup(uid_t uid, int pid)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
|
|
|
|
|
|
|
|
convertUidPidToPath(path, sizeof(path), uid, pid);
|
|
|
|
ret = rmdir(path);
|
|
|
|
|
|
|
|
convertUidToPath(path, sizeof(path), uid);
|
|
|
|
rmdir(path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void removeUidProcessGroups(const char *uid_path)
|
|
|
|
{
|
2016-02-18 23:52:46 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
|
2014-06-03 22:24:21 +02:00
|
|
|
if (uid != NULL) {
|
2016-09-28 22:29:54 +02:00
|
|
|
dirent* dir;
|
|
|
|
while ((dir = readdir(uid.get())) != nullptr) {
|
2014-06-03 22:24:21 +02:00
|
|
|
char path[PROCESSGROUP_MAX_PATH_LEN];
|
|
|
|
|
|
|
|
if (dir->d_type != DT_DIR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "removing " << path;
|
|
|
|
if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeAllProcessGroups()
|
|
|
|
{
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "removeAllProcessGroups()";
|
2016-08-03 22:29:04 +02:00
|
|
|
const char* cgroup_root_path = getCgroupRootPath();
|
2016-02-19 20:10:30 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
|
2014-06-03 22:24:21 +02:00
|
|
|
if (root == NULL) {
|
2016-08-03 22:29:04 +02:00
|
|
|
PLOG(ERROR) << "failed to open " << cgroup_root_path;
|
2014-08-20 23:11:13 +02:00
|
|
|
} else {
|
2016-09-28 22:29:54 +02:00
|
|
|
dirent* dir;
|
|
|
|
while ((dir = readdir(root.get())) != nullptr) {
|
2014-06-03 22:24:21 +02:00
|
|
|
char path[PROCESSGROUP_MAX_PATH_LEN];
|
|
|
|
|
|
|
|
if (dir->d_type != DT_DIR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:39:16 +01:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
|
2014-06-03 22:24:21 +02:00
|
|
|
removeUidProcessGroups(path);
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "removing " << path;
|
|
|
|
if (rmdir(path) == -1) PLOG(WARNING) << "failed to remove " << path;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 02:25:24 +02:00
|
|
|
static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) {
|
2014-06-03 22:24:21 +02:00
|
|
|
int processes = 0;
|
|
|
|
struct ctx ctx;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
ctx.initialized = false;
|
|
|
|
|
|
|
|
while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
|
|
|
|
processes++;
|
2014-10-14 02:45:22 +02:00
|
|
|
if (pid == 0) {
|
|
|
|
// Should never happen... but if it does, trying to kill this
|
|
|
|
// will boomerang right back and kill us! Let's not let that happen.
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?";
|
2014-10-14 02:45:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid
|
|
|
|
<< " as part of process group " << initialPid;
|
|
|
|
if (kill(pid, signal) == -1) {
|
|
|
|
PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.initialized) {
|
|
|
|
close(ctx.fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return processes;
|
|
|
|
}
|
|
|
|
|
2017-03-29 02:25:24 +02:00
|
|
|
static int killProcessGroup(uid_t uid, int initialPid, int signal, int retry) {
|
2016-06-01 23:03:55 +02:00
|
|
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2016-06-01 23:03:55 +02:00
|
|
|
int processes;
|
2017-03-29 02:25:24 +02:00
|
|
|
while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) {
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid;
|
2015-06-16 22:51:14 +02:00
|
|
|
if (retry > 0) {
|
2016-11-15 02:08:47 +01:00
|
|
|
std::this_thread::sleep_for(5ms);
|
2015-06-16 22:51:14 +02:00
|
|
|
--retry;
|
2014-06-03 22:24:21 +02:00
|
|
|
} else {
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(ERROR) << "failed to kill " << processes << " processes for processgroup "
|
|
|
|
<< initialPid;
|
2014-06-03 22:24:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:03:55 +02:00
|
|
|
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
|
|
|
LOG(VERBOSE) << "Killed process group uid " << uid << " pid " << initialPid << " in "
|
|
|
|
<< static_cast<int>(ms) << "ms, " << processes << " procs remain";
|
2014-10-14 02:45:22 +02:00
|
|
|
|
2014-06-03 22:24:21 +02:00
|
|
|
if (processes == 0) {
|
|
|
|
return removeProcessGroup(uid, initialPid);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 02:25:24 +02:00
|
|
|
int killProcessGroup(uid_t uid, int initialPid, int signal) {
|
|
|
|
return killProcessGroup(uid, initialPid, signal, 40 /*maxRetry*/);
|
|
|
|
}
|
|
|
|
|
|
|
|
int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
|
|
|
|
return killProcessGroup(uid, initialPid, signal, 0 /*maxRetry*/);
|
|
|
|
}
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
|
2014-06-03 22:24:21 +02:00
|
|
|
{
|
2016-08-02 22:30:30 +02:00
|
|
|
if (mkdir(path, mode) == -1 && errno != EEXIST) {
|
|
|
|
return false;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
if (chown(path, uid, gid) == -1) {
|
|
|
|
int saved_errno = errno;
|
2014-06-03 22:24:21 +02:00
|
|
|
rmdir(path);
|
2016-08-02 22:30:30 +02:00
|
|
|
errno = saved_errno;
|
|
|
|
return false;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
return true;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int createProcessGroup(uid_t uid, int initialPid)
|
|
|
|
{
|
|
|
|
char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
|
|
|
|
|
|
|
|
convertUidToPath(path, sizeof(path), uid);
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
|
|
|
|
PLOG(ERROR) << "failed to make and chown " << path;
|
|
|
|
return -errno;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
convertUidPidToPath(path, sizeof(path), uid, initialPid);
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
|
|
|
|
PLOG(ERROR) << "failed to make and chown " << path;
|
|
|
|
return -errno;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
|
|
|
|
|
|
|
|
int fd = open(path, O_WRONLY);
|
2016-08-02 22:30:30 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
int ret = -errno;
|
|
|
|
PLOG(ERROR) << "failed to open " << path;
|
2014-06-03 22:24:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
|
|
|
|
int len = snprintf(pid, sizeof(pid), "%d", initialPid);
|
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
int ret = 0;
|
|
|
|
if (write(fd, pid, len) < 0) {
|
2014-06-03 22:24:21 +02:00
|
|
|
ret = -errno;
|
2016-08-02 22:30:30 +02:00
|
|
|
PLOG(ERROR) << "failed to write '" << pid << "' to " << path;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|