Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Elliott Hughes | 0badbd6 | 2014-12-29 12:24:25 -0800 | [diff] [blame] | 22 | #include <errno.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Chih-Hung Hsieh | fcc8115 | 2014-11-20 17:05:15 -0800 | [diff] [blame] | 24 | #include <inttypes.h> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 25 | #include <signal.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 32 | |
| 33 | #include <chrono> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 34 | #include <memory> |
Elliott Hughes | 8d532e4 | 2016-06-06 21:19:55 -0700 | [diff] [blame] | 35 | #include <mutex> |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 36 | #include <set> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 37 | #include <string> |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 38 | #include <thread> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 39 | |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 40 | #include <android-base/file.h> |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
| 44 | #include <android-base/strings.h> |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 45 | #include <private/android_filesystem_config.h> |
| 46 | |
| 47 | #include <processgroup/processgroup.h> |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 48 | |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 49 | using android::base::GetBoolProperty; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 50 | using android::base::StartsWith; |
| 51 | using android::base::StringPrintf; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 52 | using android::base::WriteStringToFile; |
| 53 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 54 | using namespace std::chrono_literals; |
| 55 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 56 | #define MEM_CGROUP_PATH "/dev/memcg/apps" |
Martijn Coenen | 623b56a | 2016-02-08 11:42:25 +0100 | [diff] [blame] | 57 | #define MEM_CGROUP_TASKS "/dev/memcg/apps/tasks" |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 58 | #define ACCT_CGROUP_PATH "/acct" |
| 59 | |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 60 | #define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs" |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 61 | |
| 62 | std::once_flag init_path_flag; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 63 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 64 | static const std::string& GetCgroupRootPath() { |
| 65 | static std::string cgroup_root_path; |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 66 | std::call_once(init_path_flag, [&]() { |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 67 | // low-ram devices use per-app memcg by default, unlike high-end ones |
| 68 | bool low_ram_device = GetBoolProperty("ro.config.low_ram", false); |
| 69 | bool per_app_memcg = |
| 70 | GetBoolProperty("ro.config.per_app_memcg", low_ram_device); |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 71 | if (per_app_memcg) { |
| 72 | // Check if mem cgroup is mounted, only then check for |
| 73 | // write-access to avoid SELinux denials |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 74 | cgroup_root_path = |
Suren Baghdasaryan | bc131c3 | 2018-05-23 12:30:44 -0700 | [diff] [blame] | 75 | (access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ? |
| 76 | ACCT_CGROUP_PATH : MEM_CGROUP_PATH); |
| 77 | } else { |
| 78 | cgroup_root_path = ACCT_CGROUP_PATH; |
| 79 | } |
| 80 | }); |
Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 81 | return cgroup_root_path; |
| 82 | } |
| 83 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 84 | static std::string ConvertUidToPath(uid_t uid) { |
| 85 | return StringPrintf("%s/uid_%d", GetCgroupRootPath().c_str(), uid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 88 | static std::string ConvertUidPidToPath(uid_t uid, int pid) { |
| 89 | return StringPrintf("%s/uid_%d/pid_%d", GetCgroupRootPath().c_str(), uid, pid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 92 | static int RemoveProcessGroup(uid_t uid, int pid) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 93 | int ret; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 94 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 95 | auto uid_pid_path = ConvertUidPidToPath(uid, pid); |
| 96 | ret = rmdir(uid_pid_path.c_str()); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 97 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 98 | auto uid_path = ConvertUidToPath(uid); |
| 99 | rmdir(uid_path.c_str()); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 104 | static void RemoveUidProcessGroups(const std::string& uid_path) { |
| 105 | std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 106 | if (uid != NULL) { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 107 | dirent* dir; |
| 108 | while ((dir = readdir(uid.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 109 | if (dir->d_type != DT_DIR) { |
| 110 | continue; |
| 111 | } |
| 112 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 113 | if (!StartsWith(dir->d_name, "pid_")) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 114 | continue; |
| 115 | } |
| 116 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 117 | auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 118 | LOG(VERBOSE) << "Removing " << path; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 119 | if (rmdir(path.c_str()) == -1) PLOG(WARNING) << "Failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void removeAllProcessGroups() |
| 125 | { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 126 | LOG(VERBOSE) << "removeAllProcessGroups()"; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 127 | const auto& cgroup_root_path = GetCgroupRootPath(); |
| 128 | std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 129 | if (root == NULL) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 130 | PLOG(ERROR) << "Failed to open " << cgroup_root_path; |
Colin Cross | c15dd04 | 2014-08-20 14:11:13 -0700 | [diff] [blame] | 131 | } else { |
Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 132 | dirent* dir; |
| 133 | while ((dir = readdir(root.get())) != nullptr) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 134 | if (dir->d_type != DT_DIR) { |
| 135 | continue; |
| 136 | } |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 137 | |
| 138 | if (!StartsWith(dir->d_name, "uid_")) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 139 | continue; |
| 140 | } |
| 141 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 142 | auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name); |
| 143 | RemoveUidProcessGroups(path); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 144 | LOG(VERBOSE) << "Removing " << path; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 145 | if (rmdir(path.c_str()) == -1) PLOG(WARNING) << "Failed to remove " << path; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 146 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 150 | // Returns number of processes killed on success |
| 151 | // Returns 0 if there are no processes in the process cgroup left to kill |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 152 | // Returns -1 on error |
| 153 | static int DoKillProcessGroupOnce(uid_t uid, int initialPid, int signal) { |
| 154 | auto path = ConvertUidPidToPath(uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE; |
| 155 | std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose); |
| 156 | if (!fd) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 157 | PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 158 | return -1; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 161 | // We separate all of the pids in the cgroup into those pids that are also the leaders of |
| 162 | // process groups (stored in the pgids set) and those that are not (stored in the pids set). |
| 163 | std::set<pid_t> pgids; |
| 164 | pgids.emplace(initialPid); |
| 165 | std::set<pid_t> pids; |
| 166 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 167 | pid_t pid; |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 168 | int processes = 0; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 169 | while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 170 | processes++; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 171 | if (pid == 0) { |
| 172 | // Should never happen... but if it does, trying to kill this |
| 173 | // will boomerang right back and kill us! Let's not let that happen. |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 174 | LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?"; |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 175 | continue; |
| 176 | } |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 177 | pid_t pgid = getpgid(pid); |
| 178 | if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed"; |
| 179 | if (pgid == pid) { |
| 180 | pgids.emplace(pid); |
| 181 | } else { |
| 182 | pids.emplace(pid); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Erase all pids that will be killed when we kill the process groups. |
| 187 | for (auto it = pids.begin(); it != pids.end();) { |
| 188 | pid_t pgid = getpgid(pid); |
| 189 | if (pgids.count(pgid) == 1) { |
| 190 | it = pids.erase(it); |
| 191 | } else { |
| 192 | ++it; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Kill all process groups. |
| 197 | for (const auto pgid : pgids) { |
| 198 | LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid |
| 199 | << " as part of process cgroup " << initialPid; |
| 200 | |
| 201 | if (kill(-pgid, signal) == -1) { |
| 202 | PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed"; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Kill remaining pids. |
| 207 | for (const auto pid : pids) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 208 | LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup " |
| 209 | << initialPid; |
Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 210 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 211 | if (kill(pid, signal) == -1) { |
| 212 | PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 216 | return feof(fd.get()) ? processes : -1; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 219 | static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries) { |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 220 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 221 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 222 | int retry = retries; |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 223 | int processes; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 224 | while ((processes = DoKillProcessGroupOnce(uid, initialPid, signal)) > 0) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 225 | LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid; |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 226 | if (retry > 0) { |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 227 | std::this_thread::sleep_for(5ms); |
Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 228 | --retry; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 229 | } else { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 234 | if (processes < 0) { |
| 235 | PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid " |
| 236 | << initialPid; |
| 237 | return -1; |
| 238 | } |
Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 239 | |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 240 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 241 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 242 | |
| 243 | // We only calculate the number of 'processes' when killing the processes. |
| 244 | // In the retries == 0 case, we only kill the processes once and therefore |
| 245 | // will not have waited then recalculated how many processes are remaining |
| 246 | // after the first signals have been sent. |
| 247 | // Logging anything regarding the number of 'processes' here does not make sense. |
Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 248 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 249 | if (processes == 0) { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 250 | if (retries > 0) { |
| 251 | LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid |
| 252 | << " in " << static_cast<int>(ms) << "ms"; |
| 253 | } |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 254 | return RemoveProcessGroup(uid, initialPid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 255 | } else { |
Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 256 | if (retries > 0) { |
| 257 | LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid |
| 258 | << " in " << static_cast<int>(ms) << "ms, " << processes |
| 259 | << " processes remain"; |
| 260 | } |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 261 | return -1; |
| 262 | } |
| 263 | } |
| 264 | |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 265 | int killProcessGroup(uid_t uid, int initialPid, int signal) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 266 | return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/); |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | int killProcessGroupOnce(uid_t uid, int initialPid, int signal) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 270 | return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/); |
Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 273 | static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { |
| 274 | if (mkdir(path.c_str(), mode) == -1 && errno != EEXIST) { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 275 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 278 | if (chown(path.c_str(), uid, gid) == -1) { |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 279 | int saved_errno = errno; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 280 | rmdir(path.c_str()); |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 281 | errno = saved_errno; |
| 282 | return false; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 285 | return true; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | int createProcessGroup(uid_t uid, int initialPid) |
| 289 | { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 290 | auto uid_path = ConvertUidToPath(uid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 291 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 292 | if (!MkdirAndChown(uid_path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
| 293 | PLOG(ERROR) << "Failed to make and chown " << uid_path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 294 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 297 | auto uid_pid_path = ConvertUidPidToPath(uid, initialPid); |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 298 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 299 | if (!MkdirAndChown(uid_pid_path, 0750, AID_SYSTEM, AID_SYSTEM)) { |
| 300 | PLOG(ERROR) << "Failed to make and chown " << uid_pid_path; |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 301 | return -errno; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 304 | auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 305 | |
Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 306 | int ret = 0; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 307 | if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) { |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 308 | ret = -errno; |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 309 | PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file; |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 312 | return ret; |
| 313 | } |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 314 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 315 | static bool SetProcessGroupValue(uid_t uid, int pid, const std::string& file_name, int64_t value) { |
| 316 | if (GetCgroupRootPath() != MEM_CGROUP_PATH) { |
| 317 | PLOG(ERROR) << "Memcg is not mounted."; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 321 | auto path = ConvertUidPidToPath(uid, pid) + file_name; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 322 | |
| 323 | if (!WriteStringToFile(std::to_string(value), path)) { |
| 324 | PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
| 325 | return false; |
| 326 | } |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | bool setProcessGroupSwappiness(uid_t uid, int pid, int swappiness) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 331 | return SetProcessGroupValue(uid, pid, "/memory.swappiness", swappiness); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | bool setProcessGroupSoftLimit(uid_t uid, int pid, int64_t soft_limit_in_bytes) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 335 | return SetProcessGroupValue(uid, pid, "/memory.soft_limit_in_bytes", soft_limit_in_bytes); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | bool setProcessGroupLimit(uid_t uid, int pid, int64_t limit_in_bytes) { |
Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 339 | return SetProcessGroupValue(uid, pid, "/memory.limit_in_bytes", limit_in_bytes); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 340 | } |