| 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> | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> | 
 | 29 | #include <sys/types.h> | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 30 | #include <unistd.h> | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 31 |  | 
 | 32 | #include <chrono> | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 33 | #include <map> | 
| 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 | #include <processgroup/processgroup.h> | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 47 | #include <task_profiles.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 PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs" | 
| Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 57 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 58 | bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 59 |     auto controller = CgroupMap::GetInstance().FindController(cgroup_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 60 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 61 |     if (!controller.HasValue()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 62 |         return false; | 
 | 63 |     } | 
 | 64 |  | 
 | 65 |     if (path) { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 66 |         *path = controller.path(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 67 |     } | 
 | 68 |  | 
 | 69 |     return true; | 
 | 70 | } | 
 | 71 |  | 
 | 72 | bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) { | 
 | 73 |     const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
 | 74 |     const ProfileAttribute* attr = tp.GetAttribute(attr_name); | 
 | 75 |  | 
 | 76 |     if (attr == nullptr) { | 
 | 77 |         return false; | 
 | 78 |     } | 
 | 79 |  | 
 | 80 |     if (path) { | 
 | 81 |         *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str()); | 
 | 82 |     } | 
 | 83 |  | 
 | 84 |     return true; | 
 | 85 | } | 
 | 86 |  | 
 | 87 | bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { | 
 | 88 |     const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
 | 89 |     const ProfileAttribute* attr = tp.GetAttribute(attr_name); | 
 | 90 |  | 
 | 91 |     if (attr == nullptr) { | 
 | 92 |         return false; | 
 | 93 |     } | 
 | 94 |  | 
 | 95 |     if (!attr->GetPathForTask(tid, path)) { | 
 | 96 |         PLOG(ERROR) << "Failed to find cgroup for tid " << tid; | 
 | 97 |         return false; | 
 | 98 |     } | 
 | 99 |  | 
 | 100 |     return true; | 
 | 101 | } | 
 | 102 |  | 
 | 103 | bool UsePerAppMemcg() { | 
 | 104 |     bool low_ram_device = GetBoolProperty("ro.config.low_ram", false); | 
 | 105 |     return GetBoolProperty("ro.config.per_app_memcg", low_ram_device); | 
 | 106 | } | 
 | 107 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 108 | static bool isMemoryCgroupSupported() { | 
| Suren Baghdasaryan | fa7a05f | 2019-05-08 17:59:55 -0700 | [diff] [blame] | 109 |     static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 110 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 111 |     return memcg_supported; | 
| Martijn Coenen | b82bab6 | 2016-01-20 16:39:16 -0800 | [diff] [blame] | 112 | } | 
 | 113 |  | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 114 | void DropTaskProfilesResourceCaching() { | 
 | 115 |     TaskProfiles::GetInstance().DropResourceCaching(); | 
 | 116 | } | 
 | 117 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 118 | bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles, | 
 | 119 |                         bool use_fd_cache) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 120 |     const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
 | 121 |  | 
 | 122 |     for (const auto& name : profiles) { | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 123 |         TaskProfile* profile = tp.GetProfile(name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 124 |         if (profile != nullptr) { | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 125 |             if (use_fd_cache) { | 
 | 126 |                 profile->EnableResourceCaching(); | 
 | 127 |             } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 128 |             if (!profile->ExecuteForProcess(uid, pid)) { | 
 | 129 |                 PLOG(WARNING) << "Failed to apply " << name << " process profile"; | 
 | 130 |             } | 
 | 131 |         } else { | 
 | 132 |             PLOG(WARNING) << "Failed to find " << name << "process profile"; | 
 | 133 |         } | 
 | 134 |     } | 
 | 135 |  | 
 | 136 |     return true; | 
 | 137 | } | 
 | 138 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 139 | bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 140 |     const TaskProfiles& tp = TaskProfiles::GetInstance(); | 
 | 141 |  | 
 | 142 |     for (const auto& name : profiles) { | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 143 |         TaskProfile* profile = tp.GetProfile(name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 144 |         if (profile != nullptr) { | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 145 |             if (use_fd_cache) { | 
 | 146 |                 profile->EnableResourceCaching(); | 
 | 147 |             } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 148 |             if (!profile->ExecuteForTask(tid)) { | 
 | 149 |                 PLOG(WARNING) << "Failed to apply " << name << " task profile"; | 
 | 150 |             } | 
 | 151 |         } else { | 
 | 152 |             PLOG(WARNING) << "Failed to find " << name << "task profile"; | 
 | 153 |         } | 
 | 154 |     } | 
 | 155 |  | 
 | 156 |     return true; | 
 | 157 | } | 
 | 158 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 159 | static std::string ConvertUidToPath(const char* cgroup, uid_t uid) { | 
 | 160 |     return StringPrintf("%s/uid_%d", cgroup, uid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 161 | } | 
 | 162 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 163 | static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) { | 
 | 164 |     return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 165 | } | 
 | 166 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 167 | static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 168 |     int ret; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 169 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 170 |     auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid); | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 171 |     ret = rmdir(uid_pid_path.c_str()); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 172 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 173 |     auto uid_path = ConvertUidToPath(cgroup, uid); | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 174 |     rmdir(uid_path.c_str()); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 175 |  | 
 | 176 |     return ret; | 
 | 177 | } | 
 | 178 |  | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 179 | static bool RemoveUidProcessGroups(const std::string& uid_path) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 180 |     std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir); | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 181 |     bool empty = true; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 182 |     if (uid != NULL) { | 
| Elliott Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 183 |         dirent* dir; | 
 | 184 |         while ((dir = readdir(uid.get())) != nullptr) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 185 |             if (dir->d_type != DT_DIR) { | 
 | 186 |                 continue; | 
 | 187 |             } | 
 | 188 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 189 |             if (!StartsWith(dir->d_name, "pid_")) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 190 |                 continue; | 
 | 191 |             } | 
 | 192 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 193 |             auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name); | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 194 |             LOG(VERBOSE) << "Removing " << path; | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 195 |             if (rmdir(path.c_str()) == -1) { | 
 | 196 |                 if (errno != EBUSY) { | 
 | 197 |                     PLOG(WARNING) << "Failed to remove " << path; | 
 | 198 |                 } | 
 | 199 |                 empty = false; | 
 | 200 |             } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 201 |         } | 
 | 202 |     } | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 203 |     return empty; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 204 | } | 
 | 205 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 206 | void removeAllProcessGroups() { | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 207 |     LOG(VERBOSE) << "removeAllProcessGroups()"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 208 |  | 
 | 209 |     std::vector<std::string> cgroups; | 
 | 210 |     std::string path; | 
 | 211 |  | 
 | 212 |     if (CgroupGetControllerPath("cpuacct", &path)) { | 
 | 213 |         cgroups.push_back(path); | 
 | 214 |     } | 
 | 215 |     if (CgroupGetControllerPath("memory", &path)) { | 
| Vic Yang | ab8d6ab | 2019-02-19 14:09:00 -0800 | [diff] [blame] | 216 |         cgroups.push_back(path + "/apps"); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 217 |     } | 
 | 218 |  | 
 | 219 |     for (std::string cgroup_root_path : cgroups) { | 
 | 220 |         std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 221 |         if (root == NULL) { | 
 | 222 |             PLOG(ERROR) << "Failed to open " << cgroup_root_path; | 
 | 223 |         } else { | 
 | 224 |             dirent* dir; | 
 | 225 |             while ((dir = readdir(root.get())) != nullptr) { | 
 | 226 |                 if (dir->d_type != DT_DIR) { | 
 | 227 |                     continue; | 
 | 228 |                 } | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 229 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 230 |                 if (!StartsWith(dir->d_name, "uid_")) { | 
 | 231 |                     continue; | 
 | 232 |                 } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 233 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 234 |                 auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name); | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 235 |                 if (!RemoveUidProcessGroups(path)) { | 
 | 236 |                     LOG(VERBOSE) << "Skip removing " << path; | 
 | 237 |                     continue; | 
 | 238 |                 } | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 239 |                 LOG(VERBOSE) << "Removing " << path; | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 240 |                 if (rmdir(path.c_str()) == -1 && errno != EBUSY) { | 
 | 241 |                     PLOG(WARNING) << "Failed to remove " << path; | 
 | 242 |                 } | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 243 |             } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 244 |         } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 245 |     } | 
 | 246 | } | 
 | 247 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 248 | static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { | 
 | 249 |     if (mkdir(path.c_str(), mode) == -1 && errno != EEXIST) { | 
 | 250 |         return false; | 
 | 251 |     } | 
 | 252 |  | 
 | 253 |     if (chown(path.c_str(), uid, gid) == -1) { | 
 | 254 |         int saved_errno = errno; | 
 | 255 |         rmdir(path.c_str()); | 
 | 256 |         errno = saved_errno; | 
 | 257 |         return false; | 
 | 258 |     } | 
 | 259 |  | 
 | 260 |     return true; | 
 | 261 | } | 
 | 262 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 263 | // Returns number of processes killed on success | 
 | 264 | // 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] | 265 | // Returns -1 on error | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 266 | static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) { | 
 | 267 |     auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 268 |     std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose); | 
 | 269 |     if (!fd) { | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 270 |         if (errno == ENOENT) { | 
 | 271 |             // This happens when process is already dead | 
 | 272 |             return 0; | 
 | 273 |         } | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 274 |         PLOG(WARNING) << "Failed to open process cgroup uid " << uid << " pid " << initialPid; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 275 |         return -1; | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 276 |     } | 
 | 277 |  | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 278 |     // We separate all of the pids in the cgroup into those pids that are also the leaders of | 
 | 279 |     // process groups (stored in the pgids set) and those that are not (stored in the pids set). | 
 | 280 |     std::set<pid_t> pgids; | 
 | 281 |     pgids.emplace(initialPid); | 
 | 282 |     std::set<pid_t> pids; | 
 | 283 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 284 |     pid_t pid; | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 285 |     int processes = 0; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 286 |     while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 287 |         processes++; | 
| Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 288 |         if (pid == 0) { | 
 | 289 |             // Should never happen...  but if it does, trying to kill this | 
 | 290 |             // 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] | 291 |             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] | 292 |             continue; | 
 | 293 |         } | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 294 |         pid_t pgid = getpgid(pid); | 
 | 295 |         if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed"; | 
 | 296 |         if (pgid == pid) { | 
 | 297 |             pgids.emplace(pid); | 
 | 298 |         } else { | 
 | 299 |             pids.emplace(pid); | 
 | 300 |         } | 
 | 301 |     } | 
 | 302 |  | 
 | 303 |     // Erase all pids that will be killed when we kill the process groups. | 
 | 304 |     for (auto it = pids.begin(); it != pids.end();) { | 
| fengshaobo | 950f6f3 | 2018-09-13 14:57:07 +0800 | [diff] [blame] | 305 |         pid_t pgid = getpgid(*it); | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 306 |         if (pgids.count(pgid) == 1) { | 
 | 307 |             it = pids.erase(it); | 
 | 308 |         } else { | 
 | 309 |             ++it; | 
 | 310 |         } | 
 | 311 |     } | 
 | 312 |  | 
 | 313 |     // Kill all process groups. | 
 | 314 |     for (const auto pgid : pgids) { | 
 | 315 |         LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid | 
 | 316 |                      << " as part of process cgroup " << initialPid; | 
 | 317 |  | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 318 |         if (kill(-pgid, signal) == -1 && errno != ESRCH) { | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 319 |             PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed"; | 
 | 320 |         } | 
 | 321 |     } | 
 | 322 |  | 
 | 323 |     // Kill remaining pids. | 
 | 324 |     for (const auto pid : pids) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 325 |         LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup " | 
 | 326 |                      << initialPid; | 
| Tom Cherry | 70a5ed4 | 2017-06-05 19:20:17 -0700 | [diff] [blame] | 327 |  | 
| Wei Wang | 858f3e5 | 2019-02-21 11:25:16 -0800 | [diff] [blame] | 328 |         if (kill(pid, signal) == -1 && errno != ESRCH) { | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 329 |             PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed"; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 330 |         } | 
 | 331 |     } | 
 | 332 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 333 |     return feof(fd.get()) ? processes : -1; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 334 | } | 
 | 335 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 336 | static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 337 |     std::string cpuacct_path; | 
 | 338 |     std::string memory_path; | 
 | 339 |  | 
 | 340 |     CgroupGetControllerPath("cpuacct", &cpuacct_path); | 
 | 341 |     CgroupGetControllerPath("memory", &memory_path); | 
| Vic Yang | ab8d6ab | 2019-02-19 14:09:00 -0800 | [diff] [blame] | 342 |     memory_path += "/apps"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 343 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 344 |     const char* cgroup = | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 345 |             (!access(ConvertUidPidToPath(cpuacct_path.c_str(), uid, initialPid).c_str(), F_OK)) | 
 | 346 |                     ? cpuacct_path.c_str() | 
 | 347 |                     : memory_path.c_str(); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 348 |  | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 349 |     std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 350 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 351 |     int retry = retries; | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 352 |     int processes; | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 353 |     while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 354 |         LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid; | 
| Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 355 |         if (retry > 0) { | 
| Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 356 |             std::this_thread::sleep_for(5ms); | 
| Yusuke Sato | d503930 | 2015-06-16 13:51:14 -0700 | [diff] [blame] | 357 |             --retry; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 358 |         } else { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 359 |             break; | 
 | 360 |         } | 
 | 361 |     } | 
 | 362 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 363 |     if (processes < 0) { | 
 | 364 |         PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid " | 
 | 365 |                     << initialPid; | 
 | 366 |         return -1; | 
 | 367 |     } | 
| Collin Mulliner | f7e79b9 | 2016-06-01 21:03:55 +0000 | [diff] [blame] | 368 |  | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 369 |     std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 370 |     auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 371 |  | 
 | 372 |     // We only calculate the number of 'processes' when killing the processes. | 
 | 373 |     // In the retries == 0 case, we only kill the processes once and therefore | 
 | 374 |     // will not have waited then recalculated how many processes are remaining | 
 | 375 |     // after the first signals have been sent. | 
 | 376 |     // Logging anything regarding the number of 'processes' here does not make sense. | 
| Dianne Hackborn | 2c5e7e1 | 2014-10-13 17:45:22 -0700 | [diff] [blame] | 377 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 378 |     if (processes == 0) { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 379 |         if (retries > 0) { | 
 | 380 |             LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid | 
 | 381 |                       << " in " << static_cast<int>(ms) << "ms"; | 
 | 382 |         } | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 383 |         return RemoveProcessGroup(cgroup, uid, initialPid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 384 |     } else { | 
| Tom Cherry | 20514c4 | 2017-04-21 13:48:49 -0700 | [diff] [blame] | 385 |         if (retries > 0) { | 
 | 386 |             LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid | 
 | 387 |                        << " in " << static_cast<int>(ms) << "ms, " << processes | 
 | 388 |                        << " processes remain"; | 
 | 389 |         } | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 390 |         return -1; | 
 | 391 |     } | 
 | 392 | } | 
 | 393 |  | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 394 | int killProcessGroup(uid_t uid, int initialPid, int signal) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 395 |     return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/); | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 396 | } | 
 | 397 |  | 
 | 398 | int killProcessGroupOnce(uid_t uid, int initialPid, int signal) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 399 |     return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/); | 
| Keun-young Park | fac4b63 | 2017-03-28 17:25:24 -0700 | [diff] [blame] | 400 | } | 
 | 401 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 402 | int createProcessGroup(uid_t uid, int initialPid, bool memControl) { | 
 | 403 |     std::string cgroup; | 
 | 404 |     if (isMemoryCgroupSupported() && (memControl || UsePerAppMemcg())) { | 
 | 405 |         CgroupGetControllerPath("memory", &cgroup); | 
| Vic Yang | ab8d6ab | 2019-02-19 14:09:00 -0800 | [diff] [blame] | 406 |         cgroup += "/apps"; | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 407 |     } else { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 408 |         CgroupGetControllerPath("cpuacct", &cgroup); | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 409 |     } | 
 | 410 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 411 |     auto uid_path = ConvertUidToPath(cgroup.c_str(), uid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 412 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 413 |     if (!MkdirAndChown(uid_path, 0750, AID_SYSTEM, AID_SYSTEM)) { | 
 | 414 |         PLOG(ERROR) << "Failed to make and chown " << uid_path; | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 415 |         return -errno; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 416 |     } | 
 | 417 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 418 |     auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid); | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 419 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 420 |     if (!MkdirAndChown(uid_pid_path, 0750, AID_SYSTEM, AID_SYSTEM)) { | 
 | 421 |         PLOG(ERROR) << "Failed to make and chown " << uid_pid_path; | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 422 |         return -errno; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 423 |     } | 
 | 424 |  | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 425 |     auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 426 |  | 
| Elliott Hughes | 171df0a | 2016-08-02 13:30:30 -0700 | [diff] [blame] | 427 |     int ret = 0; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 428 |     if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) { | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 429 |         ret = -errno; | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 430 |         PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file; | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 431 |     } | 
 | 432 |  | 
| Colin Cross | cf8d1c2 | 2014-06-03 13:24:21 -0700 | [diff] [blame] | 433 |     return ret; | 
 | 434 | } | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 435 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 436 | static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) { | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 437 |     if (!isMemoryCgroupSupported()) { | 
| Tom Cherry | 574a081 | 2018-02-23 13:04:40 -0800 | [diff] [blame] | 438 |         PLOG(ERROR) << "Memcg is not mounted."; | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 439 |         return false; | 
 | 440 |     } | 
 | 441 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 442 |     std::string path; | 
 | 443 |     if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) { | 
 | 444 |         PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'"; | 
 | 445 |         return false; | 
 | 446 |     } | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 447 |  | 
 | 448 |     if (!WriteStringToFile(std::to_string(value), path)) { | 
 | 449 |         PLOG(ERROR) << "Failed to write '" << value << "' to " << path; | 
 | 450 |         return false; | 
 | 451 |     } | 
 | 452 |     return true; | 
 | 453 | } | 
 | 454 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 455 | bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) { | 
 | 456 |     return SetProcessGroupValue(pid, "MemSwappiness", swappiness); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 457 | } | 
 | 458 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 459 | bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) { | 
 | 460 |     return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 461 | } | 
 | 462 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 463 | bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) { | 
 | 464 |     return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes); | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 465 | } |