blob: 0fbbeb828a9d7f5145f5906b508de1129a7642ab [file] [log] [blame]
Tom Cherry2aeb1ad2019-06-26 10:46:20 -07001/*
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 Cherryb1ffb1d2019-06-26 11:22:52 -070019#include <linux/input.h>
Tom Cherry2e4c85f2019-07-09 13:33:36 -070020#include <stdlib.h>
21#include <sys/socket.h>
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070022
Daniel Norman3f42a762019-07-09 11:00:53 -070023#include <algorithm>
24#include <sstream>
25
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070026#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 Cherry2e4c85f2019-07-09 13:33:36 -070033#include "service_utils.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070034#include "util.h"
35
36#if defined(__ANDROID__)
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070037#include <android/api-level.h>
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070038#include <sys/system_properties.h>
39
40#include "selinux.h"
41#else
42#include "host_init_stubs.h"
43#endif
44
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070045using android::base::ParseInt;
46using android::base::Split;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070047using android::base::StartsWith;
48
49namespace android {
50namespace init {
51
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070052Result<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
80Result<void> ServiceParser::ParseClass(std::vector<std::string>&& args) {
81 service_->classnames_ = std::set<std::string>(args.begin() + 1, args.end());
82 return {};
83}
84
85Result<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
91Result<void> ServiceParser::ParseCritical(std::vector<std::string>&& args) {
92 service_->flags_ |= SVC_CRITICAL;
93 return {};
94}
95
96Result<void> ServiceParser::ParseDisabled(std::vector<std::string>&& args) {
97 service_->flags_ |= SVC_DISABLED;
98 service_->flags_ |= SVC_RC_DISABLED;
99 return {};
100}
101
102Result<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
116Result<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
133Result<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
144Result<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
175Result<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
193Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) {
194 auto it = args.begin() + 1;
195 if (args.size() == 2 && StartsWith(args[1], "$")) {
196 std::string expanded;
197 if (!expand_props(args[1], &expanded)) {
198 return Error() << "Could not expand property '" << args[1] << "'";
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
207 args = Split(expanded, ",");
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
227Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) {
228 service_->flags_ |= SVC_ONESHOT;
229 return {};
230}
231
232Result<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
241Result<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
256Result<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
263Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) {
264 service_->override_ = true;
265 return {};
266}
267
268Result<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
275Result<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
282Result<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
289Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) {
290 service_->limit_property_ = std::move(args[1]);
291 return {};
292}
293
294Result<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
301Result<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
309Result<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
318Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) {
319 service_->seclabel_ = std::move(args[1]);
320 return {};
321}
322
323Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) {
324 service_->sigstop_ = true;
325 return {};
326}
327
328Result<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
333Result<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
341Result<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 Cherry2e4c85f2019-07-09 13:33:36 -0700350// name type perm [ uid gid context ]
351Result<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 Cherryb1ffb1d2019-06-26 11:22:52 -0700385
386 if (args.size() > 4) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700387 auto uid = DecodeUid(args[4]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700388 if (!uid) {
389 return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
390 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700391 socket.uid = *uid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700392 }
393
394 if (args.size() > 5) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700395 auto gid = DecodeUid(args[5]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700396 if (!gid) {
397 return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
398 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700399 socket.gid = *gid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700400 }
401
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700402 socket.context = args.size() > 6 ? args[6] : "";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700403
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700404 auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(),
405 [&socket](const auto& other) { return socket.name == other.name; });
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700406
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700407 if (old != service_->sockets_.end()) {
408 return Error() << "duplicate socket descriptor '" << socket.name << "'";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700409 }
410
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700411 service_->sockets_.emplace_back(std::move(socket));
412
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700413 return {};
414}
415
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700416// name type
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700417Result<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 Cherry2e4c85f2019-07-09 13:33:36 -0700421
422 FileDescriptor file;
423 file.type = args[2];
424
425 if (!expand_props(args[1], &file.name)) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700426 return Error() << "Could not expand property in file path '" << args[1] << "'";
427 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700428 if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700429 return Error() << "file name must not be relative";
430 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700431
432 auto old = std::find_if(service_->files_.begin(), service_->files_.end(),
433 [&file](const auto& other) { return other.name == file.name; });
434
435 if (old != service_->files_.end()) {
436 return Error() << "duplicate file descriptor '" << file.name << "'";
437 }
438
439 service_->files_.emplace_back(std::move(file));
440
441 return {};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700442}
443
444Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) {
445 auto uid = DecodeUid(args[1]);
446 if (!uid) {
447 return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
448 }
449 service_->proc_attr_.uid = *uid;
450 return {};
451}
452
453Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) {
454 args.erase(args.begin());
455 service_->writepid_files_ = std::move(args);
456 return {};
457}
458
459Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) {
460 service_->updatable_ = true;
461 return {};
462}
463
464class ServiceParser::OptionParserMap : public KeywordMap<OptionParser> {
465 public:
466 OptionParserMap() {}
467
468 private:
469 const Map& map() const override;
470};
471
472const ServiceParser::OptionParserMap::Map& ServiceParser::OptionParserMap::map() const {
473 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
474 // clang-format off
475 static const Map option_parsers = {
476 {"capabilities",
477 {0, kMax, &ServiceParser::ParseCapabilities}},
478 {"class", {1, kMax, &ServiceParser::ParseClass}},
479 {"console", {0, 1, &ServiceParser::ParseConsole}},
480 {"critical", {0, 0, &ServiceParser::ParseCritical}},
481 {"disabled", {0, 0, &ServiceParser::ParseDisabled}},
482 {"enter_namespace",
483 {2, 2, &ServiceParser::ParseEnterNamespace}},
484 {"file", {2, 2, &ServiceParser::ParseFile}},
485 {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
486 {"interface", {2, 2, &ServiceParser::ParseInterface}},
487 {"ioprio", {2, 2, &ServiceParser::ParseIoprio}},
488 {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}},
489 {"memcg.limit_in_bytes",
490 {1, 1, &ServiceParser::ParseMemcgLimitInBytes}},
491 {"memcg.limit_percent",
492 {1, 1, &ServiceParser::ParseMemcgLimitPercent}},
493 {"memcg.limit_property",
494 {1, 1, &ServiceParser::ParseMemcgLimitProperty}},
495 {"memcg.soft_limit_in_bytes",
496 {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}},
497 {"memcg.swappiness",
498 {1, 1, &ServiceParser::ParseMemcgSwappiness}},
499 {"namespace", {1, 2, &ServiceParser::ParseNamespace}},
500 {"oneshot", {0, 0, &ServiceParser::ParseOneshot}},
501 {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}},
502 {"oom_score_adjust",
503 {1, 1, &ServiceParser::ParseOomScoreAdjust}},
504 {"override", {0, 0, &ServiceParser::ParseOverride}},
505 {"priority", {1, 1, &ServiceParser::ParsePriority}},
506 {"restart_period",
507 {1, 1, &ServiceParser::ParseRestartPeriod}},
508 {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}},
509 {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}},
510 {"setenv", {2, 2, &ServiceParser::ParseSetenv}},
511 {"shutdown", {1, 1, &ServiceParser::ParseShutdown}},
512 {"sigstop", {0, 0, &ServiceParser::ParseSigstop}},
513 {"socket", {3, 6, &ServiceParser::ParseSocket}},
514 {"timeout_period",
515 {1, 1, &ServiceParser::ParseTimeoutPeriod}},
516 {"updatable", {0, 0, &ServiceParser::ParseUpdatable}},
517 {"user", {1, 1, &ServiceParser::ParseUser}},
518 {"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
519 };
520 // clang-format on
521 return option_parsers;
522}
523
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700524Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
525 const std::string& filename, int line) {
526 if (args.size() < 3) {
527 return Error() << "services must have a name and a program";
528 }
529
530 const std::string& name = args[1];
531 if (!IsValidName(name)) {
532 return Error() << "invalid service name '" << name << "'";
533 }
534
535 filename_ = filename;
536
537 Subcontext* restart_action_subcontext = nullptr;
538 if (subcontexts_) {
539 for (auto& subcontext : *subcontexts_) {
540 if (StartsWith(filename, subcontext.path_prefix())) {
541 restart_action_subcontext = &subcontext;
542 break;
543 }
544 }
545 }
546
547 std::vector<std::string> str_args(args.begin() + 2, args.end());
548
549 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
550 if (str_args[0] == "/sbin/watchdogd") {
551 str_args[0] = "/system/bin/watchdogd";
552 }
553 }
554
555 service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args);
556 return {};
557}
558
559Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700560 if (!service_) {
561 return {};
562 }
563
564 static const OptionParserMap parser_map;
565 auto parser = parser_map.FindFunction(args);
566
567 if (!parser) return parser.error();
568
569 return std::invoke(*parser, this, std::move(args));
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700570}
571
572Result<void> ServiceParser::EndSection() {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700573 if (!service_) {
574 return {};
575 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700576
Daniel Norman3f42a762019-07-09 11:00:53 -0700577 if (interface_inheritance_hierarchy_) {
578 std::set<std::string> interface_names;
579 for (const std::string& intf : service_->interfaces()) {
580 interface_names.insert(Split(intf, "/")[0]);
581 }
582 std::ostringstream error_stream;
583 for (const std::string& intf : interface_names) {
584 if (interface_inheritance_hierarchy_->count(intf) == 0) {
585 error_stream << "\nInterface is not in the known set of hidl_interfaces: '" << intf
586 << "'. Please ensure the interface is spelled correctly and built "
587 << "by a hidl_interface target.";
588 continue;
589 }
590 const std::set<std::string>& required_interfaces =
591 (*interface_inheritance_hierarchy_)[intf];
592 std::set<std::string> diff;
593 std::set_difference(required_interfaces.begin(), required_interfaces.end(),
594 interface_names.begin(), interface_names.end(),
595 std::inserter(diff, diff.begin()));
596 if (!diff.empty()) {
597 error_stream << "\nInterface '" << intf << "' requires its full inheritance "
598 << "hierarchy to be listed in this init_rc file. Missing "
599 << "interfaces: [" << base::Join(diff, " ") << "]";
600 }
601 }
602 const std::string& errors = error_stream.str();
603 if (!errors.empty()) {
604 return Error() << errors;
605 }
606 }
607
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700608 Service* old_service = service_list_->FindService(service_->name());
609 if (old_service) {
610 if (!service_->is_override()) {
611 return Error() << "ignored duplicate definition of service '" << service_->name()
612 << "'";
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700613 }
614
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700615 if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
616 return Error() << "cannot update a non-updatable service '" << service_->name()
617 << "' with a config in APEX";
618 }
619
620 service_list_->RemoveService(*old_service);
621 old_service = nullptr;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700622 }
623
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700624 service_list_->AddService(std::move(service_));
625
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700626 return {};
627}
628
629bool ServiceParser::IsValidName(const std::string& name) const {
630 // Property names can be any length, but may only contain certain characters.
631 // Property values can contain any characters, but may only be a certain length.
632 // (The latter restriction is needed because `start` and `stop` work by writing
633 // the service name to the "ctl.start" and "ctl.stop" properties.)
634 return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
635}
636
637} // namespace init
638} // namespace android