blob: 144425874da1067b6c17424c0ea48330129ce614 [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
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
183Result<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
201Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) {
202 auto it = args.begin() + 1;
203 if (args.size() == 2 && StartsWith(args[1], "$")) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700204 auto expanded = ExpandProps(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900205 if (!expanded.ok()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700206 return expanded.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700207 }
208
209 // If the property is not set, it defaults to none, in which case there are no keycodes
210 // for this service.
Bernie Innocenti1cc76df2020-02-03 23:54:02 +0900211 if (*expanded == "none") {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700212 return {};
213 }
214
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700215 args = Split(*expanded, ",");
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700216 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
235Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) {
236 service_->flags_ |= SVC_ONESHOT;
237 return {};
238}
239
240Result<void> ServiceParser::ParseOnrestart(std::vector<std::string>&& args) {
241 args.erase(args.begin());
242 int line = service_->onrestart_.NumCommands() + 1;
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900243 if (auto result = service_->onrestart_.AddCommand(std::move(args), line); !result.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700244 return Error() << "cannot add Onrestart command: " << result.error();
245 }
246 return {};
247}
248
249Result<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
264Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) {
Suren Baghdasaryanc29c2ba2019-10-22 17:18:42 -0700265 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;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700269 }
270 return {};
271}
272
273Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) {
274 service_->override_ = true;
275 return {};
276}
277
278Result<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
285Result<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
292Result<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
299Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) {
300 service_->limit_property_ = std::move(args[1]);
301 return {};
302}
303
304Result<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
311Result<void> ServiceParser::ParseProcessRlimit(std::vector<std::string>&& args) {
312 auto rlimit = ParseRlimit(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900313 if (!rlimit.ok()) return rlimit.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700314
315 service_->proc_attr_.rlimits.emplace_back(*rlimit);
316 return {};
317}
318
Tom Cherry60971e62019-09-10 10:40:47 -0700319Result<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
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700331Result<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
340Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) {
341 service_->seclabel_ = std::move(args[1]);
342 return {};
343}
344
345Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) {
346 service_->sigstop_ = true;
347 return {};
348}
349
350Result<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
355Result<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
Suren Baghdasaryanc9c0bba2020-04-30 11:58:39 -0700363Result<void> ServiceParser::ParseTaskProfiles(std::vector<std::string>&& args) {
364 args.erase(args.begin());
365 service_->task_profiles_ = std::move(args);
366 return {};
367}
368
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700369Result<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
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700378// name type perm [ uid gid context ]
379Result<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 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700413
414 if (args.size() > 4) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700415 auto uid = DecodeUid(args[4]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900416 if (!uid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700417 return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
418 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700419 socket.uid = *uid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700420 }
421
422 if (args.size() > 5) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700423 auto gid = DecodeUid(args[5]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900424 if (!gid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700425 return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
426 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700427 socket.gid = *gid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700428 }
429
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700430 socket.context = args.size() > 6 ? args[6] : "";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700431
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700432 auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(),
433 [&socket](const auto& other) { return socket.name == other.name; });
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700434
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700435 if (old != service_->sockets_.end()) {
436 return Error() << "duplicate socket descriptor '" << socket.name << "'";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700437 }
438
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700439 service_->sockets_.emplace_back(std::move(socket));
440
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700441 return {};
442}
443
Tom Cherryf74b7f52019-09-23 16:16:54 -0700444Result<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
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700452// name type
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700453Result<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 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700457
458 FileDescriptor file;
459 file.type = args[2];
460
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700461 auto file_name = ExpandProps(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900462 if (!file_name.ok()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700463 return Error() << "Could not expand file path ': " << file_name.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700464 }
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700465 file.name = *file_name;
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700466 if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700467 return Error() << "file name must not be relative";
468 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700469
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 {};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700480}
481
482Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) {
483 auto uid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900484 if (!uid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700485 return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
486 }
487 service_->proc_attr_.uid = *uid;
488 return {};
489}
490
491Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) {
492 args.erase(args.begin());
493 service_->writepid_files_ = std::move(args);
494 return {};
495}
496
497Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) {
498 service_->updatable_ = true;
499 return {};
500}
501
Tom Cherryd52a5b32019-07-22 16:05:36 -0700502const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700503 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
504 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -0700505 static const KeywordMap<ServiceParser::OptionParser> parser_map = {
Tom Cherry60971e62019-09-10 10:40:47 -0700506 {"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}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700520 {"memcg.soft_limit_in_bytes",
Tom Cherry60971e62019-09-10 10:40:47 -0700521 {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}},
Tom Cherryf74b7f52019-09-23 16:16:54 -0700537 {"stdio_to_kmsg", {0, 0, &ServiceParser::ParseStdioToKmsg}},
Suren Baghdasaryanc9c0bba2020-04-30 11:58:39 -0700538 {"task_profiles", {1, kMax, &ServiceParser::ParseTaskProfiles}},
Tom Cherry60971e62019-09-10 10:40:47 -0700539 {"timeout_period", {1, 1, &ServiceParser::ParseTimeoutPeriod}},
540 {"updatable", {0, 0, &ServiceParser::ParseUpdatable}},
541 {"user", {1, 1, &ServiceParser::ParseUser}},
542 {"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700543 };
544 // clang-format on
Tom Cherryd52a5b32019-07-22 16:05:36 -0700545 return parser_map;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700546}
547
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700548Result<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;
Tom Cherry14c24722019-09-18 13:47:19 -0700562 if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
563 restart_action_subcontext = subcontext_;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700564 }
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 }
Yifan Hong8fb7f772019-10-16 14:22:12 -0700573 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_Q__) {
574 if (str_args[0] == "/charger") {
575 str_args[0] = "/system/bin/charger";
576 }
577 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700578
Nikita Ioffe091c4d12019-12-05 12:35:19 +0000579 service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args, from_apex_);
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700580 return {};
581}
582
583Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700584 if (!service_) {
585 return {};
586 }
587
Tom Cherryd52a5b32019-07-22 16:05:36 -0700588 auto parser = GetParserMap().Find(args);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700589
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900590 if (!parser.ok()) return parser.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700591
592 return std::invoke(*parser, this, std::move(args));
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700593}
594
595Result<void> ServiceParser::EndSection() {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700596 if (!service_) {
597 return {};
598 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700599
Daniel Norman3f42a762019-07-09 11:00:53 -0700600 if (interface_inheritance_hierarchy_) {
Daniel Normand2533c32019-08-02 15:13:50 -0700601 if (const auto& check_hierarchy_result = CheckInterfaceInheritanceHierarchy(
602 service_->interfaces(), *interface_inheritance_hierarchy_);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900603 !check_hierarchy_result.ok()) {
Daniel Normand2533c32019-08-02 15:13:50 -0700604 return Error() << check_hierarchy_result.error();
Daniel Norman3f42a762019-07-09 11:00:53 -0700605 }
606 }
607
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700608 Service* old_service = service_list_->FindService(service_->name());
609 if (old_service) {
610 if (!service_->is_override()) {
611 return Error() << "ignored duplicate definition of service '" << service_->name()
612 << "'";
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700613 }
614
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700615 if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
616 return Error() << "cannot update a non-updatable service '" << service_->name()
617 << "' with a config in APEX";
618 }
619
620 service_list_->RemoveService(*old_service);
621 old_service = nullptr;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700622 }
623
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700624 service_list_->AddService(std::move(service_));
625
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700626 return {};
627}
628
629bool ServiceParser::IsValidName(const std::string& name) const {
630 // Property names can be any length, but may only contain certain characters.
631 // Property values can contain any characters, but may only be a certain length.
632 // (The latter restriction is needed because `start` and `stop` work by writing
633 // the service name to the "ctl.start" and "ctl.stop" properties.)
634 return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
635}
636
637} // namespace init
638} // namespace android