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