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