blob: 4322dc70af4459aba1619bd408e169b1a75b1c93 [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
Tom Cherry60971e62019-09-10 10:40:47 -0700313Result<void> ServiceParser::ParseRebootOnFailure(std::vector<std::string>&& args) {
314 if (service_->on_failure_reboot_target_) {
315 return Error() << "Only one reboot_on_failure command may be specified";
316 }
317 if (!StartsWith(args[1], "shutdown") && !StartsWith(args[1], "reboot")) {
318 return Error()
319 << "reboot_on_failure commands must begin with either 'shutdown' or 'reboot'";
320 }
321 service_->on_failure_reboot_target_ = std::move(args[1]);
322 return {};
323}
324
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700325Result<void> ServiceParser::ParseRestartPeriod(std::vector<std::string>&& args) {
326 int period;
327 if (!ParseInt(args[1], &period, 5)) {
328 return Error() << "restart_period value must be an integer >= 5";
329 }
330 service_->restart_period_ = std::chrono::seconds(period);
331 return {};
332}
333
334Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) {
335 service_->seclabel_ = std::move(args[1]);
336 return {};
337}
338
339Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) {
340 service_->sigstop_ = true;
341 return {};
342}
343
344Result<void> ServiceParser::ParseSetenv(std::vector<std::string>&& args) {
345 service_->environment_vars_.emplace_back(std::move(args[1]), std::move(args[2]));
346 return {};
347}
348
349Result<void> ServiceParser::ParseShutdown(std::vector<std::string>&& args) {
350 if (args[1] == "critical") {
351 service_->flags_ |= SVC_SHUTDOWN_CRITICAL;
352 return {};
353 }
354 return Error() << "Invalid shutdown option";
355}
356
357Result<void> ServiceParser::ParseTimeoutPeriod(std::vector<std::string>&& args) {
358 int period;
359 if (!ParseInt(args[1], &period, 1)) {
360 return Error() << "timeout_period value must be an integer >= 1";
361 }
362 service_->timeout_period_ = std::chrono::seconds(period);
363 return {};
364}
365
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700366// name type perm [ uid gid context ]
367Result<void> ServiceParser::ParseSocket(std::vector<std::string>&& args) {
368 SocketDescriptor socket;
369 socket.name = std::move(args[1]);
370
371 auto types = Split(args[2], "+");
372 if (types[0] == "stream") {
373 socket.type = SOCK_STREAM;
374 } else if (types[0] == "dgram") {
375 socket.type = SOCK_DGRAM;
376 } else if (types[0] == "seqpacket") {
377 socket.type = SOCK_SEQPACKET;
378 } else {
379 return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket', got '" << types[0]
380 << "' instead.";
381 }
382
383 if (types.size() > 1) {
384 if (types.size() == 2 && types[1] == "passcred") {
385 socket.passcred = true;
386 } else {
387 return Error() << "Only 'passcred' may be used to modify the socket type";
388 }
389 }
390
391 errno = 0;
392 char* end = nullptr;
393 socket.perm = strtol(args[3].c_str(), &end, 8);
394 if (errno != 0) {
395 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
396 }
397 if (end == args[3].c_str() || *end != '\0') {
398 errno = EINVAL;
399 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
400 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700401
402 if (args.size() > 4) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700403 auto uid = DecodeUid(args[4]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700404 if (!uid) {
405 return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
406 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700407 socket.uid = *uid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700408 }
409
410 if (args.size() > 5) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700411 auto gid = DecodeUid(args[5]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700412 if (!gid) {
413 return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
414 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700415 socket.gid = *gid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700416 }
417
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700418 socket.context = args.size() > 6 ? args[6] : "";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700419
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700420 auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(),
421 [&socket](const auto& other) { return socket.name == other.name; });
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700422
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700423 if (old != service_->sockets_.end()) {
424 return Error() << "duplicate socket descriptor '" << socket.name << "'";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700425 }
426
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700427 service_->sockets_.emplace_back(std::move(socket));
428
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700429 return {};
430}
431
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700432// name type
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700433Result<void> ServiceParser::ParseFile(std::vector<std::string>&& args) {
434 if (args[2] != "r" && args[2] != "w" && args[2] != "rw") {
435 return Error() << "file type must be 'r', 'w' or 'rw'";
436 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700437
438 FileDescriptor file;
439 file.type = args[2];
440
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700441 auto file_name = ExpandProps(args[1]);
442 if (!file_name) {
443 return Error() << "Could not expand file path ': " << file_name.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700444 }
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700445 file.name = *file_name;
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700446 if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700447 return Error() << "file name must not be relative";
448 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700449
450 auto old = std::find_if(service_->files_.begin(), service_->files_.end(),
451 [&file](const auto& other) { return other.name == file.name; });
452
453 if (old != service_->files_.end()) {
454 return Error() << "duplicate file descriptor '" << file.name << "'";
455 }
456
457 service_->files_.emplace_back(std::move(file));
458
459 return {};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700460}
461
462Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) {
463 auto uid = DecodeUid(args[1]);
464 if (!uid) {
465 return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
466 }
467 service_->proc_attr_.uid = *uid;
468 return {};
469}
470
471Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) {
472 args.erase(args.begin());
473 service_->writepid_files_ = std::move(args);
474 return {};
475}
476
477Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) {
478 service_->updatable_ = true;
479 return {};
480}
481
Tom Cherryd52a5b32019-07-22 16:05:36 -0700482const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700483 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
484 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -0700485 static const KeywordMap<ServiceParser::OptionParser> parser_map = {
Tom Cherry60971e62019-09-10 10:40:47 -0700486 {"capabilities", {0, kMax, &ServiceParser::ParseCapabilities}},
487 {"class", {1, kMax, &ServiceParser::ParseClass}},
488 {"console", {0, 1, &ServiceParser::ParseConsole}},
489 {"critical", {0, 0, &ServiceParser::ParseCritical}},
490 {"disabled", {0, 0, &ServiceParser::ParseDisabled}},
491 {"enter_namespace", {2, 2, &ServiceParser::ParseEnterNamespace}},
492 {"file", {2, 2, &ServiceParser::ParseFile}},
493 {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
494 {"interface", {2, 2, &ServiceParser::ParseInterface}},
495 {"ioprio", {2, 2, &ServiceParser::ParseIoprio}},
496 {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}},
497 {"memcg.limit_in_bytes", {1, 1, &ServiceParser::ParseMemcgLimitInBytes}},
498 {"memcg.limit_percent", {1, 1, &ServiceParser::ParseMemcgLimitPercent}},
499 {"memcg.limit_property", {1, 1, &ServiceParser::ParseMemcgLimitProperty}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700500 {"memcg.soft_limit_in_bytes",
Tom Cherry60971e62019-09-10 10:40:47 -0700501 {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}},
502 {"memcg.swappiness", {1, 1, &ServiceParser::ParseMemcgSwappiness}},
503 {"namespace", {1, 2, &ServiceParser::ParseNamespace}},
504 {"oneshot", {0, 0, &ServiceParser::ParseOneshot}},
505 {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}},
506 {"oom_score_adjust", {1, 1, &ServiceParser::ParseOomScoreAdjust}},
507 {"override", {0, 0, &ServiceParser::ParseOverride}},
508 {"priority", {1, 1, &ServiceParser::ParsePriority}},
509 {"reboot_on_failure", {1, 1, &ServiceParser::ParseRebootOnFailure}},
510 {"restart_period", {1, 1, &ServiceParser::ParseRestartPeriod}},
511 {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}},
512 {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}},
513 {"setenv", {2, 2, &ServiceParser::ParseSetenv}},
514 {"shutdown", {1, 1, &ServiceParser::ParseShutdown}},
515 {"sigstop", {0, 0, &ServiceParser::ParseSigstop}},
516 {"socket", {3, 6, &ServiceParser::ParseSocket}},
517 {"timeout_period", {1, 1, &ServiceParser::ParseTimeoutPeriod}},
518 {"updatable", {0, 0, &ServiceParser::ParseUpdatable}},
519 {"user", {1, 1, &ServiceParser::ParseUser}},
520 {"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700521 };
522 // clang-format on
Tom Cherryd52a5b32019-07-22 16:05:36 -0700523 return parser_map;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700524}
525
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700526Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
527 const std::string& filename, int line) {
528 if (args.size() < 3) {
529 return Error() << "services must have a name and a program";
530 }
531
532 const std::string& name = args[1];
533 if (!IsValidName(name)) {
534 return Error() << "invalid service name '" << name << "'";
535 }
536
537 filename_ = filename;
538
539 Subcontext* restart_action_subcontext = nullptr;
540 if (subcontexts_) {
541 for (auto& subcontext : *subcontexts_) {
542 if (StartsWith(filename, subcontext.path_prefix())) {
543 restart_action_subcontext = &subcontext;
544 break;
545 }
546 }
547 }
548
549 std::vector<std::string> str_args(args.begin() + 2, args.end());
550
551 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
552 if (str_args[0] == "/sbin/watchdogd") {
553 str_args[0] = "/system/bin/watchdogd";
554 }
555 }
556
557 service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args);
558 return {};
559}
560
561Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700562 if (!service_) {
563 return {};
564 }
565
Tom Cherryd52a5b32019-07-22 16:05:36 -0700566 auto parser = GetParserMap().Find(args);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700567
568 if (!parser) return parser.error();
569
570 return std::invoke(*parser, this, std::move(args));
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700571}
572
573Result<void> ServiceParser::EndSection() {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700574 if (!service_) {
575 return {};
576 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700577
Daniel Norman3f42a762019-07-09 11:00:53 -0700578 if (interface_inheritance_hierarchy_) {
Daniel Normand2533c32019-08-02 15:13:50 -0700579 if (const auto& check_hierarchy_result = CheckInterfaceInheritanceHierarchy(
580 service_->interfaces(), *interface_inheritance_hierarchy_);
581 !check_hierarchy_result) {
582 return Error() << check_hierarchy_result.error();
Daniel Norman3f42a762019-07-09 11:00:53 -0700583 }
584 }
585
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700586 Service* old_service = service_list_->FindService(service_->name());
587 if (old_service) {
588 if (!service_->is_override()) {
589 return Error() << "ignored duplicate definition of service '" << service_->name()
590 << "'";
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700591 }
592
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700593 if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
594 return Error() << "cannot update a non-updatable service '" << service_->name()
595 << "' with a config in APEX";
596 }
597
598 service_list_->RemoveService(*old_service);
599 old_service = nullptr;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700600 }
601
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700602 service_list_->AddService(std::move(service_));
603
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700604 return {};
605}
606
607bool ServiceParser::IsValidName(const std::string& name) const {
608 // Property names can be any length, but may only contain certain characters.
609 // Property values can contain any characters, but may only be a certain length.
610 // (The latter restriction is needed because `start` and `stop` work by writing
611 // the service name to the "ctl.start" and "ctl.stop" properties.)
612 return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
613}
614
615} // namespace init
616} // namespace android