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> |
| 28 | #include <android-base/strings.h> |
| 29 | #include <hidl-util/FQName.h> |
| 30 | #include <system/thread_defs.h> |
| 31 | |
| 32 | #include "rlimit_parser.h" |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 33 | #include "service_utils.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 34 | #include "util.h" |
| 35 | |
| 36 | #if defined(__ANDROID__) |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 37 | #include <android/api-level.h> |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 38 | #include <sys/system_properties.h> |
| 39 | |
| 40 | #include "selinux.h" |
| 41 | #else |
| 42 | #include "host_init_stubs.h" |
| 43 | #endif |
| 44 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 45 | using android::base::ParseInt; |
| 46 | using android::base::Split; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 47 | using android::base::StartsWith; |
| 48 | |
| 49 | namespace android { |
| 50 | namespace init { |
| 51 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 52 | Result<void> ServiceParser::ParseCapabilities(std::vector<std::string>&& args) { |
| 53 | service_->capabilities_ = 0; |
| 54 | |
| 55 | if (!CapAmbientSupported()) { |
| 56 | return Error() |
| 57 | << "capabilities requested but the kernel does not support ambient capabilities"; |
| 58 | } |
| 59 | |
| 60 | unsigned int last_valid_cap = GetLastValidCap(); |
| 61 | if (last_valid_cap >= service_->capabilities_->size()) { |
| 62 | LOG(WARNING) << "last valid run-time capability is larger than CAP_LAST_CAP"; |
| 63 | } |
| 64 | |
| 65 | for (size_t i = 1; i < args.size(); i++) { |
| 66 | const std::string& arg = args[i]; |
| 67 | int res = LookupCap(arg); |
| 68 | if (res < 0) { |
| 69 | return Errorf("invalid capability '{}'", arg); |
| 70 | } |
| 71 | unsigned int cap = static_cast<unsigned int>(res); // |res| is >= 0. |
| 72 | if (cap > last_valid_cap) { |
| 73 | return Errorf("capability '{}' not supported by the kernel", arg); |
| 74 | } |
| 75 | (*service_->capabilities_)[cap] = true; |
| 76 | } |
| 77 | return {}; |
| 78 | } |
| 79 | |
| 80 | Result<void> ServiceParser::ParseClass(std::vector<std::string>&& args) { |
| 81 | service_->classnames_ = std::set<std::string>(args.begin() + 1, args.end()); |
| 82 | return {}; |
| 83 | } |
| 84 | |
| 85 | Result<void> ServiceParser::ParseConsole(std::vector<std::string>&& args) { |
Tom Cherry | f74b7f5 | 2019-09-23 16:16:54 -0700 | [diff] [blame] | 86 | if (service_->proc_attr_.stdio_to_kmsg) { |
| 87 | return Error() << "'console' and 'stdio_to_kmsg' are mutually exclusive"; |
| 88 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 89 | service_->flags_ |= SVC_CONSOLE; |
| 90 | service_->proc_attr_.console = args.size() > 1 ? "/dev/" + args[1] : ""; |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | Result<void> ServiceParser::ParseCritical(std::vector<std::string>&& args) { |
| 95 | service_->flags_ |= SVC_CRITICAL; |
| 96 | return {}; |
| 97 | } |
| 98 | |
| 99 | Result<void> ServiceParser::ParseDisabled(std::vector<std::string>&& args) { |
| 100 | service_->flags_ |= SVC_DISABLED; |
| 101 | service_->flags_ |= SVC_RC_DISABLED; |
| 102 | return {}; |
| 103 | } |
| 104 | |
| 105 | Result<void> ServiceParser::ParseEnterNamespace(std::vector<std::string>&& args) { |
| 106 | if (args[1] != "net") { |
| 107 | return Error() << "Init only supports entering network namespaces"; |
| 108 | } |
| 109 | if (!service_->namespaces_.namespaces_to_enter.empty()) { |
| 110 | return Error() << "Only one network namespace may be entered"; |
| 111 | } |
| 112 | // Network namespaces require that /sys is remounted, otherwise the old adapters will still be |
| 113 | // present. Therefore, they also require mount namespaces. |
| 114 | service_->namespaces_.flags |= CLONE_NEWNS; |
| 115 | service_->namespaces_.namespaces_to_enter.emplace_back(CLONE_NEWNET, std::move(args[2])); |
| 116 | return {}; |
| 117 | } |
| 118 | |
| 119 | Result<void> ServiceParser::ParseGroup(std::vector<std::string>&& args) { |
| 120 | auto gid = DecodeUid(args[1]); |
| 121 | if (!gid) { |
| 122 | return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error(); |
| 123 | } |
| 124 | service_->proc_attr_.gid = *gid; |
| 125 | |
| 126 | for (std::size_t n = 2; n < args.size(); n++) { |
| 127 | gid = DecodeUid(args[n]); |
| 128 | if (!gid) { |
| 129 | return Error() << "Unable to decode GID for '" << args[n] << "': " << gid.error(); |
| 130 | } |
| 131 | service_->proc_attr_.supp_gids.emplace_back(*gid); |
| 132 | } |
| 133 | return {}; |
| 134 | } |
| 135 | |
| 136 | Result<void> ServiceParser::ParsePriority(std::vector<std::string>&& args) { |
| 137 | service_->proc_attr_.priority = 0; |
| 138 | if (!ParseInt(args[1], &service_->proc_attr_.priority, |
| 139 | static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative |
| 140 | static_cast<int>(ANDROID_PRIORITY_LOWEST))) { |
| 141 | return Errorf("process priority value must be range {} - {}", ANDROID_PRIORITY_HIGHEST, |
| 142 | ANDROID_PRIORITY_LOWEST); |
| 143 | } |
| 144 | return {}; |
| 145 | } |
| 146 | |
| 147 | Result<void> ServiceParser::ParseInterface(std::vector<std::string>&& args) { |
| 148 | const std::string& interface_name = args[1]; |
| 149 | const std::string& instance_name = args[2]; |
| 150 | |
Jon Spivack | 16fb3f9 | 2019-07-26 13:14:42 -0700 | [diff] [blame] | 151 | // AIDL services don't use fully qualified names and instead just use "interface aidl <name>" |
| 152 | if (interface_name != "aidl") { |
| 153 | FQName fq_name; |
| 154 | if (!FQName::parse(interface_name, &fq_name)) { |
| 155 | return Error() << "Invalid fully-qualified name for interface '" << interface_name |
| 156 | << "'"; |
| 157 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 158 | |
Jon Spivack | 16fb3f9 | 2019-07-26 13:14:42 -0700 | [diff] [blame] | 159 | if (!fq_name.isFullyQualified()) { |
| 160 | return Error() << "Interface name not fully-qualified '" << interface_name << "'"; |
| 161 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 162 | |
Jon Spivack | 16fb3f9 | 2019-07-26 13:14:42 -0700 | [diff] [blame] | 163 | if (fq_name.isValidValueName()) { |
| 164 | return Error() << "Interface name must not be a value name '" << interface_name << "'"; |
| 165 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | const std::string fullname = interface_name + "/" + instance_name; |
| 169 | |
| 170 | for (const auto& svc : *service_list_) { |
| 171 | if (svc->interfaces().count(fullname) > 0) { |
| 172 | return Error() << "Interface '" << fullname << "' redefined in " << service_->name() |
| 173 | << " but is already defined by " << svc->name(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | service_->interfaces_.insert(fullname); |
| 178 | |
| 179 | return {}; |
| 180 | } |
| 181 | |
| 182 | Result<void> ServiceParser::ParseIoprio(std::vector<std::string>&& args) { |
| 183 | if (!ParseInt(args[2], &service_->proc_attr_.ioprio_pri, 0, 7)) { |
| 184 | return Error() << "priority value must be range 0 - 7"; |
| 185 | } |
| 186 | |
| 187 | if (args[1] == "rt") { |
| 188 | service_->proc_attr_.ioprio_class = IoSchedClass_RT; |
| 189 | } else if (args[1] == "be") { |
| 190 | service_->proc_attr_.ioprio_class = IoSchedClass_BE; |
| 191 | } else if (args[1] == "idle") { |
| 192 | service_->proc_attr_.ioprio_class = IoSchedClass_IDLE; |
| 193 | } else { |
| 194 | return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>"; |
| 195 | } |
| 196 | |
| 197 | return {}; |
| 198 | } |
| 199 | |
| 200 | Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) { |
| 201 | auto it = args.begin() + 1; |
| 202 | if (args.size() == 2 && StartsWith(args[1], "$")) { |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 203 | auto expanded = ExpandProps(args[1]); |
| 204 | if (!expanded) { |
| 205 | return expanded.error(); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // If the property is not set, it defaults to none, in which case there are no keycodes |
| 209 | // for this service. |
| 210 | if (expanded == "none") { |
| 211 | return {}; |
| 212 | } |
| 213 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 214 | args = Split(*expanded, ","); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 215 | it = args.begin(); |
| 216 | } |
| 217 | |
| 218 | for (; it != args.end(); ++it) { |
| 219 | int code; |
| 220 | if (ParseInt(*it, &code, 0, KEY_MAX)) { |
| 221 | for (auto& key : service_->keycodes_) { |
| 222 | if (key == code) return Error() << "duplicate keycode: " << *it; |
| 223 | } |
| 224 | service_->keycodes_.insert( |
| 225 | std::upper_bound(service_->keycodes_.begin(), service_->keycodes_.end(), code), |
| 226 | code); |
| 227 | } else { |
| 228 | return Error() << "invalid keycode: " << *it; |
| 229 | } |
| 230 | } |
| 231 | return {}; |
| 232 | } |
| 233 | |
| 234 | Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) { |
| 235 | service_->flags_ |= SVC_ONESHOT; |
| 236 | return {}; |
| 237 | } |
| 238 | |
| 239 | Result<void> ServiceParser::ParseOnrestart(std::vector<std::string>&& args) { |
| 240 | args.erase(args.begin()); |
| 241 | int line = service_->onrestart_.NumCommands() + 1; |
| 242 | if (auto result = service_->onrestart_.AddCommand(std::move(args), line); !result) { |
| 243 | return Error() << "cannot add Onrestart command: " << result.error(); |
| 244 | } |
| 245 | return {}; |
| 246 | } |
| 247 | |
| 248 | Result<void> ServiceParser::ParseNamespace(std::vector<std::string>&& args) { |
| 249 | for (size_t i = 1; i < args.size(); i++) { |
| 250 | if (args[i] == "pid") { |
| 251 | service_->namespaces_.flags |= CLONE_NEWPID; |
| 252 | // PID namespaces require mount namespaces. |
| 253 | service_->namespaces_.flags |= CLONE_NEWNS; |
| 254 | } else if (args[i] == "mnt") { |
| 255 | service_->namespaces_.flags |= CLONE_NEWNS; |
| 256 | } else { |
| 257 | return Error() << "namespace must be 'pid' or 'mnt'"; |
| 258 | } |
| 259 | } |
| 260 | return {}; |
| 261 | } |
| 262 | |
| 263 | Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) { |
| 264 | if (!ParseInt(args[1], &service_->oom_score_adjust_, -1000, 1000)) { |
| 265 | return Error() << "oom_score_adjust value must be in range -1000 - +1000"; |
| 266 | } |
| 267 | return {}; |
| 268 | } |
| 269 | |
| 270 | Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) { |
| 271 | service_->override_ = true; |
| 272 | return {}; |
| 273 | } |
| 274 | |
| 275 | Result<void> ServiceParser::ParseMemcgSwappiness(std::vector<std::string>&& args) { |
| 276 | if (!ParseInt(args[1], &service_->swappiness_, 0)) { |
| 277 | return Error() << "swappiness value must be equal or greater than 0"; |
| 278 | } |
| 279 | return {}; |
| 280 | } |
| 281 | |
| 282 | Result<void> ServiceParser::ParseMemcgLimitInBytes(std::vector<std::string>&& args) { |
| 283 | if (!ParseInt(args[1], &service_->limit_in_bytes_, 0)) { |
| 284 | return Error() << "limit_in_bytes value must be equal or greater than 0"; |
| 285 | } |
| 286 | return {}; |
| 287 | } |
| 288 | |
| 289 | Result<void> ServiceParser::ParseMemcgLimitPercent(std::vector<std::string>&& args) { |
| 290 | if (!ParseInt(args[1], &service_->limit_percent_, 0)) { |
| 291 | return Error() << "limit_percent value must be equal or greater than 0"; |
| 292 | } |
| 293 | return {}; |
| 294 | } |
| 295 | |
| 296 | Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) { |
| 297 | service_->limit_property_ = std::move(args[1]); |
| 298 | return {}; |
| 299 | } |
| 300 | |
| 301 | Result<void> ServiceParser::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) { |
| 302 | if (!ParseInt(args[1], &service_->soft_limit_in_bytes_, 0)) { |
| 303 | return Error() << "soft_limit_in_bytes value must be equal or greater than 0"; |
| 304 | } |
| 305 | return {}; |
| 306 | } |
| 307 | |
| 308 | Result<void> ServiceParser::ParseProcessRlimit(std::vector<std::string>&& args) { |
| 309 | auto rlimit = ParseRlimit(args); |
| 310 | if (!rlimit) return rlimit.error(); |
| 311 | |
| 312 | service_->proc_attr_.rlimits.emplace_back(*rlimit); |
| 313 | return {}; |
| 314 | } |
| 315 | |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 316 | Result<void> ServiceParser::ParseRebootOnFailure(std::vector<std::string>&& args) { |
| 317 | if (service_->on_failure_reboot_target_) { |
| 318 | return Error() << "Only one reboot_on_failure command may be specified"; |
| 319 | } |
| 320 | if (!StartsWith(args[1], "shutdown") && !StartsWith(args[1], "reboot")) { |
| 321 | return Error() |
| 322 | << "reboot_on_failure commands must begin with either 'shutdown' or 'reboot'"; |
| 323 | } |
| 324 | service_->on_failure_reboot_target_ = std::move(args[1]); |
| 325 | return {}; |
| 326 | } |
| 327 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 328 | Result<void> ServiceParser::ParseRestartPeriod(std::vector<std::string>&& args) { |
| 329 | int period; |
| 330 | if (!ParseInt(args[1], &period, 5)) { |
| 331 | return Error() << "restart_period value must be an integer >= 5"; |
| 332 | } |
| 333 | service_->restart_period_ = std::chrono::seconds(period); |
| 334 | return {}; |
| 335 | } |
| 336 | |
| 337 | Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) { |
| 338 | service_->seclabel_ = std::move(args[1]); |
| 339 | return {}; |
| 340 | } |
| 341 | |
| 342 | Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) { |
| 343 | service_->sigstop_ = true; |
| 344 | return {}; |
| 345 | } |
| 346 | |
| 347 | Result<void> ServiceParser::ParseSetenv(std::vector<std::string>&& args) { |
| 348 | service_->environment_vars_.emplace_back(std::move(args[1]), std::move(args[2])); |
| 349 | return {}; |
| 350 | } |
| 351 | |
| 352 | Result<void> ServiceParser::ParseShutdown(std::vector<std::string>&& args) { |
| 353 | if (args[1] == "critical") { |
| 354 | service_->flags_ |= SVC_SHUTDOWN_CRITICAL; |
| 355 | return {}; |
| 356 | } |
| 357 | return Error() << "Invalid shutdown option"; |
| 358 | } |
| 359 | |
| 360 | Result<void> ServiceParser::ParseTimeoutPeriod(std::vector<std::string>&& args) { |
| 361 | int period; |
| 362 | if (!ParseInt(args[1], &period, 1)) { |
| 363 | return Error() << "timeout_period value must be an integer >= 1"; |
| 364 | } |
| 365 | service_->timeout_period_ = std::chrono::seconds(period); |
| 366 | return {}; |
| 367 | } |
| 368 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 369 | // name type perm [ uid gid context ] |
| 370 | Result<void> ServiceParser::ParseSocket(std::vector<std::string>&& args) { |
| 371 | SocketDescriptor socket; |
| 372 | socket.name = std::move(args[1]); |
| 373 | |
| 374 | auto types = Split(args[2], "+"); |
| 375 | if (types[0] == "stream") { |
| 376 | socket.type = SOCK_STREAM; |
| 377 | } else if (types[0] == "dgram") { |
| 378 | socket.type = SOCK_DGRAM; |
| 379 | } else if (types[0] == "seqpacket") { |
| 380 | socket.type = SOCK_SEQPACKET; |
| 381 | } else { |
| 382 | return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket', got '" << types[0] |
| 383 | << "' instead."; |
| 384 | } |
| 385 | |
| 386 | if (types.size() > 1) { |
| 387 | if (types.size() == 2 && types[1] == "passcred") { |
| 388 | socket.passcred = true; |
| 389 | } else { |
| 390 | return Error() << "Only 'passcred' may be used to modify the socket type"; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | errno = 0; |
| 395 | char* end = nullptr; |
| 396 | socket.perm = strtol(args[3].c_str(), &end, 8); |
| 397 | if (errno != 0) { |
| 398 | return ErrnoError() << "Unable to parse permissions '" << args[3] << "'"; |
| 399 | } |
| 400 | if (end == args[3].c_str() || *end != '\0') { |
| 401 | errno = EINVAL; |
| 402 | return ErrnoError() << "Unable to parse permissions '" << args[3] << "'"; |
| 403 | } |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 404 | |
| 405 | if (args.size() > 4) { |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 406 | auto uid = DecodeUid(args[4]); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 407 | if (!uid) { |
| 408 | return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error(); |
| 409 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 410 | socket.uid = *uid; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | if (args.size() > 5) { |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 414 | auto gid = DecodeUid(args[5]); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 415 | if (!gid) { |
| 416 | return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error(); |
| 417 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 418 | socket.gid = *gid; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 421 | socket.context = args.size() > 6 ? args[6] : ""; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 422 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 423 | auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(), |
| 424 | [&socket](const auto& other) { return socket.name == other.name; }); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 425 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 426 | if (old != service_->sockets_.end()) { |
| 427 | return Error() << "duplicate socket descriptor '" << socket.name << "'"; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 430 | service_->sockets_.emplace_back(std::move(socket)); |
| 431 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 432 | return {}; |
| 433 | } |
| 434 | |
Tom Cherry | f74b7f5 | 2019-09-23 16:16:54 -0700 | [diff] [blame] | 435 | Result<void> ServiceParser::ParseStdioToKmsg(std::vector<std::string>&& args) { |
| 436 | if (service_->flags_ & SVC_CONSOLE) { |
| 437 | return Error() << "'stdio_to_kmsg' and 'console' are mutually exclusive"; |
| 438 | } |
| 439 | service_->proc_attr_.stdio_to_kmsg = true; |
| 440 | return {}; |
| 441 | } |
| 442 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 443 | // name type |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 444 | Result<void> ServiceParser::ParseFile(std::vector<std::string>&& args) { |
| 445 | if (args[2] != "r" && args[2] != "w" && args[2] != "rw") { |
| 446 | return Error() << "file type must be 'r', 'w' or 'rw'"; |
| 447 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 448 | |
| 449 | FileDescriptor file; |
| 450 | file.type = args[2]; |
| 451 | |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 452 | auto file_name = ExpandProps(args[1]); |
| 453 | if (!file_name) { |
| 454 | return Error() << "Could not expand file path ': " << file_name.error(); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 455 | } |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 456 | file.name = *file_name; |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 457 | if (file.name[0] != '/' || file.name.find("../") != std::string::npos) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 458 | return Error() << "file name must not be relative"; |
| 459 | } |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 460 | |
| 461 | auto old = std::find_if(service_->files_.begin(), service_->files_.end(), |
| 462 | [&file](const auto& other) { return other.name == file.name; }); |
| 463 | |
| 464 | if (old != service_->files_.end()) { |
| 465 | return Error() << "duplicate file descriptor '" << file.name << "'"; |
| 466 | } |
| 467 | |
| 468 | service_->files_.emplace_back(std::move(file)); |
| 469 | |
| 470 | return {}; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) { |
| 474 | auto uid = DecodeUid(args[1]); |
| 475 | if (!uid) { |
| 476 | return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error(); |
| 477 | } |
| 478 | service_->proc_attr_.uid = *uid; |
| 479 | return {}; |
| 480 | } |
| 481 | |
| 482 | Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) { |
| 483 | args.erase(args.begin()); |
| 484 | service_->writepid_files_ = std::move(args); |
| 485 | return {}; |
| 486 | } |
| 487 | |
| 488 | Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) { |
| 489 | service_->updatable_ = true; |
| 490 | return {}; |
| 491 | } |
| 492 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 493 | const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 494 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
| 495 | // clang-format off |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 496 | static const KeywordMap<ServiceParser::OptionParser> parser_map = { |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 497 | {"capabilities", {0, kMax, &ServiceParser::ParseCapabilities}}, |
| 498 | {"class", {1, kMax, &ServiceParser::ParseClass}}, |
| 499 | {"console", {0, 1, &ServiceParser::ParseConsole}}, |
| 500 | {"critical", {0, 0, &ServiceParser::ParseCritical}}, |
| 501 | {"disabled", {0, 0, &ServiceParser::ParseDisabled}}, |
| 502 | {"enter_namespace", {2, 2, &ServiceParser::ParseEnterNamespace}}, |
| 503 | {"file", {2, 2, &ServiceParser::ParseFile}}, |
| 504 | {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}}, |
| 505 | {"interface", {2, 2, &ServiceParser::ParseInterface}}, |
| 506 | {"ioprio", {2, 2, &ServiceParser::ParseIoprio}}, |
| 507 | {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}}, |
| 508 | {"memcg.limit_in_bytes", {1, 1, &ServiceParser::ParseMemcgLimitInBytes}}, |
| 509 | {"memcg.limit_percent", {1, 1, &ServiceParser::ParseMemcgLimitPercent}}, |
| 510 | {"memcg.limit_property", {1, 1, &ServiceParser::ParseMemcgLimitProperty}}, |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 511 | {"memcg.soft_limit_in_bytes", |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 512 | {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}}, |
| 513 | {"memcg.swappiness", {1, 1, &ServiceParser::ParseMemcgSwappiness}}, |
| 514 | {"namespace", {1, 2, &ServiceParser::ParseNamespace}}, |
| 515 | {"oneshot", {0, 0, &ServiceParser::ParseOneshot}}, |
| 516 | {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}}, |
| 517 | {"oom_score_adjust", {1, 1, &ServiceParser::ParseOomScoreAdjust}}, |
| 518 | {"override", {0, 0, &ServiceParser::ParseOverride}}, |
| 519 | {"priority", {1, 1, &ServiceParser::ParsePriority}}, |
| 520 | {"reboot_on_failure", {1, 1, &ServiceParser::ParseRebootOnFailure}}, |
| 521 | {"restart_period", {1, 1, &ServiceParser::ParseRestartPeriod}}, |
| 522 | {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}}, |
| 523 | {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}}, |
| 524 | {"setenv", {2, 2, &ServiceParser::ParseSetenv}}, |
| 525 | {"shutdown", {1, 1, &ServiceParser::ParseShutdown}}, |
| 526 | {"sigstop", {0, 0, &ServiceParser::ParseSigstop}}, |
| 527 | {"socket", {3, 6, &ServiceParser::ParseSocket}}, |
Tom Cherry | f74b7f5 | 2019-09-23 16:16:54 -0700 | [diff] [blame] | 528 | {"stdio_to_kmsg", {0, 0, &ServiceParser::ParseStdioToKmsg}}, |
Tom Cherry | 60971e6 | 2019-09-10 10:40:47 -0700 | [diff] [blame] | 529 | {"timeout_period", {1, 1, &ServiceParser::ParseTimeoutPeriod}}, |
| 530 | {"updatable", {0, 0, &ServiceParser::ParseUpdatable}}, |
| 531 | {"user", {1, 1, &ServiceParser::ParseUser}}, |
| 532 | {"writepid", {1, kMax, &ServiceParser::ParseWritepid}}, |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 533 | }; |
| 534 | // clang-format on |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 535 | return parser_map; |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 538 | Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args, |
| 539 | const std::string& filename, int line) { |
| 540 | if (args.size() < 3) { |
| 541 | return Error() << "services must have a name and a program"; |
| 542 | } |
| 543 | |
| 544 | const std::string& name = args[1]; |
| 545 | if (!IsValidName(name)) { |
| 546 | return Error() << "invalid service name '" << name << "'"; |
| 547 | } |
| 548 | |
| 549 | filename_ = filename; |
| 550 | |
| 551 | Subcontext* restart_action_subcontext = nullptr; |
Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 552 | if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) { |
| 553 | restart_action_subcontext = subcontext_; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | std::vector<std::string> str_args(args.begin() + 2, args.end()); |
| 557 | |
| 558 | if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) { |
| 559 | if (str_args[0] == "/sbin/watchdogd") { |
| 560 | str_args[0] = "/system/bin/watchdogd"; |
| 561 | } |
| 562 | } |
Yifan Hong | 8fb7f77 | 2019-10-16 14:22:12 -0700 | [diff] [blame^] | 563 | if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_Q__) { |
| 564 | if (str_args[0] == "/charger") { |
| 565 | str_args[0] = "/system/bin/charger"; |
| 566 | } |
| 567 | } |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 568 | |
| 569 | service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args); |
| 570 | return {}; |
| 571 | } |
| 572 | |
| 573 | Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 574 | if (!service_) { |
| 575 | return {}; |
| 576 | } |
| 577 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 578 | auto parser = GetParserMap().Find(args); |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 579 | |
| 580 | if (!parser) return parser.error(); |
| 581 | |
| 582 | return std::invoke(*parser, this, std::move(args)); |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | Result<void> ServiceParser::EndSection() { |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 586 | if (!service_) { |
| 587 | return {}; |
| 588 | } |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 589 | |
Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 590 | if (interface_inheritance_hierarchy_) { |
Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame] | 591 | if (const auto& check_hierarchy_result = CheckInterfaceInheritanceHierarchy( |
| 592 | service_->interfaces(), *interface_inheritance_hierarchy_); |
| 593 | !check_hierarchy_result) { |
| 594 | return Error() << check_hierarchy_result.error(); |
Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 598 | Service* old_service = service_list_->FindService(service_->name()); |
| 599 | if (old_service) { |
| 600 | if (!service_->is_override()) { |
| 601 | return Error() << "ignored duplicate definition of service '" << service_->name() |
| 602 | << "'"; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 605 | if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) { |
| 606 | return Error() << "cannot update a non-updatable service '" << service_->name() |
| 607 | << "' with a config in APEX"; |
| 608 | } |
| 609 | |
| 610 | service_list_->RemoveService(*old_service); |
| 611 | old_service = nullptr; |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 614 | service_list_->AddService(std::move(service_)); |
| 615 | |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 616 | return {}; |
| 617 | } |
| 618 | |
| 619 | bool ServiceParser::IsValidName(const std::string& name) const { |
| 620 | // Property names can be any length, but may only contain certain characters. |
| 621 | // Property values can contain any characters, but may only be a certain length. |
| 622 | // (The latter restriction is needed because `start` and `stop` work by writing |
| 623 | // the service name to the "ctl.start" and "ctl.stop" properties.) |
| 624 | return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX; |
| 625 | } |
| 626 | |
| 627 | } // namespace init |
| 628 | } // namespace android |