blob: 51f4c978617837a676ba9a80b8728b2fac4f5f74 [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
Suren Baghdasaryanc29c2ba2019-10-22 17:18:42 -070032#include "lmkd_service.h"
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070033#include "rlimit_parser.h"
Tom Cherry2e4c85f2019-07-09 13:33:36 -070034#include "service_utils.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070035#include "util.h"
36
Tom Cherrya2f91362020-02-20 10:50:00 -080037#ifdef INIT_FULL_SOURCES
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070038#include <android/api-level.h>
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070039#include <sys/system_properties.h>
40
41#include "selinux.h"
42#else
43#include "host_init_stubs.h"
44#endif
45
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070046using android::base::ParseInt;
47using android::base::Split;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070048using android::base::StartsWith;
49
50namespace android {
51namespace init {
52
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070053Result<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
81Result<void> ServiceParser::ParseClass(std::vector<std::string>&& args) {
82 service_->classnames_ = std::set<std::string>(args.begin() + 1, args.end());
83 return {};
84}
85
86Result<void> ServiceParser::ParseConsole(std::vector<std::string>&& args) {
Tom Cherryf74b7f52019-09-23 16:16:54 -070087 if (service_->proc_attr_.stdio_to_kmsg) {
88 return Error() << "'console' and 'stdio_to_kmsg' are mutually exclusive";
89 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070090 service_->flags_ |= SVC_CONSOLE;
91 service_->proc_attr_.console = args.size() > 1 ? "/dev/" + args[1] : "";
92 return {};
93}
94
95Result<void> ServiceParser::ParseCritical(std::vector<std::string>&& args) {
96 service_->flags_ |= SVC_CRITICAL;
97 return {};
98}
99
100Result<void> ServiceParser::ParseDisabled(std::vector<std::string>&& args) {
101 service_->flags_ |= SVC_DISABLED;
102 service_->flags_ |= SVC_RC_DISABLED;
103 return {};
104}
105
106Result<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
120Result<void> ServiceParser::ParseGroup(std::vector<std::string>&& args) {
121 auto gid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900122 if (!gid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700123 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]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900129 if (!gid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700130 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
137Result<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
148Result<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
Jon Spivack16fb3f92019-07-26 13:14:42 -0700152 // 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 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700159
Jon Spivack16fb3f92019-07-26 13:14:42 -0700160 if (!fq_name.isFullyQualified()) {
161 return Error() << "Interface name not fully-qualified '" << interface_name << "'";
162 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700163
Jon Spivack16fb3f92019-07-26 13:14:42 -0700164 if (fq_name.isValidValueName()) {
165 return Error() << "Interface name must not be a value name '" << interface_name << "'";
166 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700167 }
168
169 const std::string fullname = interface_name + "/" + instance_name;
170
Tom Cherry7205c622020-01-29 14:09:24 -0800171 auto lock = std::lock_guard{service_lock};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700172 for (const auto& svc : *service_list_) {
173 if (svc->interfaces().count(fullname) > 0) {
174 return Error() << "Interface '" << fullname << "' redefined in " << service_->name()
175 << " but is already defined by " << svc->name();
176 }
177 }
178
179 service_->interfaces_.insert(fullname);
180
181 return {};
182}
183
184Result<void> ServiceParser::ParseIoprio(std::vector<std::string>&& args) {
185 if (!ParseInt(args[2], &service_->proc_attr_.ioprio_pri, 0, 7)) {
186 return Error() << "priority value must be range 0 - 7";
187 }
188
189 if (args[1] == "rt") {
190 service_->proc_attr_.ioprio_class = IoSchedClass_RT;
191 } else if (args[1] == "be") {
192 service_->proc_attr_.ioprio_class = IoSchedClass_BE;
193 } else if (args[1] == "idle") {
194 service_->proc_attr_.ioprio_class = IoSchedClass_IDLE;
195 } else {
196 return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>";
197 }
198
199 return {};
200}
201
202Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) {
203 auto it = args.begin() + 1;
204 if (args.size() == 2 && StartsWith(args[1], "$")) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700205 auto expanded = ExpandProps(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900206 if (!expanded.ok()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700207 return expanded.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700208 }
209
210 // If the property is not set, it defaults to none, in which case there are no keycodes
211 // for this service.
Bernie Innocenti1cc76df2020-02-03 23:54:02 +0900212 if (*expanded == "none") {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700213 return {};
214 }
215
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700216 args = Split(*expanded, ",");
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700217 it = args.begin();
218 }
219
220 for (; it != args.end(); ++it) {
221 int code;
222 if (ParseInt(*it, &code, 0, KEY_MAX)) {
223 for (auto& key : service_->keycodes_) {
224 if (key == code) return Error() << "duplicate keycode: " << *it;
225 }
226 service_->keycodes_.insert(
227 std::upper_bound(service_->keycodes_.begin(), service_->keycodes_.end(), code),
228 code);
229 } else {
230 return Error() << "invalid keycode: " << *it;
231 }
232 }
233 return {};
234}
235
236Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) {
237 service_->flags_ |= SVC_ONESHOT;
238 return {};
239}
240
241Result<void> ServiceParser::ParseOnrestart(std::vector<std::string>&& args) {
242 args.erase(args.begin());
243 int line = service_->onrestart_.NumCommands() + 1;
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900244 if (auto result = service_->onrestart_.AddCommand(std::move(args), line); !result.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700245 return Error() << "cannot add Onrestart command: " << result.error();
246 }
247 return {};
248}
249
250Result<void> ServiceParser::ParseNamespace(std::vector<std::string>&& args) {
251 for (size_t i = 1; i < args.size(); i++) {
252 if (args[i] == "pid") {
253 service_->namespaces_.flags |= CLONE_NEWPID;
254 // PID namespaces require mount namespaces.
255 service_->namespaces_.flags |= CLONE_NEWNS;
256 } else if (args[i] == "mnt") {
257 service_->namespaces_.flags |= CLONE_NEWNS;
258 } else {
259 return Error() << "namespace must be 'pid' or 'mnt'";
260 }
261 }
262 return {};
263}
264
265Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) {
Suren Baghdasaryanc29c2ba2019-10-22 17:18:42 -0700266 if (!ParseInt(args[1], &service_->oom_score_adjust_, MIN_OOM_SCORE_ADJUST,
267 MAX_OOM_SCORE_ADJUST)) {
268 return Error() << "oom_score_adjust value must be in range " << MIN_OOM_SCORE_ADJUST
269 << " - +" << MAX_OOM_SCORE_ADJUST;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700270 }
271 return {};
272}
273
274Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) {
275 service_->override_ = true;
276 return {};
277}
278
279Result<void> ServiceParser::ParseMemcgSwappiness(std::vector<std::string>&& args) {
280 if (!ParseInt(args[1], &service_->swappiness_, 0)) {
281 return Error() << "swappiness value must be equal or greater than 0";
282 }
283 return {};
284}
285
286Result<void> ServiceParser::ParseMemcgLimitInBytes(std::vector<std::string>&& args) {
287 if (!ParseInt(args[1], &service_->limit_in_bytes_, 0)) {
288 return Error() << "limit_in_bytes value must be equal or greater than 0";
289 }
290 return {};
291}
292
293Result<void> ServiceParser::ParseMemcgLimitPercent(std::vector<std::string>&& args) {
294 if (!ParseInt(args[1], &service_->limit_percent_, 0)) {
295 return Error() << "limit_percent value must be equal or greater than 0";
296 }
297 return {};
298}
299
300Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) {
301 service_->limit_property_ = std::move(args[1]);
302 return {};
303}
304
305Result<void> ServiceParser::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) {
306 if (!ParseInt(args[1], &service_->soft_limit_in_bytes_, 0)) {
307 return Error() << "soft_limit_in_bytes value must be equal or greater than 0";
308 }
309 return {};
310}
311
312Result<void> ServiceParser::ParseProcessRlimit(std::vector<std::string>&& args) {
313 auto rlimit = ParseRlimit(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900314 if (!rlimit.ok()) return rlimit.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700315
316 service_->proc_attr_.rlimits.emplace_back(*rlimit);
317 return {};
318}
319
Tom Cherry60971e62019-09-10 10:40:47 -0700320Result<void> ServiceParser::ParseRebootOnFailure(std::vector<std::string>&& args) {
321 if (service_->on_failure_reboot_target_) {
322 return Error() << "Only one reboot_on_failure command may be specified";
323 }
324 if (!StartsWith(args[1], "shutdown") && !StartsWith(args[1], "reboot")) {
325 return Error()
326 << "reboot_on_failure commands must begin with either 'shutdown' or 'reboot'";
327 }
328 service_->on_failure_reboot_target_ = std::move(args[1]);
329 return {};
330}
331
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700332Result<void> ServiceParser::ParseRestartPeriod(std::vector<std::string>&& args) {
333 int period;
334 if (!ParseInt(args[1], &period, 5)) {
335 return Error() << "restart_period value must be an integer >= 5";
336 }
337 service_->restart_period_ = std::chrono::seconds(period);
338 return {};
339}
340
341Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) {
342 service_->seclabel_ = std::move(args[1]);
343 return {};
344}
345
346Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) {
347 service_->sigstop_ = true;
348 return {};
349}
350
351Result<void> ServiceParser::ParseSetenv(std::vector<std::string>&& args) {
352 service_->environment_vars_.emplace_back(std::move(args[1]), std::move(args[2]));
353 return {};
354}
355
356Result<void> ServiceParser::ParseShutdown(std::vector<std::string>&& args) {
357 if (args[1] == "critical") {
358 service_->flags_ |= SVC_SHUTDOWN_CRITICAL;
359 return {};
360 }
361 return Error() << "Invalid shutdown option";
362}
363
364Result<void> ServiceParser::ParseTimeoutPeriod(std::vector<std::string>&& args) {
365 int period;
366 if (!ParseInt(args[1], &period, 1)) {
367 return Error() << "timeout_period value must be an integer >= 1";
368 }
369 service_->timeout_period_ = std::chrono::seconds(period);
370 return {};
371}
372
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700373// name type perm [ uid gid context ]
374Result<void> ServiceParser::ParseSocket(std::vector<std::string>&& args) {
375 SocketDescriptor socket;
376 socket.name = std::move(args[1]);
377
378 auto types = Split(args[2], "+");
379 if (types[0] == "stream") {
380 socket.type = SOCK_STREAM;
381 } else if (types[0] == "dgram") {
382 socket.type = SOCK_DGRAM;
383 } else if (types[0] == "seqpacket") {
384 socket.type = SOCK_SEQPACKET;
385 } else {
386 return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket', got '" << types[0]
387 << "' instead.";
388 }
389
390 if (types.size() > 1) {
391 if (types.size() == 2 && types[1] == "passcred") {
392 socket.passcred = true;
393 } else {
394 return Error() << "Only 'passcred' may be used to modify the socket type";
395 }
396 }
397
398 errno = 0;
399 char* end = nullptr;
400 socket.perm = strtol(args[3].c_str(), &end, 8);
401 if (errno != 0) {
402 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
403 }
404 if (end == args[3].c_str() || *end != '\0') {
405 errno = EINVAL;
406 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
407 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700408
409 if (args.size() > 4) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700410 auto uid = DecodeUid(args[4]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900411 if (!uid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700412 return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
413 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700414 socket.uid = *uid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700415 }
416
417 if (args.size() > 5) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700418 auto gid = DecodeUid(args[5]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900419 if (!gid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700420 return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
421 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700422 socket.gid = *gid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700423 }
424
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700425 socket.context = args.size() > 6 ? args[6] : "";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700426
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700427 auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(),
428 [&socket](const auto& other) { return socket.name == other.name; });
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700429
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700430 if (old != service_->sockets_.end()) {
431 return Error() << "duplicate socket descriptor '" << socket.name << "'";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700432 }
433
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700434 service_->sockets_.emplace_back(std::move(socket));
435
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700436 return {};
437}
438
Tom Cherryf74b7f52019-09-23 16:16:54 -0700439Result<void> ServiceParser::ParseStdioToKmsg(std::vector<std::string>&& args) {
440 if (service_->flags_ & SVC_CONSOLE) {
441 return Error() << "'stdio_to_kmsg' and 'console' are mutually exclusive";
442 }
443 service_->proc_attr_.stdio_to_kmsg = true;
444 return {};
445}
446
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700447// name type
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700448Result<void> ServiceParser::ParseFile(std::vector<std::string>&& args) {
449 if (args[2] != "r" && args[2] != "w" && args[2] != "rw") {
450 return Error() << "file type must be 'r', 'w' or 'rw'";
451 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700452
453 FileDescriptor file;
454 file.type = args[2];
455
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700456 auto file_name = ExpandProps(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900457 if (!file_name.ok()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700458 return Error() << "Could not expand file path ': " << file_name.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700459 }
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700460 file.name = *file_name;
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700461 if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700462 return Error() << "file name must not be relative";
463 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700464
465 auto old = std::find_if(service_->files_.begin(), service_->files_.end(),
466 [&file](const auto& other) { return other.name == file.name; });
467
468 if (old != service_->files_.end()) {
469 return Error() << "duplicate file descriptor '" << file.name << "'";
470 }
471
472 service_->files_.emplace_back(std::move(file));
473
474 return {};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700475}
476
477Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) {
478 auto uid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900479 if (!uid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700480 return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
481 }
482 service_->proc_attr_.uid = *uid;
483 return {};
484}
485
486Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) {
487 args.erase(args.begin());
488 service_->writepid_files_ = std::move(args);
489 return {};
490}
491
492Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) {
493 service_->updatable_ = true;
494 return {};
495}
496
Tom Cherryd52a5b32019-07-22 16:05:36 -0700497const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700498 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
499 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -0700500 static const KeywordMap<ServiceParser::OptionParser> parser_map = {
Tom Cherry60971e62019-09-10 10:40:47 -0700501 {"capabilities", {0, kMax, &ServiceParser::ParseCapabilities}},
502 {"class", {1, kMax, &ServiceParser::ParseClass}},
503 {"console", {0, 1, &ServiceParser::ParseConsole}},
504 {"critical", {0, 0, &ServiceParser::ParseCritical}},
505 {"disabled", {0, 0, &ServiceParser::ParseDisabled}},
506 {"enter_namespace", {2, 2, &ServiceParser::ParseEnterNamespace}},
507 {"file", {2, 2, &ServiceParser::ParseFile}},
508 {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
509 {"interface", {2, 2, &ServiceParser::ParseInterface}},
510 {"ioprio", {2, 2, &ServiceParser::ParseIoprio}},
511 {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}},
512 {"memcg.limit_in_bytes", {1, 1, &ServiceParser::ParseMemcgLimitInBytes}},
513 {"memcg.limit_percent", {1, 1, &ServiceParser::ParseMemcgLimitPercent}},
514 {"memcg.limit_property", {1, 1, &ServiceParser::ParseMemcgLimitProperty}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700515 {"memcg.soft_limit_in_bytes",
Tom Cherry60971e62019-09-10 10:40:47 -0700516 {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}},
517 {"memcg.swappiness", {1, 1, &ServiceParser::ParseMemcgSwappiness}},
518 {"namespace", {1, 2, &ServiceParser::ParseNamespace}},
519 {"oneshot", {0, 0, &ServiceParser::ParseOneshot}},
520 {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}},
521 {"oom_score_adjust", {1, 1, &ServiceParser::ParseOomScoreAdjust}},
522 {"override", {0, 0, &ServiceParser::ParseOverride}},
523 {"priority", {1, 1, &ServiceParser::ParsePriority}},
524 {"reboot_on_failure", {1, 1, &ServiceParser::ParseRebootOnFailure}},
525 {"restart_period", {1, 1, &ServiceParser::ParseRestartPeriod}},
526 {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}},
527 {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}},
528 {"setenv", {2, 2, &ServiceParser::ParseSetenv}},
529 {"shutdown", {1, 1, &ServiceParser::ParseShutdown}},
530 {"sigstop", {0, 0, &ServiceParser::ParseSigstop}},
531 {"socket", {3, 6, &ServiceParser::ParseSocket}},
Tom Cherryf74b7f52019-09-23 16:16:54 -0700532 {"stdio_to_kmsg", {0, 0, &ServiceParser::ParseStdioToKmsg}},
Tom Cherry60971e62019-09-10 10:40:47 -0700533 {"timeout_period", {1, 1, &ServiceParser::ParseTimeoutPeriod}},
534 {"updatable", {0, 0, &ServiceParser::ParseUpdatable}},
535 {"user", {1, 1, &ServiceParser::ParseUser}},
536 {"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700537 };
538 // clang-format on
Tom Cherryd52a5b32019-07-22 16:05:36 -0700539 return parser_map;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700540}
541
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700542Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
543 const std::string& filename, int line) {
544 if (args.size() < 3) {
545 return Error() << "services must have a name and a program";
546 }
547
548 const std::string& name = args[1];
549 if (!IsValidName(name)) {
550 return Error() << "invalid service name '" << name << "'";
551 }
552
553 filename_ = filename;
554
555 Subcontext* restart_action_subcontext = nullptr;
Tom Cherry14c24722019-09-18 13:47:19 -0700556 if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
557 restart_action_subcontext = subcontext_;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700558 }
559
560 std::vector<std::string> str_args(args.begin() + 2, args.end());
561
562 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
563 if (str_args[0] == "/sbin/watchdogd") {
564 str_args[0] = "/system/bin/watchdogd";
565 }
566 }
Yifan Hong8fb7f772019-10-16 14:22:12 -0700567 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_Q__) {
568 if (str_args[0] == "/charger") {
569 str_args[0] = "/system/bin/charger";
570 }
571 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700572
Nikita Ioffe091c4d12019-12-05 12:35:19 +0000573 service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args, from_apex_);
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700574 return {};
575}
576
577Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700578 if (!service_) {
579 return {};
580 }
581
Tom Cherryd52a5b32019-07-22 16:05:36 -0700582 auto parser = GetParserMap().Find(args);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700583
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900584 if (!parser.ok()) return parser.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700585
586 return std::invoke(*parser, this, std::move(args));
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700587}
588
589Result<void> ServiceParser::EndSection() {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700590 if (!service_) {
591 return {};
592 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700593
Daniel Norman3f42a762019-07-09 11:00:53 -0700594 if (interface_inheritance_hierarchy_) {
Daniel Normand2533c32019-08-02 15:13:50 -0700595 if (const auto& check_hierarchy_result = CheckInterfaceInheritanceHierarchy(
596 service_->interfaces(), *interface_inheritance_hierarchy_);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900597 !check_hierarchy_result.ok()) {
Daniel Normand2533c32019-08-02 15:13:50 -0700598 return Error() << check_hierarchy_result.error();
Daniel Norman3f42a762019-07-09 11:00:53 -0700599 }
600 }
601
Tom Cherry7205c622020-01-29 14:09:24 -0800602 auto lock = std::lock_guard{service_lock};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700603 Service* old_service = service_list_->FindService(service_->name());
604 if (old_service) {
605 if (!service_->is_override()) {
606 return Error() << "ignored duplicate definition of service '" << service_->name()
607 << "'";
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700608 }
609
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700610 if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
611 return Error() << "cannot update a non-updatable service '" << service_->name()
612 << "' with a config in APEX";
613 }
614
615 service_list_->RemoveService(*old_service);
616 old_service = nullptr;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700617 }
618
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700619 service_list_->AddService(std::move(service_));
620
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700621 return {};
622}
623
624bool ServiceParser::IsValidName(const std::string& name) const {
625 // Property names can be any length, but may only contain certain characters.
626 // Property values can contain any characters, but may only be a certain length.
627 // (The latter restriction is needed because `start` and `stop` work by writing
628 // the service name to the "ctl.start" and "ctl.stop" properties.)
629 return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
630}
631
632} // namespace init
633} // namespace android