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 CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
|
2019-03-23 01:01:08 +01:00
|
|
|
auto controller = CgroupMap::GetInstance().FindController(cgroup_name);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
if (!controller.HasValue()) {
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
2019-03-23 01:01:08 +01:00
|
|
|
*path = controller.path();
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2019-05-09 02:59:55 +02:00
|
|
|
static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable();
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
return memcg_supported;
|
2016-01-21 01:39:16 +01:00
|
|
|
}
|
|
|
|
|
2019-06-18 23:53:53 +02:00
|
|
|
void DropTaskProfilesResourceCaching() {
|
|
|
|
TaskProfiles::GetInstance().DropResourceCaching();
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:28:00 +01:00
|
|
|
bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
|
|
|
|
return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles);
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 23:40:41 +01:00
|
|
|
bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
|
2019-09-16 13:07:17 +02:00
|
|
|
return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid, unsigned int retries) {
|
|
|
|
int ret = 0;
|
2021-02-04 23:09:55 +01:00
|
|
|
auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid);
|
|
|
|
auto uid_path = ConvertUidToPath(cgroup, uid);
|
2021-02-11 01:35:44 +01:00
|
|
|
|
|
|
|
if (retries == 0) {
|
|
|
|
retries = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (retries--) {
|
|
|
|
ret = rmdir(uid_pid_path.c_str());
|
|
|
|
if (!ret || errno != EBUSY) break;
|
|
|
|
std::this_thread::sleep_for(5ms);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
if (CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &path)) {
|
2018-12-21 20:41:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
/**
|
|
|
|
* Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by
|
|
|
|
* the user root. Ownership for the newly created cgroup and all of its files must thus be
|
|
|
|
* transferred for the user/group passed as uid/gid before system_server can properly access them.
|
|
|
|
*/
|
2018-12-21 20:41:50 +01:00
|
|
|
static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
|
2021-07-07 19:59:59 +02:00
|
|
|
if (mkdir(path.c_str(), mode) == -1) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
// Directory already exists and permissions have been set at the time it was created
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
|
|
|
|
|
|
|
|
if (dir == NULL) {
|
|
|
|
PLOG(ERROR) << "opendir failed for " << path;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent* dir_entry;
|
|
|
|
while ((dir_entry = readdir(dir.get()))) {
|
|
|
|
if (!strcmp("..", dir_entry->d_name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_path = path + "/" + dir_entry->d_name;
|
|
|
|
|
|
|
|
if (lchown(file_path.c_str(), uid, gid) < 0) {
|
|
|
|
PLOG(ERROR) << "lchown failed for " << file_path;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
|
|
|
|
PLOG(ERROR) << "fchmodat failed for " << file_path;
|
|
|
|
goto err;
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-02-11 01:35:44 +01:00
|
|
|
err:
|
|
|
|
int saved_errno = errno;
|
|
|
|
rmdir(path.c_str());
|
|
|
|
errno = saved_errno;
|
|
|
|
|
|
|
|
return false;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-19 23:19:40 +01:00
|
|
|
static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries,
|
|
|
|
int* max_processes) {
|
2021-02-11 01:35:44 +01:00
|
|
|
std::string hierarchy_root_path;
|
|
|
|
CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
|
|
|
|
const char* cgroup = hierarchy_root_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
|
|
|
|
2019-11-19 23:19:40 +01:00
|
|
|
if (max_processes != nullptr) {
|
|
|
|
*max_processes = 0;
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-11-19 23:19:40 +01:00
|
|
|
if (max_processes != nullptr && processes > *max_processes) {
|
|
|
|
*max_processes = processes;
|
|
|
|
}
|
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";
|
|
|
|
}
|
2021-02-11 01:35:44 +01:00
|
|
|
|
|
|
|
int err = RemoveProcessGroup(cgroup, uid, initialPid, retries);
|
|
|
|
|
|
|
|
if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
|
|
|
|
std::string memory_path;
|
|
|
|
CgroupGetControllerPath("memory", &memory_path);
|
|
|
|
memory_path += "/apps";
|
|
|
|
if (RemoveProcessGroup(memory_path.c_str(), uid, initialPid, retries)) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 23:19:40 +01:00
|
|
|
int killProcessGroup(uid_t uid, int initialPid, int signal, int* max_processes) {
|
|
|
|
return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/, max_processes);
|
2017-03-29 02:25:24 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 23:19:40 +01:00
|
|
|
int killProcessGroupOnce(uid_t uid, int initialPid, int signal, int* max_processes) {
|
|
|
|
return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/, max_processes);
|
2017-03-29 02:25:24 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 22:42:39 +02:00
|
|
|
static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup,
|
|
|
|
bool activate_controllers) {
|
2018-12-21 20:41:50 +01:00
|
|
|
auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
|
2014-06-03 22:24:21 +02:00
|
|
|
|
2021-02-11 23:44:53 +01:00
|
|
|
struct stat cgroup_stat;
|
|
|
|
mode_t cgroup_mode = 0750;
|
|
|
|
gid_t cgroup_uid = AID_SYSTEM;
|
|
|
|
uid_t cgroup_gid = AID_SYSTEM;
|
2021-07-30 22:42:39 +02:00
|
|
|
int ret = 0;
|
2021-02-11 23:44:53 +01:00
|
|
|
|
|
|
|
if (stat(cgroup.c_str(), &cgroup_stat) == 1) {
|
|
|
|
PLOG(ERROR) << "Failed to get stats for " << cgroup;
|
|
|
|
} else {
|
|
|
|
cgroup_mode = cgroup_stat.st_mode;
|
|
|
|
cgroup_uid = cgroup_stat.st_uid;
|
|
|
|
cgroup_gid = cgroup_stat.st_gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
|
2018-02-23 22:04:40 +01:00
|
|
|
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
|
|
|
}
|
2021-07-30 22:42:39 +02:00
|
|
|
if (activate_controllers) {
|
|
|
|
ret = CgroupMap::GetInstance().ActivateControllers(uid_path);
|
|
|
|
if (ret) {
|
|
|
|
LOG(ERROR) << "Failed to activate controllers in " << uid_path;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2021-02-11 23:44:53 +01:00
|
|
|
if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
|
2018-02-23 22:04:40 +01:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2021-02-11 01:35:44 +01:00
|
|
|
int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
|
|
|
|
std::string cgroup;
|
|
|
|
|
|
|
|
if (memControl && !UsePerAppMemcg()) {
|
|
|
|
PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support";
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
|
|
|
|
CgroupGetControllerPath("memory", &cgroup);
|
|
|
|
cgroup += "/apps";
|
2021-07-30 22:42:39 +02:00
|
|
|
int ret = createProcessGroupInternal(uid, initialPid, cgroup, false);
|
2021-02-11 01:35:44 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup);
|
2021-07-30 22:42:39 +02:00
|
|
|
return createProcessGroupInternal(uid, initialPid, cgroup, true);
|
2021-02-11 01:35:44 +01: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
|
|
|
}
|
2021-02-25 01:30:50 +01:00
|
|
|
|
|
|
|
bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
|
|
|
|
return CgroupGetAttributePathForTask(attr_name, tid, path);
|
|
|
|
}
|