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