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