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