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>
|
2018-02-23 22:04:40 +01:00
|
|
|
#include <signal.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2017-06-06 04:20:17 +02:00
|
|
|
#include <unistd.h>
|
2016-06-01 23:03:55 +02:00
|
|
|
|
|
|
|
#include <chrono>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <map>
|
2016-02-18 23:52:46 +01:00
|
|
|
#include <memory>
|
2016-06-07 06:19:55 +02:00
|
|
|
#include <mutex>
|
2017-06-06 04:20:17 +02:00
|
|
|
#include <set>
|
2018-02-23 22:04:40 +01:00
|
|
|
#include <string>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <thread>
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2017-07-17 04:38:11 +02:00
|
|
|
#include <android-base/file.h>
|
2016-08-02 22:30:30 +02:00
|
|
|
#include <android-base/logging.h>
|
2018-05-23 21:30:44 +02:00
|
|
|
#include <android-base/properties.h>
|
2018-02-23 22:04:40 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2019-01-25 06:29:55 +01:00
|
|
|
#include <cutils/android_filesystem_config.h>
|
2014-06-03 22:24:21 +02:00
|
|
|
#include <processgroup/processgroup.h>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <task_profiles.h>
|
2016-01-21 01:39:16 +01:00
|
|
|
|
2018-05-23 21:30:44 +02:00
|
|
|
using android::base::GetBoolProperty;
|
2018-02-23 22:04:40 +01:00
|
|
|
using android::base::StartsWith;
|
|
|
|
using android::base::StringPrintf;
|
2017-07-17 04:38:11 +02:00
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2016-01-21 01:39:16 +01:00
|
|
|
#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool CgroupSetupCgroups() {
|
|
|
|
return CgroupMap::SetupCgroups();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
|
|
|
|
const CgroupController* controller = CgroupMap::GetInstance().FindController(cgroup_name);
|
|
|
|
|
|
|
|
if (controller == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
*path = controller->path();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
|
|
|
|
const TaskProfiles& tp = TaskProfiles::GetInstance();
|
|
|
|
const ProfileAttribute* attr = tp.GetAttribute(attr_name);
|
|
|
|
|
|
|
|
if (attr == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
*path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
|
|
|
|
const TaskProfiles& tp = TaskProfiles::GetInstance();
|
|
|
|
const ProfileAttribute* attr = tp.GetAttribute(attr_name);
|
|
|
|
|
|
|
|
if (attr == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!attr->GetPathForTask(tid, path)) {
|
|
|
|
PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UsePerAppMemcg() {
|
|
|
|
bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
|
|
|
|
return GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
|
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
static bool isMemoryCgroupSupported() {
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string cgroup_name;
|
|
|
|
static bool memcg_supported = (CgroupMap::GetInstance().FindController("memory") != nullptr);
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
return memcg_supported;
|
2016-01-21 01:39:16 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
|
|
|
|
const TaskProfiles& tp = TaskProfiles::GetInstance();
|
|
|
|
|
|
|
|
for (const auto& name : profiles) {
|
|
|
|
const TaskProfile* profile = tp.GetProfile(name);
|
|
|
|
if (profile != nullptr) {
|
|
|
|
if (!profile->ExecuteForProcess(uid, pid)) {
|
|
|
|
PLOG(WARNING) << "Failed to apply " << name << " process profile";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PLOG(WARNING) << "Failed to find " << name << "process profile";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles) {
|
|
|
|
const TaskProfiles& tp = TaskProfiles::GetInstance();
|
|
|
|
|
|
|
|
for (const auto& name : profiles) {
|
|
|
|
const TaskProfile* profile = tp.GetProfile(name);
|
|
|
|
if (profile != nullptr) {
|
|
|
|
if (!profile->ExecuteForTask(tid)) {
|
|
|
|
PLOG(WARNING) << "Failed to apply " << name << " task profile";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PLOG(WARNING) << "Failed to find " << name << "task profile";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
|
|
|
|
return StringPrintf("%s/uid_%d", cgroup, uid);
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) {
|
|
|
|
return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid);
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid) {
|
2014-06-03 22:24:21 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid);
|
2018-02-23 22:04:40 +01:00
|
|
|
ret = rmdir(uid_pid_path.c_str());
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
auto uid_path = ConvertUidToPath(cgroup, uid);
|
2018-02-23 22:04:40 +01:00
|
|
|
rmdir(uid_path.c_str());
|
2014-06-03 22:24:21 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:25:16 +01:00
|
|
|
static bool RemoveUidProcessGroups(const std::string& uid_path) {
|
2018-02-23 22:04:40 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir);
|
2019-02-21 20:25:16 +01:00
|
|
|
bool empty = true;
|
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
|
|
|
if (dir->d_type != DT_DIR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
if (!StartsWith(dir->d_name, "pid_")) {
|
2014-06-03 22:24:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name);
|
2017-04-21 22:48:49 +02:00
|
|
|
LOG(VERBOSE) << "Removing " << path;
|
2019-02-21 20:25:16 +01:00
|
|
|
if (rmdir(path.c_str()) == -1) {
|
|
|
|
if (errno != EBUSY) {
|
|
|
|
PLOG(WARNING) << "Failed to remove " << path;
|
|
|
|
}
|
|
|
|
empty = false;
|
|
|
|
}
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 20:25:16 +01:00
|
|
|
return empty;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
void removeAllProcessGroups() {
|
2016-08-02 22:30:30 +02:00
|
|
|
LOG(VERBOSE) << "removeAllProcessGroups()";
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
std::vector<std::string> cgroups;
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
if (CgroupGetControllerPath("cpuacct", &path)) {
|
|
|
|
cgroups.push_back(path);
|
|
|
|
}
|
|
|
|
if (CgroupGetControllerPath("memory", &path)) {
|
2019-02-19 23:09:00 +01:00
|
|
|
cgroups.push_back(path + "/apps");
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::string cgroup_root_path : cgroups) {
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir);
|
2018-10-30 23:49:33 +01:00
|
|
|
if (root == NULL) {
|
|
|
|
PLOG(ERROR) << "Failed to open " << cgroup_root_path;
|
|
|
|
} else {
|
|
|
|
dirent* dir;
|
|
|
|
while ((dir = readdir(root.get())) != nullptr) {
|
|
|
|
if (dir->d_type != DT_DIR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!StartsWith(dir->d_name, "uid_")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name);
|
2019-02-21 20:25:16 +01:00
|
|
|
if (!RemoveUidProcessGroups(path)) {
|
|
|
|
LOG(VERBOSE) << "Skip removing " << path;
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-30 23:49:33 +01:00
|
|
|
LOG(VERBOSE) << "Removing " << path;
|
2019-02-21 20:25:16 +01:00
|
|
|
if (rmdir(path.c_str()) == -1 && errno != EBUSY) {
|
|
|
|
PLOG(WARNING) << "Failed to remove " << path;
|
|
|
|
}
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
|
|
|
|
if (mkdir(path.c_str(), mode) == -1 && errno != EEXIST) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chown(path.c_str(), uid, gid) == -1) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
rmdir(path.c_str());
|
|
|
|
errno = saved_errno;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:48:49 +02:00
|
|
|
// Returns number of processes killed on success
|
|
|
|
// Returns 0 if there are no processes in the process cgroup left to kill
|
2018-02-23 22:04:40 +01:00
|
|
|
// Returns -1 on error
|
2018-10-30 23:49:33 +01:00
|
|
|
static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) {
|
|
|
|
auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE;
|
2018-02-23 22:04:40 +01:00
|
|
|
std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose);
|
|
|
|
if (!fd) {
|
2019-02-21 20:25:16 +01:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
// This happens when process is already dead
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-21 22:48:49 +02:00
|
|
|
PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid;
|
2018-02-23 22:04:40 +01:00
|
|
|
return -1;
|
2017-04-21 22:48:49 +02:00
|
|
|
}
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2017-06-06 04:20:17 +02: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-21 22:48:49 +02:00
|
|
|
pid_t pid;
|
|
|
|
int processes = 0;
|
2018-02-23 22:04:40 +01:00
|
|
|
while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) {
|
2014-06-03 22:24:21 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-06-06 04:20:17 +02: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();) {
|
2018-09-13 08:57:07 +02:00
|
|
|
pid_t pgid = getpgid(*it);
|
2017-06-06 04:20:17 +02:00
|
|
|
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;
|
|
|
|
|
2019-02-21 20:25:16 +01:00
|
|
|
if (kill(-pgid, signal) == -1 && errno != ESRCH) {
|
2017-06-06 04:20:17 +02:00
|
|
|
PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill remaining pids.
|
|
|
|
for (const auto pid : pids) {
|
2017-04-21 22:48:49 +02:00
|
|
|
LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
|
|
|
|
<< initialPid;
|
2017-06-06 04:20:17 +02:00
|
|
|
|
2019-02-21 20:25:16 +01:00
|
|
|
if (kill(pid, signal) == -1 && errno != ESRCH) {
|
2016-08-02 22:30:30 +02:00
|
|
|
PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
return feof(fd.get()) ? processes : -1;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries) {
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string cpuacct_path;
|
|
|
|
std::string memory_path;
|
|
|
|
|
|
|
|
CgroupGetControllerPath("cpuacct", &cpuacct_path);
|
|
|
|
CgroupGetControllerPath("memory", &memory_path);
|
2019-02-19 23:09:00 +01:00
|
|
|
memory_path += "/apps";
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
const char* cgroup =
|
2018-12-21 20:41:50 +01:00
|
|
|
(!access(ConvertUidPidToPath(cpuacct_path.c_str(), uid, initialPid).c_str(), F_OK))
|
|
|
|
? cpuacct_path.c_str()
|
|
|
|
: memory_path.c_str();
|
2018-10-30 23:49:33 +01:00
|
|
|
|
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
|
|
|
|
2017-04-21 22:48:49 +02:00
|
|
|
int retry = retries;
|
2016-06-01 23:03:55 +02:00
|
|
|
int processes;
|
2018-10-30 23:49:33 +01:00
|
|
|
while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) {
|
2017-04-21 22:48:49 +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 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:48:49 +02:00
|
|
|
if (processes < 0) {
|
|
|
|
PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
|
|
|
|
<< initialPid;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 23:03:55 +02:00
|
|
|
|
2017-04-21 22:48:49 +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();
|
2017-04-21 22:48:49 +02: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 02:45:22 +02:00
|
|
|
|
2014-06-03 22:24:21 +02:00
|
|
|
if (processes == 0) {
|
2017-04-21 22:48:49 +02:00
|
|
|
if (retries > 0) {
|
|
|
|
LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
|
|
|
|
<< " in " << static_cast<int>(ms) << "ms";
|
|
|
|
}
|
2018-10-30 23:49:33 +01:00
|
|
|
return RemoveProcessGroup(cgroup, uid, initialPid);
|
2014-06-03 22:24:21 +02:00
|
|
|
} else {
|
2017-04-21 22:48:49 +02: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-03 22:24:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 02:25:24 +02:00
|
|
|
int killProcessGroup(uid_t uid, int initialPid, int signal) {
|
2018-02-23 22:04:40 +01:00
|
|
|
return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/);
|
2017-03-29 02:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
|
2018-02-23 22:04:40 +01:00
|
|
|
return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/);
|
2017-03-29 02:25:24 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
|
|
|
|
std::string cgroup;
|
|
|
|
if (isMemoryCgroupSupported() && (memControl || UsePerAppMemcg())) {
|
|
|
|
CgroupGetControllerPath("memory", &cgroup);
|
2019-02-19 23:09:00 +01:00
|
|
|
cgroup += "/apps";
|
2018-10-30 23:49:33 +01:00
|
|
|
} else {
|
2018-12-21 20:41:50 +01:00
|
|
|
CgroupGetControllerPath("cpuacct", &cgroup);
|
2018-10-30 23:49:33 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
if (!MkdirAndChown(uid_path, 0750, AID_SYSTEM, AID_SYSTEM)) {
|
|
|
|
PLOG(ERROR) << "Failed to make and chown " << uid_path;
|
2016-08-02 22:30:30 +02:00
|
|
|
return -errno;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid);
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
if (!MkdirAndChown(uid_pid_path, 0750, AID_SYSTEM, AID_SYSTEM)) {
|
|
|
|
PLOG(ERROR) << "Failed to make and chown " << uid_pid_path;
|
2016-08-02 22:30:30 +02:00
|
|
|
return -errno;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-02-23 22:04:40 +01:00
|
|
|
auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE;
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2016-08-02 22:30:30 +02:00
|
|
|
int ret = 0;
|
2018-02-23 22:04:40 +01:00
|
|
|
if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) {
|
2014-06-03 22:24:21 +02:00
|
|
|
ret = -errno;
|
2018-02-23 22:04:40 +01:00
|
|
|
PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file;
|
2014-06-03 22:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-17 04:38:11 +02:00
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) {
|
2018-10-30 23:49:33 +01:00
|
|
|
if (!isMemoryCgroupSupported()) {
|
2018-02-23 22:04:40 +01:00
|
|
|
PLOG(ERROR) << "Memcg is not mounted.";
|
2017-07-17 04:38:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string path;
|
|
|
|
if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) {
|
|
|
|
PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'";
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-17 04:38:11 +02:00
|
|
|
|
|
|
|
if (!WriteStringToFile(std::to_string(value), path)) {
|
|
|
|
PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) {
|
|
|
|
return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) {
|
|
|
|
return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) {
|
|
|
|
return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|