blob: 97621dac6b5a8368132f2b766a05b066421640df [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) {
Woody Lin45215ae2019-12-26 22:22:28 +080096 std::optional<std::string> fatal_reboot_target;
97 std::optional<std::chrono::minutes> fatal_crash_window;
98
99 for (auto it = args.begin() + 1; it != args.end(); ++it) {
100 auto arg = android::base::Split(*it, "=");
101 if (arg.size() != 2) {
102 return Error() << "critical: Argument '" << *it << "' is not supported";
103 } else if (arg[0] == "target") {
104 fatal_reboot_target = arg[1];
105 } else if (arg[0] == "window") {
106 int minutes;
107 auto window = ExpandProps(arg[1]);
108 if (!window.ok()) {
109 return Error() << "critical: Could not expand argument ': " << arg[1];
110 }
111 if (*window == "off") {
112 return {};
113 }
114 if (!ParseInt(*window, &minutes, 0)) {
115 return Error() << "critical: 'fatal_crash_window' must be an integer > 0";
116 }
117 fatal_crash_window = std::chrono::minutes(minutes);
118 } else {
119 return Error() << "critical: Argument '" << *it << "' is not supported";
120 }
121 }
122
123 if (fatal_reboot_target) {
124 service_->fatal_reboot_target_ = *fatal_reboot_target;
125 }
126 if (fatal_crash_window) {
127 service_->fatal_crash_window_ = *fatal_crash_window;
128 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700129 service_->flags_ |= SVC_CRITICAL;
130 return {};
131}
132
133Result<void> ServiceParser::ParseDisabled(std::vector<std::string>&& args) {
134 service_->flags_ |= SVC_DISABLED;
135 service_->flags_ |= SVC_RC_DISABLED;
136 return {};
137}
138
139Result<void> ServiceParser::ParseEnterNamespace(std::vector<std::string>&& args) {
140 if (args[1] != "net") {
141 return Error() << "Init only supports entering network namespaces";
142 }
143 if (!service_->namespaces_.namespaces_to_enter.empty()) {
144 return Error() << "Only one network namespace may be entered";
145 }
146 // Network namespaces require that /sys is remounted, otherwise the old adapters will still be
147 // present. Therefore, they also require mount namespaces.
148 service_->namespaces_.flags |= CLONE_NEWNS;
149 service_->namespaces_.namespaces_to_enter.emplace_back(CLONE_NEWNET, std::move(args[2]));
150 return {};
151}
152
153Result<void> ServiceParser::ParseGroup(std::vector<std::string>&& args) {
154 auto gid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900155 if (!gid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700156 return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error();
157 }
158 service_->proc_attr_.gid = *gid;
159
160 for (std::size_t n = 2; n < args.size(); n++) {
161 gid = DecodeUid(args[n]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900162 if (!gid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700163 return Error() << "Unable to decode GID for '" << args[n] << "': " << gid.error();
164 }
165 service_->proc_attr_.supp_gids.emplace_back(*gid);
166 }
167 return {};
168}
169
170Result<void> ServiceParser::ParsePriority(std::vector<std::string>&& args) {
171 service_->proc_attr_.priority = 0;
172 if (!ParseInt(args[1], &service_->proc_attr_.priority,
173 static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative
174 static_cast<int>(ANDROID_PRIORITY_LOWEST))) {
175 return Errorf("process priority value must be range {} - {}", ANDROID_PRIORITY_HIGHEST,
176 ANDROID_PRIORITY_LOWEST);
177 }
178 return {};
179}
180
181Result<void> ServiceParser::ParseInterface(std::vector<std::string>&& args) {
182 const std::string& interface_name = args[1];
183 const std::string& instance_name = args[2];
184
Jon Spivack16fb3f92019-07-26 13:14:42 -0700185 // AIDL services don't use fully qualified names and instead just use "interface aidl <name>"
186 if (interface_name != "aidl") {
187 FQName fq_name;
188 if (!FQName::parse(interface_name, &fq_name)) {
189 return Error() << "Invalid fully-qualified name for interface '" << interface_name
190 << "'";
191 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700192
Jon Spivack16fb3f92019-07-26 13:14:42 -0700193 if (!fq_name.isFullyQualified()) {
194 return Error() << "Interface name not fully-qualified '" << interface_name << "'";
195 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700196
Jon Spivack16fb3f92019-07-26 13:14:42 -0700197 if (fq_name.isValidValueName()) {
198 return Error() << "Interface name must not be a value name '" << interface_name << "'";
199 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700200 }
201
202 const std::string fullname = interface_name + "/" + instance_name;
203
204 for (const auto& svc : *service_list_) {
205 if (svc->interfaces().count(fullname) > 0) {
206 return Error() << "Interface '" << fullname << "' redefined in " << service_->name()
207 << " but is already defined by " << svc->name();
208 }
209 }
210
211 service_->interfaces_.insert(fullname);
212
213 return {};
214}
215
216Result<void> ServiceParser::ParseIoprio(std::vector<std::string>&& args) {
217 if (!ParseInt(args[2], &service_->proc_attr_.ioprio_pri, 0, 7)) {
218 return Error() << "priority value must be range 0 - 7";
219 }
220
221 if (args[1] == "rt") {
222 service_->proc_attr_.ioprio_class = IoSchedClass_RT;
223 } else if (args[1] == "be") {
224 service_->proc_attr_.ioprio_class = IoSchedClass_BE;
225 } else if (args[1] == "idle") {
226 service_->proc_attr_.ioprio_class = IoSchedClass_IDLE;
227 } else {
228 return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>";
229 }
230
231 return {};
232}
233
234Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) {
235 auto it = args.begin() + 1;
236 if (args.size() == 2 && StartsWith(args[1], "$")) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700237 auto expanded = ExpandProps(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900238 if (!expanded.ok()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700239 return expanded.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700240 }
241
242 // If the property is not set, it defaults to none, in which case there are no keycodes
243 // for this service.
Bernie Innocenti1cc76df2020-02-03 23:54:02 +0900244 if (*expanded == "none") {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700245 return {};
246 }
247
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700248 args = Split(*expanded, ",");
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700249 it = args.begin();
250 }
251
252 for (; it != args.end(); ++it) {
253 int code;
254 if (ParseInt(*it, &code, 0, KEY_MAX)) {
255 for (auto& key : service_->keycodes_) {
256 if (key == code) return Error() << "duplicate keycode: " << *it;
257 }
258 service_->keycodes_.insert(
259 std::upper_bound(service_->keycodes_.begin(), service_->keycodes_.end(), code),
260 code);
261 } else {
262 return Error() << "invalid keycode: " << *it;
263 }
264 }
265 return {};
266}
267
268Result<void> ServiceParser::ParseOneshot(std::vector<std::string>&& args) {
269 service_->flags_ |= SVC_ONESHOT;
270 return {};
271}
272
273Result<void> ServiceParser::ParseOnrestart(std::vector<std::string>&& args) {
274 args.erase(args.begin());
275 int line = service_->onrestart_.NumCommands() + 1;
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900276 if (auto result = service_->onrestart_.AddCommand(std::move(args), line); !result.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700277 return Error() << "cannot add Onrestart command: " << result.error();
278 }
279 return {};
280}
281
282Result<void> ServiceParser::ParseNamespace(std::vector<std::string>&& args) {
283 for (size_t i = 1; i < args.size(); i++) {
284 if (args[i] == "pid") {
285 service_->namespaces_.flags |= CLONE_NEWPID;
286 // PID namespaces require mount namespaces.
287 service_->namespaces_.flags |= CLONE_NEWNS;
288 } else if (args[i] == "mnt") {
289 service_->namespaces_.flags |= CLONE_NEWNS;
290 } else {
291 return Error() << "namespace must be 'pid' or 'mnt'";
292 }
293 }
294 return {};
295}
296
297Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) {
Suren Baghdasaryanc29c2ba2019-10-22 17:18:42 -0700298 if (!ParseInt(args[1], &service_->oom_score_adjust_, MIN_OOM_SCORE_ADJUST,
299 MAX_OOM_SCORE_ADJUST)) {
300 return Error() << "oom_score_adjust value must be in range " << MIN_OOM_SCORE_ADJUST
301 << " - +" << MAX_OOM_SCORE_ADJUST;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700302 }
303 return {};
304}
305
306Result<void> ServiceParser::ParseOverride(std::vector<std::string>&& args) {
307 service_->override_ = true;
308 return {};
309}
310
311Result<void> ServiceParser::ParseMemcgSwappiness(std::vector<std::string>&& args) {
312 if (!ParseInt(args[1], &service_->swappiness_, 0)) {
313 return Error() << "swappiness value must be equal or greater than 0";
314 }
315 return {};
316}
317
318Result<void> ServiceParser::ParseMemcgLimitInBytes(std::vector<std::string>&& args) {
319 if (!ParseInt(args[1], &service_->limit_in_bytes_, 0)) {
320 return Error() << "limit_in_bytes value must be equal or greater than 0";
321 }
322 return {};
323}
324
325Result<void> ServiceParser::ParseMemcgLimitPercent(std::vector<std::string>&& args) {
326 if (!ParseInt(args[1], &service_->limit_percent_, 0)) {
327 return Error() << "limit_percent value must be equal or greater than 0";
328 }
329 return {};
330}
331
332Result<void> ServiceParser::ParseMemcgLimitProperty(std::vector<std::string>&& args) {
333 service_->limit_property_ = std::move(args[1]);
334 return {};
335}
336
337Result<void> ServiceParser::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) {
338 if (!ParseInt(args[1], &service_->soft_limit_in_bytes_, 0)) {
339 return Error() << "soft_limit_in_bytes value must be equal or greater than 0";
340 }
341 return {};
342}
343
344Result<void> ServiceParser::ParseProcessRlimit(std::vector<std::string>&& args) {
345 auto rlimit = ParseRlimit(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900346 if (!rlimit.ok()) return rlimit.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700347
348 service_->proc_attr_.rlimits.emplace_back(*rlimit);
349 return {};
350}
351
Tom Cherry60971e62019-09-10 10:40:47 -0700352Result<void> ServiceParser::ParseRebootOnFailure(std::vector<std::string>&& args) {
353 if (service_->on_failure_reboot_target_) {
354 return Error() << "Only one reboot_on_failure command may be specified";
355 }
356 if (!StartsWith(args[1], "shutdown") && !StartsWith(args[1], "reboot")) {
357 return Error()
358 << "reboot_on_failure commands must begin with either 'shutdown' or 'reboot'";
359 }
360 service_->on_failure_reboot_target_ = std::move(args[1]);
361 return {};
362}
363
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700364Result<void> ServiceParser::ParseRestartPeriod(std::vector<std::string>&& args) {
365 int period;
366 if (!ParseInt(args[1], &period, 5)) {
367 return Error() << "restart_period value must be an integer >= 5";
368 }
369 service_->restart_period_ = std::chrono::seconds(period);
370 return {};
371}
372
373Result<void> ServiceParser::ParseSeclabel(std::vector<std::string>&& args) {
374 service_->seclabel_ = std::move(args[1]);
375 return {};
376}
377
378Result<void> ServiceParser::ParseSigstop(std::vector<std::string>&& args) {
379 service_->sigstop_ = true;
380 return {};
381}
382
383Result<void> ServiceParser::ParseSetenv(std::vector<std::string>&& args) {
384 service_->environment_vars_.emplace_back(std::move(args[1]), std::move(args[2]));
385 return {};
386}
387
388Result<void> ServiceParser::ParseShutdown(std::vector<std::string>&& args) {
389 if (args[1] == "critical") {
390 service_->flags_ |= SVC_SHUTDOWN_CRITICAL;
391 return {};
392 }
393 return Error() << "Invalid shutdown option";
394}
395
Suren Baghdasaryanc9c0bba2020-04-30 11:58:39 -0700396Result<void> ServiceParser::ParseTaskProfiles(std::vector<std::string>&& args) {
397 args.erase(args.begin());
398 service_->task_profiles_ = std::move(args);
399 return {};
400}
401
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700402Result<void> ServiceParser::ParseTimeoutPeriod(std::vector<std::string>&& args) {
403 int period;
404 if (!ParseInt(args[1], &period, 1)) {
405 return Error() << "timeout_period value must be an integer >= 1";
406 }
407 service_->timeout_period_ = std::chrono::seconds(period);
408 return {};
409}
410
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700411// name type perm [ uid gid context ]
412Result<void> ServiceParser::ParseSocket(std::vector<std::string>&& args) {
413 SocketDescriptor socket;
414 socket.name = std::move(args[1]);
415
416 auto types = Split(args[2], "+");
417 if (types[0] == "stream") {
418 socket.type = SOCK_STREAM;
419 } else if (types[0] == "dgram") {
420 socket.type = SOCK_DGRAM;
421 } else if (types[0] == "seqpacket") {
422 socket.type = SOCK_SEQPACKET;
423 } else {
424 return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket', got '" << types[0]
425 << "' instead.";
426 }
427
428 if (types.size() > 1) {
429 if (types.size() == 2 && types[1] == "passcred") {
430 socket.passcred = true;
431 } else {
432 return Error() << "Only 'passcred' may be used to modify the socket type";
433 }
434 }
435
436 errno = 0;
437 char* end = nullptr;
438 socket.perm = strtol(args[3].c_str(), &end, 8);
439 if (errno != 0) {
440 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
441 }
442 if (end == args[3].c_str() || *end != '\0') {
443 errno = EINVAL;
444 return ErrnoError() << "Unable to parse permissions '" << args[3] << "'";
445 }
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700446
447 if (args.size() > 4) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700448 auto uid = DecodeUid(args[4]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900449 if (!uid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700450 return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
451 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700452 socket.uid = *uid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700453 }
454
455 if (args.size() > 5) {
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700456 auto gid = DecodeUid(args[5]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900457 if (!gid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700458 return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
459 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700460 socket.gid = *gid;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700461 }
462
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700463 socket.context = args.size() > 6 ? args[6] : "";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700464
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700465 auto old = std::find_if(service_->sockets_.begin(), service_->sockets_.end(),
466 [&socket](const auto& other) { return socket.name == other.name; });
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700467
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700468 if (old != service_->sockets_.end()) {
469 return Error() << "duplicate socket descriptor '" << socket.name << "'";
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700470 }
471
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700472 service_->sockets_.emplace_back(std::move(socket));
473
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700474 return {};
475}
476
Tom Cherryf74b7f52019-09-23 16:16:54 -0700477Result<void> ServiceParser::ParseStdioToKmsg(std::vector<std::string>&& args) {
478 if (service_->flags_ & SVC_CONSOLE) {
479 return Error() << "'stdio_to_kmsg' and 'console' are mutually exclusive";
480 }
481 service_->proc_attr_.stdio_to_kmsg = true;
482 return {};
483}
484
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700485// name type
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700486Result<void> ServiceParser::ParseFile(std::vector<std::string>&& args) {
487 if (args[2] != "r" && args[2] != "w" && args[2] != "rw") {
488 return Error() << "file type must be 'r', 'w' or 'rw'";
489 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700490
491 FileDescriptor file;
492 file.type = args[2];
493
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700494 auto file_name = ExpandProps(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900495 if (!file_name.ok()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700496 return Error() << "Could not expand file path ': " << file_name.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700497 }
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700498 file.name = *file_name;
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700499 if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700500 return Error() << "file name must not be relative";
501 }
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700502
503 auto old = std::find_if(service_->files_.begin(), service_->files_.end(),
504 [&file](const auto& other) { return other.name == file.name; });
505
506 if (old != service_->files_.end()) {
507 return Error() << "duplicate file descriptor '" << file.name << "'";
508 }
509
510 service_->files_.emplace_back(std::move(file));
511
512 return {};
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700513}
514
515Result<void> ServiceParser::ParseUser(std::vector<std::string>&& args) {
516 auto uid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900517 if (!uid.ok()) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700518 return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
519 }
520 service_->proc_attr_.uid = *uid;
521 return {};
522}
523
524Result<void> ServiceParser::ParseWritepid(std::vector<std::string>&& args) {
525 args.erase(args.begin());
526 service_->writepid_files_ = std::move(args);
527 return {};
528}
529
530Result<void> ServiceParser::ParseUpdatable(std::vector<std::string>&& args) {
531 service_->updatable_ = true;
532 return {};
533}
534
Tom Cherryd52a5b32019-07-22 16:05:36 -0700535const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700536 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
537 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -0700538 static const KeywordMap<ServiceParser::OptionParser> parser_map = {
Tom Cherry60971e62019-09-10 10:40:47 -0700539 {"capabilities", {0, kMax, &ServiceParser::ParseCapabilities}},
540 {"class", {1, kMax, &ServiceParser::ParseClass}},
541 {"console", {0, 1, &ServiceParser::ParseConsole}},
Woody Lin45215ae2019-12-26 22:22:28 +0800542 {"critical", {0, 2, &ServiceParser::ParseCritical}},
Tom Cherry60971e62019-09-10 10:40:47 -0700543 {"disabled", {0, 0, &ServiceParser::ParseDisabled}},
544 {"enter_namespace", {2, 2, &ServiceParser::ParseEnterNamespace}},
545 {"file", {2, 2, &ServiceParser::ParseFile}},
546 {"group", {1, NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
547 {"interface", {2, 2, &ServiceParser::ParseInterface}},
548 {"ioprio", {2, 2, &ServiceParser::ParseIoprio}},
549 {"keycodes", {1, kMax, &ServiceParser::ParseKeycodes}},
550 {"memcg.limit_in_bytes", {1, 1, &ServiceParser::ParseMemcgLimitInBytes}},
551 {"memcg.limit_percent", {1, 1, &ServiceParser::ParseMemcgLimitPercent}},
552 {"memcg.limit_property", {1, 1, &ServiceParser::ParseMemcgLimitProperty}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700553 {"memcg.soft_limit_in_bytes",
Tom Cherry60971e62019-09-10 10:40:47 -0700554 {1, 1, &ServiceParser::ParseMemcgSoftLimitInBytes}},
555 {"memcg.swappiness", {1, 1, &ServiceParser::ParseMemcgSwappiness}},
556 {"namespace", {1, 2, &ServiceParser::ParseNamespace}},
557 {"oneshot", {0, 0, &ServiceParser::ParseOneshot}},
558 {"onrestart", {1, kMax, &ServiceParser::ParseOnrestart}},
559 {"oom_score_adjust", {1, 1, &ServiceParser::ParseOomScoreAdjust}},
560 {"override", {0, 0, &ServiceParser::ParseOverride}},
561 {"priority", {1, 1, &ServiceParser::ParsePriority}},
562 {"reboot_on_failure", {1, 1, &ServiceParser::ParseRebootOnFailure}},
563 {"restart_period", {1, 1, &ServiceParser::ParseRestartPeriod}},
564 {"rlimit", {3, 3, &ServiceParser::ParseProcessRlimit}},
565 {"seclabel", {1, 1, &ServiceParser::ParseSeclabel}},
566 {"setenv", {2, 2, &ServiceParser::ParseSetenv}},
567 {"shutdown", {1, 1, &ServiceParser::ParseShutdown}},
568 {"sigstop", {0, 0, &ServiceParser::ParseSigstop}},
569 {"socket", {3, 6, &ServiceParser::ParseSocket}},
Tom Cherryf74b7f52019-09-23 16:16:54 -0700570 {"stdio_to_kmsg", {0, 0, &ServiceParser::ParseStdioToKmsg}},
Suren Baghdasaryanc9c0bba2020-04-30 11:58:39 -0700571 {"task_profiles", {1, kMax, &ServiceParser::ParseTaskProfiles}},
Tom Cherry60971e62019-09-10 10:40:47 -0700572 {"timeout_period", {1, 1, &ServiceParser::ParseTimeoutPeriod}},
573 {"updatable", {0, 0, &ServiceParser::ParseUpdatable}},
574 {"user", {1, 1, &ServiceParser::ParseUser}},
575 {"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700576 };
577 // clang-format on
Tom Cherryd52a5b32019-07-22 16:05:36 -0700578 return parser_map;
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700579}
580
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700581Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
582 const std::string& filename, int line) {
583 if (args.size() < 3) {
584 return Error() << "services must have a name and a program";
585 }
586
587 const std::string& name = args[1];
588 if (!IsValidName(name)) {
589 return Error() << "invalid service name '" << name << "'";
590 }
591
592 filename_ = filename;
593
594 Subcontext* restart_action_subcontext = nullptr;
Tom Cherry14c24722019-09-18 13:47:19 -0700595 if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
596 restart_action_subcontext = subcontext_;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700597 }
598
599 std::vector<std::string> str_args(args.begin() + 2, args.end());
600
601 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
602 if (str_args[0] == "/sbin/watchdogd") {
603 str_args[0] = "/system/bin/watchdogd";
604 }
605 }
Yifan Hong8fb7f772019-10-16 14:22:12 -0700606 if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_Q__) {
607 if (str_args[0] == "/charger") {
608 str_args[0] = "/system/bin/charger";
609 }
610 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700611
Nikita Ioffe091c4d12019-12-05 12:35:19 +0000612 service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args, from_apex_);
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700613 return {};
614}
615
616Result<void> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700617 if (!service_) {
618 return {};
619 }
620
Tom Cherryd52a5b32019-07-22 16:05:36 -0700621 auto parser = GetParserMap().Find(args);
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700622
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900623 if (!parser.ok()) return parser.error();
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700624
625 return std::invoke(*parser, this, std::move(args));
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700626}
627
628Result<void> ServiceParser::EndSection() {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700629 if (!service_) {
630 return {};
631 }
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700632
Daniel Norman3f42a762019-07-09 11:00:53 -0700633 if (interface_inheritance_hierarchy_) {
Daniel Normand2533c32019-08-02 15:13:50 -0700634 if (const auto& check_hierarchy_result = CheckInterfaceInheritanceHierarchy(
635 service_->interfaces(), *interface_inheritance_hierarchy_);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900636 !check_hierarchy_result.ok()) {
Daniel Normand2533c32019-08-02 15:13:50 -0700637 return Error() << check_hierarchy_result.error();
Daniel Norman3f42a762019-07-09 11:00:53 -0700638 }
639 }
640
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100641 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) {
642 if ((service_->flags() & SVC_CRITICAL) != 0 && (service_->flags() & SVC_ONESHOT) != 0) {
643 return Error() << "service '" << service_->name()
644 << "' can't be both critical and oneshot";
645 }
646 }
647
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700648 Service* old_service = service_list_->FindService(service_->name());
649 if (old_service) {
650 if (!service_->is_override()) {
651 return Error() << "ignored duplicate definition of service '" << service_->name()
652 << "'";
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700653 }
654
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700655 if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
656 return Error() << "cannot update a non-updatable service '" << service_->name()
657 << "' with a config in APEX";
658 }
659
660 service_list_->RemoveService(*old_service);
661 old_service = nullptr;
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700662 }
663
Tom Cherryb1ffb1d2019-06-26 11:22:52 -0700664 service_list_->AddService(std::move(service_));
665
Tom Cherry2aeb1ad2019-06-26 10:46:20 -0700666 return {};
667}
668
669bool ServiceParser::IsValidName(const std::string& name) const {
670 // Property names can be any length, but may only contain certain characters.
671 // Property values can contain any characters, but may only be a certain length.
672 // (The latter restriction is needed because `start` and `stop` work by writing
673 // the service name to the "ctl.start" and "ctl.stop" properties.)
674 return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
675}
676
677} // namespace init
678} // namespace android