blob: 543be2329a7c788cb809dd048a8488bd86e34e61 [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
Jon Spivack16fb3f92019-07-26 13:14:42 -0700148 // AIDL services don't use fully qualified names and instead just use "interface aidl <name>"
149 if (interface_name != "aidl") {
150 FQName fq_name;
151 if (!FQName::parse(interface_name, &fq_name)) {
152 return Error() << "Invalid fully-qualified name for interface '" << interface_name
153 << "'";
154 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700155
Jon Spivack16fb3f92019-07-26 13:14:42 -0700156 if (!fq_name.isFullyQualified()) {
157 return Error() << "Interface name not fully-qualified '" << interface_name << "'";
158 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700159
Jon Spivack16fb3f92019-07-26 13:14:42 -0700160 if (fq_name.isValidValueName()) {
161 return Error() << "Interface name must not be a value name '" << interface_name << "'";
162 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700163 }
164
165 const std::string fullname = interface_name + "/" + instance_name;
166
167 for (const auto& svc : *service_list_) {
168 if (svc->interfaces().count(fullname) > 0) {
169 return Error() << "Interface '" << fullname << "' redefined in " << service_->name()
170 << " but is already defined by " << svc->name();
171 }
172 }
173
174 service_->interfaces_.insert(fullname);
175
176 return {};
177}
178
179Result<void> ServiceParser::ParseIoprio(std::vector<std::string>&& args) {
180 if (!ParseInt(args[2], &service_->proc_attr_.ioprio_pri, 0, 7)) {
181 return Error() << "priority value must be range 0 - 7";
182 }
183
184 if (args[1] == "rt") {
185 service_->proc_attr_.ioprio_class = IoSchedClass_RT;
186 } else if (args[1] == "be") {
187 service_->proc_attr_.ioprio_class = IoSchedClass_BE;
188 } else if (args[1] == "idle") {
189 service_->proc_attr_.ioprio_class = IoSchedClass_IDLE;
190 } else {
191 return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>";
192 }
193
194 return {};
195}
196
197Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) {
198 auto it = args.begin() + 1;
199 if (args.size() == 2 && StartsWith(args[1], "$")) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700200 auto expanded = ExpandProps(args[1]);
201 if (!expanded) {
202 return expanded.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700203 }
204
205 // If the property is not set, it defaults to none, in which case there are no keycodes
206 // for this service.
207 if (expanded == "none") {
208 return {};
209 }
210
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700211 args = Split(*expanded, ",");
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700212 it = args.begin();
213 }
214
215 for (; it != args.end(); ++it) {
216 int code;
217 if (ParseInt(*it, &code, 0, KEY_MAX)) {
218 for (auto& key : service_->keycodes_) {
219 if (key == code) return Error() << "duplicate keycode: " << *it;
220 }
221 service_->keycodes_.insert(
222 std::upper_bound(service_->keycodes_.begin(), service_->keycodes_.end(), code),
223 code);
224 } else {
225 return Error() << "invalid keycode: " << *it;
226 }
227 }
228 return {};
229}
230
231Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) {
232 service_->flags_ |= SVC_ONESHOT;
233 return {};
234}
235
236Result<void> ServiceParser::ParseOnrestart(std::vector<std::string>&& args) {
237 args.erase(args.begin());
238 int line = service_->onrestart_.NumCommands() + 1;
239 if (auto result = service_->onrestart_.AddCommand(std::move(args), line); !result) {
240 return Error() << "cannot add Onrestart command: " << result.error();
241 }
242 return {};
243}
244
245Result<void> ServiceParser::ParseNamespace(std::vector<std::string>&& args) {
246 for (size_t i = 1; i < args.size(); i++) {
247 if (args[i] == "pid") {
248 service_->namespaces_.flags |= CLONE_NEWPID;
249 // PID namespaces require mount namespaces.
250 service_->namespaces_.flags |= CLONE_NEWNS;
251 } else if (args[i] == "mnt") {
252 service_->namespaces_.flags |= CLONE_NEWNS;
253 } else {
254 return Error() << "namespace must be 'pid' or 'mnt'";
255 }
256 }
257 return {};
258}
259
260Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) {
261 if (!ParseInt(args[1], &service_->oom_score_adjust_, -1000, 1000)) {
262 return Error() << "oom_score_adjust value must be in range -1000 - +1000";
263 }
264 return {};
265}
266
267Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) {
268 service_->override_ = true;
269 return {};
270}
271
272Result<void> ServiceParser::ParseMemcgSwappiness(std::vector<std::string>&& args) {
273 if (!ParseInt(args[1], &service_->swappiness_, 0)) {
274 return Error() << "swappiness value must be equal or greater than 0";
275 }
276 return {};
277}
278
279Result<void> ServiceParser::ParseMemcgLimitInBytes(std::vector<std::string>&& args) {
280 if (!ParseInt(args[1], &service_->limit_in_bytes_, 0)) {
281 return Error() << "limit_in_bytes value must be equal or greater than 0";
282 }
283 return {};
284}
285
286Result<void> ServiceParser::ParseMemcgLimitPercent(std::vector<std::string>&& args) {
287 if (!ParseInt(args[1], &service_->limit_percent_, 0)) {
288 return Error() << "limit_percent value must be equal or greater than 0";
289 }
290 return {};
291}
292
293Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) {
294 service_->limit_property_ = std::move(args[1]);
295 return {};
296}
297
298Result<void> ServiceParser::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) {
299 if (!ParseInt(args[1], &service_->soft_limit_in_bytes_, 0)) {
300 return Error() << "soft_limit_in_bytes value must be equal or greater than 0";
301 }
302 return {};
303}
304
305Result<void> ServiceParser::ParseProcessRlimit(std::vector<std::string>&& args) {
306 auto rlimit = ParseRlimit(args);
307 if (!rlimit) return rlimit.error();
308
309 service_->proc_attr_.rlimits.emplace_back(*rlimit);
310 return {};
311}
312
313Result<void> ServiceParser::ParseRestartPeriod(std::vector<std::string>&& args) {
314 int period;
315 if (!ParseInt(args[1], &period, 5)) {
316 return Error() << "restart_period value must be an integer >= 5";
317 }
318 service_->restart_period_ = std::chrono::seconds(period);
319 return {};
320}
321
322Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) {
323 service_->seclabel_ = std::move(args[1]);
324 return {};
325}
326
327Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) {
328 service_->sigstop_ = true;
329 return {};
330}
331
332Result<void> ServiceParser::ParseSetenv(std::vector<std::string>&& args) {
333 service_->environment_vars_.emplace_back(std::move(args[1]), std::move(args[2]));
334 return {};
335}
336
337Result<void> ServiceParser::ParseShutdown(std::vector<std::string>&& args) {
338 if (args[1] == "critical") {
339 service_->flags_ |= SVC_SHUTDOWN_CRITICAL;
340 return {};
341 }
342 return Error() << "Invalid shutdown option";
343}
344
345Result<void> ServiceParser::ParseTimeoutPeriod(std::vector<std::string>&& args) {
346 int period;
347 if (!ParseInt(args[1], &period, 1)) {
348 return Error() << "timeout_period value must be an integer >= 1";
349 }
350 service_->timeout_period_ = std::chrono::seconds(period);
351 return {};
352}
353
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700354// name type perm [ uid gid context ]
355Result<void> ServiceParser::ParseSocket(std::vector<std::string>&& args) {
356 SocketDescriptor socket;
357 socket.name = std::move(args[1]);
358
359 auto types = Split(args[2], "+");
360 if (types[0] == "stream") {
361 socket.type = SOCK_STREAM;
362 } else if (types[0] == "dgram") {
363 socket.type = SOCK_DGRAM;
364 } else if (types[0] == "seqpacket") {
365 socket.type = SOCK_SEQPACKET;
366 } else {
367 return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket', got '" << types[0]
368 << "' instead.";
369 }
370
371 if (types.size() > 1) {
372 if (types.size() == 2 && types[1] == "passcred") {
373 socket.passcred = true;
374 } else {
375 return Error() << "Only 'passcred' may be used to modify the socket type";
376 }
377 }
378
379 errno = 0;
380 char* end = nullptr;
381 socket.perm = strtol(args[3].c_str(), &end, 8);
382 if (errno != 0) {
383 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
384 }
385 if (end == args[3].c_str() || *end != '\0') {
386 errno = EINVAL;
387 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
388 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700389
390 if (args.size() > 4) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700391 auto uid = DecodeUid(args[4]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700392 if (!uid) {
393 return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
394 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700395 socket.uid = *uid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700396 }
397
398 if (args.size() > 5) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700399 auto gid = DecodeUid(args[5]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700400 if (!gid) {
401 return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
402 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700403 socket.gid = *gid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700404 }
405
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700406 socket.context = args.size() > 6 ? args[6] : "";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700407
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700408 auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(),
409 [&socket](const auto& other) { return socket.name == other.name; });
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700410
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700411 if (old != service_->sockets_.end()) {
412 return Error() << "duplicate socket descriptor '" << socket.name << "'";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700413 }
414
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700415 service_->sockets_.emplace_back(std::move(socket));
416
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700417 return {};
418}
419
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700420// name type
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700421Result<void> ServiceParser::ParseFile(std::vector<std::string>&& args) {
422 if (args[2] != "r" && args[2] != "w" && args[2] != "rw") {
423 return Error() << "file type must be 'r', 'w' or 'rw'";
424 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700425
426 FileDescriptor file;
427 file.type = args[2];
428
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700429 auto file_name = ExpandProps(args[1]);
430 if (!file_name) {
431 return Error() << "Could not expand file path ': " << file_name.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700432 }
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700433 file.name = *file_name;
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700434 if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700435 return Error() << "file name must not be relative";
436 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700437
438 auto old = std::find_if(service_->files_.begin(), service_->files_.end(),
439 [&file](const auto& other) { return other.name == file.name; });
440
441 if (old != service_->files_.end()) {
442 return Error() << "duplicate file descriptor '" << file.name << "'";
443 }
444
445 service_->files_.emplace_back(std::move(file));
446
447 return {};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700448}
449
450Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) {
451 auto uid = DecodeUid(args[1]);
452 if (!uid) {
453 return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
454 }
455 service_->proc_attr_.uid = *uid;
456 return {};
457}
458
459Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) {
460 args.erase(args.begin());
461 service_->writepid_files_ = std::move(args);
462 return {};
463}
464
465Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) {
466 service_->updatable_ = true;
467 return {};
468}
469
Tom Cherryd52a5b32019-07-22 16:05:36 -0700470const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700471 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
472 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -0700473 static const KeywordMap<ServiceParser::OptionParser> parser_map = {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700474 {"capabilities",
475 {0, kMax, &ServiceParser::ParseCapabilities}},
476 {"class", {1, kMax, &ServiceParser::ParseClass}},
477 {"console", {0, 1, &ServiceParser::ParseConsole}},
478 {"critical", {0, 0, &ServiceParser::ParseCritical}},
479 {"disabled", {0, 0, &ServiceParser::ParseDisabled}},
480 {"enter_namespace",
481 {2, 2, &ServiceParser::ParseEnterNamespace}},
482 {"file", {2, 2, &ServiceParser::ParseFile}},
483 {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
484 {"interface", {2, 2, &ServiceParser::ParseInterface}},
485 {"ioprio", {2, 2, &ServiceParser::ParseIoprio}},
486 {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}},
487 {"memcg.limit_in_bytes",
488 {1, 1, &ServiceParser::ParseMemcgLimitInBytes}},
489 {"memcg.limit_percent",
490 {1, 1, &ServiceParser::ParseMemcgLimitPercent}},
491 {"memcg.limit_property",
492 {1, 1, &ServiceParser::ParseMemcgLimitProperty}},
493 {"memcg.soft_limit_in_bytes",
494 {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}},
495 {"memcg.swappiness",
496 {1, 1, &ServiceParser::ParseMemcgSwappiness}},
497 {"namespace", {1, 2, &ServiceParser::ParseNamespace}},
498 {"oneshot", {0, 0, &ServiceParser::ParseOneshot}},
499 {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}},
500 {"oom_score_adjust",
501 {1, 1, &ServiceParser::ParseOomScoreAdjust}},
502 {"override", {0, 0, &ServiceParser::ParseOverride}},
503 {"priority", {1, 1, &ServiceParser::ParsePriority}},
504 {"restart_period",
505 {1, 1, &ServiceParser::ParseRestartPeriod}},
506 {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}},
507 {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}},
508 {"setenv", {2, 2, &ServiceParser::ParseSetenv}},
509 {"shutdown", {1, 1, &ServiceParser::ParseShutdown}},
510 {"sigstop", {0, 0, &ServiceParser::ParseSigstop}},
511 {"socket", {3, 6, &ServiceParser::ParseSocket}},
512 {"timeout_period",
513 {1, 1, &ServiceParser::ParseTimeoutPeriod}},
514 {"updatable", {0, 0, &ServiceParser::ParseUpdatable}},
515 {"user", {1, 1, &ServiceParser::ParseUser}},
516 {"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
517 };
518 // clang-format on
Tom Cherryd52a5b32019-07-22 16:05:36 -0700519 return parser_map;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700520}
521
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700522Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
523 const std::string& filename, int line) {
524 if (args.size() < 3) {
525 return Error() << "services must have a name and a program";
526 }
527
528 const std::string& name = args[1];
529 if (!IsValidName(name)) {
530 return Error() << "invalid service name '" << name << "'";
531 }
532
533 filename_ = filename;
534
535 Subcontext* restart_action_subcontext = nullptr;
536 if (subcontexts_) {
537 for (auto& subcontext : *subcontexts_) {
538 if (StartsWith(filename, subcontext.path_prefix())) {
539 restart_action_subcontext = &subcontext;
540 break;
541 }
542 }
543 }
544
545 std::vector<std::string> str_args(args.begin() + 2, args.end());
546
547 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
548 if (str_args[0] == "/sbin/watchdogd") {
549 str_args[0] = "/system/bin/watchdogd";
550 }
551 }
552
553 service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args);
554 return {};
555}
556
557Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700558 if (!service_) {
559 return {};
560 }
561
Tom Cherryd52a5b32019-07-22 16:05:36 -0700562 auto parser = GetParserMap().Find(args);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700563
564 if (!parser) return parser.error();
565
566 return std::invoke(*parser, this, std::move(args));
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700567}
568
569Result<void> ServiceParser::EndSection() {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700570 if (!service_) {
571 return {};
572 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700573
Daniel Norman3f42a762019-07-09 11:00:53 -0700574 if (interface_inheritance_hierarchy_) {
Daniel Normand2533c32019-08-02 15:13:50 -0700575 if (const auto& check_hierarchy_result = CheckInterfaceInheritanceHierarchy(
576 service_->interfaces(), *interface_inheritance_hierarchy_);
577 !check_hierarchy_result) {
578 return Error() << check_hierarchy_result.error();
Daniel Norman3f42a762019-07-09 11:00:53 -0700579 }
580 }
581
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700582 Service* old_service = service_list_->FindService(service_->name());
583 if (old_service) {
584 if (!service_->is_override()) {
585 return Error() << "ignored duplicate definition of service '" << service_->name()
586 << "'";
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700587 }
588
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700589 if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
590 return Error() << "cannot update a non-updatable service '" << service_->name()
591 << "' with a config in APEX";
592 }
593
594 service_list_->RemoveService(*old_service);
595 old_service = nullptr;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700596 }
597
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700598 service_list_->AddService(std::move(service_));
599
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700600 return {};
601}
602
603bool ServiceParser::IsValidName(const std::string& name) const {
604 // Property names can be any length, but may only contain certain characters.
605 // Property values can contain any characters, but may only be a certain length.
606 // (The latter restriction is needed because `start` and `stop` work by writing
607 // the service name to the "ctl.start" and "ctl.stop" properties.)
608 return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
609}
610
611} // namespace init
612} // namespace android