2014-06-04 04:24:21 +08: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-30 04:24:25 +08:00
|
|
|
#include <errno.h>
|
2014-06-04 04:24:21 +08:00
|
|
|
#include <fcntl.h>
|
2014-11-21 09:05:15 +08:00
|
|
|
#include <inttypes.h>
|
2014-06-04 04:24:21 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2017-06-06 10:20:17 +08:00
|
|
|
#include <unistd.h>
|
2016-06-02 05:03:55 +08:00
|
|
|
|
|
|
|
#include <chrono>
|
2016-02-19 06:52:46 +08:00
|
|
|
#include <memory>
|
2016-06-07 12:19:55 +08:00
|
|
|
#include <mutex>
|
2017-06-06 10:20:17 +08:00
|
|
|
#include <set>
|
2016-11-15 09:08:47 +08:00
|
|
|
#include <thread>
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2017-07-17 10:38:11 +08:00
|
|
|
#include <android-base/file.h>
|
2016-08-03 04:30:30 +08:00
|
|
|
#include <android-base/logging.h>
|
2017-04-22 04:48:49 +08:00
|
|
|
#include <android-base/unique_fd.h>
|
2014-06-04 04:24:21 +08:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
|
|
|
#include <processgroup/processgroup.h>
|
2016-01-21 08:39:16 +08:00
|
|
|
|
2017-07-17 10:38:11 +08:00
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
2016-11-15 09:08:47 +08:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2016-01-21 08:39:16 +08:00
|
|
|
#define MEM_CGROUP_PATH "/dev/memcg/apps"
|
2016-02-08 18:42:25 +08:00
|
|
|
#define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks"
|
2016-01-21 08:39:16 +08: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-04 04:24:21 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
class ProcessGroup {
|
|
|
|
public:
|
|
|
|
ProcessGroup() : buf_ptr_(buf_), buf_len_(0) {}
|
|
|
|
|
|
|
|
bool Open(uid_t uid, int pid);
|
|
|
|
|
|
|
|
// Return positive number and sets *pid = next pid in process cgroup on success
|
|
|
|
// Returns 0 if there are no pids left in the process cgroup
|
|
|
|
// Returns -errno if an error was encountered
|
|
|
|
int GetOneAppProcess(pid_t* pid);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Returns positive number of bytes filled on success
|
|
|
|
// Returns 0 if there was nothing to read
|
|
|
|
// Returns -errno if an error was encountered
|
|
|
|
int RefillBuffer();
|
|
|
|
|
|
|
|
android::base::unique_fd fd_;
|
|
|
|
char buf_[128];
|
|
|
|
char* buf_ptr_;
|
|
|
|
size_t buf_len_;
|
2014-06-04 04:24:21 +08:00
|
|
|
};
|
|
|
|
|
2016-01-21 08:39:16 +08:00
|
|
|
static const char* getCgroupRootPath() {
|
|
|
|
static const char* cgroup_root_path = NULL;
|
|
|
|
std::call_once(init_path_flag, [&]() {
|
2016-02-08 18:42:25 +08: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 08:39:16 +08:00
|
|
|
});
|
|
|
|
return cgroup_root_path;
|
|
|
|
}
|
|
|
|
|
2014-06-04 04:24:21 +08:00
|
|
|
static int convertUidToPath(char *path, size_t size, uid_t uid)
|
|
|
|
{
|
|
|
|
return snprintf(path, size, "%s/%s%d",
|
2016-01-21 08:39:16 +08:00
|
|
|
getCgroupRootPath(),
|
2014-06-04 04:24:21 +08: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 08:39:16 +08:00
|
|
|
getCgroupRootPath(),
|
2014-06-04 04:24:21 +08:00
|
|
|
PROCESSGROUP_UID_PREFIX,
|
|
|
|
uid,
|
|
|
|
PROCESSGROUP_PID_PREFIX,
|
|
|
|
pid);
|
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
bool ProcessGroup::Open(uid_t uid, int pid) {
|
2014-06-04 04:24:21 +08:00
|
|
|
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);
|
2017-04-22 04:48:49 +08:00
|
|
|
if (fd < 0) return false;
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
fd_.reset(fd);
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
LOG(VERBOSE) << "Initialized context for " << path;
|
2014-10-14 08:45:22 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
return true;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
int ProcessGroup::RefillBuffer() {
|
|
|
|
memmove(buf_, buf_ptr_, buf_len_);
|
|
|
|
buf_ptr_ = buf_;
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
ssize_t ret = read(fd_, buf_ptr_ + buf_len_, sizeof(buf_) - buf_len_ - 1);
|
2014-06-04 04:24:21 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
return -errno;
|
|
|
|
} else if (ret == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
buf_len_ += ret;
|
|
|
|
buf_[buf_len_] = 0;
|
|
|
|
LOG(VERBOSE) << "Read " << ret << " to buffer: " << buf_;
|
2014-10-14 08:45:22 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
assert(buf_len_ <= sizeof(buf_));
|
2014-06-04 04:24:21 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
int ProcessGroup::GetOneAppProcess(pid_t* out_pid) {
|
|
|
|
*out_pid = 0;
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
char* eptr;
|
|
|
|
while ((eptr = static_cast<char*>(memchr(buf_ptr_, '\n', buf_len_))) == nullptr) {
|
|
|
|
int ret = RefillBuffer();
|
|
|
|
if (ret <= 0) return ret;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*eptr = '\0';
|
2017-04-22 04:48:49 +08:00
|
|
|
char* pid_eptr = nullptr;
|
2014-06-04 04:24:21 +08:00
|
|
|
errno = 0;
|
2017-04-22 04:48:49 +08:00
|
|
|
long pid = strtol(buf_ptr_, &pid_eptr, 10);
|
2014-06-04 04:24:21 +08:00
|
|
|
if (errno != 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if (pid_eptr != eptr) {
|
2017-04-22 04:48:49 +08:00
|
|
|
errno = EINVAL;
|
|
|
|
return -errno;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
buf_len_ -= (eptr - buf_ptr_) + 1;
|
|
|
|
buf_ptr_ = eptr + 1;
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
*out_pid = static_cast<pid_t>(pid);
|
|
|
|
return 1;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-19 06:52:46 +08:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path), closedir);
|
2014-06-04 04:24:21 +08:00
|
|
|
if (uid != NULL) {
|
2016-09-29 04:29:54 +08:00
|
|
|
dirent* dir;
|
|
|
|
while ((dir = readdir(uid.get())) != nullptr) {
|
2014-06-04 04:24:21 +08: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);
|
2017-04-22 04:48:49 +08:00
|
|
|
LOG(VERBOSE) << "Removing " << path;
|
|
|
|
if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeAllProcessGroups()
|
|
|
|
{
|
2016-08-03 04:30:30 +08:00
|
|
|
LOG(VERBOSE) << "removeAllProcessGroups()";
|
2016-08-04 04:29:04 +08:00
|
|
|
const char* cgroup_root_path = getCgroupRootPath();
|
2016-02-20 03:10:30 +08:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path), closedir);
|
2014-06-04 04:24:21 +08:00
|
|
|
if (root == NULL) {
|
2017-04-22 04:48:49 +08:00
|
|
|
PLOG(ERROR) << "Failed to open " << cgroup_root_path;
|
2014-08-21 05:11:13 +08:00
|
|
|
} else {
|
2016-09-29 04:29:54 +08:00
|
|
|
dirent* dir;
|
|
|
|
while ((dir = readdir(root.get())) != nullptr) {
|
2014-06-04 04:24:21 +08: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 08:39:16 +08:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name);
|
2014-06-04 04:24:21 +08:00
|
|
|
removeUidProcessGroups(path);
|
2017-04-22 04:48:49 +08:00
|
|
|
LOG(VERBOSE) << "Removing " << path;
|
|
|
|
if (rmdir(path) == -1) PLOG(WARNING) << "Failed to remove " << path;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
// Returns number of processes killed on success
|
|
|
|
// Returns 0 if there are no processes in the process cgroup left to kill
|
|
|
|
// Returns -errno on error
|
2017-03-29 08:25:24 +08:00
|
|
|
static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) {
|
2017-04-22 04:48:49 +08:00
|
|
|
ProcessGroup process_group;
|
|
|
|
if (!process_group.Open(uid, initialPid)) {
|
|
|
|
PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
|
|
|
|
return -errno;
|
|
|
|
}
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2017-06-06 10:20:17 +08:00
|
|
|
// We separate all of the pids in the cgroup into those pids that are also the leaders of
|
|
|
|
// process groups (stored in the pgids set) and those that are not (stored in the pids set).
|
|
|
|
std::set<pid_t> pgids;
|
|
|
|
pgids.emplace(initialPid);
|
|
|
|
std::set<pid_t> pids;
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
int ret;
|
|
|
|
pid_t pid;
|
|
|
|
int processes = 0;
|
|
|
|
while ((ret = process_group.GetOneAppProcess(&pid)) > 0 && pid >= 0) {
|
2014-06-04 04:24:21 +08:00
|
|
|
processes++;
|
2014-10-14 08:45:22 +08: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-03 04:30:30 +08:00
|
|
|
LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?";
|
2014-10-14 08:45:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-06 10:20:17 +08:00
|
|
|
pid_t pgid = getpgid(pid);
|
|
|
|
if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
|
|
|
|
if (pgid == pid) {
|
|
|
|
pgids.emplace(pid);
|
|
|
|
} else {
|
|
|
|
pids.emplace(pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Erase all pids that will be killed when we kill the process groups.
|
|
|
|
for (auto it = pids.begin(); it != pids.end();) {
|
|
|
|
pid_t pgid = getpgid(pid);
|
|
|
|
if (pgids.count(pgid) == 1) {
|
|
|
|
it = pids.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill all process groups.
|
|
|
|
for (const auto pgid : pgids) {
|
|
|
|
LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
|
|
|
|
<< " as part of process cgroup " << initialPid;
|
|
|
|
|
|
|
|
if (kill(-pgid, signal) == -1) {
|
|
|
|
PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill remaining pids.
|
|
|
|
for (const auto pid : pids) {
|
2017-04-22 04:48:49 +08:00
|
|
|
LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
|
|
|
|
<< initialPid;
|
2017-06-06 10:20:17 +08:00
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
if (kill(pid, signal) == -1) {
|
|
|
|
PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
return ret >= 0 ? processes : ret;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
static int killProcessGroup(uid_t uid, int initialPid, int signal, int retries) {
|
2016-06-02 05:03:55 +08:00
|
|
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
2014-06-04 04:24:21 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
int retry = retries;
|
2016-06-02 05:03:55 +08:00
|
|
|
int processes;
|
2017-03-29 08:25:24 +08:00
|
|
|
while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) {
|
2017-04-22 04:48:49 +08:00
|
|
|
LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
|
2015-06-17 04:51:14 +08:00
|
|
|
if (retry > 0) {
|
2016-11-15 09:08:47 +08:00
|
|
|
std::this_thread::sleep_for(5ms);
|
2015-06-17 04:51:14 +08:00
|
|
|
--retry;
|
2014-06-04 04:24:21 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
if (processes < 0) {
|
|
|
|
PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
|
|
|
|
<< initialPid;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-02 05:03:55 +08:00
|
|
|
|
2017-04-22 04:48:49 +08:00
|
|
|
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
|
2016-08-03 04:30:30 +08:00
|
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
2017-04-22 04:48:49 +08:00
|
|
|
|
|
|
|
// We only calculate the number of 'processes' when killing the processes.
|
|
|
|
// In the retries == 0 case, we only kill the processes once and therefore
|
|
|
|
// will not have waited then recalculated how many processes are remaining
|
|
|
|
// after the first signals have been sent.
|
|
|
|
// Logging anything regarding the number of 'processes' here does not make sense.
|
2014-10-14 08:45:22 +08:00
|
|
|
|
2014-06-04 04:24:21 +08:00
|
|
|
if (processes == 0) {
|
2017-04-22 04:48:49 +08:00
|
|
|
if (retries > 0) {
|
|
|
|
LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
|
|
|
|
<< " in " << static_cast<int>(ms) << "ms";
|
|
|
|
}
|
2014-06-04 04:24:21 +08:00
|
|
|
return removeProcessGroup(uid, initialPid);
|
|
|
|
} else {
|
2017-04-22 04:48:49 +08:00
|
|
|
if (retries > 0) {
|
|
|
|
LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
|
|
|
|
<< " in " << static_cast<int>(ms) << "ms, " << processes
|
|
|
|
<< " processes remain";
|
|
|
|
}
|
2014-06-04 04:24:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 08:25:24 +08:00
|
|
|
int killProcessGroup(uid_t uid, int initialPid, int signal) {
|
2017-04-22 04:48:49 +08:00
|
|
|
return killProcessGroup(uid, initialPid, signal, 40 /*retries*/);
|
2017-03-29 08:25:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
|
2017-04-22 04:48:49 +08:00
|
|
|
return killProcessGroup(uid, initialPid, signal, 0 /*retries*/);
|
2017-03-29 08:25:24 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
|
2014-06-04 04:24:21 +08:00
|
|
|
{
|
2016-08-03 04:30:30 +08:00
|
|
|
if (mkdir(path, mode) == -1 && errno != EEXIST) {
|
|
|
|
return false;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
if (chown(path, uid, gid) == -1) {
|
|
|
|
int saved_errno = errno;
|
2014-06-04 04:24:21 +08:00
|
|
|
rmdir(path);
|
2016-08-03 04:30:30 +08:00
|
|
|
errno = saved_errno;
|
|
|
|
return false;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
return true;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int createProcessGroup(uid_t uid, int initialPid)
|
|
|
|
{
|
|
|
|
char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
|
|
|
|
|
|
|
|
convertUidToPath(path, sizeof(path), uid);
|
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
|
2017-04-22 04:48:49 +08:00
|
|
|
PLOG(ERROR) << "Failed to make and chown " << path;
|
2016-08-03 04:30:30 +08:00
|
|
|
return -errno;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
convertUidPidToPath(path, sizeof(path), uid, initialPid);
|
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
if (!mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM)) {
|
2017-04-22 04:48:49 +08:00
|
|
|
PLOG(ERROR) << "Failed to make and chown " << path;
|
2016-08-03 04:30:30 +08:00
|
|
|
return -errno;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
|
|
|
|
|
2016-08-03 04:30:30 +08:00
|
|
|
int ret = 0;
|
2017-07-17 10:38:11 +08:00
|
|
|
if (!WriteStringToFile(std::to_string(initialPid), path)) {
|
2014-06-04 04:24:21 +08:00
|
|
|
ret = -errno;
|
2017-07-17 10:38:11 +08:00
|
|
|
PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << path;
|
2014-06-04 04:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-17 10:38:11 +08:00
|
|
|
|
|
|
|
static bool setProcessGroupValue(uid_t uid, int pid, const char* fileName, int64_t value) {
|
|
|
|
char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
|
|
|
|
if (strcmp(getCgroupRootPath(), MEM_CGROUP_PATH)) {
|
|
|
|
PLOG(ERROR) << "Memcg is not mounted." << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
convertUidPidToPath(path, sizeof(path), uid, pid);
|
|
|
|
strlcat(path, fileName, sizeof(path));
|
|
|
|
|
|
|
|
if (!WriteStringToFile(std::to_string(value), path)) {
|
|
|
|
PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setProcessGroupSwappiness(uid_t uid, int pid, int swappiness) {
|
|
|
|
return setProcessGroupValue(uid, pid, "/memory.swappiness", swappiness);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setProcessGroupSoftLimit(uid_t uid, int pid, int64_t soft_limit_in_bytes) {
|
|
|
|
return setProcessGroupValue(uid, pid, "/memory.soft_limit_in_bytes", soft_limit_in_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setProcessGroupLimit(uid_t uid, int pid, int64_t limit_in_bytes) {
|
|
|
|
return setProcessGroupValue(uid, pid, "/memory.limit_in_bytes", limit_in_bytes);
|
|
|
|
}
|