blob: cc445be4c329ac1cc3220ed79e7ef6c376e2faf4 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 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
Tom Cherryb7349902015-08-26 11:43:36 -070017#include "builtins.h"
18
Tom Cherry3041a512019-05-21 17:48:33 -070019#include <android/api-level.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070020#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070021#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <fcntl.h>
Wei Wang542aae42017-08-04 13:22:36 -070023#include <fts.h>
Jiyong Parkc2404402018-11-08 17:14:35 +090024#include <glob.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070025#include <linux/loop.h>
26#include <linux/module.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070027#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070028#include <net/if.h>
Mark Salyzynad575e02016-04-05 08:10:25 -070029#include <sched.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070030#include <signal.h>
Ray Essick35ffd692021-10-07 15:48:11 -070031#include <stdint.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070032#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070033#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070034#include <string.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035#include <sys/mount.h>
36#include <sys/resource.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070037#include <sys/socket.h>
38#include <sys/stat.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070039#include <sys/syscall.h>
Tom Cherryf57c0bf2017-04-07 13:46:21 -070040#include <sys/system_properties.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080041#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070042#include <sys/types.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070043#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070044#include <unistd.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050045
Ray Essick35ffd692021-10-07 15:48:11 -070046#include <map>
Nikita Ioffe12a36072019-10-23 20:11:32 +010047#include <memory>
48
Nikita Ioffe23dbd6d2019-11-14 01:21:24 +000049#include <InitProperties.sysprop.h>
Tom Cherryede0d532017-07-06 14:20:11 -070050#include <android-base/chrono_utils.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070051#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070052#include <android-base/logging.h>
Mark Salyzynffa52e92020-05-14 09:20:30 -070053#include <android-base/parsedouble.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080054#include <android-base/parseint.h>
Tom Cherryccf23532017-03-28 16:40:41 -070055#include <android-base/properties.h>
Tom Cherry70379912017-08-01 14:10:11 -070056#include <android-base/stringprintf.h>
Tom Cherryccf23532017-03-28 16:40:41 -070057#include <android-base/strings.h>
Tom Cherry70379912017-08-01 14:10:11 -070058#include <android-base/unique_fd.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070059#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070060#include <cutils/android_reboot.h>
Tom Cherryccf23532017-03-28 16:40:41 -070061#include <fs_mgr.h>
Eric Biggersf05da4a2018-10-23 13:10:33 -070062#include <fscrypt/fscrypt.h>
David Anderson0e330f12019-01-03 18:16:56 -080063#include <libgsi/libgsi.h>
Kiyoung Kime4d3f212019-12-16 14:31:04 +090064#include <logwrap/logwrap.h>
Oli Landc516722020-01-03 10:04:31 +000065#include <private/android_filesystem_config.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070066#include <selinux/android.h>
67#include <selinux/label.h>
68#include <selinux/selinux.h>
Wei Wang02628f32017-08-16 11:34:50 -070069#include <system/thread_defs.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070070
Tom Cherry7fd3bc22018-02-13 15:36:14 -080071#include "action_manager.h"
Tom Cherryb7349902015-08-26 11:43:36 -070072#include "bootchart.h"
Nikita Ioffec0df1872019-11-13 21:47:06 +000073#include "builtin_arguments.h"
Paul Crowley052f31c2019-08-26 10:33:17 -070074#include "fscrypt_init_extensions.h"
Tom Cherrybac32992015-07-31 12:45:25 -070075#include "init.h"
Jiyong Park68660412019-01-16 23:00:59 +090076#include "mount_namespace.h"
Tom Cherry67dee622017-07-27 12:54:48 -070077#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070078#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070079#include "reboot.h"
Tom Cherry7ac013d2017-08-25 10:39:25 -070080#include "rlimit_parser.h"
Vic Yang92c236e2019-05-28 15:58:35 -070081#include "selabel.h"
Joel Galenson4b591f12017-11-27 14:45:26 -080082#include "selinux.h"
Tom Cherrybac32992015-07-31 12:45:25 -070083#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070084#include "service_list.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070085#include "subcontext.h"
Tom Cherrybac32992015-07-31 12:45:25 -070086#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087
Paul Crowley0b8b2302016-12-19 13:03:47 -080088using namespace std::literals::string_literals;
89
Tom Cherrycf80b6d2019-01-07 14:25:31 -080090using android::base::Basename;
Jiyong Parkd185d4a2021-12-11 10:45:20 +090091using android::base::ResultError;
Tom Cherryc88d8f92019-08-19 15:21:25 -070092using android::base::SetProperty;
Wei Wang49d25982020-11-18 15:56:14 -080093using android::base::Split;
Tom Cherry1ab3dfc2019-04-22 17:46:37 -070094using android::base::StartsWith;
Tom Cherry7896e7a2019-09-04 15:26:52 -070095using android::base::StringPrintf;
Tom Cherry70379912017-08-01 14:10:11 -070096using android::base::unique_fd;
Tom Cherrya3530e62019-01-30 13:25:35 -080097using android::fs_mgr::Fstab;
98using android::fs_mgr::ReadFstabFromFile;
Tom Cherry70379912017-08-01 14:10:11 -070099
Nick Kralevichbc609542015-01-31 21:39:46 -0800100#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
101
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700102namespace android {
103namespace init {
104
Tom Cherryd17c3792019-07-30 10:51:59 -0700105// There are many legacy paths in rootdir/init.rc that will virtually never exist on a new
106// device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there
107// are 81 such failures on cuttlefish. Instead of spamming the log reporting them, we do not
108// report such failures unless we're running at the DEBUG log level.
109class ErrorIgnoreEnoent {
110 public:
111 ErrorIgnoreEnoent()
112 : ignore_error_(errno == ENOENT &&
113 android::base::GetMinimumLogSeverity() > android::base::DEBUG) {}
114 explicit ErrorIgnoreEnoent(int errno_to_append)
115 : error_(errno_to_append),
116 ignore_error_(errno_to_append == ENOENT &&
117 android::base::GetMinimumLogSeverity() > android::base::DEBUG) {}
118
119 template <typename T>
Jiyong Parkd185d4a2021-12-11 10:45:20 +0900120 operator android::base::expected<T, ResultError<int>>() {
Tom Cherryd17c3792019-07-30 10:51:59 -0700121 if (ignore_error_) {
122 return {};
123 }
124 return error_;
125 }
126
127 template <typename T>
128 ErrorIgnoreEnoent& operator<<(T&& t) {
129 error_ << t;
130 return *this;
131 }
132
133 private:
Jiyong Parkd185d4a2021-12-11 10:45:20 +0900134 Error<> error_;
Tom Cherryd17c3792019-07-30 10:51:59 -0700135 bool ignore_error_;
136};
137
138inline ErrorIgnoreEnoent ErrnoErrorIgnoreEnoent() {
139 return ErrorIgnoreEnoent(errno);
140}
141
Tom Cherry172c83f2019-06-26 14:44:37 -0700142std::vector<std::string> late_import_paths;
143
Elliott Hughes9605a942016-11-10 17:43:47 -0800144static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800145
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700146static Result<void> reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyc73b2152018-04-13 17:38:57 +0000147 LOG(ERROR) << "Rebooting into recovery";
Paul Crowleyaf8be582016-05-10 08:52:06 -0700148 std::string err;
149 if (!write_bootloader_message(options, &err)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700150 return Error() << "Failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700151 }
Tom Cherry18278d22019-11-12 16:21:20 -0800152 trigger_shutdown("reboot,recovery");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700153 return {};
Yusuke Sato0df08272015-07-08 14:57:07 -0700154}
155
Tom Cherry911b9b12017-07-27 16:20:58 -0700156template <typename F>
157static void ForEachServiceInClass(const std::string& classname, F function) {
158 for (const auto& service : ServiceList::GetInstance()) {
159 if (service->classnames().count(classname)) std::invoke(function, service);
160 }
161}
162
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700163static Result<void> do_class_start(const BuiltinArguments& args) {
Daniel Rosenbergca00b0e2018-11-27 22:08:02 -0800164 // Do not start a class if it has a property persist.dont_start_class.CLASS set to 1.
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200165 if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700166 return {};
Tom Cherry911b9b12017-07-27 16:20:58 -0700167 // Starting a class does not start services which are explicitly disabled.
168 // They must be started individually.
Tom Cherry20acdef2017-10-03 13:15:03 -0700169 for (const auto& service : ServiceList::GetInstance()) {
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200170 if (service->classnames().count(args[1])) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900171 if (auto result = service->StartIfNotDisabled(); !result.ok()) {
Tom Cherry20acdef2017-10-03 13:15:03 -0700172 LOG(ERROR) << "Could not start service '" << service->name()
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200173 << "' as part of class '" << args[1] << "': " << result.error();
Tom Cherry20acdef2017-10-03 13:15:03 -0700174 }
175 }
176 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700177 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178}
179
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700180static Result<void> do_class_stop(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700181 ForEachServiceInClass(args[1], &Service::Stop);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700182 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700183}
184
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700185static Result<void> do_class_reset(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700186 ForEachServiceInClass(args[1], &Service::Reset);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700187 return {};
Ken Sumrall752923c2010-12-03 16:33:31 -0800188}
189
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700190static Result<void> do_class_restart(const BuiltinArguments& args) {
Daniel Rosenbergca00b0e2018-11-27 22:08:02 -0800191 // Do not restart a class if it has a property persist.dont_start_class.CLASS set to 1.
192 if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700193 return {};
David Anderson2285b522021-11-09 18:26:39 -0800194
195 std::string classname;
196
197 CHECK(args.size() == 2 || args.size() == 3);
198
199 bool only_enabled = false;
200 if (args.size() == 3) {
201 if (args[1] != "--only-enabled") {
202 return Error() << "Unexpected argument: " << args[1];
203 }
204 only_enabled = true;
205 classname = args[2];
206 } else if (args.size() == 2) {
207 classname = args[1];
208 }
209
210 for (const auto& service : ServiceList::GetInstance()) {
211 if (!service->classnames().count(classname)) {
212 continue;
213 }
214 if (only_enabled && !service->IsEnabled()) {
215 continue;
216 }
217 service->Restart();
218 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700219 return {};
Steven Moreland2b63d542017-03-10 14:04:37 -0800220}
221
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700222static Result<void> do_domainname(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900223 if (auto result = WriteFile("/proc/sys/kernel/domainname", args[1]); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700224 return Error() << "Unable to write to /proc/sys/kernel/domainname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700225 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700226 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700229static Result<void> do_enable(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700230 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700231 if (!svc) return Error() << "Could not find service";
232
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900233 if (auto result = svc->Enable(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700234 return Error() << "Could not enable service: " << result.error();
235 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700236
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700237 return {};
JP Abgrall3beec7e2014-05-02 21:14:29 -0700238}
239
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700240static Result<void> do_exec(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700241 auto service = Service::MakeTemporaryOneshotService(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900242 if (!service.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -0700243 return Error() << "Could not create exec service: " << service.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700244 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900245 if (auto result = (*service)->ExecStart(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700246 return Error() << "Could not start exec service: " << result.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700247 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700248
Tom Cherry4772f1d2019-07-30 09:34:41 -0700249 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700250 return {};
Tom Cherryb27004a2017-03-27 16:27:30 -0700251}
252
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700253static Result<void> do_exec_background(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700254 auto service = Service::MakeTemporaryOneshotService(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900255 if (!service.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -0700256 return Error() << "Could not create exec background service: " << service.error();
Tom Cherry3631c542017-09-18 12:16:27 -0700257 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900258 if (auto result = (*service)->Start(); !result.ok()) {
Tom Cherry3631c542017-09-18 12:16:27 -0700259 return Error() << "Could not start exec background service: " << result.error();
260 }
261
Tom Cherry4772f1d2019-07-30 09:34:41 -0700262 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700263 return {};
Tom Cherry3631c542017-09-18 12:16:27 -0700264}
265
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700266static Result<void> do_exec_start(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700267 Service* service = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700268 if (!service) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700269 return Error() << "Service not found";
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700270 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700271
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900272 if (auto result = service->ExecStart(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700273 return Error() << "Could not start exec service: " << result.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700274 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700275
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700276 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277}
278
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700279static Result<void> do_export(const BuiltinArguments& args) {
Tom Cherry6de21f12017-08-22 15:41:03 -0700280 if (setenv(args[1].c_str(), args[2].c_str(), 1) == -1) {
281 return ErrnoError() << "setenv() failed";
Tom Cherry7fa62c52017-08-01 13:50:23 -0700282 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700283 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700284}
285
Artur Satayev500946b2021-02-15 20:48:49 +0000286static Result<void> do_load_exports(const BuiltinArguments& args) {
287 auto file_contents = ReadFile(args[1]);
288 if (!file_contents.ok()) {
289 return Error() << "Could not read input file '" << args[1]
290 << "': " << file_contents.error();
291 }
292
293 auto lines = Split(*file_contents, "\n");
294 for (const auto& line : lines) {
295 if (line.empty()) {
296 continue;
297 }
298
299 auto env = Split(line, " ");
300
301 if (env.size() != 3) {
302 return ErrnoError() << "Expected a line as `export <name> <value>`, found: `" << line
303 << "`";
304 }
305
306 if (env[0] != "export") {
307 return ErrnoError() << "Unknown action: '" << env[0] << "', expected 'export'";
308 }
309
310 if (setenv(env[1].c_str(), env[2].c_str(), 1) == -1) {
311 return ErrnoError() << "Failed to export '" << line << "' from " << args[1];
312 }
313 }
314
315 return {};
316}
317
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700318static Result<void> do_hostname(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900319 if (auto result = WriteFile("/proc/sys/kernel/hostname", args[1]); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700320 return Error() << "Unable to write to /proc/sys/kernel/hostname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700321 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700322 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323}
324
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700325static Result<void> do_ifup(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700326 struct ifreq ifr;
327
328 strlcpy(ifr.ifr_name, args[1].c_str(), IFNAMSIZ);
329
Tom Cherry247ffbf2019-07-08 15:09:36 -0700330 unique_fd s(TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)));
Tom Cherry7fa62c52017-08-01 13:50:23 -0700331 if (s < 0) return ErrnoError() << "opening socket failed";
332
333 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
334 return ErrnoError() << "ioctl(..., SIOCGIFFLAGS, ...) failed";
335 }
336
337 ifr.ifr_flags |= IFF_UP;
338
339 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
340 return ErrnoError() << "ioctl(..., SIOCSIFFLAGS, ...) failed";
341 }
342
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700343 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700344}
345
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700346static Result<void> do_insmod(const BuiltinArguments& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800347 int flags = 0;
348 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800349
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800350 if (!(*it).compare("-f")) {
351 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
352 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800353 }
354
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800355 std::string filename = *it++;
356 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
Tom Cherry7fa62c52017-08-01 13:50:23 -0700357
358 unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
359 if (fd == -1) return ErrnoError() << "open(\"" << filename << "\") failed";
360
361 int rc = syscall(__NR_finit_module, fd.get(), options.c_str(), flags);
362 if (rc == -1) return ErrnoError() << "finit_module for \"" << filename << "\" failed";
363
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700364 return {};
The Android Open Source Project35237d12008-12-17 18:08:08 -0800365}
366
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700367static Result<void> do_interface_restart(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700368 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
369 if (!svc) return Error() << "interface " << args[1] << " not found";
370 svc->Restart();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700371 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700372}
373
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700374static Result<void> do_interface_start(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700375 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
376 if (!svc) return Error() << "interface " << args[1] << " not found";
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900377 if (auto result = svc->Start(); !result.ok()) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700378 return Error() << "Could not start interface: " << result.error();
379 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700380 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700381}
382
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700383static Result<void> do_interface_stop(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700384 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
385 if (!svc) return Error() << "interface " << args[1] << " not found";
386 svc->Stop();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700387 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700388}
389
Oli Lan13e51e72019-11-19 18:08:45 +0000390static Result<void> make_dir_with_options(const MkdirOptions& options) {
Paul Crowley68258e82019-10-28 07:55:03 -0700391 std::string ref_basename;
Oli Lan13e51e72019-11-19 18:08:45 +0000392 if (options.ref_option == "ref") {
Paul Crowley68258e82019-10-28 07:55:03 -0700393 ref_basename = fscrypt_key_ref;
Oli Lan13e51e72019-11-19 18:08:45 +0000394 } else if (options.ref_option == "per_boot_ref") {
Paul Crowley68258e82019-10-28 07:55:03 -0700395 ref_basename = fscrypt_key_per_boot_ref;
396 } else {
Oli Lan13e51e72019-11-19 18:08:45 +0000397 return Error() << "Unknown key option: '" << options.ref_option << "'";
Paul Crowley1b4e7322019-08-25 12:18:43 -0700398 }
Paul Crowley68258e82019-10-28 07:55:03 -0700399
Paul Crowley1b4e7322019-08-25 12:18:43 -0700400 struct stat mstat;
Oli Lan13e51e72019-11-19 18:08:45 +0000401 if (lstat(options.target.c_str(), &mstat) != 0) {
Paul Crowley1b4e7322019-08-25 12:18:43 -0700402 if (errno != ENOENT) {
Oli Lan13e51e72019-11-19 18:08:45 +0000403 return ErrnoError() << "lstat() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700404 }
Oli Lan13e51e72019-11-19 18:08:45 +0000405 if (!make_dir(options.target, options.mode)) {
406 return ErrnoErrorIgnoreEnoent() << "mkdir() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700407 }
Oli Lan13e51e72019-11-19 18:08:45 +0000408 if (lstat(options.target.c_str(), &mstat) != 0) {
409 return ErrnoError() << "lstat() failed on new " << options.target;
Benoit Goby5c8574b2012-08-14 15:43:46 -0700410 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700411 }
Paul Crowley1b4e7322019-08-25 12:18:43 -0700412 if (!S_ISDIR(mstat.st_mode)) {
Oli Lan13e51e72019-11-19 18:08:45 +0000413 return Error() << "Not a directory on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700414 }
Oli Lan13e51e72019-11-19 18:08:45 +0000415 bool needs_chmod = (mstat.st_mode & ~S_IFMT) != options.mode;
416 if ((options.uid != static_cast<uid_t>(-1) && options.uid != mstat.st_uid) ||
417 (options.gid != static_cast<gid_t>(-1) && options.gid != mstat.st_gid)) {
418 if (lchown(options.target.c_str(), options.uid, options.gid) == -1) {
419 return ErrnoError() << "lchown failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700420 }
421 // chown may have cleared S_ISUID and S_ISGID, chmod again
422 needs_chmod = true;
423 }
424 if (needs_chmod) {
Oli Lan13e51e72019-11-19 18:08:45 +0000425 if (fchmodat(AT_FDCWD, options.target.c_str(), options.mode, AT_SYMLINK_NOFOLLOW) == -1) {
426 return ErrnoError() << "fchmodat() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700427 }
428 }
Eric Biggersf05da4a2018-10-23 13:10:33 -0700429 if (fscrypt_is_native()) {
Oli Lan13e51e72019-11-19 18:08:45 +0000430 if (!FscryptSetDirectoryPolicy(ref_basename, options.fscrypt_action, options.target)) {
Paul Crowleyc73b2152018-04-13 17:38:57 +0000431 return reboot_into_recovery(
Oli Lan13e51e72019-11-19 18:08:45 +0000432 {"--prompt_and_wipe_data", "--reason=set_policy_failed:"s + options.target});
Paul Crowleyaf8be582016-05-10 08:52:06 -0700433 }
434 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700435 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700436}
437
Oli Lan13e51e72019-11-19 18:08:45 +0000438// mkdir <path> [mode] [owner] [group] [<option> ...]
439static Result<void> do_mkdir(const BuiltinArguments& args) {
440 auto options = ParseMkdir(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900441 if (!options.ok()) return options.error();
Oli Lan13e51e72019-11-19 18:08:45 +0000442 return make_dir_with_options(*options);
443}
444
Alex Light68ab20f2016-06-23 11:11:39 -0700445/* umount <path> */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700446static Result<void> do_umount(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700447 if (umount(args[1].c_str()) < 0) {
448 return ErrnoError() << "umount() failed";
449 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700450 return {};
Alex Light68ab20f2016-06-23 11:11:39 -0700451}
452
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453static struct {
454 const char *name;
455 unsigned flag;
456} mount_flags[] = {
457 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200458 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700459 { "nosuid", MS_NOSUID },
460 { "nodev", MS_NODEV },
461 { "nodiratime", MS_NODIRATIME },
462 { "ro", MS_RDONLY },
463 { "rw", 0 },
464 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700465 { "bind", MS_BIND },
466 { "rec", MS_REC },
467 { "unbindable", MS_UNBINDABLE },
468 { "private", MS_PRIVATE },
469 { "slave", MS_SLAVE },
470 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471 { "defaults", 0 },
472 { 0, 0 },
473};
474
Ken Sumrall752923c2010-12-03 16:33:31 -0800475#define DATA_MNT_POINT "/data"
476
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700477/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700478static Result<void> do_mount(const BuiltinArguments& args) {
Tom Cherry70379912017-08-01 14:10:11 -0700479 const char* options = nullptr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700480 unsigned flags = 0;
Tom Cherry70379912017-08-01 14:10:11 -0700481 bool wait = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482
Tom Cherry70379912017-08-01 14:10:11 -0700483 for (size_t na = 4; na < args.size(); na++) {
484 size_t i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700485 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700486 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487 flags |= mount_flags[i].flag;
488 break;
489 }
490 }
491
Colin Crosscd0f1732010-04-19 17:10:24 -0700492 if (!mount_flags[i].name) {
Tom Cherry70379912017-08-01 14:10:11 -0700493 if (!args[na].compare("wait")) {
494 wait = true;
495 // If our last argument isn't a flag, wolf it up as an option string.
496 } else if (na + 1 == args.size()) {
Tom Cherry96f67312015-07-30 13:52:55 -0700497 options = args[na].c_str();
Tom Cherry70379912017-08-01 14:10:11 -0700498 }
Colin Crosscd0f1732010-04-19 17:10:24 -0700499 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700500 }
501
Tom Cherry70379912017-08-01 14:10:11 -0700502 const char* system = args[1].c_str();
503 const char* source = args[2].c_str();
504 const char* target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000505
Tom Cherry70379912017-08-01 14:10:11 -0700506 if (android::base::StartsWith(source, "loop@")) {
507 int mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
508 unique_fd fd(TEMP_FAILURE_RETRY(open(source + 5, mode | O_CLOEXEC)));
Tom Cherry7fa62c52017-08-01 13:50:23 -0700509 if (fd < 0) return ErrnoError() << "open(" << source + 5 << ", " << mode << ") failed";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000510
Tom Cherry70379912017-08-01 14:10:11 -0700511 for (size_t n = 0;; n++) {
512 std::string tmp = android::base::StringPrintf("/dev/block/loop%zu", n);
513 unique_fd loop(TEMP_FAILURE_RETRY(open(tmp.c_str(), mode | O_CLOEXEC)));
Tom Cherry7fa62c52017-08-01 13:50:23 -0700514 if (loop < 0) return ErrnoError() << "open(" << tmp << ", " << mode << ") failed";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000515
Tom Cherry70379912017-08-01 14:10:11 -0700516 loop_info info;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000517 /* if it is a blank loop device */
518 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
519 /* if it becomes our loop device */
Tom Cherry70379912017-08-01 14:10:11 -0700520 if (ioctl(loop, LOOP_SET_FD, fd.get()) >= 0) {
521 if (mount(tmp.c_str(), target, system, flags, options) < 0) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000522 ioctl(loop, LOOP_CLR_FD, 0);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700523 return ErrnoError() << "mount() failed";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000524 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700525 return {};
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000526 }
527 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000528 }
529
Tom Cherry7fa62c52017-08-01 13:50:23 -0700530 return Error() << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000531 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700532 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800533 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000534 if (mount(source, target, system, flags, options) < 0) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700535 return ErrnoErrorIgnoreEnoent() << "mount() failed";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000536 }
537
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700538 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800539
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700540 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700541}
542
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800543/* Imports .rc files from the specified paths. Default ones are applied if none is given.
544 *
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700545 * rc_paths: list of paths to rc files to import
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800546 */
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700547static void import_late(const std::vector<std::string>& rc_paths) {
Tom Cherry67dee622017-07-27 12:54:48 -0700548 auto& action_manager = ActionManager::GetInstance();
Tom Cherry911b9b12017-07-27 16:20:58 -0700549 auto& service_list = ServiceList::GetInstance();
550 Parser parser = CreateParser(action_manager, service_list);
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700551 if (rc_paths.empty()) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900552 // Fallbacks for partitions on which early mount isn't enabled.
Tom Cherry67dee622017-07-27 12:54:48 -0700553 for (const auto& path : late_import_paths) {
554 parser.ParseConfig(path);
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900555 }
Tom Cherry67dee622017-07-27 12:54:48 -0700556 late_import_paths.clear();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800557 } else {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700558 for (const auto& rc_path : rc_paths) {
559 parser.ParseConfig(rc_path);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800560 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700561 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700562
563 // Turning this on and letting the INFO logging be discarded adds 0.2s to
564 // Nexus 9 boot time, so it's disabled by default.
Tom Cherry30a6f272017-04-19 15:31:58 -0700565 if (false) DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700566}
567
Wei Wangd61a7e22016-08-23 11:58:09 -0700568/* Queue event based on fs_mgr return code.
569 *
570 * code: return code of fs_mgr_mount_all
571 *
572 * This function might request a reboot, in which case it will
573 * not return.
574 *
575 * return code is processed based on input code
576 */
Nikita Ioffe63898aa2019-11-08 12:24:02 +0000577static Result<void> queue_fs_event(int code, bool userdata_remount) {
Eric Biggerse5b5e372021-11-08 16:38:54 -0800578 if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Tom Cherryc88d8f92019-08-19 15:21:25 -0700579 SetProperty("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700580 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700581 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700582 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700583 /* Setup a wipe via recovery, and reboot into recovery */
David Anderson0e330f12019-01-03 18:16:56 -0800584 if (android::gsi::IsGsiRunning()) {
585 return Error() << "cannot wipe within GSI";
586 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700587 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800588 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
Paul Crowleyc73b2152018-04-13 17:38:57 +0000589 return reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700590 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700591 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Nikita Ioffe1f40c942019-12-04 17:47:37 +0000592 if (!FscryptInstallKeyring()) {
Paul Crowley68258e82019-10-28 07:55:03 -0700593 return Error() << "FscryptInstallKeyring() failed";
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000594 }
Tom Cherryc88d8f92019-08-19 15:21:25 -0700595 SetProperty("ro.crypto.state", "encrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000596
597 // Although encrypted, we have device key, so we do not need to
598 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700599 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700600 return {};
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700601 } else if (code == FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED) {
Nikita Ioffe1f40c942019-12-04 17:47:37 +0000602 if (!FscryptInstallKeyring()) {
Paul Crowley68258e82019-10-28 07:55:03 -0700603 return Error() << "FscryptInstallKeyring() failed";
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700604 }
Tom Cherryc88d8f92019-08-19 15:21:25 -0700605 SetProperty("ro.crypto.state", "encrypted");
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700606
Paul Crowleyc6846962018-01-30 09:56:03 -0800607 // Although encrypted, vold has already set the device up, so we do not need to
608 // do anything different from the nonencrypted case.
609 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700610 return {};
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700611 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
Nikita Ioffe1f40c942019-12-04 17:47:37 +0000612 if (!FscryptInstallKeyring()) {
Paul Crowley68258e82019-10-28 07:55:03 -0700613 return Error() << "FscryptInstallKeyring() failed";
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700614 }
Tom Cherryc88d8f92019-08-19 15:21:25 -0700615 SetProperty("ro.crypto.state", "encrypted");
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700616
Paul Crowleyc6846962018-01-30 09:56:03 -0800617 // Although encrypted, vold has already set the device up, so we do not need to
618 // do anything different from the nonencrypted case.
619 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700620 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700621 } else if (code > 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700622 Error() << "fs_mgr_mount_all() returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700623 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700624 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700625
Tom Cherry7fa62c52017-08-01 13:50:23 -0700626 return Error() << "Invalid code: " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700627}
628
Nikita Ioffe12a36072019-10-23 20:11:32 +0100629static int initial_mount_fstab_return_code = -1;
630
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700631/* <= Q: mount_all <fstab> [ <path> ]* [--<options>]*
632 * >= R: mount_all [ <fstab> ] [--<options>]*
Wei Wangd61a7e22016-08-23 11:58:09 -0700633 *
634 * This function might request a reboot, in which case it will
635 * not return.
636 */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700637static Result<void> do_mount_all(const BuiltinArguments& args) {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700638 auto mount_all = ParseMountAll(args.args);
639 if (!mount_all.ok()) return mount_all.error();
Wei Wangd61a7e22016-08-23 11:58:09 -0700640
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700641 const char* prop_post_fix = "default";
642 bool queue_event = true;
643 if (mount_all->mode == MOUNT_MODE_EARLY) {
644 prop_post_fix = "early";
645 queue_event = false;
646 } else if (mount_all->mode == MOUNT_MODE_LATE) {
647 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700648 }
649
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700650 std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
Tom Cherryede0d532017-07-06 14:20:11 -0700651 android::base::Timer t;
Tom Cherry990483d2019-04-17 12:55:50 -0700652
653 Fstab fstab;
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700654 if (mount_all->fstab_path.empty()) {
655 if (!ReadDefaultFstab(&fstab)) {
656 return Error() << "Could not read default fstab";
657 }
658 } else {
659 if (!ReadFstabFromFile(mount_all->fstab_path, &fstab)) {
660 return Error() << "Could not read fstab";
661 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700662 }
Nikita Ioffe12a36072019-10-23 20:11:32 +0100663
Nikita Ioffe9ede7ec2020-09-07 10:27:25 +0100664 auto mount_fstab_result = fs_mgr_mount_all(&fstab, mount_all->mode);
Tom Cherryc88d8f92019-08-19 15:21:25 -0700665 SetProperty(prop_name, std::to_string(t.duration().count()));
Wei Wangd61a7e22016-08-23 11:58:09 -0700666
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700667 if (mount_all->import_rc) {
668 import_late(mount_all->rc_paths);
Wei Wangd61a7e22016-08-23 11:58:09 -0700669 }
670
Nikita Ioffe9ede7ec2020-09-07 10:27:25 +0100671 if (mount_fstab_result.userdata_mounted) {
672 // This call to fs_mgr_mount_all mounted userdata. Keep the result in
673 // order for userspace reboot to correctly remount userdata.
674 LOG(INFO) << "Userdata mounted using "
675 << (mount_all->fstab_path.empty() ? "(default fstab)" : mount_all->fstab_path)
676 << " result : " << mount_fstab_result.code;
677 initial_mount_fstab_return_code = mount_fstab_result.code;
678 }
679
Wei Wangd61a7e22016-08-23 11:58:09 -0700680 if (queue_event) {
681 /* queue_fs_event will queue event based on mount_fstab return code
682 * and return processed return code*/
Nikita Ioffe9ede7ec2020-09-07 10:27:25 +0100683 auto queue_fs_result = queue_fs_event(mount_fstab_result.code, false);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900684 if (!queue_fs_result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700685 return Error() << "queue_fs_event() failed: " << queue_fs_result.error();
686 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700687 }
688
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700689 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700690}
691
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700692/* umount_all [ <fstab> ] */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700693static Result<void> do_umount_all(const BuiltinArguments& args) {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700694 auto umount_all = ParseUmountAll(args.args);
695 if (!umount_all.ok()) return umount_all.error();
696
Tom Cherry990483d2019-04-17 12:55:50 -0700697 Fstab fstab;
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700698 if (umount_all->empty()) {
699 if (!ReadDefaultFstab(&fstab)) {
700 return Error() << "Could not read default fstab";
701 }
702 } else {
703 if (!ReadFstabFromFile(*umount_all, &fstab)) {
704 return Error() << "Could not read fstab";
705 }
Tom Cherry990483d2019-04-17 12:55:50 -0700706 }
707
Tom Cherry3707d322019-08-15 13:07:24 -0700708 if (auto result = fs_mgr_umount_all(&fstab); result != 0) {
709 return Error() << "umount_fstab() failed " << result;
Yifan Hong402633d2019-04-09 10:11:34 -0700710 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700711 return {};
Yifan Hong402633d2019-04-09 10:11:34 -0700712}
713
Alistair Delvade28a862020-06-08 11:04:53 -0700714/* swapon_all [ <fstab> ] */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700715static Result<void> do_swapon_all(const BuiltinArguments& args) {
Alistair Delvade28a862020-06-08 11:04:53 -0700716 auto swapon_all = ParseSwaponAll(args.args);
717 if (!swapon_all.ok()) return swapon_all.error();
718
Tom Cherry30554572018-11-30 15:21:04 -0800719 Fstab fstab;
Alistair Delvade28a862020-06-08 11:04:53 -0700720 if (swapon_all->empty()) {
721 if (!ReadDefaultFstab(&fstab)) {
722 return Error() << "Could not read default fstab";
723 }
724 } else {
725 if (!ReadFstabFromFile(*swapon_all, &fstab)) {
726 return Error() << "Could not read fstab '" << *swapon_all << "'";
727 }
Tom Cherry30554572018-11-30 15:21:04 -0800728 }
Ken Sumralla76baaa2013-07-09 18:42:09 -0700729
Tom Cherry3707d322019-08-15 13:07:24 -0700730 if (!fs_mgr_swapon_all(fstab)) {
731 return Error() << "fs_mgr_swapon_all() failed";
Tom Cherry30554572018-11-30 15:21:04 -0800732 }
Ken Sumralla76baaa2013-07-09 18:42:09 -0700733
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700734 return {};
Ken Sumralla76baaa2013-07-09 18:42:09 -0700735}
736
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700737static Result<void> do_setprop(const BuiltinArguments& args) {
Tom Cherry1ab3dfc2019-04-22 17:46:37 -0700738 if (StartsWith(args[1], "ctl.")) {
739 return Error()
740 << "Cannot set ctl. properties from init; call the Service functions directly";
741 }
742 if (args[1] == kRestoreconProperty) {
743 return Error() << "Cannot set '" << kRestoreconProperty
744 << "' from init; use the restorecon builtin directly";
745 }
746
Tom Cherryc88d8f92019-08-19 15:21:25 -0700747 SetProperty(args[1], args[2]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700748 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700749}
750
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700751static Result<void> do_setrlimit(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700752 auto rlimit = ParseRlimit(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900753 if (!rlimit.ok()) return rlimit.error();
Tom Cherry7fa62c52017-08-01 13:50:23 -0700754
Tom Cherry7ac013d2017-08-25 10:39:25 -0700755 if (setrlimit(rlimit->first, &rlimit->second) == -1) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700756 return ErrnoError() << "setrlimit failed";
757 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700758 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700759}
760
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700761static Result<void> do_start(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700762 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700763 if (!svc) return Error() << "service " << args[1] << " not found";
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900764 if (auto result = svc->Start(); !result.ok()) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700765 return ErrorIgnoreEnoent() << "Could not start service: " << result.error();
Tom Cherry76af7e62017-08-22 16:13:59 -0700766 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700767 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700768}
769
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700770static Result<void> do_stop(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700771 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700772 if (!svc) return Error() << "service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700773 svc->Stop();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700774 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700775}
776
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700777static Result<void> do_restart(const BuiltinArguments& args) {
David Anderson6d7c7a22021-12-03 15:48:16 -0800778 bool only_if_running = false;
779 if (args.size() == 3) {
780 if (args[1] == "--only-if-running") {
781 only_if_running = true;
782 } else {
783 return Error() << "Unknown argument to restart: " << args[1];
784 }
785 }
786
787 const auto& classname = args[args.size() - 1];
788 Service* svc = ServiceList::GetInstance().FindService(classname);
789 if (!svc) return Error() << "service " << classname << " not found";
790 if (only_if_running && !svc->IsRunning()) {
791 return {};
792 }
Tom Cherrybac32992015-07-31 12:45:25 -0700793 svc->Restart();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700794 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795}
796
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700797static Result<void> do_trigger(const BuiltinArguments& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700798 ActionManager::GetInstance().QueueEventTrigger(args[1]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700799 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800}
801
Joel Galenson4b591f12017-11-27 14:45:26 -0800802static int MakeSymlink(const std::string& target, const std::string& linkpath) {
803 std::string secontext;
804 // Passing 0 for mode should work.
805 if (SelabelLookupFileContext(linkpath, 0, &secontext) && !secontext.empty()) {
806 setfscreatecon(secontext.c_str());
807 }
808
809 int rc = symlink(target.c_str(), linkpath.c_str());
810
811 if (!secontext.empty()) {
812 int save_errno = errno;
813 setfscreatecon(nullptr);
814 errno = save_errno;
815 }
816
817 return rc;
818}
819
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700820static Result<void> do_symlink(const BuiltinArguments& args) {
Joel Galenson4b591f12017-11-27 14:45:26 -0800821 if (MakeSymlink(args[1], args[2]) < 0) {
Tom Cherry68f2a462017-08-22 16:15:26 -0700822 // The symlink builtin is often used to create symlinks for older devices to be backwards
823 // compatible with new paths, therefore we skip reporting this error.
Tom Cherryd17c3792019-07-30 10:51:59 -0700824 return ErrnoErrorIgnoreEnoent() << "symlink() failed";
Elliott Hughesda46b392016-10-11 17:09:00 -0700825 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700826 return {};
The Android Open Source Project35237d12008-12-17 18:08:08 -0800827}
828
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700829static Result<void> do_rm(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700830 if (unlink(args[1].c_str()) < 0) {
831 return ErrnoError() << "unlink() failed";
832 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700833 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700834}
835
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700836static Result<void> do_rmdir(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700837 if (rmdir(args[1].c_str()) < 0) {
838 return ErrnoError() << "rmdir() failed";
839 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700840 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700841}
842
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700843static Result<void> do_sysclktz(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700844 struct timezone tz = {};
845 if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) {
846 return Error() << "Unable to parse mins_west_of_gmt";
847 }
848
849 if (settimeofday(nullptr, &tz) == -1) {
850 return ErrnoError() << "settimeofday() failed";
851 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700852 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700853}
854
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700855static Result<void> do_verity_update_state(const BuiltinArguments& args) {
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800856 int mode;
857 if (!fs_mgr_load_verity_state(&mode)) {
858 return Error() << "fs_mgr_load_verity_state() failed";
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700859 }
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800860
861 Fstab fstab;
862 if (!ReadDefaultFstab(&fstab)) {
863 return Error() << "Failed to read default fstab";
864 }
865
866 for (const auto& entry : fstab) {
867 if (!fs_mgr_is_verity_enabled(entry)) {
868 continue;
869 }
870
871 // To be consistent in vboot 1.0 and vboot 2.0 (AVB), use "system" for the partition even
872 // for system as root, so it has property [partition.system.verified].
873 std::string partition = entry.mount_point == "/" ? "system" : Basename(entry.mount_point);
Tom Cherryc88d8f92019-08-19 15:21:25 -0700874 SetProperty("partition." + partition + ".verified", std::to_string(mode));
Tianjie327237d2021-01-20 19:02:34 -0800875
Tianjie10bec652021-08-30 16:10:09 -0700876 auto hashtree_info = fs_mgr_get_hashtree_info(entry);
877 if (hashtree_info) {
878 SetProperty("partition." + partition + ".verified.hash_alg", hashtree_info->algorithm);
879 SetProperty("partition." + partition + ".verified.root_digest",
880 hashtree_info->root_digest);
Tianjie327237d2021-01-20 19:02:34 -0800881 }
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800882 }
883
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700884 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885}
886
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700887static Result<void> do_write(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900888 if (auto result = WriteFile(args[1], args[2]); !result.ok()) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700889 return ErrorIgnoreEnoent()
890 << "Unable to write to file '" << args[1] << "': " << result.error();
Tom Cherry7fa62c52017-08-01 13:50:23 -0700891 }
892
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700893 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700894}
895
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700896static Result<void> readahead_file(const std::string& filename, bool fully) {
Tom Cherry247ffbf2019-07-08 15:09:36 -0700897 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_CLOEXEC)));
Wei Wang02628f32017-08-16 11:34:50 -0700898 if (fd == -1) {
899 return ErrnoError() << "Error opening file";
900 }
901 if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED)) {
902 return ErrnoError() << "Error posix_fadvise file";
903 }
904 if (readahead(fd, 0, std::numeric_limits<size_t>::max())) {
905 return ErrnoError() << "Error readahead file";
906 }
907 if (fully) {
908 char buf[BUFSIZ];
909 ssize_t n;
910 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
911 }
912 if (n != 0) {
913 return ErrnoError() << "Error reading file";
914 }
915 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700916 return {};
Wei Wang02628f32017-08-16 11:34:50 -0700917}
918
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700919static Result<void> do_readahead(const BuiltinArguments& args) {
Wei Wang542aae42017-08-04 13:22:36 -0700920 struct stat sb;
921
922 if (stat(args[1].c_str(), &sb)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700923 return ErrnoError() << "Error opening " << args[1];
Wei Wang542aae42017-08-04 13:22:36 -0700924 }
925
Wei Wang02628f32017-08-16 11:34:50 -0700926 bool readfully = false;
927 if (args.size() == 3 && args[2] == "--fully") {
928 readfully = true;
929 }
Wei Wang542aae42017-08-04 13:22:36 -0700930 // We will do readahead in a forked process in order not to block init
931 // since it may block while it reads the
932 // filesystem metadata needed to locate the requested blocks. This
933 // occurs frequently with ext[234] on large files using indirect blocks
934 // instead of extents, giving the appearance that the call blocks until
935 // the requested data has been read.
936 pid_t pid = fork();
937 if (pid == 0) {
Wei Wang02628f32017-08-16 11:34:50 -0700938 if (setpriority(PRIO_PROCESS, 0, static_cast<int>(ANDROID_PRIORITY_LOWEST)) != 0) {
939 PLOG(WARNING) << "setpriority failed";
940 }
941 if (android_set_ioprio(0, IoSchedClass_IDLE, 7)) {
942 PLOG(WARNING) << "ioprio_get failed";
943 }
Wei Wang542aae42017-08-04 13:22:36 -0700944 android::base::Timer t;
945 if (S_ISREG(sb.st_mode)) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900946 if (auto result = readahead_file(args[1], readfully); !result.ok()) {
Wei Wang02628f32017-08-16 11:34:50 -0700947 LOG(WARNING) << "Unable to readahead '" << args[1] << "': " << result.error();
Wei Wang542aae42017-08-04 13:22:36 -0700948 _exit(EXIT_FAILURE);
949 }
950 } else if (S_ISDIR(sb.st_mode)) {
951 char* paths[] = {const_cast<char*>(args[1].data()), nullptr};
952 std::unique_ptr<FTS, decltype(&fts_close)> fts(
953 fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr), fts_close);
954 if (!fts) {
955 PLOG(ERROR) << "Error opening directory: " << args[1];
956 _exit(EXIT_FAILURE);
957 }
958 // Traverse the entire hierarchy and do readahead
959 for (FTSENT* ftsent = fts_read(fts.get()); ftsent != nullptr;
960 ftsent = fts_read(fts.get())) {
961 if (ftsent->fts_info & FTS_F) {
Wei Wang02628f32017-08-16 11:34:50 -0700962 const std::string filename = ftsent->fts_accpath;
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900963 if (auto result = readahead_file(filename, readfully); !result.ok()) {
Wei Wang02628f32017-08-16 11:34:50 -0700964 LOG(WARNING)
965 << "Unable to readahead '" << filename << "': " << result.error();
Wei Wang542aae42017-08-04 13:22:36 -0700966 }
967 }
968 }
969 }
Wei Wang02628f32017-08-16 11:34:50 -0700970 LOG(INFO) << "Readahead " << args[1] << " took " << t << " asynchronously";
Wei Wang542aae42017-08-04 13:22:36 -0700971 _exit(0);
972 } else if (pid < 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700973 return ErrnoError() << "Fork failed";
Wei Wang542aae42017-08-04 13:22:36 -0700974 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700975 return {};
Wei Wang542aae42017-08-04 13:22:36 -0700976}
977
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700978static Result<void> do_copy(const BuiltinArguments& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700979 auto file_contents = ReadFile(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900980 if (!file_contents.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700981 return Error() << "Could not read input file '" << args[1] << "': " << file_contents.error();
San Mehat7c44fe52009-08-26 16:39:25 -0700982 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900983 if (auto result = WriteFile(args[2], *file_contents); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700984 return Error() << "Could not write to output file '" << args[2] << "': " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700985 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700986
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700987 return {};
San Mehat7c44fe52009-08-26 16:39:25 -0700988}
989
Wei Wang49d25982020-11-18 15:56:14 -0800990static Result<void> do_copy_per_line(const BuiltinArguments& args) {
991 std::string file_contents;
992 if (!android::base::ReadFileToString(args[1], &file_contents, true)) {
993 return Error() << "Could not read input file '" << args[1] << "'";
994 }
995 auto lines = Split(file_contents, "\n");
996 for (const auto& line : lines) {
997 auto result = WriteFile(args[2], line);
998 if (!result.ok()) {
999 LOG(VERBOSE) << "Could not write to output file '" << args[2] << "' with '" << line
1000 << "' : " << result.error();
1001 }
1002 }
1003
1004 return {};
1005}
1006
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001007static Result<void> do_chown(const BuiltinArguments& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -07001008 auto uid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001009 if (!uid.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001010 return Error() << "Unable to decode UID for '" << args[1] << "': " << uid.error();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001011 }
Tom Cherry517e1f12017-05-04 17:40:33 -07001012
1013 // GID is optional and pushes the index of path out by one if specified.
1014 const std::string& path = (args.size() == 4) ? args[3] : args[2];
Tom Cherry62ca6632017-08-03 12:54:07 -07001015 Result<gid_t> gid = -1;
Tom Cherry517e1f12017-05-04 17:40:33 -07001016
1017 if (args.size() == 4) {
Tom Cherry62ca6632017-08-03 12:54:07 -07001018 gid = DecodeUid(args[2]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001019 if (!gid.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001020 return Error() << "Unable to decode GID for '" << args[2] << "': " << gid.error();
Tom Cherry517e1f12017-05-04 17:40:33 -07001021 }
1022 }
1023
Tom Cherry7fa62c52017-08-01 13:50:23 -07001024 if (lchown(path.c_str(), *uid, *gid) == -1) {
Tom Cherryd17c3792019-07-30 10:51:59 -07001025 return ErrnoErrorIgnoreEnoent() << "lchown() failed";
Tom Cherry7fa62c52017-08-01 13:50:23 -07001026 }
Tom Cherry517e1f12017-05-04 17:40:33 -07001027
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001028 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001029}
1030
1031static mode_t get_mode(const char *s) {
1032 mode_t mode = 0;
1033 while (*s) {
1034 if (*s >= '0' && *s <= '7') {
1035 mode = (mode<<3) | (*s-'0');
1036 } else {
1037 return -1;
1038 }
1039 s++;
1040 }
1041 return mode;
1042}
1043
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001044static Result<void> do_chmod(const BuiltinArguments& args) {
Tom Cherry96f67312015-07-30 13:52:55 -07001045 mode_t mode = get_mode(args[1].c_str());
1046 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
Tom Cherryd17c3792019-07-30 10:51:59 -07001047 return ErrnoErrorIgnoreEnoent() << "fchmodat() failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001048 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001049 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001050}
1051
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001052static Result<void> do_restorecon(const BuiltinArguments& args) {
Tom Cherry4772f1d2019-07-30 09:34:41 -07001053 auto restorecon_info = ParseRestorecon(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001054 if (!restorecon_info.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -07001055 return restorecon_info.error();
1056 }
1057
1058 const auto& [flag, paths] = *restorecon_info;
1059
Stephen Smalley726e8f72013-10-09 16:02:09 -04001060 int ret = 0;
Tom Cherry4772f1d2019-07-30 09:34:41 -07001061 for (const auto& path : paths) {
1062 if (selinux_android_restorecon(path.c_str(), flag) < 0) {
1063 ret = errno;
Paul Lawrencea8d84342016-11-14 15:40:18 -08001064 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001065 }
Tom Cherry7fa62c52017-08-01 13:50:23 -07001066
Tom Cherryd17c3792019-07-30 10:51:59 -07001067 if (ret) return ErrnoErrorIgnoreEnoent() << "selinux_android_restorecon() failed";
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001068 return {};
Stephen Smalley726e8f72013-10-09 16:02:09 -04001069}
1070
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001071static Result<void> do_restorecon_recursive(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001072 std::vector<std::string> non_const_args(args.args);
Paul Lawrencea8d84342016-11-14 15:40:18 -08001073 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001074 return do_restorecon({std::move(non_const_args), args.context});
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001075}
1076
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001077static Result<void> do_loglevel(const BuiltinArguments& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001078 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -07001079 int log_level = -1;
1080 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001081 android::base::LogSeverity severity;
1082 switch (log_level) {
1083 case 7: severity = android::base::DEBUG; break;
1084 case 6: severity = android::base::INFO; break;
1085 case 5:
1086 case 4: severity = android::base::WARNING; break;
1087 case 3: severity = android::base::ERROR; break;
1088 case 2:
1089 case 1:
1090 case 0: severity = android::base::FATAL; break;
1091 default:
Tom Cherry7fa62c52017-08-01 13:50:23 -07001092 return Error() << "invalid log level " << log_level;
Riley Andrews1bbef882014-06-26 13:55:03 -07001093 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001094 android::base::SetMinimumLogSeverity(severity);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001095 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001096}
1097
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001098static Result<void> do_load_persist_props(const BuiltinArguments& args) {
Tom Cherry1ab3dfc2019-04-22 17:46:37 -07001099 SendLoadPersistentPropertiesMessage();
1100
1101 start_waiting_for_property("ro.persistent_properties.ready", "true");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001102 return {};
Ken Sumrallc5c51032011-03-08 17:01:29 -08001103}
1104
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001105static Result<void> do_load_system_props(const BuiltinArguments& args) {
Jiyong Park3b316ee2019-01-13 02:42:31 +09001106 LOG(INFO) << "deprecated action `load_system_props` called.";
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001107 return {};
Riley Andrewse4b7b292014-06-16 15:06:21 -07001108}
1109
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001110static Result<void> do_wait(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001111 auto timeout = kCommandRetryTimeout;
1112 if (args.size() == 3) {
Mark Salyzynffa52e92020-05-14 09:20:30 -07001113 double timeout_double;
1114 if (!android::base::ParseDouble(args[2], &timeout_double, 0)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001115 return Error() << "failed to parse timeout";
Elliott Hughesda46b392016-10-11 17:09:00 -07001116 }
Mark Salyzynffa52e92020-05-14 09:20:30 -07001117 timeout = std::chrono::duration_cast<std::chrono::nanoseconds>(
1118 std::chrono::duration<double>(timeout_double));
Elliott Hughesda46b392016-10-11 17:09:00 -07001119 }
Tom Cherry7fa62c52017-08-01 13:50:23 -07001120
1121 if (wait_for_file(args[1].c_str(), timeout) != 0) {
1122 return Error() << "wait_for_file() failed";
1123 }
1124
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001125 return {};
Colin Crosscd0f1732010-04-19 17:10:24 -07001126}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001127
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001128static Result<void> do_wait_for_prop(const BuiltinArguments& args) {
Wei Wang132ac312017-01-25 16:27:03 -08001129 const char* name = args[1].c_str();
1130 const char* value = args[2].c_str();
1131 size_t value_len = strlen(value);
1132
Tom Cherryde6bd502018-02-13 16:50:08 -08001133 if (!IsLegalPropertyName(name)) {
1134 return Error() << "IsLegalPropertyName(" << name << ") failed";
Wei Wang132ac312017-01-25 16:27:03 -08001135 }
1136 if (value_len >= PROP_VALUE_MAX) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001137 return Error() << "value too long";
Wei Wang132ac312017-01-25 16:27:03 -08001138 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -08001139 if (!start_waiting_for_property(name, value)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001140 return Error() << "already waiting for a property";
Wei Wang132ac312017-01-25 16:27:03 -08001141 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001142 return {};
Wei Wang132ac312017-01-25 16:27:03 -08001143}
1144
Paul Crowley749af8c2015-05-28 17:35:06 +01001145static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -07001146 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +01001147}
1148
Tom Cherry7896e7a2019-09-04 15:26:52 -07001149static Result<void> ExecWithFunctionOnFailure(const std::vector<std::string>& args,
1150 std::function<void(const std::string&)> function) {
1151 auto service = Service::MakeTemporaryOneshotService(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001152 if (!service.ok()) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001153 function("MakeTemporaryOneshotService failed: " + service.error().message());
Paul Crowleyc73b2152018-04-13 17:38:57 +00001154 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001155 (*service)->AddReapCallback([function](const siginfo_t& siginfo) {
Paul Crowleyc73b2152018-04-13 17:38:57 +00001156 if (siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001157 function(StringPrintf("Exec service failed, status %d", siginfo.si_status));
Paul Crowleyc73b2152018-04-13 17:38:57 +00001158 }
1159 });
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001160 if (auto result = (*service)->ExecStart(); !result.ok()) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001161 function("ExecStart failed: " + result.error().message());
Paul Crowleyc73b2152018-04-13 17:38:57 +00001162 }
Tom Cherry4772f1d2019-07-30 09:34:41 -07001163 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001164 return {};
Paul Crowleyc73b2152018-04-13 17:38:57 +00001165}
1166
Tom Cherry7896e7a2019-09-04 15:26:52 -07001167static Result<void> ExecVdcRebootOnFailure(const std::string& vdc_arg) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001168 bool should_reboot_into_recovery = true;
Tom Cherry7896e7a2019-09-04 15:26:52 -07001169 auto reboot_reason = vdc_arg + "_failed";
Nikita Ioffe05506f02020-01-15 14:43:55 +00001170 if (android::sysprop::InitProperties::userspace_reboot_in_progress().value_or(false)) {
1171 should_reboot_into_recovery = false;
Nikita Ioffe4a787d92020-01-15 23:23:13 +00001172 reboot_reason = "userspace_failed," + vdc_arg;
Nikita Ioffe05506f02020-01-15 14:43:55 +00001173 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001174
Nikita Ioffe05506f02020-01-15 14:43:55 +00001175 auto reboot = [reboot_reason, should_reboot_into_recovery](const std::string& message) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001176 // TODO (b/122850122): support this in gsi
Nikita Ioffe05506f02020-01-15 14:43:55 +00001177 if (should_reboot_into_recovery) {
1178 if (fscrypt_is_native() && !android::gsi::IsGsiRunning()) {
1179 LOG(ERROR) << message << ": Rebooting into recovery, reason: " << reboot_reason;
1180 if (auto result = reboot_into_recovery(
1181 {"--prompt_and_wipe_data", "--reason="s + reboot_reason});
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001182 !result.ok()) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001183 LOG(FATAL) << "Could not reboot into recovery: " << result.error();
1184 }
1185 } else {
1186 LOG(ERROR) << "Failure (reboot suppressed): " << reboot_reason;
Tom Cherry7896e7a2019-09-04 15:26:52 -07001187 }
1188 } else {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001189 LOG(ERROR) << message << ": rebooting, reason: " << reboot_reason;
1190 trigger_shutdown("reboot," + reboot_reason);
Tom Cherry7896e7a2019-09-04 15:26:52 -07001191 }
1192 };
1193
1194 std::vector<std::string> args = {"exec", "/system/bin/vdc", "--wait", "cryptfs", vdc_arg};
1195 return ExecWithFunctionOnFailure(args, reboot);
1196}
1197
Nikita Ioffe12a36072019-10-23 20:11:32 +01001198static Result<void> do_remount_userdata(const BuiltinArguments& args) {
1199 if (initial_mount_fstab_return_code == -1) {
1200 return Error() << "Calling remount_userdata too early";
1201 }
1202 Fstab fstab;
1203 if (!ReadDefaultFstab(&fstab)) {
1204 // TODO(b/135984674): should we reboot here?
1205 return Error() << "Failed to read fstab";
1206 }
1207 // TODO(b/135984674): check that fstab contains /data.
1208 if (auto rc = fs_mgr_remount_userdata_into_checkpointing(&fstab); rc < 0) {
Gavin Corkery8c922562020-05-11 14:10:09 +01001209 std::string proc_mounts_output;
1210 android::base::ReadFileToString("/proc/mounts", &proc_mounts_output, true);
1211 android::base::WriteStringToFile(proc_mounts_output,
1212 "/metadata/userspacereboot/mount_info.txt");
Nikita Ioffe4a787d92020-01-15 23:23:13 +00001213 trigger_shutdown("reboot,mount_userdata_failed");
Nikita Ioffe12a36072019-10-23 20:11:32 +01001214 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001215 if (auto result = queue_fs_event(initial_mount_fstab_return_code, true); !result.ok()) {
Nikita Ioffe12a36072019-10-23 20:11:32 +01001216 return Error() << "queue_fs_event() failed: " << result.error();
1217 }
1218 return {};
1219}
1220
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001221static Result<void> do_installkey(const BuiltinArguments& args) {
1222 if (!is_file_crypto()) return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -07001223
Eric Biggersf05da4a2018-10-23 13:10:33 -07001224 auto unencrypted_dir = args[1] + fscrypt_unencrypted_folder;
Tom Cherryc3692b32017-08-10 12:22:44 -07001225 if (!make_dir(unencrypted_dir, 0700) && errno != EEXIST) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001226 return ErrnoError() << "Failed to create " << unencrypted_dir;
Janis Danisevskis9cc51722017-03-29 14:50:01 -07001227 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001228 return ExecVdcRebootOnFailure("enablefilecrypto");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001229}
Paul Crowley749af8c2015-05-28 17:35:06 +01001230
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001231static Result<void> do_init_user0(const BuiltinArguments& args) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001232 return ExecVdcRebootOnFailure("init_user0");
Paul Crowley59497452016-02-01 16:37:13 +00001233}
1234
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001235static Result<void> do_mark_post_data(const BuiltinArguments& args) {
Martijn Coenen70788f92019-04-23 16:26:01 +02001236 ServiceList::GetInstance().MarkPostData();
1237
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001238 return {};
Martijn Coenen70788f92019-04-23 16:26:01 +02001239}
1240
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001241static Result<void> GenerateLinkerConfiguration() {
Kiyoung Kim03b9bca2020-11-30 14:49:36 +09001242 const char* linkerconfig_binary = "/apex/com.android.runtime/bin/linkerconfig";
Kiyoung Kim3b2dbe92019-12-30 18:44:41 +09001243 const char* linkerconfig_target = "/linkerconfig";
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001244 const char* arguments[] = {linkerconfig_binary, "--target", linkerconfig_target};
1245
1246 if (logwrap_fork_execvp(arraysize(arguments), arguments, nullptr, false, LOG_KLOG, false,
1247 nullptr) != 0) {
1248 return ErrnoError() << "failed to execute linkerconfig";
1249 }
1250
Kiyoung Kim0cbee0d2021-03-02 16:45:27 +09001251 auto current_mount_ns = GetCurrentMountNamespace();
1252 if (!current_mount_ns.ok()) {
1253 return current_mount_ns.error();
1254 }
1255 if (*current_mount_ns == NS_DEFAULT) {
1256 SetDefaultMountNamespaceReady();
1257 }
1258
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001259 LOG(INFO) << "linkerconfig generated " << linkerconfig_target
1260 << " with mounted APEX modules info";
1261
1262 return {};
1263}
1264
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001265static Result<void> MountLinkerConfigForDefaultNamespace() {
1266 // No need to mount linkerconfig for default mount namespace if the path does not exist (which
1267 // would mean it is already mounted)
1268 if (access("/linkerconfig/default", 0) != 0) {
1269 return {};
1270 }
1271
1272 if (mount("/linkerconfig/default", "/linkerconfig", nullptr, MS_BIND | MS_REC, nullptr) != 0) {
1273 return ErrnoError() << "Failed to mount linker configuration for default mount namespace.";
1274 }
1275
1276 return {};
1277}
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001278static Result<void> do_update_linker_config(const BuiltinArguments&) {
Shikha Malhotra720694d2021-07-30 12:35:27 +00001279 return GenerateLinkerConfiguration();
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001280}
1281
Oli Lan13e51e72019-11-19 18:08:45 +00001282static Result<void> parse_apex_configs() {
Jiyong Parkc2404402018-11-08 17:14:35 +09001283 glob_t glob_result;
Ray Essick35ffd692021-10-07 15:48:11 -07001284 static constexpr char glob_pattern[] = "/apex/*/etc/*rc";
Jiyong Parkf169f722018-11-16 16:23:20 +09001285 const int ret = glob(glob_pattern, GLOB_MARK, nullptr, &glob_result);
1286 if (ret != 0 && ret != GLOB_NOMATCH) {
Jiyong Parkc2404402018-11-08 17:14:35 +09001287 globfree(&glob_result);
1288 return Error() << "glob pattern '" << glob_pattern << "' failed";
1289 }
1290 std::vector<std::string> configs;
Nikita Ioffe091c4d12019-12-05 12:35:19 +00001291 Parser parser = CreateServiceOnlyParser(ServiceList::GetInstance(), true);
Jiyong Parkc2404402018-11-08 17:14:35 +09001292 for (size_t i = 0; i < glob_result.gl_pathc; i++) {
Jiyong Parkd2217b52019-06-07 19:18:46 +09001293 std::string path = glob_result.gl_pathv[i];
1294 // Filter-out /apex/<name>@<ver> paths. The paths are bind-mounted to
1295 // /apex/<name> paths, so unless we filter them out, we will parse the
1296 // same file twice.
1297 std::vector<std::string> paths = android::base::Split(path, "/");
Jooyung Han9c4fa662019-07-05 11:22:18 +09001298 if (paths.size() >= 3 && paths[2].find('@') != std::string::npos) {
Jiyong Parkd2217b52019-06-07 19:18:46 +09001299 continue;
1300 }
Ray Essick35ffd692021-10-07 15:48:11 -07001301 // Filter directories
1302 if (path.back() == '/') {
1303 continue;
1304 }
Jiyong Parkd2217b52019-06-07 19:18:46 +09001305 configs.push_back(path);
Jiyong Parkc2404402018-11-08 17:14:35 +09001306 }
1307 globfree(&glob_result);
1308
Ray Essick35ffd692021-10-07 15:48:11 -07001309 // Compare all files /apex/path.#rc and /apex/path.rc with the same "/apex/path" prefix,
1310 // choosing the one with the highest # that doesn't exceed the system's SDK.
1311 // (.rc == .0rc for ranking purposes)
1312 //
1313 int active_sdk = android::base::GetIntProperty("ro.build.version.sdk", INT_MAX);
1314
1315 std::map<std::string, std::pair<std::string, int>> script_map;
1316
Jiyong Parkc2404402018-11-08 17:14:35 +09001317 for (const auto& c : configs) {
Ray Essick35ffd692021-10-07 15:48:11 -07001318 int sdk = 0;
1319 const std::vector<std::string> parts = android::base::Split(c, ".");
1320 std::string base;
1321 if (parts.size() < 2) {
Jiyong Parkc2404402018-11-08 17:14:35 +09001322 continue;
1323 }
Ray Essick35ffd692021-10-07 15:48:11 -07001324
1325 // parts[size()-1], aka the suffix, should be "rc" or "#rc"
1326 // any other pattern gets discarded
1327
1328 const auto& suffix = parts[parts.size() - 1];
1329 if (suffix == "rc") {
1330 sdk = 0;
1331 } else {
1332 char trailer[9] = {0};
1333 int r = sscanf(suffix.c_str(), "%d%8s", &sdk, trailer);
1334 if (r != 2) {
1335 continue;
1336 }
1337 if (strlen(trailer) > 2 || strcmp(trailer, "rc") != 0) {
1338 continue;
1339 }
1340 }
1341
1342 if (sdk < 0 || sdk > active_sdk) {
1343 continue;
1344 }
1345
1346 base = parts[0];
1347 for (unsigned int i = 1; i < parts.size() - 1; i++) {
1348 base = base + "." + parts[i];
1349 }
1350
1351 // is this preferred over what we already have
1352 auto it = script_map.find(base);
1353 if (it == script_map.end() || it->second.second < sdk) {
1354 script_map[base] = std::make_pair(c, sdk);
1355 }
1356 }
1357
1358 bool success = true;
1359 for (const auto& m : script_map) {
1360 success &= parser.ParseConfigFile(m.second.first);
Jiyong Parkc2404402018-11-08 17:14:35 +09001361 }
Jiyong Park80aa4472018-11-12 12:08:41 +09001362 ServiceList::GetInstance().MarkServicesUpdate();
Jiyong Parkc2404402018-11-08 17:14:35 +09001363 if (success) {
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001364 return {};
Jiyong Parkc2404402018-11-08 17:14:35 +09001365 } else {
1366 return Error() << "Could not parse apex configs";
1367 }
1368}
1369
Oli Lan13e51e72019-11-19 18:08:45 +00001370/*
1371 * Creates a directory under /data/misc/apexdata/ for each APEX.
1372 */
1373static Result<void> create_apex_data_dirs() {
1374 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/apex"), closedir);
1375 if (!dirp) {
1376 return ErrnoError() << "Unable to open apex directory";
1377 }
1378 struct dirent* entry;
1379 while ((entry = readdir(dirp.get())) != nullptr) {
1380 if (entry->d_type != DT_DIR) continue;
1381
1382 const char* name = entry->d_name;
1383 // skip any starting with "."
1384 if (name[0] == '.') continue;
1385
1386 if (strchr(name, '@') != nullptr) continue;
1387
1388 auto path = "/data/misc/apexdata/" + std::string(name);
Oli Lan90c523b2020-01-17 11:41:04 +00001389 auto options = MkdirOptions{path, 0771, AID_ROOT, AID_SYSTEM, FscryptAction::kNone, "ref"};
Oli Lan13e51e72019-11-19 18:08:45 +00001390 make_dir_with_options(options);
1391 }
1392 return {};
1393}
1394
1395static Result<void> do_perform_apex_config(const BuiltinArguments& args) {
1396 auto create_dirs = create_apex_data_dirs();
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001397 if (!create_dirs.ok()) {
Oli Lan13e51e72019-11-19 18:08:45 +00001398 return create_dirs.error();
1399 }
1400 auto parse_configs = parse_apex_configs();
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001401 if (!parse_configs.ok()) {
Oli Lan13e51e72019-11-19 18:08:45 +00001402 return parse_configs.error();
1403 }
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001404
1405 auto update_linker_config = do_update_linker_config(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001406 if (!update_linker_config.ok()) {
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001407 return update_linker_config.error();
1408 }
1409
Oli Lan13e51e72019-11-19 18:08:45 +00001410 return {};
1411}
1412
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001413static Result<void> do_enter_default_mount_ns(const BuiltinArguments& args) {
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001414 if (auto result = SwitchToMountNamespaceIfNeeded(NS_DEFAULT); !result.ok()) {
1415 return result.error();
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001416 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001417 if (auto result = MountLinkerConfigForDefaultNamespace(); !result.ok()) {
1418 return result.error();
1419 }
1420 LOG(INFO) << "Switched to default mount namespace";
1421 return {};
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001422}
1423
Tom Cherryde6bd502018-02-13 16:50:08 -08001424// Builtin-function-map start
Tom Cherryd52a5b32019-07-22 16:05:36 -07001425const BuiltinFunctionMap& GetBuiltinFunctionMap() {
Tom Cherryb7349902015-08-26 11:43:36 -07001426 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -07001427 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -07001428 static const BuiltinFunctionMap builtin_functions = {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001429 {"bootchart", {1, 1, {false, do_bootchart}}},
1430 {"chmod", {2, 2, {true, do_chmod}}},
1431 {"chown", {2, 3, {true, do_chown}}},
1432 {"class_reset", {1, 1, {false, do_class_reset}}},
David Anderson2285b522021-11-09 18:26:39 -08001433 {"class_restart", {1, 2, {false, do_class_restart}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001434 {"class_start", {1, 1, {false, do_class_start}}},
1435 {"class_stop", {1, 1, {false, do_class_stop}}},
1436 {"copy", {2, 2, {true, do_copy}}},
Wei Wang49d25982020-11-18 15:56:14 -08001437 {"copy_per_line", {2, 2, {true, do_copy_per_line}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001438 {"domainname", {1, 1, {true, do_domainname}}},
1439 {"enable", {1, 1, {false, do_enable}}},
1440 {"exec", {1, kMax, {false, do_exec}}},
1441 {"exec_background", {1, kMax, {false, do_exec_background}}},
1442 {"exec_start", {1, 1, {false, do_exec_start}}},
1443 {"export", {2, 2, {false, do_export}}},
1444 {"hostname", {1, 1, {true, do_hostname}}},
1445 {"ifup", {1, 1, {true, do_ifup}}},
1446 {"init_user0", {0, 0, {false, do_init_user0}}},
1447 {"insmod", {1, kMax, {true, do_insmod}}},
1448 {"installkey", {1, 1, {false, do_installkey}}},
Steven Moreland612d7a42018-05-08 11:21:37 -07001449 {"interface_restart", {1, 1, {false, do_interface_restart}}},
1450 {"interface_start", {1, 1, {false, do_interface_start}}},
1451 {"interface_stop", {1, 1, {false, do_interface_stop}}},
Artur Satayev500946b2021-02-15 20:48:49 +00001452 {"load_exports", {1, 1, {false, do_load_exports}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001453 {"load_persist_props", {0, 0, {false, do_load_persist_props}}},
1454 {"load_system_props", {0, 0, {false, do_load_system_props}}},
1455 {"loglevel", {1, 1, {false, do_loglevel}}},
Martijn Coenen70788f92019-04-23 16:26:01 +02001456 {"mark_post_data", {0, 0, {false, do_mark_post_data}}},
Paul Crowley68258e82019-10-28 07:55:03 -07001457 {"mkdir", {1, 6, {true, do_mkdir}}},
Tom Cherry880d5662018-02-05 08:01:54 -08001458 // TODO: Do mount operations in vendor_init.
1459 // mount_all is currently too complex to run in vendor_init as it queues action triggers,
1460 // imports rc scripts, etc. It should be simplified and run in vendor_init context.
1461 // mount and umount are run in the same context as mount_all for symmetry.
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -07001462 {"mount_all", {0, kMax, {false, do_mount_all}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001463 {"mount", {3, kMax, {false, do_mount}}},
Oli Lan13e51e72019-11-19 18:08:45 +00001464 {"perform_apex_config", {0, 0, {false, do_perform_apex_config}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001465 {"umount", {1, 1, {false, do_umount}}},
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -07001466 {"umount_all", {0, 1, {false, do_umount_all}}},
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001467 {"update_linker_config", {0, 0, {false, do_update_linker_config}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001468 {"readahead", {1, 2, {true, do_readahead}}},
Nikita Ioffe12a36072019-10-23 20:11:32 +01001469 {"remount_userdata", {0, 0, {false, do_remount_userdata}}},
David Anderson6d7c7a22021-12-03 15:48:16 -08001470 {"restart", {1, 2, {false, do_restart}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001471 {"restorecon", {1, kMax, {true, do_restorecon}}},
1472 {"restorecon_recursive", {1, kMax, {true, do_restorecon_recursive}}},
1473 {"rm", {1, 1, {true, do_rm}}},
1474 {"rmdir", {1, 1, {true, do_rmdir}}},
Tom Cherry32228482018-01-18 16:14:25 -08001475 {"setprop", {2, 2, {true, do_setprop}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001476 {"setrlimit", {3, 3, {false, do_setrlimit}}},
1477 {"start", {1, 1, {false, do_start}}},
1478 {"stop", {1, 1, {false, do_stop}}},
Alistair Delvade28a862020-06-08 11:04:53 -07001479 {"swapon_all", {0, 1, {false, do_swapon_all}}},
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001480 {"enter_default_mount_ns", {0, 0, {false, do_enter_default_mount_ns}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001481 {"symlink", {2, 2, {true, do_symlink}}},
1482 {"sysclktz", {1, 1, {false, do_sysclktz}}},
1483 {"trigger", {1, 1, {false, do_trigger}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001484 {"verity_update_state", {0, 0, {false, do_verity_update_state}}},
1485 {"wait", {1, 2, {true, do_wait}}},
Tom Cherryfa3e52c2017-10-17 10:43:52 -07001486 {"wait_for_prop", {2, 2, {false, do_wait_for_prop}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001487 {"write", {2, 2, {true, do_write}}},
Tom Cherryb7349902015-08-26 11:43:36 -07001488 };
Tom Cherryb27004a2017-03-27 16:27:30 -07001489 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -07001490 return builtin_functions;
1491}
Tom Cherryde6bd502018-02-13 16:50:08 -08001492// Builtin-function-map end
Tom Cherry81f5d3e2017-06-22 12:53:17 -07001493
1494} // namespace init
1495} // namespace android