Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [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 | #include "service_parser.h" |
| 18 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 19 | #include <linux/input.h> |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <sys/socket.h> |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 22 | |
Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <sstream> |
| 25 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/parseint.h> |
Steven Moreland | e534919 | 2023-04-24 23:54:59 +0000 | [diff] [blame] | 28 | #include <android-base/properties.h> |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Suren Baghdasaryan | 746ede9 | 2022-03-31 21:15:11 +0000 | [diff] [blame] | 30 | #include <processgroup/processgroup.h> |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 31 | #include <system/thread_defs.h> |
| 32 | |
Suren Baghdasaryan | c29c2ba | 2019-10-22 17:18:42 -0700 | [diff] [blame] | 33 | #include "lmkd_service.h" |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 34 | #include "rlimit_parser.h" |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 35 | #include "service_utils.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 36 | #include "util.h" |
| 37 | |
Tom Cherry | a2f9136 | 2020-02-20 10:50:00 -0800 | [diff] [blame] | 38 | #ifdef INIT_FULL_SOURCES |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 39 | #include <android/api-level.h> |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 40 | #include <sys/system_properties.h> |
| 41 | |
| 42 | #include "selinux.h" |
| 43 | #else |
| 44 | #include "host_init_stubs.h" |
| 45 | #endif |
| 46 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 47 | using android::base::ParseInt; |
| 48 | using android::base::Split; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 49 | using android::base::StartsWith; |
| 50 | |
| 51 | namespace android { |
| 52 | namespace init { |
| 53 | |
Steven Moreland | 81a1b3e | 2024-05-20 22:31:11 +0000 | [diff] [blame] | 54 | #ifdef INIT_FULL_SOURCES |
| 55 | // on full sources, we have better information on device to |
| 56 | // make this decision |
| 57 | constexpr bool kAlwaysErrorUserRoot = false; |
| 58 | #else |
| 59 | constexpr uint64_t kBuildShippingApiLevel = BUILD_SHIPPING_API_LEVEL + 0 /* +0 if empty */; |
| 60 | // on partial sources, the host build, we don't have the specific |
| 61 | // vendor API level, but we can enforce things based on the |
| 62 | // shipping API level. |
| 63 | constexpr bool kAlwaysErrorUserRoot = kBuildShippingApiLevel > __ANDROID_API_V__; |
| 64 | #endif |
| 65 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 66 | Result<void> ServiceParser::ParseCapabilities(std::vector<std::string>&& args) { |
| 67 | service_->capabilities_ = 0; |
| 68 | |
| 69 | if (!CapAmbientSupported()) { |
| 70 | return Error() |
| 71 | << "capabilities requested but the kernel does not support ambient capabilities"; |
| 72 | } |
| 73 | |
| 74 | unsigned int last_valid_cap = GetLastValidCap(); |
| 75 | if (last_valid_cap >= service_->capabilities_->size()) { |
| 76 | LOG(WARNING) << "last valid run-time capability is larger than CAP_LAST_CAP"; |
| 77 | } |
| 78 | |
| 79 | for (size_t i = 1; i < args.size(); i++) { |
| 80 | const std::string& arg = args[i]; |
| 81 | int res = LookupCap(arg); |
| 82 | if (res < 0) { |
| 83 | return Errorf("invalid capability '{}'", arg); |
| 84 | } |
| 85 | unsigned int cap = static_cast<unsigned int>(res); // |res| is >= 0. |
| 86 | if (cap > last_valid_cap) { |
| 87 | return Errorf("capability '{}' not supported by the kernel", arg); |
| 88 | } |
| 89 | (*service_->capabilities_)[cap] = true; |
| 90 | } |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | Result<void> ServiceParser::ParseClass(std::vector<std::string>&& args) { |
| 95 | service_->classnames_ = std::set<std::string>(args.begin() + 1, args.end()); |
| 96 | return {}; |
| 97 | } |
| 98 | |
| 99 | Result<void> ServiceParser::ParseConsole(std::vector<std::string>&& args) { |
Tom Cherry | f74b7f5 | 2019-09-23 16:16:54 -0700 | [diff] [blame] | 100 | if (service_->proc_attr_.stdio_to_kmsg) { |
| 101 | return Error() << "'console' and 'stdio_to_kmsg' are mutually exclusive"; |
| 102 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 103 | service_->flags_ |= SVC_CONSOLE; |
| 104 | service_->proc_attr_.console = args.size() > 1 ? "/dev/" + args[1] : ""; |
| 105 | return {}; |
| 106 | } |
| 107 | |
| 108 | Result<void> ServiceParser::ParseCritical(std::vector<std::string>&& args) { |
Woody Lin | 45215ae | 2019-12-26 22:22:28 +0800 | [diff] [blame] | 109 | std::optional<std::string> fatal_reboot_target; |
| 110 | std::optional<std::chrono::minutes> fatal_crash_window; |
| 111 | |
| 112 | for (auto it = args.begin() + 1; it != args.end(); ++it) { |
| 113 | auto arg = android::base::Split(*it, "="); |
| 114 | if (arg.size() != 2) { |
| 115 | return Error() << "critical: Argument '" << *it << "' is not supported"; |
| 116 | } else if (arg[0] == "target") { |
| 117 | fatal_reboot_target = arg[1]; |
| 118 | } else if (arg[0] == "window") { |
| 119 | int minutes; |
| 120 | auto window = ExpandProps(arg[1]); |
| 121 | if (!window.ok()) { |
| 122 | return Error() << "critical: Could not expand argument ': " << arg[1]; |
| 123 | } |
| 124 | if (*window == "off") { |
| 125 | return {}; |
| 126 | } |
| 127 | if (!ParseInt(*window, &minutes, 0)) { |
| 128 | return Error() << "critical: 'fatal_crash_window' must be an integer > 0"; |
| 129 | } |
| 130 | fatal_crash_window = std::chrono::minutes(minutes); |
| 131 | } else { |
| 132 | return Error() << "critical: Argument '" << *it << "' is not supported"; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (fatal_reboot_target) { |
| 137 | service_->fatal_reboot_target_ = *fatal_reboot_target; |
| 138 | } |
| 139 | if (fatal_crash_window) { |
| 140 | service_->fatal_crash_window_ = *fatal_crash_window; |
| 141 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 142 | service_->flags_ |= SVC_CRITICAL; |
| 143 | return {}; |
| 144 | } |
| 145 | |
| 146 | Result<void> ServiceParser::ParseDisabled(std::vector<std::string>&& args) { |
| 147 | service_->flags_ |= SVC_DISABLED; |
| 148 | service_->flags_ |= SVC_RC_DISABLED; |
| 149 | return {}; |
| 150 | } |
| 151 | |
| 152 | Result<void> ServiceParser::ParseEnterNamespace(std::vector<std::string>&& args) { |
| 153 | if (args[1] != "net") { |
| 154 | return Error() << "Init only supports entering network namespaces"; |
| 155 | } |
| 156 | if (!service_->namespaces_.namespaces_to_enter.empty()) { |
| 157 | return Error() << "Only one network namespace may be entered"; |
| 158 | } |
| 159 | // Network namespaces require that /sys is remounted, otherwise the old adapters will still be |
| 160 | // present. Therefore, they also require mount namespaces. |
| 161 | service_->namespaces_.flags |= CLONE_NEWNS; |
| 162 | service_->namespaces_.namespaces_to_enter.emplace_back(CLONE_NEWNET, std::move(args[2])); |
| 163 | return {}; |
| 164 | } |
| 165 | |
Daniel Rosenberg | de76688 | 2022-12-01 15:44:31 -0800 | [diff] [blame] | 166 | Result<void> ServiceParser::ParseGentleKill(std::vector<std::string>&& args) { |
| 167 | service_->flags_ |= SVC_GENTLE_KILL; |
| 168 | return {}; |
| 169 | } |
| 170 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 171 | Result<void> ServiceParser::ParseGroup(std::vector<std::string>&& args) { |
| 172 | auto gid = DecodeUid(args[1]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 173 | if (!gid.ok()) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 174 | return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error(); |
| 175 | } |
| 176 | service_->proc_attr_.gid = *gid; |
| 177 | |
| 178 | for (std::size_t n = 2; n < args.size(); n++) { |
| 179 | gid = DecodeUid(args[n]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 180 | if (!gid.ok()) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 181 | return Error() << "Unable to decode GID for '" << args[n] << "': " << gid.error(); |
| 182 | } |
| 183 | service_->proc_attr_.supp_gids.emplace_back(*gid); |
| 184 | } |
| 185 | return {}; |
| 186 | } |
| 187 | |
| 188 | Result<void> ServiceParser::ParsePriority(std::vector<std::string>&& args) { |
| 189 | service_->proc_attr_.priority = 0; |
| 190 | if (!ParseInt(args[1], &service_->proc_attr_.priority, |
| 191 | static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative |
| 192 | static_cast<int>(ANDROID_PRIORITY_LOWEST))) { |
Henri Chataing | 6bdb5f8 | 2023-11-01 17:25:37 +0000 | [diff] [blame] | 193 | return Errorf("process priority value must be range {} - {}", |
| 194 | static_cast<int>(ANDROID_PRIORITY_HIGHEST), |
| 195 | static_cast<int>(ANDROID_PRIORITY_LOWEST)); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 196 | } |
| 197 | return {}; |
| 198 | } |
| 199 | |
| 200 | Result<void> ServiceParser::ParseInterface(std::vector<std::string>&& args) { |
| 201 | const std::string& interface_name = args[1]; |
| 202 | const std::string& instance_name = args[2]; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 203 | const std::string fullname = interface_name + "/" + instance_name; |
| 204 | |
| 205 | for (const auto& svc : *service_list_) { |
Alexander Koskovich | e5f0520 | 2022-03-06 15:51:51 -0700 | [diff] [blame] | 206 | if (svc->interfaces().count(fullname) > 0 && !service_->is_override()) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 207 | return Error() << "Interface '" << fullname << "' redefined in " << service_->name() |
| 208 | << " but is already defined by " << svc->name(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | service_->interfaces_.insert(fullname); |
| 213 | |
| 214 | return {}; |
| 215 | } |
| 216 | |
| 217 | Result<void> ServiceParser::ParseIoprio(std::vector<std::string>&& args) { |
| 218 | if (!ParseInt(args[2], &service_->proc_attr_.ioprio_pri, 0, 7)) { |
| 219 | return Error() << "priority value must be range 0 - 7"; |
| 220 | } |
| 221 | |
| 222 | if (args[1] == "rt") { |
| 223 | service_->proc_attr_.ioprio_class = IoSchedClass_RT; |
| 224 | } else if (args[1] == "be") { |
| 225 | service_->proc_attr_.ioprio_class = IoSchedClass_BE; |
| 226 | } else if (args[1] == "idle") { |
| 227 | service_->proc_attr_.ioprio_class = IoSchedClass_IDLE; |
| 228 | } else { |
| 229 | return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>"; |
| 230 | } |
| 231 | |
| 232 | return {}; |
| 233 | } |
| 234 | |
| 235 | Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) { |
| 236 | auto it = args.begin() + 1; |
| 237 | if (args.size() == 2 && StartsWith(args[1], "$")) { |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 238 | auto expanded = ExpandProps(args[1]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 239 | if (!expanded.ok()) { |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 240 | return expanded.error(); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // If the property is not set, it defaults to none, in which case there are no keycodes |
| 244 | // for this service. |
Bernie Innocenti | 1cc76df | 2020-02-03 23:54:02 +0900 | [diff] [blame] | 245 | if (*expanded == "none") { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 246 | return {}; |
| 247 | } |
| 248 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 249 | args = Split(*expanded, ","); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 250 | it = args.begin(); |
| 251 | } |
| 252 | |
| 253 | for (; it != args.end(); ++it) { |
| 254 | int code; |
| 255 | if (ParseInt(*it, &code, 0, KEY_MAX)) { |
| 256 | for (auto& key : service_->keycodes_) { |
| 257 | if (key == code) return Error() << "duplicate keycode: " << *it; |
| 258 | } |
| 259 | service_->keycodes_.insert( |
| 260 | std::upper_bound(service_->keycodes_.begin(), service_->keycodes_.end(), code), |
| 261 | code); |
| 262 | } else { |
| 263 | return Error() << "invalid keycode: " << *it; |
| 264 | } |
| 265 | } |
| 266 | return {}; |
| 267 | } |
| 268 | |
| 269 | Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) { |
| 270 | service_->flags_ |= SVC_ONESHOT; |
| 271 | return {}; |
| 272 | } |
| 273 | |
| 274 | Result<void> ServiceParser::ParseOnrestart(std::vector<std::string>&& args) { |
| 275 | args.erase(args.begin()); |
| 276 | int line = service_->onrestart_.NumCommands() + 1; |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 277 | if (auto result = service_->onrestart_.AddCommand(std::move(args), line); !result.ok()) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 278 | return Error() << "cannot add Onrestart command: " << result.error(); |
| 279 | } |
| 280 | return {}; |
| 281 | } |
| 282 | |
| 283 | Result<void> ServiceParser::ParseNamespace(std::vector<std::string>&& args) { |
| 284 | for (size_t i = 1; i < args.size(); i++) { |
| 285 | if (args[i] == "pid") { |
| 286 | service_->namespaces_.flags |= CLONE_NEWPID; |
| 287 | // PID namespaces require mount namespaces. |
| 288 | service_->namespaces_.flags |= CLONE_NEWNS; |
| 289 | } else if (args[i] == "mnt") { |
| 290 | service_->namespaces_.flags |= CLONE_NEWNS; |
| 291 | } else { |
| 292 | return Error() << "namespace must be 'pid' or 'mnt'"; |
| 293 | } |
| 294 | } |
| 295 | return {}; |
| 296 | } |
| 297 | |
| 298 | Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) { |
Suren Baghdasaryan | c29c2ba | 2019-10-22 17:18:42 -0700 | [diff] [blame] | 299 | if (!ParseInt(args[1], &service_->oom_score_adjust_, MIN_OOM_SCORE_ADJUST, |
| 300 | MAX_OOM_SCORE_ADJUST)) { |
| 301 | return Error() << "oom_score_adjust value must be in range " << MIN_OOM_SCORE_ADJUST |
| 302 | << " - +" << MAX_OOM_SCORE_ADJUST; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 303 | } |
| 304 | return {}; |
| 305 | } |
| 306 | |
| 307 | Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) { |
| 308 | service_->override_ = true; |
| 309 | return {}; |
| 310 | } |
| 311 | |
| 312 | Result<void> ServiceParser::ParseMemcgSwappiness(std::vector<std::string>&& args) { |
| 313 | if (!ParseInt(args[1], &service_->swappiness_, 0)) { |
| 314 | return Error() << "swappiness value must be equal or greater than 0"; |
| 315 | } |
| 316 | return {}; |
| 317 | } |
| 318 | |
| 319 | Result<void> ServiceParser::ParseMemcgLimitInBytes(std::vector<std::string>&& args) { |
| 320 | if (!ParseInt(args[1], &service_->limit_in_bytes_, 0)) { |
| 321 | return Error() << "limit_in_bytes value must be equal or greater than 0"; |
| 322 | } |
| 323 | return {}; |
| 324 | } |
| 325 | |
| 326 | Result<void> ServiceParser::ParseMemcgLimitPercent(std::vector<std::string>&& args) { |
| 327 | if (!ParseInt(args[1], &service_->limit_percent_, 0)) { |
| 328 | return Error() << "limit_percent value must be equal or greater than 0"; |
| 329 | } |
| 330 | return {}; |
| 331 | } |
| 332 | |
| 333 | Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) { |
| 334 | service_->limit_property_ = std::move(args[1]); |
| 335 | return {}; |
| 336 | } |
| 337 | |
| 338 | Result<void> ServiceParser::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) { |
| 339 | if (!ParseInt(args[1], &service_->soft_limit_in_bytes_, 0)) { |
| 340 | return Error() << "soft_limit_in_bytes value must be equal or greater than 0"; |
| 341 | } |
| 342 | return {}; |
| 343 | } |
| 344 | |
| 345 | Result<void> ServiceParser::ParseProcessRlimit(std::vector<std::string>&& args) { |
| 346 | auto rlimit = ParseRlimit(args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 347 | if (!rlimit.ok()) return rlimit.error(); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 348 | |
| 349 | service_->proc_attr_.rlimits.emplace_back(*rlimit); |
| 350 | return {}; |
| 351 | } |
| 352 | |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 353 | Result<void> ServiceParser::ParseRebootOnFailure(std::vector<std::string>&& args) { |
| 354 | if (service_->on_failure_reboot_target_) { |
| 355 | return Error() << "Only one reboot_on_failure command may be specified"; |
| 356 | } |
| 357 | if (!StartsWith(args[1], "shutdown") && !StartsWith(args[1], "reboot")) { |
| 358 | return Error() |
| 359 | << "reboot_on_failure commands must begin with either 'shutdown' or 'reboot'"; |
| 360 | } |
| 361 | service_->on_failure_reboot_target_ = std::move(args[1]); |
| 362 | return {}; |
| 363 | } |
| 364 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 365 | Result<void> ServiceParser::ParseRestartPeriod(std::vector<std::string>&& args) { |
| 366 | int period; |
Jiyong Park | 0d277d7 | 2023-06-09 09:52:49 +0900 | [diff] [blame] | 367 | if (!ParseInt(args[1], &period, 0)) { |
| 368 | return Error() << "restart_period value must be an integer >= 0"; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 369 | } |
| 370 | service_->restart_period_ = std::chrono::seconds(period); |
| 371 | return {}; |
| 372 | } |
| 373 | |
| 374 | Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) { |
| 375 | service_->seclabel_ = std::move(args[1]); |
| 376 | return {}; |
| 377 | } |
| 378 | |
| 379 | Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) { |
| 380 | service_->sigstop_ = true; |
| 381 | return {}; |
| 382 | } |
| 383 | |
| 384 | Result<void> ServiceParser::ParseSetenv(std::vector<std::string>&& args) { |
| 385 | service_->environment_vars_.emplace_back(std::move(args[1]), std::move(args[2])); |
| 386 | return {}; |
| 387 | } |
| 388 | |
| 389 | Result<void> ServiceParser::ParseShutdown(std::vector<std::string>&& args) { |
| 390 | if (args[1] == "critical") { |
| 391 | service_->flags_ |= SVC_SHUTDOWN_CRITICAL; |
| 392 | return {}; |
| 393 | } |
| 394 | return Error() << "Invalid shutdown option"; |
| 395 | } |
| 396 | |
Suren Baghdasaryan | c9c0bba | 2020-04-30 11:58:39 -0700 | [diff] [blame] | 397 | Result<void> ServiceParser::ParseTaskProfiles(std::vector<std::string>&& args) { |
| 398 | args.erase(args.begin()); |
Suren Baghdasaryan | 746ede9 | 2022-03-31 21:15:11 +0000 | [diff] [blame] | 399 | if (service_->task_profiles_.empty()) { |
| 400 | service_->task_profiles_ = std::move(args); |
| 401 | } else { |
| 402 | // Some task profiles might have been added during writepid conversions |
| 403 | service_->task_profiles_.insert(service_->task_profiles_.end(), |
| 404 | std::make_move_iterator(args.begin()), |
| 405 | std::make_move_iterator(args.end())); |
| 406 | args.clear(); |
| 407 | } |
Suren Baghdasaryan | c9c0bba | 2020-04-30 11:58:39 -0700 | [diff] [blame] | 408 | return {}; |
| 409 | } |
| 410 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 411 | Result<void> ServiceParser::ParseTimeoutPeriod(std::vector<std::string>&& args) { |
| 412 | int period; |
| 413 | if (!ParseInt(args[1], &period, 1)) { |
| 414 | return Error() << "timeout_period value must be an integer >= 1"; |
| 415 | } |
| 416 | service_->timeout_period_ = std::chrono::seconds(period); |
| 417 | return {}; |
| 418 | } |
| 419 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 420 | // name type perm [ uid gid context ] |
| 421 | Result<void> ServiceParser::ParseSocket(std::vector<std::string>&& args) { |
| 422 | SocketDescriptor socket; |
| 423 | socket.name = std::move(args[1]); |
| 424 | |
| 425 | auto types = Split(args[2], "+"); |
| 426 | if (types[0] == "stream") { |
| 427 | socket.type = SOCK_STREAM; |
| 428 | } else if (types[0] == "dgram") { |
| 429 | socket.type = SOCK_DGRAM; |
| 430 | } else if (types[0] == "seqpacket") { |
| 431 | socket.type = SOCK_SEQPACKET; |
| 432 | } else { |
| 433 | return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket', got '" << types[0] |
| 434 | << "' instead."; |
| 435 | } |
| 436 | |
Adam Langley | ecc14a5 | 2022-05-11 22:32:47 +0000 | [diff] [blame] | 437 | for (size_t i = 1; i < types.size(); i++) { |
| 438 | if (types[i] == "passcred") { |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 439 | socket.passcred = true; |
Adam Langley | ecc14a5 | 2022-05-11 22:32:47 +0000 | [diff] [blame] | 440 | } else if (types[i] == "listen") { |
| 441 | socket.listen = true; |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 442 | } else { |
Adam Langley | ecc14a5 | 2022-05-11 22:32:47 +0000 | [diff] [blame] | 443 | return Error() << "Unknown socket type decoration '" << types[i] |
| 444 | << "'. Known values are ['passcred', 'listen']"; |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | errno = 0; |
| 449 | char* end = nullptr; |
| 450 | socket.perm = strtol(args[3].c_str(), &end, 8); |
| 451 | if (errno != 0) { |
| 452 | return ErrnoError() << "Unable to parse permissions '" << args[3] << "'"; |
| 453 | } |
| 454 | if (end == args[3].c_str() || *end != '\0') { |
| 455 | errno = EINVAL; |
| 456 | return ErrnoError() << "Unable to parse permissions '" << args[3] << "'"; |
| 457 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 458 | |
| 459 | if (args.size() > 4) { |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 460 | auto uid = DecodeUid(args[4]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 461 | if (!uid.ok()) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 462 | return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error(); |
| 463 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 464 | socket.uid = *uid; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | if (args.size() > 5) { |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 468 | auto gid = DecodeUid(args[5]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 469 | if (!gid.ok()) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 470 | return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error(); |
| 471 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 472 | socket.gid = *gid; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 475 | socket.context = args.size() > 6 ? args[6] : ""; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 476 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 477 | auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(), |
| 478 | [&socket](const auto& other) { return socket.name == other.name; }); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 479 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 480 | if (old != service_->sockets_.end()) { |
| 481 | return Error() << "duplicate socket descriptor '" << socket.name << "'"; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 484 | service_->sockets_.emplace_back(std::move(socket)); |
| 485 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 486 | return {}; |
| 487 | } |
| 488 | |
Tom Cherry | f74b7f5 | 2019-09-23 16:16:54 -0700 | [diff] [blame] | 489 | Result<void> ServiceParser::ParseStdioToKmsg(std::vector<std::string>&& args) { |
| 490 | if (service_->flags_ & SVC_CONSOLE) { |
| 491 | return Error() << "'stdio_to_kmsg' and 'console' are mutually exclusive"; |
| 492 | } |
| 493 | service_->proc_attr_.stdio_to_kmsg = true; |
| 494 | return {}; |
| 495 | } |
| 496 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 497 | // name type |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 498 | Result<void> ServiceParser::ParseFile(std::vector<std::string>&& args) { |
| 499 | if (args[2] != "r" && args[2] != "w" && args[2] != "rw") { |
| 500 | return Error() << "file type must be 'r', 'w' or 'rw'"; |
| 501 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 502 | |
| 503 | FileDescriptor file; |
| 504 | file.type = args[2]; |
| 505 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 506 | auto file_name = ExpandProps(args[1]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 507 | if (!file_name.ok()) { |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 508 | return Error() << "Could not expand file path ': " << file_name.error(); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 509 | } |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 510 | file.name = *file_name; |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 511 | if (file.name[0] != '/' || file.name.find("../") != std::string::npos) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 512 | return Error() << "file name must not be relative"; |
| 513 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 514 | |
| 515 | auto old = std::find_if(service_->files_.begin(), service_->files_.end(), |
| 516 | [&file](const auto& other) { return other.name == file.name; }); |
| 517 | |
| 518 | if (old != service_->files_.end()) { |
| 519 | return Error() << "duplicate file descriptor '" << file.name << "'"; |
| 520 | } |
| 521 | |
| 522 | service_->files_.emplace_back(std::move(file)); |
| 523 | |
| 524 | return {}; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) { |
| 528 | auto uid = DecodeUid(args[1]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 529 | if (!uid.ok()) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 530 | return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error(); |
| 531 | } |
Steven Moreland | f5d22ef | 2023-04-03 23:29:22 +0000 | [diff] [blame] | 532 | service_->proc_attr_.parsed_uid = *uid; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 533 | return {}; |
| 534 | } |
| 535 | |
Suren Baghdasaryan | 746ede9 | 2022-03-31 21:15:11 +0000 | [diff] [blame] | 536 | // Convert legacy paths used to migrate processes between cgroups using writepid command. |
| 537 | // We can't get these paths from TaskProfiles because profile definitions are changing |
| 538 | // when we migrate to cgroups v2 while these hardcoded paths stay the same. |
| 539 | static std::optional<const std::string> ConvertTaskFileToProfile(const std::string& file) { |
| 540 | static const std::map<const std::string, const std::string> map = { |
| 541 | {"/dev/stune/top-app/tasks", "MaxPerformance"}, |
| 542 | {"/dev/stune/foreground/tasks", "HighPerformance"}, |
| 543 | {"/dev/cpuset/camera-daemon/tasks", "CameraServiceCapacity"}, |
| 544 | {"/dev/cpuset/foreground/tasks", "ProcessCapacityHigh"}, |
| 545 | {"/dev/cpuset/system-background/tasks", "ServiceCapacityLow"}, |
| 546 | {"/dev/stune/nnapi-hal/tasks", "NNApiHALPerformance"}, |
| 547 | {"/dev/blkio/background/tasks", "LowIoPriority"}, |
| 548 | }; |
| 549 | auto iter = map.find(file); |
| 550 | return iter == map.end() ? std::nullopt : std::make_optional<const std::string>(iter->second); |
| 551 | } |
| 552 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 553 | Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) { |
| 554 | args.erase(args.begin()); |
Suren Baghdasaryan | 746ede9 | 2022-03-31 21:15:11 +0000 | [diff] [blame] | 555 | // Convert any cgroup writes into appropriate task_profiles |
| 556 | for (auto iter = args.begin(); iter != args.end();) { |
| 557 | auto task_profile = ConvertTaskFileToProfile(*iter); |
| 558 | if (task_profile) { |
| 559 | LOG(WARNING) << "'writepid " << *iter << "' is converted into 'task_profiles " |
| 560 | << task_profile.value() << "' for service " << service_->name(); |
| 561 | service_->task_profiles_.push_back(task_profile.value()); |
| 562 | iter = args.erase(iter); |
| 563 | } else { |
| 564 | ++iter; |
| 565 | } |
| 566 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 567 | service_->writepid_files_ = std::move(args); |
| 568 | return {}; |
| 569 | } |
| 570 | |
| 571 | Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) { |
| 572 | service_->updatable_ = true; |
| 573 | return {}; |
| 574 | } |
| 575 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 576 | const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 577 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
| 578 | // clang-format off |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 579 | static const KeywordMap<ServiceParser::OptionParser> parser_map = { |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 580 | {"capabilities", {0, kMax, &ServiceParser::ParseCapabilities}}, |
| 581 | {"class", {1, kMax, &ServiceParser::ParseClass}}, |
| 582 | {"console", {0, 1, &ServiceParser::ParseConsole}}, |
Woody Lin | 45215ae | 2019-12-26 22:22:28 +0800 | [diff] [blame] | 583 | {"critical", {0, 2, &ServiceParser::ParseCritical}}, |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 584 | {"disabled", {0, 0, &ServiceParser::ParseDisabled}}, |
| 585 | {"enter_namespace", {2, 2, &ServiceParser::ParseEnterNamespace}}, |
| 586 | {"file", {2, 2, &ServiceParser::ParseFile}}, |
Daniel Rosenberg | de76688 | 2022-12-01 15:44:31 -0800 | [diff] [blame] | 587 | {"gentle_kill", {0, 0, &ServiceParser::ParseGentleKill}}, |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 588 | {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}}, |
| 589 | {"interface", {2, 2, &ServiceParser::ParseInterface}}, |
| 590 | {"ioprio", {2, 2, &ServiceParser::ParseIoprio}}, |
| 591 | {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}}, |
| 592 | {"memcg.limit_in_bytes", {1, 1, &ServiceParser::ParseMemcgLimitInBytes}}, |
| 593 | {"memcg.limit_percent", {1, 1, &ServiceParser::ParseMemcgLimitPercent}}, |
| 594 | {"memcg.limit_property", {1, 1, &ServiceParser::ParseMemcgLimitProperty}}, |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 595 | {"memcg.soft_limit_in_bytes", |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 596 | {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}}, |
| 597 | {"memcg.swappiness", {1, 1, &ServiceParser::ParseMemcgSwappiness}}, |
| 598 | {"namespace", {1, 2, &ServiceParser::ParseNamespace}}, |
| 599 | {"oneshot", {0, 0, &ServiceParser::ParseOneshot}}, |
| 600 | {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}}, |
| 601 | {"oom_score_adjust", {1, 1, &ServiceParser::ParseOomScoreAdjust}}, |
| 602 | {"override", {0, 0, &ServiceParser::ParseOverride}}, |
| 603 | {"priority", {1, 1, &ServiceParser::ParsePriority}}, |
| 604 | {"reboot_on_failure", {1, 1, &ServiceParser::ParseRebootOnFailure}}, |
| 605 | {"restart_period", {1, 1, &ServiceParser::ParseRestartPeriod}}, |
| 606 | {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}}, |
| 607 | {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}}, |
| 608 | {"setenv", {2, 2, &ServiceParser::ParseSetenv}}, |
| 609 | {"shutdown", {1, 1, &ServiceParser::ParseShutdown}}, |
| 610 | {"sigstop", {0, 0, &ServiceParser::ParseSigstop}}, |
| 611 | {"socket", {3, 6, &ServiceParser::ParseSocket}}, |
Tom Cherry | f74b7f5 | 2019-09-23 16:16:54 -0700 | [diff] [blame] | 612 | {"stdio_to_kmsg", {0, 0, &ServiceParser::ParseStdioToKmsg}}, |
Suren Baghdasaryan | c9c0bba | 2020-04-30 11:58:39 -0700 | [diff] [blame] | 613 | {"task_profiles", {1, kMax, &ServiceParser::ParseTaskProfiles}}, |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 614 | {"timeout_period", {1, 1, &ServiceParser::ParseTimeoutPeriod}}, |
| 615 | {"updatable", {0, 0, &ServiceParser::ParseUpdatable}}, |
| 616 | {"user", {1, 1, &ServiceParser::ParseUser}}, |
| 617 | {"writepid", {1, kMax, &ServiceParser::ParseWritepid}}, |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 618 | }; |
| 619 | // clang-format on |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 620 | return parser_map; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 623 | Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args, |
| 624 | const std::string& filename, int line) { |
| 625 | if (args.size() < 3) { |
| 626 | return Error() << "services must have a name and a program"; |
| 627 | } |
| 628 | |
| 629 | const std::string& name = args[1]; |
| 630 | if (!IsValidName(name)) { |
| 631 | return Error() << "invalid service name '" << name << "'"; |
| 632 | } |
| 633 | |
| 634 | filename_ = filename; |
| 635 | |
| 636 | Subcontext* restart_action_subcontext = nullptr; |
Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 637 | if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) { |
| 638 | restart_action_subcontext = subcontext_; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | std::vector<std::string> str_args(args.begin() + 2, args.end()); |
| 642 | |
| 643 | if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) { |
| 644 | if (str_args[0] == "/sbin/watchdogd") { |
| 645 | str_args[0] = "/system/bin/watchdogd"; |
| 646 | } |
| 647 | } |
Yifan Hong | 8fb7f77 | 2019-10-16 14:22:12 -0700 | [diff] [blame] | 648 | if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_Q__) { |
| 649 | if (str_args[0] == "/charger") { |
| 650 | str_args[0] = "/system/bin/charger"; |
| 651 | } |
| 652 | } |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 653 | |
Deyao Ren | df40ed1 | 2022-07-14 22:51:10 +0000 | [diff] [blame] | 654 | service_ = std::make_unique<Service>(name, restart_action_subcontext, filename, str_args); |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 655 | return {}; |
| 656 | } |
| 657 | |
| 658 | Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 659 | if (!service_) { |
| 660 | return {}; |
| 661 | } |
| 662 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 663 | auto parser = GetParserMap().Find(args); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 664 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 665 | if (!parser.ok()) return parser.error(); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 666 | |
| 667 | return std::invoke(*parser, this, std::move(args)); |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | Result<void> ServiceParser::EndSection() { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 671 | if (!service_) { |
| 672 | return {}; |
| 673 | } |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 674 | |
Steven Moreland | f5d22ef | 2023-04-03 23:29:22 +0000 | [diff] [blame] | 675 | if (service_->proc_attr_.parsed_uid == std::nullopt) { |
Steven Moreland | 81a1b3e | 2024-05-20 22:31:11 +0000 | [diff] [blame] | 676 | if (kAlwaysErrorUserRoot || |
| 677 | android::base::GetIntProperty("ro.vendor.api_level", 0) > 202404) { |
Steven Moreland | e534919 | 2023-04-24 23:54:59 +0000 | [diff] [blame] | 678 | return Error() << "No user specified for service '" << service_->name() |
Steven Moreland | a8a2c5a | 2024-05-20 21:56:19 +0000 | [diff] [blame] | 679 | << "', so it would have been root."; |
Steven Moreland | e534919 | 2023-04-24 23:54:59 +0000 | [diff] [blame] | 680 | } else { |
| 681 | LOG(WARNING) << "No user specified for service '" << service_->name() |
Steven Moreland | a8a2c5a | 2024-05-20 21:56:19 +0000 | [diff] [blame] | 682 | << "', so it is root."; |
Steven Moreland | e534919 | 2023-04-24 23:54:59 +0000 | [diff] [blame] | 683 | } |
Steven Moreland | f5d22ef | 2023-04-03 23:29:22 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Nikita Ioffe | 51c251c | 2020-04-30 19:40:39 +0100 | [diff] [blame] | 686 | if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) { |
| 687 | if ((service_->flags() & SVC_CRITICAL) != 0 && (service_->flags() & SVC_ONESHOT) != 0) { |
| 688 | return Error() << "service '" << service_->name() |
| 689 | << "' can't be both critical and oneshot"; |
| 690 | } |
| 691 | } |
| 692 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 693 | Service* old_service = service_list_->FindService(service_->name()); |
| 694 | if (old_service) { |
| 695 | if (!service_->is_override()) { |
| 696 | return Error() << "ignored duplicate definition of service '" << service_->name() |
| 697 | << "'"; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 700 | if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) { |
| 701 | return Error() << "cannot update a non-updatable service '" << service_->name() |
| 702 | << "' with a config in APEX"; |
| 703 | } |
| 704 | |
Daniel Norman | f597fa5 | 2020-11-09 17:28:24 -0800 | [diff] [blame] | 705 | std::string context = service_->subcontext() ? service_->subcontext()->context() : ""; |
| 706 | std::string old_context = |
| 707 | old_service->subcontext() ? old_service->subcontext()->context() : ""; |
| 708 | if (context != old_context) { |
| 709 | return Error() << "service '" << service_->name() << "' overrides another service " |
| 710 | << "across the treble boundary."; |
| 711 | } |
| 712 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 713 | service_list_->RemoveService(*old_service); |
| 714 | old_service = nullptr; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 717 | service_list_->AddService(std::move(service_)); |
| 718 | |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 719 | return {}; |
| 720 | } |
| 721 | |
| 722 | bool ServiceParser::IsValidName(const std::string& name) const { |
| 723 | // Property names can be any length, but may only contain certain characters. |
| 724 | // Property values can contain any characters, but may only be a certain length. |
| 725 | // (The latter restriction is needed because `start` and `stop` work by writing |
| 726 | // the service name to the "ctl.start" and "ctl.stop" properties.) |
| 727 | return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX; |
| 728 | } |
| 729 | |
| 730 | } // namespace init |
| 731 | } // namespace android |