blob: e7808a9996d2458f6dd121337d898e96732c6c93 [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) {
Tom Cherryf74b7f52019-09-23 16:16:54 -070086 if (service_->proc_attr_.stdio_to_kmsg) {
87 return Error() << "'console' and 'stdio_to_kmsg' are mutually exclusive";
88 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070089 service_->flags_ |= SVC_CONSOLE;
90 service_->proc_attr_.console = args.size() > 1 ? "/dev/" + args[1] : "";
91 return {};
92}
93
94Result<void> ServiceParser::ParseCritical(std::vector<std::string>&& args) {
95 service_->flags_ |= SVC_CRITICAL;
96 return {};
97}
98
99Result<void> ServiceParser::ParseDisabled(std::vector<std::string>&& args) {
100 service_->flags_ |= SVC_DISABLED;
101 service_->flags_ |= SVC_RC_DISABLED;
102 return {};
103}
104
105Result<void> ServiceParser::ParseEnterNamespace(std::vector<std::string>&& args) {
106 if (args[1] != "net") {
107 return Error() << "Init only supports entering network namespaces";
108 }
109 if (!service_->namespaces_.namespaces_to_enter.empty()) {
110 return Error() << "Only one network namespace may be entered";
111 }
112 // Network namespaces require that /sys is remounted, otherwise the old adapters will still be
113 // present. Therefore, they also require mount namespaces.
114 service_->namespaces_.flags |= CLONE_NEWNS;
115 service_->namespaces_.namespaces_to_enter.emplace_back(CLONE_NEWNET, std::move(args[2]));
116 return {};
117}
118
119Result<void> ServiceParser::ParseGroup(std::vector<std::string>&& args) {
120 auto gid = DecodeUid(args[1]);
121 if (!gid) {
122 return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error();
123 }
124 service_->proc_attr_.gid = *gid;
125
126 for (std::size_t n = 2; n < args.size(); n++) {
127 gid = DecodeUid(args[n]);
128 if (!gid) {
129 return Error() << "Unable to decode GID for '" << args[n] << "': " << gid.error();
130 }
131 service_->proc_attr_.supp_gids.emplace_back(*gid);
132 }
133 return {};
134}
135
136Result<void> ServiceParser::ParsePriority(std::vector<std::string>&& args) {
137 service_->proc_attr_.priority = 0;
138 if (!ParseInt(args[1], &service_->proc_attr_.priority,
139 static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative
140 static_cast<int>(ANDROID_PRIORITY_LOWEST))) {
141 return Errorf("process priority value must be range {} - {}", ANDROID_PRIORITY_HIGHEST,
142 ANDROID_PRIORITY_LOWEST);
143 }
144 return {};
145}
146
147Result<void> ServiceParser::ParseInterface(std::vector<std::string>&& args) {
148 const std::string& interface_name = args[1];
149 const std::string& instance_name = args[2];
150
Jon Spivack16fb3f92019-07-26 13:14:42 -0700151 // AIDL services don't use fully qualified names and instead just use "interface aidl <name>"
152 if (interface_name != "aidl") {
153 FQName fq_name;
154 if (!FQName::parse(interface_name, &fq_name)) {
155 return Error() << "Invalid fully-qualified name for interface '" << interface_name
156 << "'";
157 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700158
Jon Spivack16fb3f92019-07-26 13:14:42 -0700159 if (!fq_name.isFullyQualified()) {
160 return Error() << "Interface name not fully-qualified '" << interface_name << "'";
161 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700162
Jon Spivack16fb3f92019-07-26 13:14:42 -0700163 if (fq_name.isValidValueName()) {
164 return Error() << "Interface name must not be a value name '" << interface_name << "'";
165 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700166 }
167
168 const std::string fullname = interface_name + "/" + instance_name;
169
170 for (const auto& svc : *service_list_) {
171 if (svc->interfaces().count(fullname) > 0) {
172 return Error() << "Interface '" << fullname << "' redefined in " << service_->name()
173 << " but is already defined by " << svc->name();
174 }
175 }
176
177 service_->interfaces_.insert(fullname);
178
179 return {};
180}
181
182Result<void> ServiceParser::ParseIoprio(std::vector<std::string>&& args) {
183 if (!ParseInt(args[2], &service_->proc_attr_.ioprio_pri, 0, 7)) {
184 return Error() << "priority value must be range 0 - 7";
185 }
186
187 if (args[1] == "rt") {
188 service_->proc_attr_.ioprio_class = IoSchedClass_RT;
189 } else if (args[1] == "be") {
190 service_->proc_attr_.ioprio_class = IoSchedClass_BE;
191 } else if (args[1] == "idle") {
192 service_->proc_attr_.ioprio_class = IoSchedClass_IDLE;
193 } else {
194 return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>";
195 }
196
197 return {};
198}
199
200Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) {
201 auto it = args.begin() + 1;
202 if (args.size() == 2 && StartsWith(args[1], "$")) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700203 auto expanded = ExpandProps(args[1]);
204 if (!expanded) {
205 return expanded.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700206 }
207
208 // If the property is not set, it defaults to none, in which case there are no keycodes
209 // for this service.
210 if (expanded == "none") {
211 return {};
212 }
213
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700214 args = Split(*expanded, ",");
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700215 it = args.begin();
216 }
217
218 for (; it != args.end(); ++it) {
219 int code;
220 if (ParseInt(*it, &code, 0, KEY_MAX)) {
221 for (auto& key : service_->keycodes_) {
222 if (key == code) return Error() << "duplicate keycode: " << *it;
223 }
224 service_->keycodes_.insert(
225 std::upper_bound(service_->keycodes_.begin(), service_->keycodes_.end(), code),
226 code);
227 } else {
228 return Error() << "invalid keycode: " << *it;
229 }
230 }
231 return {};
232}
233
234Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) {
235 service_->flags_ |= SVC_ONESHOT;
236 return {};
237}
238
239Result<void> ServiceParser::ParseOnrestart(std::vector<std::string>&& args) {
240 args.erase(args.begin());
241 int line = service_->onrestart_.NumCommands() + 1;
242 if (auto result = service_->onrestart_.AddCommand(std::move(args), line); !result) {
243 return Error() << "cannot add Onrestart command: " << result.error();
244 }
245 return {};
246}
247
248Result<void> ServiceParser::ParseNamespace(std::vector<std::string>&& args) {
249 for (size_t i = 1; i < args.size(); i++) {
250 if (args[i] == "pid") {
251 service_->namespaces_.flags |= CLONE_NEWPID;
252 // PID namespaces require mount namespaces.
253 service_->namespaces_.flags |= CLONE_NEWNS;
254 } else if (args[i] == "mnt") {
255 service_->namespaces_.flags |= CLONE_NEWNS;
256 } else {
257 return Error() << "namespace must be 'pid' or 'mnt'";
258 }
259 }
260 return {};
261}
262
263Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) {
264 if (!ParseInt(args[1], &service_->oom_score_adjust_, -1000, 1000)) {
265 return Error() << "oom_score_adjust value must be in range -1000 - +1000";
266 }
267 return {};
268}
269
270Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) {
271 service_->override_ = true;
272 return {};
273}
274
275Result<void> ServiceParser::ParseMemcgSwappiness(std::vector<std::string>&& args) {
276 if (!ParseInt(args[1], &service_->swappiness_, 0)) {
277 return Error() << "swappiness value must be equal or greater than 0";
278 }
279 return {};
280}
281
282Result<void> ServiceParser::ParseMemcgLimitInBytes(std::vector<std::string>&& args) {
283 if (!ParseInt(args[1], &service_->limit_in_bytes_, 0)) {
284 return Error() << "limit_in_bytes value must be equal or greater than 0";
285 }
286 return {};
287}
288
289Result<void> ServiceParser::ParseMemcgLimitPercent(std::vector<std::string>&& args) {
290 if (!ParseInt(args[1], &service_->limit_percent_, 0)) {
291 return Error() << "limit_percent value must be equal or greater than 0";
292 }
293 return {};
294}
295
296Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) {
297 service_->limit_property_ = std::move(args[1]);
298 return {};
299}
300
301Result<void> ServiceParser::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) {
302 if (!ParseInt(args[1], &service_->soft_limit_in_bytes_, 0)) {
303 return Error() << "soft_limit_in_bytes value must be equal or greater than 0";
304 }
305 return {};
306}
307
308Result<void> ServiceParser::ParseProcessRlimit(std::vector<std::string>&& args) {
309 auto rlimit = ParseRlimit(args);
310 if (!rlimit) return rlimit.error();
311
312 service_->proc_attr_.rlimits.emplace_back(*rlimit);
313 return {};
314}
315
Tom Cherry60971e62019-09-10 10:40:47 -0700316Result<void> ServiceParser::ParseRebootOnFailure(std::vector<std::string>&& args) {
317 if (service_->on_failure_reboot_target_) {
318 return Error() << "Only one reboot_on_failure command may be specified";
319 }
320 if (!StartsWith(args[1], "shutdown") && !StartsWith(args[1], "reboot")) {
321 return Error()
322 << "reboot_on_failure commands must begin with either 'shutdown' or 'reboot'";
323 }
324 service_->on_failure_reboot_target_ = std::move(args[1]);
325 return {};
326}
327
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700328Result<void> ServiceParser::ParseRestartPeriod(std::vector<std::string>&& args) {
329 int period;
330 if (!ParseInt(args[1], &period, 5)) {
331 return Error() << "restart_period value must be an integer >= 5";
332 }
333 service_->restart_period_ = std::chrono::seconds(period);
334 return {};
335}
336
337Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) {
338 service_->seclabel_ = std::move(args[1]);
339 return {};
340}
341
342Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) {
343 service_->sigstop_ = true;
344 return {};
345}
346
347Result<void> ServiceParser::ParseSetenv(std::vector<std::string>&& args) {
348 service_->environment_vars_.emplace_back(std::move(args[1]), std::move(args[2]));
349 return {};
350}
351
352Result<void> ServiceParser::ParseShutdown(std::vector<std::string>&& args) {
353 if (args[1] == "critical") {
354 service_->flags_ |= SVC_SHUTDOWN_CRITICAL;
355 return {};
356 }
357 return Error() << "Invalid shutdown option";
358}
359
360Result<void> ServiceParser::ParseTimeoutPeriod(std::vector<std::string>&& args) {
361 int period;
362 if (!ParseInt(args[1], &period, 1)) {
363 return Error() << "timeout_period value must be an integer >= 1";
364 }
365 service_->timeout_period_ = std::chrono::seconds(period);
366 return {};
367}
368
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700369// name type perm [ uid gid context ]
370Result<void> ServiceParser::ParseSocket(std::vector<std::string>&& args) {
371 SocketDescriptor socket;
372 socket.name = std::move(args[1]);
373
374 auto types = Split(args[2], "+");
375 if (types[0] == "stream") {
376 socket.type = SOCK_STREAM;
377 } else if (types[0] == "dgram") {
378 socket.type = SOCK_DGRAM;
379 } else if (types[0] == "seqpacket") {
380 socket.type = SOCK_SEQPACKET;
381 } else {
382 return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket', got '" << types[0]
383 << "' instead.";
384 }
385
386 if (types.size() > 1) {
387 if (types.size() == 2 && types[1] == "passcred") {
388 socket.passcred = true;
389 } else {
390 return Error() << "Only 'passcred' may be used to modify the socket type";
391 }
392 }
393
394 errno = 0;
395 char* end = nullptr;
396 socket.perm = strtol(args[3].c_str(), &end, 8);
397 if (errno != 0) {
398 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
399 }
400 if (end == args[3].c_str() || *end != '\0') {
401 errno = EINVAL;
402 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
403 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700404
405 if (args.size() > 4) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700406 auto uid = DecodeUid(args[4]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700407 if (!uid) {
408 return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
409 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700410 socket.uid = *uid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700411 }
412
413 if (args.size() > 5) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700414 auto gid = DecodeUid(args[5]);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700415 if (!gid) {
416 return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
417 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700418 socket.gid = *gid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700419 }
420
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700421 socket.context = args.size() > 6 ? args[6] : "";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700422
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700423 auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(),
424 [&socket](const auto& other) { return socket.name == other.name; });
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700425
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700426 if (old != service_->sockets_.end()) {
427 return Error() << "duplicate socket descriptor '" << socket.name << "'";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700428 }
429
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700430 service_->sockets_.emplace_back(std::move(socket));
431
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700432 return {};
433}
434
Tom Cherryf74b7f52019-09-23 16:16:54 -0700435Result<void> ServiceParser::ParseStdioToKmsg(std::vector<std::string>&& args) {
436 if (service_->flags_ & SVC_CONSOLE) {
437 return Error() << "'stdio_to_kmsg' and 'console' are mutually exclusive";
438 }
439 service_->proc_attr_.stdio_to_kmsg = true;
440 return {};
441}
442
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700443// name type
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700444Result<void> ServiceParser::ParseFile(std::vector<std::string>&& args) {
445 if (args[2] != "r" && args[2] != "w" && args[2] != "rw") {
446 return Error() << "file type must be 'r', 'w' or 'rw'";
447 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700448
449 FileDescriptor file;
450 file.type = args[2];
451
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700452 auto file_name = ExpandProps(args[1]);
453 if (!file_name) {
454 return Error() << "Could not expand file path ': " << file_name.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700455 }
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700456 file.name = *file_name;
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700457 if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700458 return Error() << "file name must not be relative";
459 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700460
461 auto old = std::find_if(service_->files_.begin(), service_->files_.end(),
462 [&file](const auto& other) { return other.name == file.name; });
463
464 if (old != service_->files_.end()) {
465 return Error() << "duplicate file descriptor '" << file.name << "'";
466 }
467
468 service_->files_.emplace_back(std::move(file));
469
470 return {};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700471}
472
473Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) {
474 auto uid = DecodeUid(args[1]);
475 if (!uid) {
476 return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
477 }
478 service_->proc_attr_.uid = *uid;
479 return {};
480}
481
482Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) {
483 args.erase(args.begin());
484 service_->writepid_files_ = std::move(args);
485 return {};
486}
487
488Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) {
489 service_->updatable_ = true;
490 return {};
491}
492
Tom Cherryd52a5b32019-07-22 16:05:36 -0700493const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700494 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
495 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -0700496 static const KeywordMap<ServiceParser::OptionParser> parser_map = {
Tom Cherry60971e62019-09-10 10:40:47 -0700497 {"capabilities", {0, kMax, &ServiceParser::ParseCapabilities}},
498 {"class", {1, kMax, &ServiceParser::ParseClass}},
499 {"console", {0, 1, &ServiceParser::ParseConsole}},
500 {"critical", {0, 0, &ServiceParser::ParseCritical}},
501 {"disabled", {0, 0, &ServiceParser::ParseDisabled}},
502 {"enter_namespace", {2, 2, &ServiceParser::ParseEnterNamespace}},
503 {"file", {2, 2, &ServiceParser::ParseFile}},
504 {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
505 {"interface", {2, 2, &ServiceParser::ParseInterface}},
506 {"ioprio", {2, 2, &ServiceParser::ParseIoprio}},
507 {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}},
508 {"memcg.limit_in_bytes", {1, 1, &ServiceParser::ParseMemcgLimitInBytes}},
509 {"memcg.limit_percent", {1, 1, &ServiceParser::ParseMemcgLimitPercent}},
510 {"memcg.limit_property", {1, 1, &ServiceParser::ParseMemcgLimitProperty}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700511 {"memcg.soft_limit_in_bytes",
Tom Cherry60971e62019-09-10 10:40:47 -0700512 {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}},
513 {"memcg.swappiness", {1, 1, &ServiceParser::ParseMemcgSwappiness}},
514 {"namespace", {1, 2, &ServiceParser::ParseNamespace}},
515 {"oneshot", {0, 0, &ServiceParser::ParseOneshot}},
516 {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}},
517 {"oom_score_adjust", {1, 1, &ServiceParser::ParseOomScoreAdjust}},
518 {"override", {0, 0, &ServiceParser::ParseOverride}},
519 {"priority", {1, 1, &ServiceParser::ParsePriority}},
520 {"reboot_on_failure", {1, 1, &ServiceParser::ParseRebootOnFailure}},
521 {"restart_period", {1, 1, &ServiceParser::ParseRestartPeriod}},
522 {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}},
523 {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}},
524 {"setenv", {2, 2, &ServiceParser::ParseSetenv}},
525 {"shutdown", {1, 1, &ServiceParser::ParseShutdown}},
526 {"sigstop", {0, 0, &ServiceParser::ParseSigstop}},
527 {"socket", {3, 6, &ServiceParser::ParseSocket}},
Tom Cherryf74b7f52019-09-23 16:16:54 -0700528 {"stdio_to_kmsg", {0, 0, &ServiceParser::ParseStdioToKmsg}},
Tom Cherry60971e62019-09-10 10:40:47 -0700529 {"timeout_period", {1, 1, &ServiceParser::ParseTimeoutPeriod}},
530 {"updatable", {0, 0, &ServiceParser::ParseUpdatable}},
531 {"user", {1, 1, &ServiceParser::ParseUser}},
532 {"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700533 };
534 // clang-format on
Tom Cherryd52a5b32019-07-22 16:05:36 -0700535 return parser_map;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700536}
537
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700538Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
539 const std::string& filename, int line) {
540 if (args.size() < 3) {
541 return Error() << "services must have a name and a program";
542 }
543
544 const std::string& name = args[1];
545 if (!IsValidName(name)) {
546 return Error() << "invalid service name '" << name << "'";
547 }
548
549 filename_ = filename;
550
551 Subcontext* restart_action_subcontext = nullptr;
Tom Cherry14c24722019-09-18 13:47:19 -0700552 if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
553 restart_action_subcontext = subcontext_;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700554 }
555
556 std::vector<std::string> str_args(args.begin() + 2, args.end());
557
558 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
559 if (str_args[0] == "/sbin/watchdogd") {
560 str_args[0] = "/system/bin/watchdogd";
561 }
562 }
563
564 service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args);
565 return {};
566}
567
568Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700569 if (!service_) {
570 return {};
571 }
572
Tom Cherryd52a5b32019-07-22 16:05:36 -0700573 auto parser = GetParserMap().Find(args);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700574
575 if (!parser) return parser.error();
576
577 return std::invoke(*parser, this, std::move(args));
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700578}
579
580Result<void> ServiceParser::EndSection() {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700581 if (!service_) {
582 return {};
583 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700584
Daniel Norman3f42a762019-07-09 11:00:53 -0700585 if (interface_inheritance_hierarchy_) {
Daniel Normand2533c32019-08-02 15:13:50 -0700586 if (const auto& check_hierarchy_result = CheckInterfaceInheritanceHierarchy(
587 service_->interfaces(), *interface_inheritance_hierarchy_);
588 !check_hierarchy_result) {
589 return Error() << check_hierarchy_result.error();
Daniel Norman3f42a762019-07-09 11:00:53 -0700590 }
591 }
592
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700593 Service* old_service = service_list_->FindService(service_->name());
594 if (old_service) {
595 if (!service_->is_override()) {
596 return Error() << "ignored duplicate definition of service '" << service_->name()
597 << "'";
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700598 }
599
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700600 if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
601 return Error() << "cannot update a non-updatable service '" << service_->name()
602 << "' with a config in APEX";
603 }
604
605 service_list_->RemoveService(*old_service);
606 old_service = nullptr;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700607 }
608
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700609 service_list_->AddService(std::move(service_));
610
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700611 return {};
612}
613
614bool ServiceParser::IsValidName(const std::string& name) const {
615 // Property names can be any length, but may only contain certain characters.
616 // Property values can contain any characters, but may only be a certain length.
617 // (The latter restriction is needed because `start` and `stop` work by writing
618 // the service name to the "ctl.start" and "ctl.stop" properties.)
619 return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
620}
621
622} // namespace init
623} // namespace android