Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 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 <fcntl.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 21 | #include <task_profiles.h> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 26 | #include <android-base/properties.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 29 | #include <android-base/threads.h> |
| 30 | |
| 31 | #include <cutils/android_filesystem_config.h> |
| 32 | |
| 33 | #include <json/reader.h> |
| 34 | #include <json/value.h> |
| 35 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 36 | // To avoid issues in sdk_mac build |
| 37 | #if defined(__ANDROID__) |
| 38 | #include <sys/prctl.h> |
| 39 | #endif |
| 40 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 41 | using android::base::GetThreadId; |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 42 | using android::base::GetUintProperty; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 43 | using android::base::StringPrintf; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 44 | using android::base::StringReplace; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 45 | using android::base::unique_fd; |
| 46 | using android::base::WriteStringToFile; |
| 47 | |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 48 | static constexpr const char* TASK_PROFILE_DB_FILE = "/etc/task_profiles.json"; |
| 49 | static constexpr const char* TASK_PROFILE_DB_VENDOR_FILE = "/vendor/etc/task_profiles.json"; |
| 50 | |
| 51 | static constexpr const char* TEMPLATE_TASK_PROFILE_API_FILE = |
| 52 | "/etc/task_profiles/task_profiles_%u.json"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 53 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 54 | class FdCacheHelper { |
| 55 | public: |
| 56 | enum FdState { |
| 57 | FDS_INACCESSIBLE = -1, |
| 58 | FDS_APP_DEPENDENT = -2, |
| 59 | FDS_NOT_CACHED = -3, |
| 60 | }; |
| 61 | |
| 62 | static void Cache(const std::string& path, android::base::unique_fd& fd); |
| 63 | static void Drop(android::base::unique_fd& fd); |
| 64 | static void Init(const std::string& path, android::base::unique_fd& fd); |
| 65 | static bool IsCached(const android::base::unique_fd& fd) { return fd > FDS_INACCESSIBLE; } |
| 66 | |
| 67 | private: |
| 68 | static bool IsAppDependentPath(const std::string& path); |
| 69 | }; |
| 70 | |
| 71 | void FdCacheHelper::Init(const std::string& path, android::base::unique_fd& fd) { |
| 72 | // file descriptors for app-dependent paths can't be cached |
| 73 | if (IsAppDependentPath(path)) { |
| 74 | // file descriptor is not cached |
| 75 | fd.reset(FDS_APP_DEPENDENT); |
| 76 | return; |
| 77 | } |
| 78 | // file descriptor can be cached later on request |
| 79 | fd.reset(FDS_NOT_CACHED); |
| 80 | } |
| 81 | |
| 82 | void FdCacheHelper::Cache(const std::string& path, android::base::unique_fd& fd) { |
| 83 | if (fd != FDS_NOT_CACHED) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (access(path.c_str(), W_OK) != 0) { |
| 88 | // file is not accessible |
| 89 | fd.reset(FDS_INACCESSIBLE); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 94 | if (tmp_fd < 0) { |
| 95 | PLOG(ERROR) << "Failed to cache fd '" << path << "'"; |
| 96 | fd.reset(FDS_INACCESSIBLE); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | fd = std::move(tmp_fd); |
| 101 | } |
| 102 | |
| 103 | void FdCacheHelper::Drop(android::base::unique_fd& fd) { |
| 104 | if (fd == FDS_NOT_CACHED) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | fd.reset(FDS_NOT_CACHED); |
| 109 | } |
| 110 | |
| 111 | bool FdCacheHelper::IsAppDependentPath(const std::string& path) { |
| 112 | return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos; |
| 113 | } |
| 114 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 115 | IProfileAttribute::~IProfileAttribute() = default; |
| 116 | |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 117 | const std::string& ProfileAttribute::file_name() const { |
| 118 | if (controller()->version() == 2 && !file_v2_name_.empty()) return file_v2_name_; |
| 119 | return file_name_; |
| 120 | } |
| 121 | |
| 122 | void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name, |
| 123 | const std::string& file_v2_name) { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 124 | controller_ = controller; |
| 125 | file_name_ = file_name; |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 126 | file_v2_name_ = file_v2_name; |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 129 | bool ProfileAttribute::GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const { |
| 130 | if (controller()->version() == 2) { |
| 131 | // all cgroup v2 attributes use the same process group hierarchy |
| 132 | *path = StringPrintf("%s/uid_%u/pid_%d/%s", controller()->path(), uid, pid, |
| 133 | file_name().c_str()); |
| 134 | return true; |
| 135 | } |
| 136 | return GetPathForTask(pid, path); |
| 137 | } |
| 138 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 139 | bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const { |
| 140 | std::string subgroup; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 141 | if (!controller()->GetTaskGroup(tid, &subgroup)) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
| 145 | if (path == nullptr) { |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | if (subgroup.empty()) { |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 150 | *path = StringPrintf("%s/%s", controller()->path(), file_name().c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 151 | } else { |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 152 | *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(), |
| 153 | file_name().c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 158 | bool ProfileAttribute::GetPathForUID(uid_t uid, std::string* path) const { |
| 159 | if (path == nullptr) { |
| 160 | return true; |
| 161 | } |
| 162 | |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 163 | *path = StringPrintf("%s/uid_%u/%s", controller()->path(), uid, file_name().c_str()); |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 167 | bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const { |
| 168 | // TODO: add support when kernel supports util_clamp |
| 169 | LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported"; |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | bool SetClampsAction::ExecuteForTask(int) const { |
| 174 | // TODO: add support when kernel supports util_clamp |
| 175 | LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported"; |
| 176 | return false; |
| 177 | } |
| 178 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 179 | // To avoid issues in sdk_mac build |
| 180 | #if defined(__ANDROID__) |
| 181 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 182 | bool SetTimerSlackAction::IsTimerSlackSupported(int tid) { |
| 183 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); |
| 184 | |
| 185 | return (access(file.c_str(), W_OK) == 0); |
| 186 | } |
| 187 | |
| 188 | bool SetTimerSlackAction::ExecuteForTask(int tid) const { |
| 189 | static bool sys_supports_timerslack = IsTimerSlackSupported(tid); |
| 190 | |
| 191 | // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface. |
| 192 | // TODO: once we've backported this, log if the open(2) fails. |
| 193 | if (sys_supports_timerslack) { |
| 194 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); |
| 195 | if (!WriteStringToFile(std::to_string(slack_), file)) { |
Suren Baghdasaryan | 2bc5228 | 2019-02-12 17:30:26 -0800 | [diff] [blame] | 196 | if (errno == ENOENT) { |
| 197 | // This happens when process is already dead |
| 198 | return true; |
| 199 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 200 | PLOG(ERROR) << "set_timerslack_ns write failed"; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported. |
| 205 | if (tid == 0 || tid == GetThreadId()) { |
| 206 | if (prctl(PR_SET_TIMERSLACK, slack_) == -1) { |
| 207 | PLOG(ERROR) << "set_timerslack_ns prctl failed"; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return true; |
| 212 | } |
| 213 | |
Bart Van Assche | 20d59bd | 2022-01-24 19:45:59 +0000 | [diff] [blame] | 214 | #else |
| 215 | |
| 216 | bool SetTimerSlackAction::ExecuteForTask(int) const { |
| 217 | return true; |
| 218 | }; |
| 219 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 220 | #endif |
| 221 | |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 222 | bool SetAttributeAction::WriteValueToFile(const std::string& path) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 223 | if (!WriteStringToFile(value_, path)) { |
Bart Van Assche | 9b5a232 | 2022-03-22 16:15:00 -0700 | [diff] [blame] | 224 | if (access(path.c_str(), F_OK) < 0) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 225 | if (optional_) { |
| 226 | return true; |
| 227 | } else { |
| 228 | LOG(ERROR) << "No such cgroup attribute: " << path; |
| 229 | return false; |
| 230 | } |
| 231 | } |
Bart Van Assche | 54136f8 | 2022-03-31 11:26:42 -0700 | [diff] [blame] | 232 | // The PLOG() statement below uses the error code stored in `errno` by |
| 233 | // WriteStringToFile() because access() only overwrites `errno` if it fails |
| 234 | // and because this code is only reached if the access() function returns 0. |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 235 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 242 | bool SetAttributeAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 243 | std::string path; |
| 244 | |
| 245 | if (!attribute_->GetPathForProcess(uid, pid, &path)) { |
| 246 | LOG(ERROR) << "Failed to find cgroup for uid " << uid << " pid " << pid; |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | return WriteValueToFile(path); |
| 251 | } |
| 252 | |
| 253 | bool SetAttributeAction::ExecuteForTask(int tid) const { |
| 254 | std::string path; |
| 255 | |
| 256 | if (!attribute_->GetPathForTask(tid, &path)) { |
| 257 | LOG(ERROR) << "Failed to find cgroup for tid " << tid; |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | return WriteValueToFile(path); |
| 262 | } |
| 263 | |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 264 | bool SetAttributeAction::ExecuteForUID(uid_t uid) const { |
| 265 | std::string path; |
| 266 | |
| 267 | if (!attribute_->GetPathForUID(uid, &path)) { |
| 268 | LOG(ERROR) << "Failed to find cgroup for uid " << uid; |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | if (!WriteStringToFile(value_, path)) { |
| 273 | if (access(path.c_str(), F_OK) < 0) { |
| 274 | if (optional_) { |
| 275 | return true; |
| 276 | } else { |
| 277 | LOG(ERROR) << "No such cgroup attribute: " << path; |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; |
| 282 | return false; |
| 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 287 | bool SetAttributeAction::IsValidForProcess(uid_t, pid_t pid) const { |
| 288 | return IsValidForTask(pid); |
| 289 | } |
| 290 | |
| 291 | bool SetAttributeAction::IsValidForTask(int tid) const { |
| 292 | std::string path; |
| 293 | |
| 294 | if (!attribute_->GetPathForTask(tid, &path)) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | if (!access(path.c_str(), W_OK)) { |
| 299 | // operation will succeed |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | if (!access(path.c_str(), F_OK)) { |
| 304 | // file exists but not writable |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | // file does not exist, ignore if optional |
| 309 | return optional_; |
| 310 | } |
| 311 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 312 | SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p) |
| 313 | : controller_(c), path_(p) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 314 | FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]); |
| 315 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 316 | FdCacheHelper::Init(controller_.GetProcsFilePath(path_, 0, 0), fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 319 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd, ResourceCacheType cache_type) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 320 | if (tid <= 0) { |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | std::string value = std::to_string(tid); |
| 325 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 326 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) == value.length()) { |
| 327 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 330 | // If the thread is in the process of exiting, don't flag an error |
| 331 | if (errno == ESRCH) { |
| 332 | return true; |
| 333 | } |
| 334 | |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 335 | const char* controller_name = controller()->name(); |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 336 | // ENOSPC is returned when cpuset cgroup that we are joining has no online cpus |
| 337 | if (errno == ENOSPC && !strcmp(controller_name, "cpuset")) { |
| 338 | // This is an abnormal case happening only in testing, so report it only once |
| 339 | static bool empty_cpuset_reported = false; |
| 340 | |
| 341 | if (empty_cpuset_reported) { |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | LOG(ERROR) << "Failed to add task '" << value |
| 346 | << "' into cpuset because all cpus in that cpuset are offline"; |
| 347 | empty_cpuset_reported = true; |
| 348 | } else { |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 349 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; path=" << path_ << "; " |
| 350 | << (cache_type == RCT_TASK ? "task" : "process"); |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 356 | ProfileAction::CacheUseResult SetCgroupAction::UseCachedFd(ResourceCacheType cache_type, |
| 357 | int id) const { |
| 358 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 359 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
| 360 | // fd is cached, reuse it |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 361 | if (!AddTidToCgroup(id, fd_[cache_type], cache_type)) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 362 | LOG(ERROR) << "Failed to add task into cgroup"; |
| 363 | return ProfileAction::FAIL; |
| 364 | } |
| 365 | return ProfileAction::SUCCESS; |
| 366 | } |
| 367 | |
| 368 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 369 | // no permissions to access the file, ignore |
| 370 | return ProfileAction::SUCCESS; |
| 371 | } |
| 372 | |
| 373 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 374 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 375 | // application-dependent path can't be used with tid |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 376 | LOG(ERROR) << Name() << ": application profile can't be applied to a thread"; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 377 | return ProfileAction::FAIL; |
| 378 | } |
| 379 | |
| 380 | return ProfileAction::UNUSED; |
| 381 | } |
| 382 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 383 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 384 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_PROCESS, pid); |
| 385 | if (result != ProfileAction::UNUSED) { |
| 386 | return result == ProfileAction::SUCCESS; |
| 387 | } |
| 388 | |
| 389 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 390 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 391 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 392 | if (tmp_fd < 0) { |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 393 | PLOG(WARNING) << Name() << "::" << __func__ << ": failed to open " << procs_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 394 | return false; |
| 395 | } |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 396 | if (!AddTidToCgroup(pid, tmp_fd, RCT_PROCESS)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 397 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 398 | return false; |
| 399 | } |
| 400 | |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | bool SetCgroupAction::ExecuteForTask(int tid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 405 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, tid); |
| 406 | if (result != ProfileAction::UNUSED) { |
| 407 | return result == ProfileAction::SUCCESS; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 410 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 411 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 412 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 413 | if (tmp_fd < 0) { |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 414 | PLOG(WARNING) << Name() << "::" << __func__ << ": failed to open " << tasks_path; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 415 | return false; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 416 | } |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 417 | if (!AddTidToCgroup(tid, tmp_fd, RCT_TASK)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 418 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | |
| 422 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 425 | void SetCgroupAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 426 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 427 | // Return early to prevent unnecessary calls to controller_.Get{Tasks|Procs}FilePath() which |
| 428 | // include regex evaluations |
| 429 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 430 | return; |
| 431 | } |
| 432 | switch (cache_type) { |
| 433 | case (ProfileAction::RCT_TASK): |
| 434 | FdCacheHelper::Cache(controller_.GetTasksFilePath(path_), fd_[cache_type]); |
| 435 | break; |
| 436 | case (ProfileAction::RCT_PROCESS): |
| 437 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 438 | FdCacheHelper::Cache(controller_.GetProcsFilePath(path_, 0, 0), fd_[cache_type]); |
| 439 | break; |
| 440 | default: |
| 441 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 442 | break; |
| 443 | } |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 446 | void SetCgroupAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 447 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 448 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 451 | bool SetCgroupAction::IsValidForProcess(uid_t uid, pid_t pid) const { |
| 452 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 453 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_PROCESS])) { |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | if (fd_[ProfileAction::RCT_PROCESS] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
| 462 | return access(procs_path.c_str(), W_OK) == 0; |
| 463 | } |
| 464 | |
| 465 | bool SetCgroupAction::IsValidForTask(int) const { |
| 466 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 467 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_TASK])) { |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 476 | // application-dependent path can't be used with tid |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
| 481 | return access(tasks_path.c_str(), W_OK) == 0; |
| 482 | } |
| 483 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 484 | WriteFileAction::WriteFileAction(const std::string& task_path, const std::string& proc_path, |
| 485 | const std::string& value, bool logfailures) |
| 486 | : task_path_(task_path), proc_path_(proc_path), value_(value), logfailures_(logfailures) { |
| 487 | FdCacheHelper::Init(task_path_, fd_[ProfileAction::RCT_TASK]); |
| 488 | if (!proc_path_.empty()) FdCacheHelper::Init(proc_path_, fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 489 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 490 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 491 | bool WriteFileAction::WriteValueToFile(const std::string& value_, ResourceCacheType cache_type, |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 492 | uid_t uid, pid_t pid, bool logfailures) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 493 | std::string value(value_); |
| 494 | |
| 495 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 496 | value = StringReplace(value, "<pid>", std::to_string(pid), true); |
| 497 | |
| 498 | CacheUseResult result = UseCachedFd(cache_type, value); |
| 499 | |
| 500 | if (result != ProfileAction::UNUSED) { |
| 501 | return result == ProfileAction::SUCCESS; |
| 502 | } |
| 503 | |
| 504 | std::string path; |
| 505 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 506 | path = task_path_; |
| 507 | } else { |
| 508 | path = proc_path_; |
| 509 | } |
| 510 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 511 | // Use WriteStringToFd instead of WriteStringToFile because the latter will open file with |
| 512 | // O_TRUNC which causes kernfs_mutex contention |
| 513 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC))); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 514 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 515 | if (tmp_fd < 0) { |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 516 | if (logfailures) PLOG(WARNING) << Name() << "::" << __func__ << ": failed to open " << path; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
| 520 | if (!WriteStringToFd(value, tmp_fd)) { |
| 521 | if (logfailures) PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 522 | return false; |
| 523 | } |
| 524 | |
| 525 | return true; |
| 526 | } |
| 527 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 528 | ProfileAction::CacheUseResult WriteFileAction::UseCachedFd(ResourceCacheType cache_type, |
| 529 | const std::string& value) const { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 530 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 531 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 532 | // fd is cached, reuse it |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 533 | bool ret = WriteStringToFd(value, fd_[cache_type]); |
| 534 | |
| 535 | if (!ret && logfailures_) { |
| 536 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 537 | PLOG(ERROR) << "Failed to write '" << value << "' to " << task_path_; |
| 538 | } else { |
| 539 | PLOG(ERROR) << "Failed to write '" << value << "' to " << proc_path_; |
| 540 | } |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 541 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 542 | return ret ? ProfileAction::SUCCESS : ProfileAction::FAIL; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 545 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 546 | // no permissions to access the file, ignore |
| 547 | return ProfileAction::SUCCESS; |
| 548 | } |
| 549 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 550 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 551 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 552 | // application-dependent path can't be used with tid |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 553 | LOG(ERROR) << Name() << ": application profile can't be applied to a thread"; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 554 | return ProfileAction::FAIL; |
| 555 | } |
| 556 | return ProfileAction::UNUSED; |
| 557 | } |
| 558 | |
| 559 | bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 560 | if (!proc_path_.empty()) { |
| 561 | return WriteValueToFile(value_, ProfileAction::RCT_PROCESS, uid, pid, logfailures_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 564 | DIR* d; |
| 565 | struct dirent* de; |
| 566 | char proc_path[255]; |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame^] | 567 | pid_t t_pid; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 568 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 569 | sprintf(proc_path, "/proc/%d/task", pid); |
| 570 | if (!(d = opendir(proc_path))) { |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | while ((de = readdir(d))) { |
| 575 | if (de->d_name[0] == '.') { |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | t_pid = atoi(de->d_name); |
| 580 | |
| 581 | if (!t_pid) { |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | WriteValueToFile(value_, ProfileAction::RCT_TASK, uid, t_pid, logfailures_); |
| 586 | } |
| 587 | |
| 588 | closedir(d); |
| 589 | |
| 590 | return true; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 591 | } |
| 592 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 593 | bool WriteFileAction::ExecuteForTask(int tid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 594 | return WriteValueToFile(value_, ProfileAction::RCT_TASK, getuid(), tid, logfailures_); |
| 595 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 596 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 597 | void WriteFileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
| 598 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 599 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 600 | return; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 601 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 602 | switch (cache_type) { |
| 603 | case (ProfileAction::RCT_TASK): |
| 604 | FdCacheHelper::Cache(task_path_, fd_[cache_type]); |
| 605 | break; |
| 606 | case (ProfileAction::RCT_PROCESS): |
| 607 | if (!proc_path_.empty()) FdCacheHelper::Cache(proc_path_, fd_[cache_type]); |
| 608 | break; |
| 609 | default: |
| 610 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 611 | break; |
| 612 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 613 | } |
| 614 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 615 | void WriteFileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 616 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 617 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 620 | bool WriteFileAction::IsValidForProcess(uid_t, pid_t) const { |
| 621 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 622 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_PROCESS])) { |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | if (fd_[ProfileAction::RCT_PROCESS] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | return access(proc_path_.empty() ? task_path_.c_str() : proc_path_.c_str(), W_OK) == 0; |
| 631 | } |
| 632 | |
| 633 | bool WriteFileAction::IsValidForTask(int) const { |
| 634 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 635 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_TASK])) { |
| 636 | return true; |
| 637 | } |
| 638 | |
| 639 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 644 | // application-dependent path can't be used with tid |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | return access(task_path_.c_str(), W_OK) == 0; |
| 649 | } |
| 650 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 651 | bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 652 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 653 | profile->ExecuteForProcess(uid, pid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 654 | } |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | bool ApplyProfileAction::ExecuteForTask(int tid) const { |
| 659 | for (const auto& profile : profiles_) { |
Wei Wang | 8722e4d | 2021-05-14 12:34:54 -0700 | [diff] [blame] | 660 | profile->ExecuteForTask(tid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 661 | } |
| 662 | return true; |
| 663 | } |
| 664 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 665 | void ApplyProfileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 666 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 667 | profile->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 671 | void ApplyProfileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 672 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 673 | profile->DropResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 677 | bool ApplyProfileAction::IsValidForProcess(uid_t uid, pid_t pid) const { |
| 678 | for (const auto& profile : profiles_) { |
| 679 | if (!profile->IsValidForProcess(uid, pid)) { |
| 680 | return false; |
| 681 | } |
| 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | bool ApplyProfileAction::IsValidForTask(int tid) const { |
| 687 | for (const auto& profile : profiles_) { |
| 688 | if (!profile->IsValidForTask(tid)) { |
| 689 | return false; |
| 690 | } |
| 691 | } |
| 692 | return true; |
| 693 | } |
| 694 | |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 695 | void TaskProfile::MoveTo(TaskProfile* profile) { |
| 696 | profile->elements_ = std::move(elements_); |
| 697 | profile->res_cached_ = res_cached_; |
| 698 | } |
| 699 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 700 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 701 | for (const auto& element : elements_) { |
| 702 | if (!element->ExecuteForProcess(uid, pid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 703 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 704 | return false; |
| 705 | } |
| 706 | } |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | bool TaskProfile::ExecuteForTask(int tid) const { |
| 711 | if (tid == 0) { |
| 712 | tid = GetThreadId(); |
| 713 | } |
| 714 | for (const auto& element : elements_) { |
| 715 | if (!element->ExecuteForTask(tid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 716 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 717 | return false; |
| 718 | } |
| 719 | } |
| 720 | return true; |
| 721 | } |
| 722 | |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 723 | bool TaskProfile::ExecuteForUID(uid_t uid) const { |
| 724 | for (const auto& element : elements_) { |
| 725 | if (!element->ExecuteForUID(uid)) { |
| 726 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
| 727 | return false; |
| 728 | } |
| 729 | } |
| 730 | return true; |
| 731 | } |
| 732 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 733 | void TaskProfile::EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 734 | if (res_cached_) { |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 739 | element->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | res_cached_ = true; |
| 743 | } |
| 744 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 745 | void TaskProfile::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 746 | if (!res_cached_) { |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 751 | element->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | res_cached_ = false; |
| 755 | } |
| 756 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 757 | bool TaskProfile::IsValidForProcess(uid_t uid, pid_t pid) const { |
| 758 | for (const auto& element : elements_) { |
| 759 | if (!element->IsValidForProcess(uid, pid)) return false; |
| 760 | } |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | bool TaskProfile::IsValidForTask(int tid) const { |
| 765 | for (const auto& element : elements_) { |
| 766 | if (!element->IsValidForTask(tid)) return false; |
| 767 | } |
| 768 | return true; |
| 769 | } |
| 770 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 771 | void TaskProfiles::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 772 | for (auto& iter : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 773 | iter.second->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 777 | TaskProfiles& TaskProfiles::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 778 | // Deliberately leak this object to avoid a race between destruction on |
| 779 | // process exit and concurrent access from another thread. |
| 780 | static auto* instance = new TaskProfiles; |
| 781 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | TaskProfiles::TaskProfiles() { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 785 | // load system task profiles |
| 786 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { |
| 787 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; |
| 788 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 789 | |
| 790 | // load API-level specific system task profiles if available |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 791 | unsigned int api_level = GetUintProperty<unsigned int>("ro.product.first_api_level", 0); |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 792 | if (api_level > 0) { |
| 793 | std::string api_profiles_path = |
| 794 | android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level); |
| 795 | if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 796 | if (!Load(CgroupMap::GetInstance(), api_profiles_path)) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 797 | LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 798 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 802 | // load vendor task profiles if the file exists |
| 803 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && |
| 804 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { |
| 805 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() |
| 806 | << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 810 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 811 | std::string json_doc; |
| 812 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 813 | if (!android::base::ReadFileToString(file_name, &json_doc)) { |
| 814 | LOG(ERROR) << "Failed to read task profiles from " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 815 | return false; |
| 816 | } |
| 817 | |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 818 | Json::CharReaderBuilder builder; |
| 819 | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 820 | Json::Value root; |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 821 | std::string errorMessage; |
| 822 | if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) { |
| 823 | LOG(ERROR) << "Failed to parse task profiles: " << errorMessage; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 824 | return false; |
| 825 | } |
| 826 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 827 | const Json::Value& attr = root["Attributes"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 828 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { |
| 829 | std::string name = attr[i]["Name"].asString(); |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 830 | std::string controller_name = attr[i]["Controller"].asString(); |
| 831 | std::string file_attr = attr[i]["File"].asString(); |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 832 | std::string file_v2_attr = attr[i]["FileV2"].asString(); |
| 833 | |
| 834 | if (!file_v2_attr.empty() && file_attr.empty()) { |
| 835 | LOG(ERROR) << "Attribute " << name << " has FileV2 but no File property"; |
| 836 | return false; |
| 837 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 838 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 839 | auto controller = cg_map.FindController(controller_name); |
| 840 | if (controller.HasValue()) { |
| 841 | auto iter = attributes_.find(name); |
| 842 | if (iter == attributes_.end()) { |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 843 | attributes_[name] = |
| 844 | std::make_unique<ProfileAttribute>(controller, file_attr, file_v2_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 845 | } else { |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 846 | iter->second->Reset(controller, file_attr, file_v2_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 847 | } |
| 848 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 849 | LOG(WARNING) << "Controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 853 | const Json::Value& profiles_val = root["Profiles"]; |
| 854 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { |
| 855 | const Json::Value& profile_val = profiles_val[i]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 856 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 857 | std::string profile_name = profile_val["Name"].asString(); |
| 858 | const Json::Value& actions = profile_val["Actions"]; |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 859 | auto profile = std::make_shared<TaskProfile>(profile_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 860 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 861 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { |
| 862 | const Json::Value& action_val = actions[act_idx]; |
| 863 | std::string action_name = action_val["Name"].asString(); |
| 864 | const Json::Value& params_val = action_val["Params"]; |
| 865 | if (action_name == "JoinCgroup") { |
| 866 | std::string controller_name = params_val["Controller"].asString(); |
| 867 | std::string path = params_val["Path"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 868 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 869 | auto controller = cg_map.FindController(controller_name); |
| 870 | if (controller.HasValue()) { |
Bart Van Assche | 2953a92 | 2023-11-14 07:33:00 -0800 | [diff] [blame] | 871 | if (controller.version() == 1) { |
| 872 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); |
| 873 | } else { |
| 874 | LOG(WARNING) << "A JoinCgroup action in the " << profile_name |
| 875 | << " profile is used for controller " << controller_name |
| 876 | << " in the cgroup v2 hierarchy and will be ignored"; |
| 877 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 878 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 879 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 880 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 881 | } else if (action_name == "SetTimerSlack") { |
| 882 | std::string slack_value = params_val["Slack"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 883 | char* end; |
| 884 | unsigned long slack; |
| 885 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 886 | slack = strtoul(slack_value.c_str(), &end, 10); |
| 887 | if (end > slack_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 888 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); |
| 889 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 890 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 891 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 892 | } else if (action_name == "SetAttribute") { |
| 893 | std::string attr_name = params_val["Name"].asString(); |
| 894 | std::string attr_value = params_val["Value"].asString(); |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 895 | bool optional = strcmp(params_val["Optional"].asString().c_str(), "true") == 0; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 896 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 897 | auto iter = attributes_.find(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 898 | if (iter != attributes_.end()) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 899 | profile->Add(std::make_unique<SetAttributeAction>(iter->second.get(), |
| 900 | attr_value, optional)); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 901 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 902 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 903 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 904 | } else if (action_name == "SetClamps") { |
| 905 | std::string boost_value = params_val["Boost"].asString(); |
| 906 | std::string clamp_value = params_val["Clamp"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 907 | char* end; |
| 908 | unsigned long boost; |
| 909 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 910 | boost = strtoul(boost_value.c_str(), &end, 10); |
| 911 | if (end > boost_value.c_str()) { |
| 912 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); |
| 913 | if (end > clamp_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 914 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); |
| 915 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 916 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 917 | } |
| 918 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 919 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 920 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 921 | } else if (action_name == "WriteFile") { |
| 922 | std::string attr_filepath = params_val["FilePath"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 923 | std::string attr_procfilepath = params_val["ProcFilePath"].asString(); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 924 | std::string attr_value = params_val["Value"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 925 | // FilePath and Value are mandatory |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 926 | if (!attr_filepath.empty() && !attr_value.empty()) { |
Rick Yiu | 49fce95 | 2021-04-08 22:10:06 +0800 | [diff] [blame] | 927 | std::string attr_logfailures = params_val["LogFailures"].asString(); |
| 928 | bool logfailures = attr_logfailures.empty() || attr_logfailures == "true"; |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 929 | profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_procfilepath, |
| 930 | attr_value, logfailures)); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 931 | } else if (attr_filepath.empty()) { |
| 932 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 933 | << "empty filepath"; |
| 934 | } else if (attr_value.empty()) { |
| 935 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 936 | << "empty value"; |
| 937 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 938 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 939 | LOG(WARNING) << "Unknown profile action: " << action_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 940 | } |
| 941 | } |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 942 | auto iter = profiles_.find(profile_name); |
| 943 | if (iter == profiles_.end()) { |
| 944 | profiles_[profile_name] = profile; |
| 945 | } else { |
| 946 | // Move the content rather that replace the profile because old profile might be |
| 947 | // referenced from an aggregate profile if vendor overrides task profiles |
| 948 | profile->MoveTo(iter->second.get()); |
| 949 | profile.reset(); |
| 950 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | const Json::Value& aggregateprofiles_val = root["AggregateProfiles"]; |
| 954 | for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) { |
| 955 | const Json::Value& aggregateprofile_val = aggregateprofiles_val[i]; |
| 956 | |
| 957 | std::string aggregateprofile_name = aggregateprofile_val["Name"].asString(); |
| 958 | const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"]; |
| 959 | std::vector<std::shared_ptr<TaskProfile>> profiles; |
| 960 | bool ret = true; |
| 961 | |
| 962 | for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) { |
| 963 | std::string profile_name = aggregateprofiles[pf_idx].asString(); |
| 964 | |
| 965 | if (profile_name == aggregateprofile_name) { |
| 966 | LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name; |
| 967 | ret = false; |
| 968 | break; |
| 969 | } else if (profiles_.find(profile_name) == profiles_.end()) { |
| 970 | LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name; |
| 971 | ret = false; |
| 972 | break; |
| 973 | } else { |
| 974 | profiles.push_back(profiles_[profile_name]); |
| 975 | } |
| 976 | } |
| 977 | if (ret) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 978 | auto profile = std::make_shared<TaskProfile>(aggregateprofile_name); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 979 | profile->Add(std::make_unique<ApplyProfileAction>(profiles)); |
| 980 | profiles_[aggregateprofile_name] = profile; |
| 981 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | return true; |
| 985 | } |
| 986 | |
Bart Van Assche | d0b8ce2 | 2022-08-02 13:06:26 -0700 | [diff] [blame] | 987 | TaskProfile* TaskProfiles::GetProfile(std::string_view name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 988 | auto iter = profiles_.find(name); |
| 989 | |
| 990 | if (iter != profiles_.end()) { |
| 991 | return iter->second.get(); |
| 992 | } |
| 993 | return nullptr; |
| 994 | } |
| 995 | |
Bart Van Assche | d0b8ce2 | 2022-08-02 13:06:26 -0700 | [diff] [blame] | 996 | const IProfileAttribute* TaskProfiles::GetAttribute(std::string_view name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 997 | auto iter = attributes_.find(name); |
| 998 | |
| 999 | if (iter != attributes_.end()) { |
| 1000 | return iter->second.get(); |
| 1001 | } |
| 1002 | return nullptr; |
| 1003 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1004 | |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1005 | template <typename T> |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 1006 | bool TaskProfiles::SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache) { |
| 1007 | for (const auto& name : profiles) { |
| 1008 | TaskProfile* profile = GetProfile(name); |
| 1009 | if (profile != nullptr) { |
| 1010 | if (use_fd_cache) { |
| 1011 | profile->EnableResourceCaching(ProfileAction::RCT_PROCESS); |
| 1012 | } |
| 1013 | if (!profile->ExecuteForUID(uid)) { |
| 1014 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; |
| 1015 | } |
| 1016 | } else { |
| 1017 | PLOG(WARNING) << "Failed to find " << name << "process profile"; |
| 1018 | } |
| 1019 | } |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
| 1023 | template <typename T> |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1024 | bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, |
| 1025 | bool use_fd_cache) { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1026 | bool success = true; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1027 | for (const auto& name : profiles) { |
| 1028 | TaskProfile* profile = GetProfile(name); |
| 1029 | if (profile != nullptr) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 1030 | if (use_fd_cache) { |
| 1031 | profile->EnableResourceCaching(ProfileAction::RCT_PROCESS); |
| 1032 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1033 | if (!profile->ExecuteForProcess(uid, pid)) { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1034 | LOG(WARNING) << "Failed to apply " << name << " process profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1035 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1036 | } |
| 1037 | } else { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1038 | LOG(WARNING) << "Failed to find " << name << " process profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1039 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1040 | } |
| 1041 | } |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1042 | return success; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1045 | template <typename T> |
| 1046 | bool TaskProfiles::SetTaskProfiles(int tid, std::span<const T> profiles, bool use_fd_cache) { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1047 | bool success = true; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1048 | for (const auto& name : profiles) { |
| 1049 | TaskProfile* profile = GetProfile(name); |
| 1050 | if (profile != nullptr) { |
| 1051 | if (use_fd_cache) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 1052 | profile->EnableResourceCaching(ProfileAction::RCT_TASK); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1053 | } |
| 1054 | if (!profile->ExecuteForTask(tid)) { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1055 | LOG(WARNING) << "Failed to apply " << name << " task profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1056 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1057 | } |
| 1058 | } else { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1059 | LOG(WARNING) << "Failed to find " << name << " task profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1060 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1061 | } |
| 1062 | } |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1063 | return success; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1064 | } |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1065 | |
| 1066 | template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
| 1067 | std::span<const std::string> profiles, |
| 1068 | bool use_fd_cache); |
| 1069 | template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
| 1070 | std::span<const std::string_view> profiles, |
| 1071 | bool use_fd_cache); |
| 1072 | template bool TaskProfiles::SetTaskProfiles(int tid, std::span<const std::string> profiles, |
| 1073 | bool use_fd_cache); |
| 1074 | template bool TaskProfiles::SetTaskProfiles(int tid, std::span<const std::string_view> profiles, |
| 1075 | bool use_fd_cache); |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 1076 | template bool TaskProfiles::SetUserProfiles(uid_t uid, std::span<const std::string> profiles, |
| 1077 | bool use_fd_cache); |