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