blob: 7715424f51622332bed7b3acb873b67876209881 [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>
tangjie12b26bdf2023-04-13 17:15:23 +080063#include <libdm/dm.h>
64#include <libdm/loop_control.h>
David Anderson0e330f12019-01-03 18:16:56 -080065#include <libgsi/libgsi.h>
Kiyoung Kime4d3f212019-12-16 14:31:04 +090066#include <logwrap/logwrap.h>
Oli Landc516722020-01-03 10:04:31 +000067#include <private/android_filesystem_config.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070068#include <selinux/android.h>
69#include <selinux/label.h>
70#include <selinux/selinux.h>
Wei Wang02628f32017-08-16 11:34:50 -070071#include <system/thread_defs.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070072
Tom Cherry7fd3bc22018-02-13 15:36:14 -080073#include "action_manager.h"
Deyao Ren238e9092022-07-21 23:05:13 +000074#include "apex_init_util.h"
Tom Cherryb7349902015-08-26 11:43:36 -070075#include "bootchart.h"
Nikita Ioffec0df1872019-11-13 21:47:06 +000076#include "builtin_arguments.h"
Paul Crowley052f31c2019-08-26 10:33:17 -070077#include "fscrypt_init_extensions.h"
Tom Cherrybac32992015-07-31 12:45:25 -070078#include "init.h"
Jiyong Park68660412019-01-16 23:00:59 +090079#include "mount_namespace.h"
Tom Cherry67dee622017-07-27 12:54:48 -070080#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070081#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070082#include "reboot.h"
Tom Cherry7ac013d2017-08-25 10:39:25 -070083#include "rlimit_parser.h"
Vic Yang92c236e2019-05-28 15:58:35 -070084#include "selabel.h"
Joel Galenson4b591f12017-11-27 14:45:26 -080085#include "selinux.h"
Tom Cherrybac32992015-07-31 12:45:25 -070086#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070087#include "service_list.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070088#include "subcontext.h"
Tom Cherrybac32992015-07-31 12:45:25 -070089#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070090
Paul Crowley0b8b2302016-12-19 13:03:47 -080091using namespace std::literals::string_literals;
92
Tom Cherrycf80b6d2019-01-07 14:25:31 -080093using android::base::Basename;
Jiyong Parkd185d4a2021-12-11 10:45:20 +090094using android::base::ResultError;
Tom Cherryc88d8f92019-08-19 15:21:25 -070095using android::base::SetProperty;
Wei Wang49d25982020-11-18 15:56:14 -080096using android::base::Split;
Tom Cherry1ab3dfc2019-04-22 17:46:37 -070097using android::base::StartsWith;
Tom Cherry7896e7a2019-09-04 15:26:52 -070098using android::base::StringPrintf;
Tom Cherry70379912017-08-01 14:10:11 -070099using android::base::unique_fd;
Tom Cherrya3530e62019-01-30 13:25:35 -0800100using android::fs_mgr::Fstab;
101using android::fs_mgr::ReadFstabFromFile;
Tom Cherry70379912017-08-01 14:10:11 -0700102
Nick Kralevichbc609542015-01-31 21:39:46 -0800103#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
104
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700105namespace android {
106namespace init {
107
Tom Cherryd17c3792019-07-30 10:51:59 -0700108// There are many legacy paths in rootdir/init.rc that will virtually never exist on a new
109// device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there
110// are 81 such failures on cuttlefish. Instead of spamming the log reporting them, we do not
111// report such failures unless we're running at the DEBUG log level.
112class ErrorIgnoreEnoent {
113 public:
114 ErrorIgnoreEnoent()
115 : ignore_error_(errno == ENOENT &&
116 android::base::GetMinimumLogSeverity() > android::base::DEBUG) {}
117 explicit ErrorIgnoreEnoent(int errno_to_append)
118 : error_(errno_to_append),
119 ignore_error_(errno_to_append == ENOENT &&
120 android::base::GetMinimumLogSeverity() > android::base::DEBUG) {}
121
122 template <typename T>
Jiyong Park705abe22021-12-14 22:55:34 +0900123 operator android::base::expected<T, ResultError<android::base::Errno>>() {
Tom Cherryd17c3792019-07-30 10:51:59 -0700124 if (ignore_error_) {
125 return {};
126 }
127 return error_;
128 }
129
130 template <typename T>
131 ErrorIgnoreEnoent& operator<<(T&& t) {
132 error_ << t;
133 return *this;
134 }
135
136 private:
Jiyong Parkd185d4a2021-12-11 10:45:20 +0900137 Error<> error_;
Tom Cherryd17c3792019-07-30 10:51:59 -0700138 bool ignore_error_;
139};
140
141inline ErrorIgnoreEnoent ErrnoErrorIgnoreEnoent() {
142 return ErrorIgnoreEnoent(errno);
143}
144
Tom Cherry172c83f2019-06-26 14:44:37 -0700145std::vector<std::string> late_import_paths;
146
Elliott Hughes9605a942016-11-10 17:43:47 -0800147static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800148
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700149static Result<void> reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyc73b2152018-04-13 17:38:57 +0000150 LOG(ERROR) << "Rebooting into recovery";
Paul Crowleyaf8be582016-05-10 08:52:06 -0700151 std::string err;
152 if (!write_bootloader_message(options, &err)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700153 return Error() << "Failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700154 }
Tom Cherry18278d22019-11-12 16:21:20 -0800155 trigger_shutdown("reboot,recovery");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700156 return {};
Yusuke Sato0df08272015-07-08 14:57:07 -0700157}
158
Tom Cherry911b9b12017-07-27 16:20:58 -0700159template <typename F>
160static void ForEachServiceInClass(const std::string& classname, F function) {
161 for (const auto& service : ServiceList::GetInstance()) {
162 if (service->classnames().count(classname)) std::invoke(function, service);
163 }
164}
165
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700166static Result<void> do_class_start(const BuiltinArguments& args) {
Daniel Rosenbergca00b0e2018-11-27 22:08:02 -0800167 // 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 +0200168 if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700169 return {};
Tom Cherry911b9b12017-07-27 16:20:58 -0700170 // Starting a class does not start services which are explicitly disabled.
171 // They must be started individually.
Tom Cherry20acdef2017-10-03 13:15:03 -0700172 for (const auto& service : ServiceList::GetInstance()) {
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200173 if (service->classnames().count(args[1])) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900174 if (auto result = service->StartIfNotDisabled(); !result.ok()) {
Tom Cherry20acdef2017-10-03 13:15:03 -0700175 LOG(ERROR) << "Could not start service '" << service->name()
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200176 << "' as part of class '" << args[1] << "': " << result.error();
Tom Cherry20acdef2017-10-03 13:15:03 -0700177 }
178 }
179 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700180 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181}
182
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700183static Result<void> do_class_stop(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700184 ForEachServiceInClass(args[1], &Service::Stop);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700185 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186}
187
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700188static Result<void> do_class_reset(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700189 ForEachServiceInClass(args[1], &Service::Reset);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700190 return {};
Ken Sumrall752923c2010-12-03 16:33:31 -0800191}
192
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700193static Result<void> do_class_restart(const BuiltinArguments& args) {
Daniel Rosenbergca00b0e2018-11-27 22:08:02 -0800194 // Do not restart a class if it has a property persist.dont_start_class.CLASS set to 1.
195 if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700196 return {};
David Anderson2285b522021-11-09 18:26:39 -0800197
198 std::string classname;
199
200 CHECK(args.size() == 2 || args.size() == 3);
201
202 bool only_enabled = false;
203 if (args.size() == 3) {
204 if (args[1] != "--only-enabled") {
205 return Error() << "Unexpected argument: " << args[1];
206 }
207 only_enabled = true;
208 classname = args[2];
209 } else if (args.size() == 2) {
210 classname = args[1];
211 }
212
213 for (const auto& service : ServiceList::GetInstance()) {
214 if (!service->classnames().count(classname)) {
215 continue;
216 }
217 if (only_enabled && !service->IsEnabled()) {
218 continue;
219 }
220 service->Restart();
221 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700222 return {};
Steven Moreland2b63d542017-03-10 14:04:37 -0800223}
224
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700225static Result<void> do_domainname(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900226 if (auto result = WriteFile("/proc/sys/kernel/domainname", args[1]); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700227 return Error() << "Unable to write to /proc/sys/kernel/domainname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700228 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700229 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230}
231
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700232static Result<void> do_enable(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700233 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700234 if (!svc) return Error() << "Could not find service";
235
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900236 if (auto result = svc->Enable(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700237 return Error() << "Could not enable service: " << result.error();
238 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700239
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700240 return {};
JP Abgrall3beec7e2014-05-02 21:14:29 -0700241}
242
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700243static Result<void> do_exec(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700244 auto service = Service::MakeTemporaryOneshotService(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900245 if (!service.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -0700246 return Error() << "Could not create exec service: " << service.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700247 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900248 if (auto result = (*service)->ExecStart(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700249 return Error() << "Could not start exec service: " << result.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700250 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700251
Tom Cherry4772f1d2019-07-30 09:34:41 -0700252 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700253 return {};
Tom Cherryb27004a2017-03-27 16:27:30 -0700254}
255
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700256static Result<void> do_exec_background(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700257 auto service = Service::MakeTemporaryOneshotService(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900258 if (!service.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -0700259 return Error() << "Could not create exec background service: " << service.error();
Tom Cherry3631c542017-09-18 12:16:27 -0700260 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900261 if (auto result = (*service)->Start(); !result.ok()) {
Tom Cherry3631c542017-09-18 12:16:27 -0700262 return Error() << "Could not start exec background service: " << result.error();
263 }
264
Tom Cherry4772f1d2019-07-30 09:34:41 -0700265 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700266 return {};
Tom Cherry3631c542017-09-18 12:16:27 -0700267}
268
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700269static Result<void> do_exec_start(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700270 Service* service = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700271 if (!service) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700272 return Error() << "Service not found";
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700273 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700274
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900275 if (auto result = service->ExecStart(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700276 return Error() << "Could not start exec service: " << result.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700277 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700278
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700279 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280}
281
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700282static Result<void> do_export(const BuiltinArguments& args) {
Tom Cherry6de21f12017-08-22 15:41:03 -0700283 if (setenv(args[1].c_str(), args[2].c_str(), 1) == -1) {
284 return ErrnoError() << "setenv() failed";
Tom Cherry7fa62c52017-08-01 13:50:23 -0700285 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700286 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287}
288
Artur Satayev500946b2021-02-15 20:48:49 +0000289static Result<void> do_load_exports(const BuiltinArguments& args) {
290 auto file_contents = ReadFile(args[1]);
291 if (!file_contents.ok()) {
292 return Error() << "Could not read input file '" << args[1]
293 << "': " << file_contents.error();
294 }
295
296 auto lines = Split(*file_contents, "\n");
297 for (const auto& line : lines) {
298 if (line.empty()) {
299 continue;
300 }
301
302 auto env = Split(line, " ");
303
304 if (env.size() != 3) {
305 return ErrnoError() << "Expected a line as `export <name> <value>`, found: `" << line
306 << "`";
307 }
308
309 if (env[0] != "export") {
310 return ErrnoError() << "Unknown action: '" << env[0] << "', expected 'export'";
311 }
312
313 if (setenv(env[1].c_str(), env[2].c_str(), 1) == -1) {
314 return ErrnoError() << "Failed to export '" << line << "' from " << args[1];
315 }
316 }
317
318 return {};
319}
320
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700321static Result<void> do_hostname(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900322 if (auto result = WriteFile("/proc/sys/kernel/hostname", args[1]); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700323 return Error() << "Unable to write to /proc/sys/kernel/hostname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700324 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700325 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326}
327
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700328static Result<void> do_ifup(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700329 struct ifreq ifr;
330
331 strlcpy(ifr.ifr_name, args[1].c_str(), IFNAMSIZ);
332
Tom Cherry247ffbf2019-07-08 15:09:36 -0700333 unique_fd s(TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)));
Tom Cherry7fa62c52017-08-01 13:50:23 -0700334 if (s < 0) return ErrnoError() << "opening socket failed";
335
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800336 if (ioctl(s.get(), SIOCGIFFLAGS, &ifr) < 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700337 return ErrnoError() << "ioctl(..., SIOCGIFFLAGS, ...) failed";
338 }
339
340 ifr.ifr_flags |= IFF_UP;
341
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800342 if (ioctl(s.get(), SIOCSIFFLAGS, &ifr) < 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700343 return ErrnoError() << "ioctl(..., SIOCSIFFLAGS, ...) failed";
344 }
345
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700346 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347}
348
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700349static Result<void> do_insmod(const BuiltinArguments& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800350 int flags = 0;
351 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800352
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800353 if (!(*it).compare("-f")) {
354 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
355 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800356 }
357
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800358 std::string filename = *it++;
359 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
Tom Cherry7fa62c52017-08-01 13:50:23 -0700360
361 unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
362 if (fd == -1) return ErrnoError() << "open(\"" << filename << "\") failed";
363
364 int rc = syscall(__NR_finit_module, fd.get(), options.c_str(), flags);
365 if (rc == -1) return ErrnoError() << "finit_module for \"" << filename << "\" failed";
366
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700367 return {};
The Android Open Source Project35237d12008-12-17 18:08:08 -0800368}
369
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700370static Result<void> do_interface_restart(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700371 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
372 if (!svc) return Error() << "interface " << args[1] << " not found";
373 svc->Restart();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700374 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700375}
376
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700377static Result<void> do_interface_start(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700378 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
379 if (!svc) return Error() << "interface " << args[1] << " not found";
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900380 if (auto result = svc->Start(); !result.ok()) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700381 return Error() << "Could not start interface: " << result.error();
382 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700383 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700384}
385
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700386static Result<void> do_interface_stop(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700387 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
388 if (!svc) return Error() << "interface " << args[1] << " not found";
389 svc->Stop();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700390 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700391}
392
Oli Lan13e51e72019-11-19 18:08:45 +0000393static Result<void> make_dir_with_options(const MkdirOptions& options) {
Paul Crowley68258e82019-10-28 07:55:03 -0700394 std::string ref_basename;
Oli Lan13e51e72019-11-19 18:08:45 +0000395 if (options.ref_option == "ref") {
Paul Crowley68258e82019-10-28 07:55:03 -0700396 ref_basename = fscrypt_key_ref;
Oli Lan13e51e72019-11-19 18:08:45 +0000397 } else if (options.ref_option == "per_boot_ref") {
Paul Crowley68258e82019-10-28 07:55:03 -0700398 ref_basename = fscrypt_key_per_boot_ref;
399 } else {
Oli Lan13e51e72019-11-19 18:08:45 +0000400 return Error() << "Unknown key option: '" << options.ref_option << "'";
Paul Crowley1b4e7322019-08-25 12:18:43 -0700401 }
Paul Crowley68258e82019-10-28 07:55:03 -0700402
Paul Crowley1b4e7322019-08-25 12:18:43 -0700403 struct stat mstat;
Oli Lan13e51e72019-11-19 18:08:45 +0000404 if (lstat(options.target.c_str(), &mstat) != 0) {
Paul Crowley1b4e7322019-08-25 12:18:43 -0700405 if (errno != ENOENT) {
Oli Lan13e51e72019-11-19 18:08:45 +0000406 return ErrnoError() << "lstat() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700407 }
Oli Lan13e51e72019-11-19 18:08:45 +0000408 if (!make_dir(options.target, options.mode)) {
409 return ErrnoErrorIgnoreEnoent() << "mkdir() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700410 }
Oli Lan13e51e72019-11-19 18:08:45 +0000411 if (lstat(options.target.c_str(), &mstat) != 0) {
412 return ErrnoError() << "lstat() failed on new " << options.target;
Benoit Goby5c8574b2012-08-14 15:43:46 -0700413 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700414 }
Paul Crowley1b4e7322019-08-25 12:18:43 -0700415 if (!S_ISDIR(mstat.st_mode)) {
Oli Lan13e51e72019-11-19 18:08:45 +0000416 return Error() << "Not a directory on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700417 }
Oli Lan13e51e72019-11-19 18:08:45 +0000418 bool needs_chmod = (mstat.st_mode & ~S_IFMT) != options.mode;
419 if ((options.uid != static_cast<uid_t>(-1) && options.uid != mstat.st_uid) ||
420 (options.gid != static_cast<gid_t>(-1) && options.gid != mstat.st_gid)) {
421 if (lchown(options.target.c_str(), options.uid, options.gid) == -1) {
422 return ErrnoError() << "lchown failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700423 }
424 // chown may have cleared S_ISUID and S_ISGID, chmod again
425 needs_chmod = true;
426 }
427 if (needs_chmod) {
Oli Lan13e51e72019-11-19 18:08:45 +0000428 if (fchmodat(AT_FDCWD, options.target.c_str(), options.mode, AT_SYMLINK_NOFOLLOW) == -1) {
429 return ErrnoError() << "fchmodat() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700430 }
431 }
Eric Biggers893b1ae2022-06-15 18:52:39 +0000432 if (IsFbeEnabled()) {
Oli Lan13e51e72019-11-19 18:08:45 +0000433 if (!FscryptSetDirectoryPolicy(ref_basename, options.fscrypt_action, options.target)) {
Paul Crowleyc73b2152018-04-13 17:38:57 +0000434 return reboot_into_recovery(
Oli Lan13e51e72019-11-19 18:08:45 +0000435 {"--prompt_and_wipe_data", "--reason=set_policy_failed:"s + options.target});
Paul Crowleyaf8be582016-05-10 08:52:06 -0700436 }
437 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700438 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700439}
440
Oli Lan13e51e72019-11-19 18:08:45 +0000441// mkdir <path> [mode] [owner] [group] [<option> ...]
442static Result<void> do_mkdir(const BuiltinArguments& args) {
443 auto options = ParseMkdir(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900444 if (!options.ok()) return options.error();
Oli Lan13e51e72019-11-19 18:08:45 +0000445 return make_dir_with_options(*options);
446}
447
Alex Light68ab20f2016-06-23 11:11:39 -0700448/* umount <path> */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700449static Result<void> do_umount(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700450 if (umount(args[1].c_str()) < 0) {
451 return ErrnoError() << "umount() failed";
452 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700453 return {};
Alex Light68ab20f2016-06-23 11:11:39 -0700454}
455
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456static struct {
457 const char *name;
458 unsigned flag;
459} mount_flags[] = {
460 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200461 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700462 { "nosuid", MS_NOSUID },
463 { "nodev", MS_NODEV },
464 { "nodiratime", MS_NODIRATIME },
465 { "ro", MS_RDONLY },
466 { "rw", 0 },
467 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700468 { "bind", MS_BIND },
469 { "rec", MS_REC },
470 { "unbindable", MS_UNBINDABLE },
471 { "private", MS_PRIVATE },
472 { "slave", MS_SLAVE },
473 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700474 { "defaults", 0 },
475 { 0, 0 },
476};
477
Ken Sumrall752923c2010-12-03 16:33:31 -0800478#define DATA_MNT_POINT "/data"
479
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700480/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700481static Result<void> do_mount(const BuiltinArguments& args) {
Tom Cherry70379912017-08-01 14:10:11 -0700482 const char* options = nullptr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483 unsigned flags = 0;
Tom Cherry70379912017-08-01 14:10:11 -0700484 bool wait = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700485
Tom Cherry70379912017-08-01 14:10:11 -0700486 for (size_t na = 4; na < args.size(); na++) {
487 size_t i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700488 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700489 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700490 flags |= mount_flags[i].flag;
491 break;
492 }
493 }
494
Colin Crosscd0f1732010-04-19 17:10:24 -0700495 if (!mount_flags[i].name) {
Tom Cherry70379912017-08-01 14:10:11 -0700496 if (!args[na].compare("wait")) {
497 wait = true;
498 // If our last argument isn't a flag, wolf it up as an option string.
499 } else if (na + 1 == args.size()) {
Tom Cherry96f67312015-07-30 13:52:55 -0700500 options = args[na].c_str();
Tom Cherry70379912017-08-01 14:10:11 -0700501 }
Colin Crosscd0f1732010-04-19 17:10:24 -0700502 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700503 }
504
Tom Cherry70379912017-08-01 14:10:11 -0700505 const char* system = args[1].c_str();
506 const char* source = args[2].c_str();
507 const char* target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000508
Tom Cherry70379912017-08-01 14:10:11 -0700509 if (android::base::StartsWith(source, "loop@")) {
510 int mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
tangjie12b26bdf2023-04-13 17:15:23 +0800511 const char* file_path = source + strlen("loop@");
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000512
tangjie12b26bdf2023-04-13 17:15:23 +0800513 // Open source file
514 if (wait) {
515 wait_for_file(file_path, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000516 }
517
tangjie12b26bdf2023-04-13 17:15:23 +0800518 unique_fd fd(TEMP_FAILURE_RETRY(open(file_path, mode | O_CLOEXEC)));
519 if (fd < 0) {
520 return ErrnoError() << "open(" << file_path << ", " << mode << ") failed";
521 }
522
523 // Allocate loop device and attach it to file_path.
524 android::dm::LoopControl loop_control;
525 std::string loop_device;
526 if (!loop_control.Attach(fd.get(), 5s, &loop_device)) {
527 return ErrnoError() << "loop_control.Attach " << file_path << " failed";
528 }
529
530 if (mount(loop_device.c_str(), target, system, flags, options) < 0) {
531 loop_control.Detach(loop_device);
532 return ErrnoError() << "mount() failed";
533 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000534 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700535 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800536 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000537 if (mount(source, target, system, flags, options) < 0) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700538 return ErrnoErrorIgnoreEnoent() << "mount() failed";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000539 }
540
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700541 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800542
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700543 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700544}
545
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800546/* Imports .rc files from the specified paths. Default ones are applied if none is given.
547 *
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700548 * rc_paths: list of paths to rc files to import
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800549 */
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700550static void import_late(const std::vector<std::string>& rc_paths) {
Tom Cherry67dee622017-07-27 12:54:48 -0700551 auto& action_manager = ActionManager::GetInstance();
Tom Cherry911b9b12017-07-27 16:20:58 -0700552 auto& service_list = ServiceList::GetInstance();
553 Parser parser = CreateParser(action_manager, service_list);
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700554 if (rc_paths.empty()) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900555 // Fallbacks for partitions on which early mount isn't enabled.
Tom Cherry67dee622017-07-27 12:54:48 -0700556 for (const auto& path : late_import_paths) {
557 parser.ParseConfig(path);
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900558 }
Tom Cherry67dee622017-07-27 12:54:48 -0700559 late_import_paths.clear();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800560 } else {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700561 for (const auto& rc_path : rc_paths) {
562 parser.ParseConfig(rc_path);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800563 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700564 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700565
566 // Turning this on and letting the INFO logging be discarded adds 0.2s to
567 // Nexus 9 boot time, so it's disabled by default.
Tom Cherry30a6f272017-04-19 15:31:58 -0700568 if (false) DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700569}
570
Wei Wangd61a7e22016-08-23 11:58:09 -0700571/* Queue event based on fs_mgr return code.
572 *
573 * code: return code of fs_mgr_mount_all
574 *
575 * This function might request a reboot, in which case it will
576 * not return.
577 *
578 * return code is processed based on input code
579 */
Eric Biggersab74dbb2023-07-10 17:58:33 +0000580static Result<void> queue_fs_event(int code) {
Eric Biggerse5b5e372021-11-08 16:38:54 -0800581 if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Tom Cherryc88d8f92019-08-19 15:21:25 -0700582 SetProperty("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700583 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700584 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700585 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700586 /* Setup a wipe via recovery, and reboot into recovery */
David Anderson0e330f12019-01-03 18:16:56 -0800587 if (android::gsi::IsGsiRunning()) {
588 return Error() << "cannot wipe within GSI";
589 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700590 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800591 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
Paul Crowleyc73b2152018-04-13 17:38:57 +0000592 return reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700593 /* If reboot worked, there is no return. */
Eric Biggersab74dbb2023-07-10 17:58:33 +0000594 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED ||
595 code == FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED ||
596 code == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
Nikita Ioffe1f40c942019-12-04 17:47:37 +0000597 if (!FscryptInstallKeyring()) {
Paul Crowley68258e82019-10-28 07:55:03 -0700598 return Error() << "FscryptInstallKeyring() failed";
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700599 }
Tom Cherryc88d8f92019-08-19 15:21:25 -0700600 SetProperty("ro.crypto.state", "encrypted");
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700601
Paul Crowleyc6846962018-01-30 09:56:03 -0800602 // Although encrypted, vold has already set the device up, so we do not need to
603 // do anything different from the nonencrypted case.
604 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700605 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700606 } else if (code > 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700607 Error() << "fs_mgr_mount_all() returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700608 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700609 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700610
Tom Cherry7fa62c52017-08-01 13:50:23 -0700611 return Error() << "Invalid code: " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700612}
613
Nikita Ioffe12a36072019-10-23 20:11:32 +0100614static int initial_mount_fstab_return_code = -1;
615
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700616/* <= Q: mount_all <fstab> [ <path> ]* [--<options>]*
617 * >= R: mount_all [ <fstab> ] [--<options>]*
Wei Wangd61a7e22016-08-23 11:58:09 -0700618 *
619 * This function might request a reboot, in which case it will
620 * not return.
621 */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700622static Result<void> do_mount_all(const BuiltinArguments& args) {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700623 auto mount_all = ParseMountAll(args.args);
624 if (!mount_all.ok()) return mount_all.error();
Wei Wangd61a7e22016-08-23 11:58:09 -0700625
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700626 const char* prop_post_fix = "default";
627 bool queue_event = true;
628 if (mount_all->mode == MOUNT_MODE_EARLY) {
629 prop_post_fix = "early";
630 queue_event = false;
631 } else if (mount_all->mode == MOUNT_MODE_LATE) {
632 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700633 }
634
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700635 std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
Tom Cherryede0d532017-07-06 14:20:11 -0700636 android::base::Timer t;
Tom Cherry990483d2019-04-17 12:55:50 -0700637
638 Fstab fstab;
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700639 if (mount_all->fstab_path.empty()) {
640 if (!ReadDefaultFstab(&fstab)) {
641 return Error() << "Could not read default fstab";
642 }
643 } else {
644 if (!ReadFstabFromFile(mount_all->fstab_path, &fstab)) {
645 return Error() << "Could not read fstab";
646 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700647 }
Nikita Ioffe12a36072019-10-23 20:11:32 +0100648
Nikita Ioffe9ede7ec2020-09-07 10:27:25 +0100649 auto mount_fstab_result = fs_mgr_mount_all(&fstab, mount_all->mode);
Tom Cherryc88d8f92019-08-19 15:21:25 -0700650 SetProperty(prop_name, std::to_string(t.duration().count()));
Wei Wangd61a7e22016-08-23 11:58:09 -0700651
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700652 if (mount_all->import_rc) {
653 import_late(mount_all->rc_paths);
Wei Wangd61a7e22016-08-23 11:58:09 -0700654 }
655
Nikita Ioffe9ede7ec2020-09-07 10:27:25 +0100656 if (mount_fstab_result.userdata_mounted) {
657 // This call to fs_mgr_mount_all mounted userdata. Keep the result in
658 // order for userspace reboot to correctly remount userdata.
659 LOG(INFO) << "Userdata mounted using "
660 << (mount_all->fstab_path.empty() ? "(default fstab)" : mount_all->fstab_path)
661 << " result : " << mount_fstab_result.code;
662 initial_mount_fstab_return_code = mount_fstab_result.code;
663 }
664
Wei Wangd61a7e22016-08-23 11:58:09 -0700665 if (queue_event) {
666 /* queue_fs_event will queue event based on mount_fstab return code
667 * and return processed return code*/
Eric Biggersab74dbb2023-07-10 17:58:33 +0000668 auto queue_fs_result = queue_fs_event(mount_fstab_result.code);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900669 if (!queue_fs_result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700670 return Error() << "queue_fs_event() failed: " << queue_fs_result.error();
671 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700672 }
673
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700674 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700675}
676
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700677/* umount_all [ <fstab> ] */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700678static Result<void> do_umount_all(const BuiltinArguments& args) {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700679 auto umount_all = ParseUmountAll(args.args);
680 if (!umount_all.ok()) return umount_all.error();
681
Tom Cherry990483d2019-04-17 12:55:50 -0700682 Fstab fstab;
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700683 if (umount_all->empty()) {
684 if (!ReadDefaultFstab(&fstab)) {
685 return Error() << "Could not read default fstab";
686 }
687 } else {
688 if (!ReadFstabFromFile(*umount_all, &fstab)) {
689 return Error() << "Could not read fstab";
690 }
Tom Cherry990483d2019-04-17 12:55:50 -0700691 }
692
Tom Cherry3707d322019-08-15 13:07:24 -0700693 if (auto result = fs_mgr_umount_all(&fstab); result != 0) {
694 return Error() << "umount_fstab() failed " << result;
Yifan Hong402633d2019-04-09 10:11:34 -0700695 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700696 return {};
Yifan Hong402633d2019-04-09 10:11:34 -0700697}
698
Alistair Delvade28a862020-06-08 11:04:53 -0700699/* swapon_all [ <fstab> ] */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700700static Result<void> do_swapon_all(const BuiltinArguments& args) {
Alistair Delvade28a862020-06-08 11:04:53 -0700701 auto swapon_all = ParseSwaponAll(args.args);
702 if (!swapon_all.ok()) return swapon_all.error();
703
Tom Cherry30554572018-11-30 15:21:04 -0800704 Fstab fstab;
Alistair Delvade28a862020-06-08 11:04:53 -0700705 if (swapon_all->empty()) {
706 if (!ReadDefaultFstab(&fstab)) {
707 return Error() << "Could not read default fstab";
708 }
709 } else {
710 if (!ReadFstabFromFile(*swapon_all, &fstab)) {
711 return Error() << "Could not read fstab '" << *swapon_all << "'";
712 }
Tom Cherry30554572018-11-30 15:21:04 -0800713 }
Ken Sumralla76baaa2013-07-09 18:42:09 -0700714
Tom Cherry3707d322019-08-15 13:07:24 -0700715 if (!fs_mgr_swapon_all(fstab)) {
716 return Error() << "fs_mgr_swapon_all() failed";
Tom Cherry30554572018-11-30 15:21:04 -0800717 }
Ken Sumralla76baaa2013-07-09 18:42:09 -0700718
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700719 return {};
Ken Sumralla76baaa2013-07-09 18:42:09 -0700720}
721
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700722static Result<void> do_setprop(const BuiltinArguments& args) {
Tom Cherry1ab3dfc2019-04-22 17:46:37 -0700723 if (StartsWith(args[1], "ctl.")) {
724 return Error()
725 << "Cannot set ctl. properties from init; call the Service functions directly";
726 }
727 if (args[1] == kRestoreconProperty) {
728 return Error() << "Cannot set '" << kRestoreconProperty
729 << "' from init; use the restorecon builtin directly";
730 }
731
Tom Cherryc88d8f92019-08-19 15:21:25 -0700732 SetProperty(args[1], args[2]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700733 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700734}
735
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700736static Result<void> do_setrlimit(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700737 auto rlimit = ParseRlimit(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900738 if (!rlimit.ok()) return rlimit.error();
Tom Cherry7fa62c52017-08-01 13:50:23 -0700739
Tom Cherry7ac013d2017-08-25 10:39:25 -0700740 if (setrlimit(rlimit->first, &rlimit->second) == -1) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700741 return ErrnoError() << "setrlimit failed";
742 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700743 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700744}
745
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700746static Result<void> do_start(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700747 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700748 if (!svc) return Error() << "service " << args[1] << " not found";
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900749 if (auto result = svc->Start(); !result.ok()) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700750 return ErrorIgnoreEnoent() << "Could not start service: " << result.error();
Tom Cherry76af7e62017-08-22 16:13:59 -0700751 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700752 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700753}
754
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700755static Result<void> do_stop(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700756 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700757 if (!svc) return Error() << "service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700758 svc->Stop();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700759 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700760}
761
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700762static Result<void> do_restart(const BuiltinArguments& args) {
David Anderson6d7c7a22021-12-03 15:48:16 -0800763 bool only_if_running = false;
764 if (args.size() == 3) {
765 if (args[1] == "--only-if-running") {
766 only_if_running = true;
767 } else {
768 return Error() << "Unknown argument to restart: " << args[1];
769 }
770 }
771
772 const auto& classname = args[args.size() - 1];
773 Service* svc = ServiceList::GetInstance().FindService(classname);
774 if (!svc) return Error() << "service " << classname << " not found";
775 if (only_if_running && !svc->IsRunning()) {
776 return {};
777 }
Tom Cherrybac32992015-07-31 12:45:25 -0700778 svc->Restart();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700779 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780}
781
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700782static Result<void> do_trigger(const BuiltinArguments& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700783 ActionManager::GetInstance().QueueEventTrigger(args[1]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700784 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700785}
786
Joel Galenson4b591f12017-11-27 14:45:26 -0800787static int MakeSymlink(const std::string& target, const std::string& linkpath) {
788 std::string secontext;
789 // Passing 0 for mode should work.
790 if (SelabelLookupFileContext(linkpath, 0, &secontext) && !secontext.empty()) {
791 setfscreatecon(secontext.c_str());
792 }
793
794 int rc = symlink(target.c_str(), linkpath.c_str());
795
796 if (!secontext.empty()) {
797 int save_errno = errno;
798 setfscreatecon(nullptr);
799 errno = save_errno;
800 }
801
802 return rc;
803}
804
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700805static Result<void> do_symlink(const BuiltinArguments& args) {
Joel Galenson4b591f12017-11-27 14:45:26 -0800806 if (MakeSymlink(args[1], args[2]) < 0) {
Tom Cherry68f2a462017-08-22 16:15:26 -0700807 // The symlink builtin is often used to create symlinks for older devices to be backwards
808 // compatible with new paths, therefore we skip reporting this error.
Tom Cherryd17c3792019-07-30 10:51:59 -0700809 return ErrnoErrorIgnoreEnoent() << "symlink() failed";
Elliott Hughesda46b392016-10-11 17:09:00 -0700810 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700811 return {};
The Android Open Source Project35237d12008-12-17 18:08:08 -0800812}
813
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700814static Result<void> do_rm(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700815 if (unlink(args[1].c_str()) < 0) {
816 return ErrnoError() << "unlink() failed";
817 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700818 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700819}
820
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700821static Result<void> do_rmdir(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700822 if (rmdir(args[1].c_str()) < 0) {
823 return ErrnoError() << "rmdir() failed";
824 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700825 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700826}
827
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700828static Result<void> do_sysclktz(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700829 struct timezone tz = {};
830 if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) {
831 return Error() << "Unable to parse mins_west_of_gmt";
832 }
833
834 if (settimeofday(nullptr, &tz) == -1) {
835 return ErrnoError() << "settimeofday() failed";
836 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700837 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700838}
839
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700840static Result<void> do_verity_update_state(const BuiltinArguments& args) {
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800841 int mode;
842 if (!fs_mgr_load_verity_state(&mode)) {
843 return Error() << "fs_mgr_load_verity_state() failed";
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700844 }
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800845
846 Fstab fstab;
847 if (!ReadDefaultFstab(&fstab)) {
848 return Error() << "Failed to read default fstab";
849 }
850
851 for (const auto& entry : fstab) {
852 if (!fs_mgr_is_verity_enabled(entry)) {
853 continue;
854 }
855
856 // To be consistent in vboot 1.0 and vboot 2.0 (AVB), use "system" for the partition even
857 // for system as root, so it has property [partition.system.verified].
858 std::string partition = entry.mount_point == "/" ? "system" : Basename(entry.mount_point);
Tom Cherryc88d8f92019-08-19 15:21:25 -0700859 SetProperty("partition." + partition + ".verified", std::to_string(mode));
Tianjie327237d2021-01-20 19:02:34 -0800860
Tianjie10bec652021-08-30 16:10:09 -0700861 auto hashtree_info = fs_mgr_get_hashtree_info(entry);
862 if (hashtree_info) {
863 SetProperty("partition." + partition + ".verified.hash_alg", hashtree_info->algorithm);
864 SetProperty("partition." + partition + ".verified.root_digest",
865 hashtree_info->root_digest);
Nathan Huckleberry997d7382022-10-21 20:55:49 +0000866 SetProperty("partition." + partition + ".verified.check_at_most_once",
867 hashtree_info->check_at_most_once ? "1" : "0");
Tianjie327237d2021-01-20 19:02:34 -0800868 }
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800869 }
870
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700871 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700872}
873
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700874static Result<void> do_write(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900875 if (auto result = WriteFile(args[1], args[2]); !result.ok()) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700876 return ErrorIgnoreEnoent()
877 << "Unable to write to file '" << args[1] << "': " << result.error();
Tom Cherry7fa62c52017-08-01 13:50:23 -0700878 }
879
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700880 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700881}
882
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700883static Result<void> readahead_file(const std::string& filename, bool fully) {
Tom Cherry247ffbf2019-07-08 15:09:36 -0700884 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_CLOEXEC)));
Wei Wang02628f32017-08-16 11:34:50 -0700885 if (fd == -1) {
886 return ErrnoError() << "Error opening file";
887 }
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800888 if (posix_fadvise(fd.get(), 0, 0, POSIX_FADV_WILLNEED)) {
Wei Wang02628f32017-08-16 11:34:50 -0700889 return ErrnoError() << "Error posix_fadvise file";
890 }
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800891 if (readahead(fd.get(), 0, std::numeric_limits<size_t>::max())) {
Wei Wang02628f32017-08-16 11:34:50 -0700892 return ErrnoError() << "Error readahead file";
893 }
894 if (fully) {
895 char buf[BUFSIZ];
896 ssize_t n;
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800897 while ((n = TEMP_FAILURE_RETRY(read(fd.get(), &buf[0], sizeof(buf)))) > 0) {
Wei Wang02628f32017-08-16 11:34:50 -0700898 }
899 if (n != 0) {
900 return ErrnoError() << "Error reading file";
901 }
902 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700903 return {};
Wei Wang02628f32017-08-16 11:34:50 -0700904}
905
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700906static Result<void> do_readahead(const BuiltinArguments& args) {
Wei Wang542aae42017-08-04 13:22:36 -0700907 struct stat sb;
908
909 if (stat(args[1].c_str(), &sb)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700910 return ErrnoError() << "Error opening " << args[1];
Wei Wang542aae42017-08-04 13:22:36 -0700911 }
912
Wei Wang02628f32017-08-16 11:34:50 -0700913 bool readfully = false;
914 if (args.size() == 3 && args[2] == "--fully") {
915 readfully = true;
916 }
Wei Wang542aae42017-08-04 13:22:36 -0700917 // We will do readahead in a forked process in order not to block init
918 // since it may block while it reads the
919 // filesystem metadata needed to locate the requested blocks. This
920 // occurs frequently with ext[234] on large files using indirect blocks
921 // instead of extents, giving the appearance that the call blocks until
922 // the requested data has been read.
923 pid_t pid = fork();
924 if (pid == 0) {
Wei Wang02628f32017-08-16 11:34:50 -0700925 if (setpriority(PRIO_PROCESS, 0, static_cast<int>(ANDROID_PRIORITY_LOWEST)) != 0) {
926 PLOG(WARNING) << "setpriority failed";
927 }
928 if (android_set_ioprio(0, IoSchedClass_IDLE, 7)) {
929 PLOG(WARNING) << "ioprio_get failed";
930 }
Wei Wang542aae42017-08-04 13:22:36 -0700931 android::base::Timer t;
932 if (S_ISREG(sb.st_mode)) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900933 if (auto result = readahead_file(args[1], readfully); !result.ok()) {
Wei Wang02628f32017-08-16 11:34:50 -0700934 LOG(WARNING) << "Unable to readahead '" << args[1] << "': " << result.error();
Wei Wang542aae42017-08-04 13:22:36 -0700935 _exit(EXIT_FAILURE);
936 }
937 } else if (S_ISDIR(sb.st_mode)) {
938 char* paths[] = {const_cast<char*>(args[1].data()), nullptr};
939 std::unique_ptr<FTS, decltype(&fts_close)> fts(
940 fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr), fts_close);
941 if (!fts) {
942 PLOG(ERROR) << "Error opening directory: " << args[1];
943 _exit(EXIT_FAILURE);
944 }
945 // Traverse the entire hierarchy and do readahead
946 for (FTSENT* ftsent = fts_read(fts.get()); ftsent != nullptr;
947 ftsent = fts_read(fts.get())) {
948 if (ftsent->fts_info & FTS_F) {
Wei Wang02628f32017-08-16 11:34:50 -0700949 const std::string filename = ftsent->fts_accpath;
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900950 if (auto result = readahead_file(filename, readfully); !result.ok()) {
Wei Wang02628f32017-08-16 11:34:50 -0700951 LOG(WARNING)
952 << "Unable to readahead '" << filename << "': " << result.error();
Wei Wang542aae42017-08-04 13:22:36 -0700953 }
954 }
955 }
956 }
Wei Wang02628f32017-08-16 11:34:50 -0700957 LOG(INFO) << "Readahead " << args[1] << " took " << t << " asynchronously";
Wei Wang542aae42017-08-04 13:22:36 -0700958 _exit(0);
959 } else if (pid < 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700960 return ErrnoError() << "Fork failed";
Wei Wang542aae42017-08-04 13:22:36 -0700961 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700962 return {};
Wei Wang542aae42017-08-04 13:22:36 -0700963}
964
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700965static Result<void> do_copy(const BuiltinArguments& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700966 auto file_contents = ReadFile(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900967 if (!file_contents.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700968 return Error() << "Could not read input file '" << args[1] << "': " << file_contents.error();
San Mehat7c44fe52009-08-26 16:39:25 -0700969 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900970 if (auto result = WriteFile(args[2], *file_contents); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700971 return Error() << "Could not write to output file '" << args[2] << "': " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700972 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700973
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700974 return {};
San Mehat7c44fe52009-08-26 16:39:25 -0700975}
976
Wei Wang49d25982020-11-18 15:56:14 -0800977static Result<void> do_copy_per_line(const BuiltinArguments& args) {
978 std::string file_contents;
979 if (!android::base::ReadFileToString(args[1], &file_contents, true)) {
980 return Error() << "Could not read input file '" << args[1] << "'";
981 }
982 auto lines = Split(file_contents, "\n");
983 for (const auto& line : lines) {
984 auto result = WriteFile(args[2], line);
985 if (!result.ok()) {
986 LOG(VERBOSE) << "Could not write to output file '" << args[2] << "' with '" << line
987 << "' : " << result.error();
988 }
989 }
990
991 return {};
992}
993
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700994static Result<void> do_chown(const BuiltinArguments& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700995 auto uid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900996 if (!uid.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700997 return Error() << "Unable to decode UID for '" << args[1] << "': " << uid.error();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998 }
Tom Cherry517e1f12017-05-04 17:40:33 -0700999
1000 // GID is optional and pushes the index of path out by one if specified.
1001 const std::string& path = (args.size() == 4) ? args[3] : args[2];
Tom Cherry62ca6632017-08-03 12:54:07 -07001002 Result<gid_t> gid = -1;
Tom Cherry517e1f12017-05-04 17:40:33 -07001003
1004 if (args.size() == 4) {
Tom Cherry62ca6632017-08-03 12:54:07 -07001005 gid = DecodeUid(args[2]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001006 if (!gid.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001007 return Error() << "Unable to decode GID for '" << args[2] << "': " << gid.error();
Tom Cherry517e1f12017-05-04 17:40:33 -07001008 }
1009 }
1010
Tom Cherry7fa62c52017-08-01 13:50:23 -07001011 if (lchown(path.c_str(), *uid, *gid) == -1) {
Tom Cherryd17c3792019-07-30 10:51:59 -07001012 return ErrnoErrorIgnoreEnoent() << "lchown() failed";
Tom Cherry7fa62c52017-08-01 13:50:23 -07001013 }
Tom Cherry517e1f12017-05-04 17:40:33 -07001014
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001015 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001016}
1017
1018static mode_t get_mode(const char *s) {
1019 mode_t mode = 0;
1020 while (*s) {
1021 if (*s >= '0' && *s <= '7') {
1022 mode = (mode<<3) | (*s-'0');
1023 } else {
1024 return -1;
1025 }
1026 s++;
1027 }
1028 return mode;
1029}
1030
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001031static Result<void> do_chmod(const BuiltinArguments& args) {
Tom Cherry96f67312015-07-30 13:52:55 -07001032 mode_t mode = get_mode(args[1].c_str());
1033 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
Tom Cherryd17c3792019-07-30 10:51:59 -07001034 return ErrnoErrorIgnoreEnoent() << "fchmodat() failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001035 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001036 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001037}
1038
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001039static Result<void> do_restorecon(const BuiltinArguments& args) {
Tom Cherry4772f1d2019-07-30 09:34:41 -07001040 auto restorecon_info = ParseRestorecon(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001041 if (!restorecon_info.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -07001042 return restorecon_info.error();
1043 }
1044
1045 const auto& [flag, paths] = *restorecon_info;
1046
Stephen Smalley726e8f72013-10-09 16:02:09 -04001047 int ret = 0;
Tom Cherry4772f1d2019-07-30 09:34:41 -07001048 for (const auto& path : paths) {
1049 if (selinux_android_restorecon(path.c_str(), flag) < 0) {
1050 ret = errno;
Paul Lawrencea8d84342016-11-14 15:40:18 -08001051 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001052 }
Tom Cherry7fa62c52017-08-01 13:50:23 -07001053
Tom Cherryd17c3792019-07-30 10:51:59 -07001054 if (ret) return ErrnoErrorIgnoreEnoent() << "selinux_android_restorecon() failed";
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001055 return {};
Stephen Smalley726e8f72013-10-09 16:02:09 -04001056}
1057
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001058static Result<void> do_restorecon_recursive(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001059 std::vector<std::string> non_const_args(args.args);
Paul Lawrencea8d84342016-11-14 15:40:18 -08001060 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
Bart Van Assche3dfb8bc2023-02-22 10:01:33 -08001061 return do_restorecon({.args = std::move(non_const_args), .context = args.context});
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001062}
1063
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001064static Result<void> do_loglevel(const BuiltinArguments& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001065 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -07001066 int log_level = -1;
1067 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001068 android::base::LogSeverity severity;
1069 switch (log_level) {
1070 case 7: severity = android::base::DEBUG; break;
1071 case 6: severity = android::base::INFO; break;
1072 case 5:
1073 case 4: severity = android::base::WARNING; break;
1074 case 3: severity = android::base::ERROR; break;
1075 case 2:
1076 case 1:
1077 case 0: severity = android::base::FATAL; break;
1078 default:
Tom Cherry7fa62c52017-08-01 13:50:23 -07001079 return Error() << "invalid log level " << log_level;
Riley Andrews1bbef882014-06-26 13:55:03 -07001080 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001081 android::base::SetMinimumLogSeverity(severity);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001082 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001083}
1084
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001085static Result<void> do_load_persist_props(const BuiltinArguments& args) {
Tom Cherry1ab3dfc2019-04-22 17:46:37 -07001086 SendLoadPersistentPropertiesMessage();
1087
1088 start_waiting_for_property("ro.persistent_properties.ready", "true");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001089 return {};
Ken Sumrallc5c51032011-03-08 17:01:29 -08001090}
1091
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001092static Result<void> do_load_system_props(const BuiltinArguments& args) {
Jiyong Park3b316ee2019-01-13 02:42:31 +09001093 LOG(INFO) << "deprecated action `load_system_props` called.";
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001094 return {};
Riley Andrewse4b7b292014-06-16 15:06:21 -07001095}
1096
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001097static Result<void> do_wait(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001098 auto timeout = kCommandRetryTimeout;
1099 if (args.size() == 3) {
Mark Salyzynffa52e92020-05-14 09:20:30 -07001100 double timeout_double;
1101 if (!android::base::ParseDouble(args[2], &timeout_double, 0)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001102 return Error() << "failed to parse timeout";
Elliott Hughesda46b392016-10-11 17:09:00 -07001103 }
Mark Salyzynffa52e92020-05-14 09:20:30 -07001104 timeout = std::chrono::duration_cast<std::chrono::nanoseconds>(
1105 std::chrono::duration<double>(timeout_double));
Elliott Hughesda46b392016-10-11 17:09:00 -07001106 }
Tom Cherry7fa62c52017-08-01 13:50:23 -07001107
1108 if (wait_for_file(args[1].c_str(), timeout) != 0) {
1109 return Error() << "wait_for_file() failed";
1110 }
1111
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001112 return {};
Colin Crosscd0f1732010-04-19 17:10:24 -07001113}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001114
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001115static Result<void> do_wait_for_prop(const BuiltinArguments& args) {
Wei Wang132ac312017-01-25 16:27:03 -08001116 const char* name = args[1].c_str();
1117 const char* value = args[2].c_str();
1118 size_t value_len = strlen(value);
1119
Tom Cherryde6bd502018-02-13 16:50:08 -08001120 if (!IsLegalPropertyName(name)) {
1121 return Error() << "IsLegalPropertyName(" << name << ") failed";
Wei Wang132ac312017-01-25 16:27:03 -08001122 }
1123 if (value_len >= PROP_VALUE_MAX) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001124 return Error() << "value too long";
Wei Wang132ac312017-01-25 16:27:03 -08001125 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -08001126 if (!start_waiting_for_property(name, value)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001127 return Error() << "already waiting for a property";
Wei Wang132ac312017-01-25 16:27:03 -08001128 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001129 return {};
Wei Wang132ac312017-01-25 16:27:03 -08001130}
1131
Paul Crowley749af8c2015-05-28 17:35:06 +01001132static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -07001133 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +01001134}
1135
Tom Cherry7896e7a2019-09-04 15:26:52 -07001136static Result<void> ExecWithFunctionOnFailure(const std::vector<std::string>& args,
1137 std::function<void(const std::string&)> function) {
1138 auto service = Service::MakeTemporaryOneshotService(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001139 if (!service.ok()) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001140 function("MakeTemporaryOneshotService failed: " + service.error().message());
Paul Crowleyc73b2152018-04-13 17:38:57 +00001141 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001142 (*service)->AddReapCallback([function](const siginfo_t& siginfo) {
Paul Crowleyc73b2152018-04-13 17:38:57 +00001143 if (siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001144 function(StringPrintf("Exec service failed, status %d", siginfo.si_status));
Paul Crowleyc73b2152018-04-13 17:38:57 +00001145 }
1146 });
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001147 if (auto result = (*service)->ExecStart(); !result.ok()) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001148 function("ExecStart failed: " + result.error().message());
Paul Crowleyc73b2152018-04-13 17:38:57 +00001149 }
Tom Cherry4772f1d2019-07-30 09:34:41 -07001150 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001151 return {};
Paul Crowleyc73b2152018-04-13 17:38:57 +00001152}
1153
Tom Cherry7896e7a2019-09-04 15:26:52 -07001154static Result<void> ExecVdcRebootOnFailure(const std::string& vdc_arg) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001155 bool should_reboot_into_recovery = true;
Tom Cherry7896e7a2019-09-04 15:26:52 -07001156 auto reboot_reason = vdc_arg + "_failed";
Nikita Ioffe05506f02020-01-15 14:43:55 +00001157 if (android::sysprop::InitProperties::userspace_reboot_in_progress().value_or(false)) {
1158 should_reboot_into_recovery = false;
Nikita Ioffe4a787d92020-01-15 23:23:13 +00001159 reboot_reason = "userspace_failed," + vdc_arg;
Nikita Ioffe05506f02020-01-15 14:43:55 +00001160 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001161
Nikita Ioffe05506f02020-01-15 14:43:55 +00001162 auto reboot = [reboot_reason, should_reboot_into_recovery](const std::string& message) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001163 // TODO (b/122850122): support this in gsi
Nikita Ioffe05506f02020-01-15 14:43:55 +00001164 if (should_reboot_into_recovery) {
Eric Biggers893b1ae2022-06-15 18:52:39 +00001165 if (IsFbeEnabled() && !android::gsi::IsGsiRunning()) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001166 LOG(ERROR) << message << ": Rebooting into recovery, reason: " << reboot_reason;
1167 if (auto result = reboot_into_recovery(
1168 {"--prompt_and_wipe_data", "--reason="s + reboot_reason});
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001169 !result.ok()) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001170 LOG(FATAL) << "Could not reboot into recovery: " << result.error();
1171 }
1172 } else {
1173 LOG(ERROR) << "Failure (reboot suppressed): " << reboot_reason;
Tom Cherry7896e7a2019-09-04 15:26:52 -07001174 }
1175 } else {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001176 LOG(ERROR) << message << ": rebooting, reason: " << reboot_reason;
1177 trigger_shutdown("reboot," + reboot_reason);
Tom Cherry7896e7a2019-09-04 15:26:52 -07001178 }
1179 };
1180
1181 std::vector<std::string> args = {"exec", "/system/bin/vdc", "--wait", "cryptfs", vdc_arg};
1182 return ExecWithFunctionOnFailure(args, reboot);
1183}
1184
Nikita Ioffe12a36072019-10-23 20:11:32 +01001185static Result<void> do_remount_userdata(const BuiltinArguments& args) {
1186 if (initial_mount_fstab_return_code == -1) {
1187 return Error() << "Calling remount_userdata too early";
1188 }
1189 Fstab fstab;
1190 if (!ReadDefaultFstab(&fstab)) {
1191 // TODO(b/135984674): should we reboot here?
1192 return Error() << "Failed to read fstab";
1193 }
1194 // TODO(b/135984674): check that fstab contains /data.
1195 if (auto rc = fs_mgr_remount_userdata_into_checkpointing(&fstab); rc < 0) {
Gavin Corkery8c922562020-05-11 14:10:09 +01001196 std::string proc_mounts_output;
1197 android::base::ReadFileToString("/proc/mounts", &proc_mounts_output, true);
1198 android::base::WriteStringToFile(proc_mounts_output,
1199 "/metadata/userspacereboot/mount_info.txt");
Nikita Ioffe4a787d92020-01-15 23:23:13 +00001200 trigger_shutdown("reboot,mount_userdata_failed");
Nikita Ioffe12a36072019-10-23 20:11:32 +01001201 }
Eric Biggersab74dbb2023-07-10 17:58:33 +00001202 if (auto result = queue_fs_event(initial_mount_fstab_return_code); !result.ok()) {
Nikita Ioffe12a36072019-10-23 20:11:32 +01001203 return Error() << "queue_fs_event() failed: " << result.error();
1204 }
1205 return {};
1206}
1207
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001208static Result<void> do_installkey(const BuiltinArguments& args) {
1209 if (!is_file_crypto()) return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -07001210
Eric Biggersf05da4a2018-10-23 13:10:33 -07001211 auto unencrypted_dir = args[1] + fscrypt_unencrypted_folder;
Tom Cherryc3692b32017-08-10 12:22:44 -07001212 if (!make_dir(unencrypted_dir, 0700) && errno != EEXIST) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001213 return ErrnoError() << "Failed to create " << unencrypted_dir;
Janis Danisevskis9cc51722017-03-29 14:50:01 -07001214 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001215 return ExecVdcRebootOnFailure("enablefilecrypto");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001216}
Paul Crowley749af8c2015-05-28 17:35:06 +01001217
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001218static Result<void> do_init_user0(const BuiltinArguments& args) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001219 return ExecVdcRebootOnFailure("init_user0");
Paul Crowley59497452016-02-01 16:37:13 +00001220}
1221
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001222static Result<void> do_mark_post_data(const BuiltinArguments& args) {
Martijn Coenen70788f92019-04-23 16:26:01 +02001223 ServiceList::GetInstance().MarkPostData();
1224
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001225 return {};
Martijn Coenen70788f92019-04-23 16:26:01 +02001226}
1227
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001228static Result<void> GenerateLinkerConfiguration() {
Kiyoung Kim03b9bca2020-11-30 14:49:36 +09001229 const char* linkerconfig_binary = "/apex/com.android.runtime/bin/linkerconfig";
Kiyoung Kim3b2dbe92019-12-30 18:44:41 +09001230 const char* linkerconfig_target = "/linkerconfig";
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001231 const char* arguments[] = {linkerconfig_binary, "--target", linkerconfig_target};
1232
1233 if (logwrap_fork_execvp(arraysize(arguments), arguments, nullptr, false, LOG_KLOG, false,
1234 nullptr) != 0) {
1235 return ErrnoError() << "failed to execute linkerconfig";
1236 }
1237
Kiyoung Kim0cbee0d2021-03-02 16:45:27 +09001238 auto current_mount_ns = GetCurrentMountNamespace();
1239 if (!current_mount_ns.ok()) {
1240 return current_mount_ns.error();
1241 }
1242 if (*current_mount_ns == NS_DEFAULT) {
1243 SetDefaultMountNamespaceReady();
1244 }
1245
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001246 LOG(INFO) << "linkerconfig generated " << linkerconfig_target
1247 << " with mounted APEX modules info";
1248
1249 return {};
1250}
1251
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001252static Result<void> MountLinkerConfigForDefaultNamespace() {
1253 // No need to mount linkerconfig for default mount namespace if the path does not exist (which
1254 // would mean it is already mounted)
1255 if (access("/linkerconfig/default", 0) != 0) {
1256 return {};
1257 }
1258
1259 if (mount("/linkerconfig/default", "/linkerconfig", nullptr, MS_BIND | MS_REC, nullptr) != 0) {
1260 return ErrnoError() << "Failed to mount linker configuration for default mount namespace.";
1261 }
1262
1263 return {};
1264}
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001265static Result<void> do_update_linker_config(const BuiltinArguments&) {
Shikha Malhotra720694d2021-07-30 12:35:27 +00001266 return GenerateLinkerConfiguration();
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001267}
1268
Oli Lan13e51e72019-11-19 18:08:45 +00001269/*
1270 * Creates a directory under /data/misc/apexdata/ for each APEX.
1271 */
Jooyung Han2982f092023-08-11 14:14:37 +09001272static void create_apex_data_dirs() {
1273 for (const auto& name : GetApexListFrom("/apex")) {
1274 auto path = "/data/misc/apexdata/" + name;
Oli Lan90c523b2020-01-17 11:41:04 +00001275 auto options = MkdirOptions{path, 0771, AID_ROOT, AID_SYSTEM, FscryptAction::kNone, "ref"};
Jooyung Han2982f092023-08-11 14:14:37 +09001276 auto result = make_dir_with_options(options);
1277 if (!result.ok()) {
1278 LOG(ERROR) << result.error();
1279 }
Oli Lan13e51e72019-11-19 18:08:45 +00001280 }
Oli Lan13e51e72019-11-19 18:08:45 +00001281}
1282
1283static Result<void> do_perform_apex_config(const BuiltinArguments& args) {
Jooyung Han5c4217c2023-08-10 13:12:53 +09001284 bool bootstrap = false;
1285 if (args.size() == 2) {
1286 if (args[1] != "--bootstrap") {
1287 return Error() << "Unexpected argument: " << args[1];
1288 }
1289 bootstrap = true;
Oli Lan13e51e72019-11-19 18:08:45 +00001290 }
Jooyung Han5c4217c2023-08-10 13:12:53 +09001291
1292 if (!bootstrap) {
Jooyung Han2982f092023-08-11 14:14:37 +09001293 create_apex_data_dirs();
Jooyung Han5c4217c2023-08-10 13:12:53 +09001294 }
1295
Jooyung Han55ef3d62023-08-10 18:27:22 +09001296 auto parse_result = ParseRcScriptsFromAllApexes(bootstrap);
1297 if (!parse_result.ok()) {
1298 return parse_result.error();
Oli Lan13e51e72019-11-19 18:08:45 +00001299 }
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001300
1301 auto update_linker_config = do_update_linker_config(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001302 if (!update_linker_config.ok()) {
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001303 return update_linker_config.error();
1304 }
1305
Jooyung Han5c4217c2023-08-10 13:12:53 +09001306 if (!bootstrap) {
Jooyung Han646e0012023-08-21 17:34:37 +09001307 ServiceList::GetInstance().StartDelayedServices();
Jooyung Han5c4217c2023-08-10 13:12:53 +09001308 }
Oli Lan13e51e72019-11-19 18:08:45 +00001309 return {};
1310}
1311
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001312static Result<void> do_enter_default_mount_ns(const BuiltinArguments& args) {
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001313 if (auto result = SwitchToMountNamespaceIfNeeded(NS_DEFAULT); !result.ok()) {
1314 return result.error();
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001315 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001316 if (auto result = MountLinkerConfigForDefaultNamespace(); !result.ok()) {
1317 return result.error();
1318 }
1319 LOG(INFO) << "Switched to default mount namespace";
1320 return {};
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001321}
1322
Tom Cherryde6bd502018-02-13 16:50:08 -08001323// Builtin-function-map start
Tom Cherryd52a5b32019-07-22 16:05:36 -07001324const BuiltinFunctionMap& GetBuiltinFunctionMap() {
Tom Cherryb7349902015-08-26 11:43:36 -07001325 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -07001326 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -07001327 static const BuiltinFunctionMap builtin_functions = {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001328 {"bootchart", {1, 1, {false, do_bootchart}}},
1329 {"chmod", {2, 2, {true, do_chmod}}},
1330 {"chown", {2, 3, {true, do_chown}}},
1331 {"class_reset", {1, 1, {false, do_class_reset}}},
David Anderson2285b522021-11-09 18:26:39 -08001332 {"class_restart", {1, 2, {false, do_class_restart}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001333 {"class_start", {1, 1, {false, do_class_start}}},
1334 {"class_stop", {1, 1, {false, do_class_stop}}},
1335 {"copy", {2, 2, {true, do_copy}}},
Wei Wang49d25982020-11-18 15:56:14 -08001336 {"copy_per_line", {2, 2, {true, do_copy_per_line}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001337 {"domainname", {1, 1, {true, do_domainname}}},
1338 {"enable", {1, 1, {false, do_enable}}},
1339 {"exec", {1, kMax, {false, do_exec}}},
1340 {"exec_background", {1, kMax, {false, do_exec_background}}},
1341 {"exec_start", {1, 1, {false, do_exec_start}}},
1342 {"export", {2, 2, {false, do_export}}},
1343 {"hostname", {1, 1, {true, do_hostname}}},
1344 {"ifup", {1, 1, {true, do_ifup}}},
1345 {"init_user0", {0, 0, {false, do_init_user0}}},
1346 {"insmod", {1, kMax, {true, do_insmod}}},
1347 {"installkey", {1, 1, {false, do_installkey}}},
Steven Moreland612d7a42018-05-08 11:21:37 -07001348 {"interface_restart", {1, 1, {false, do_interface_restart}}},
1349 {"interface_start", {1, 1, {false, do_interface_start}}},
1350 {"interface_stop", {1, 1, {false, do_interface_stop}}},
Artur Satayev500946b2021-02-15 20:48:49 +00001351 {"load_exports", {1, 1, {false, do_load_exports}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001352 {"load_persist_props", {0, 0, {false, do_load_persist_props}}},
1353 {"load_system_props", {0, 0, {false, do_load_system_props}}},
1354 {"loglevel", {1, 1, {false, do_loglevel}}},
Martijn Coenen70788f92019-04-23 16:26:01 +02001355 {"mark_post_data", {0, 0, {false, do_mark_post_data}}},
Paul Crowley68258e82019-10-28 07:55:03 -07001356 {"mkdir", {1, 6, {true, do_mkdir}}},
Tom Cherry880d5662018-02-05 08:01:54 -08001357 // TODO: Do mount operations in vendor_init.
1358 // mount_all is currently too complex to run in vendor_init as it queues action triggers,
1359 // imports rc scripts, etc. It should be simplified and run in vendor_init context.
1360 // mount and umount are run in the same context as mount_all for symmetry.
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -07001361 {"mount_all", {0, kMax, {false, do_mount_all}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001362 {"mount", {3, kMax, {false, do_mount}}},
Jooyung Han5c4217c2023-08-10 13:12:53 +09001363 {"perform_apex_config", {0, 1, {false, do_perform_apex_config}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001364 {"umount", {1, 1, {false, do_umount}}},
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -07001365 {"umount_all", {0, 1, {false, do_umount_all}}},
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001366 {"update_linker_config", {0, 0, {false, do_update_linker_config}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001367 {"readahead", {1, 2, {true, do_readahead}}},
Nikita Ioffe12a36072019-10-23 20:11:32 +01001368 {"remount_userdata", {0, 0, {false, do_remount_userdata}}},
David Anderson6d7c7a22021-12-03 15:48:16 -08001369 {"restart", {1, 2, {false, do_restart}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001370 {"restorecon", {1, kMax, {true, do_restorecon}}},
1371 {"restorecon_recursive", {1, kMax, {true, do_restorecon_recursive}}},
1372 {"rm", {1, 1, {true, do_rm}}},
1373 {"rmdir", {1, 1, {true, do_rmdir}}},
Tom Cherry32228482018-01-18 16:14:25 -08001374 {"setprop", {2, 2, {true, do_setprop}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001375 {"setrlimit", {3, 3, {false, do_setrlimit}}},
1376 {"start", {1, 1, {false, do_start}}},
1377 {"stop", {1, 1, {false, do_stop}}},
Alistair Delvade28a862020-06-08 11:04:53 -07001378 {"swapon_all", {0, 1, {false, do_swapon_all}}},
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001379 {"enter_default_mount_ns", {0, 0, {false, do_enter_default_mount_ns}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001380 {"symlink", {2, 2, {true, do_symlink}}},
1381 {"sysclktz", {1, 1, {false, do_sysclktz}}},
1382 {"trigger", {1, 1, {false, do_trigger}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001383 {"verity_update_state", {0, 0, {false, do_verity_update_state}}},
1384 {"wait", {1, 2, {true, do_wait}}},
Tom Cherryfa3e52c2017-10-17 10:43:52 -07001385 {"wait_for_prop", {2, 2, {false, do_wait_for_prop}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001386 {"write", {2, 2, {true, do_write}}},
Tom Cherryb7349902015-08-26 11:43:36 -07001387 };
Tom Cherryb27004a2017-03-27 16:27:30 -07001388 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -07001389 return builtin_functions;
1390}
Tom Cherryde6bd502018-02-13 16:50:08 -08001391// Builtin-function-map end
Tom Cherry81f5d3e2017-06-22 12:53:17 -07001392
1393} // namespace init
1394} // namespace android