blob: f743a30e0291ce51dfd34b59563f4ba2f548986b [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>
terryguan43997542024-09-05 17:35:41 -070039#include <sys/swap.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070040#include <sys/syscall.h>
Tom Cherryf57c0bf2017-04-07 13:46:21 -070041#include <sys/system_properties.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080042#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070043#include <sys/types.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070044#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070045#include <unistd.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050046
Ray Essick35ffd692021-10-07 15:48:11 -070047#include <map>
Nikita Ioffe12a36072019-10-23 20:11:32 +010048#include <memory>
49
Nikita Ioffe23dbd6d2019-11-14 01:21:24 +000050#include <InitProperties.sysprop.h>
Tom Cherryede0d532017-07-06 14:20:11 -070051#include <android-base/chrono_utils.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070052#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070053#include <android-base/logging.h>
Mark Salyzynffa52e92020-05-14 09:20:30 -070054#include <android-base/parsedouble.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080055#include <android-base/parseint.h>
Tom Cherryccf23532017-03-28 16:40:41 -070056#include <android-base/properties.h>
Tom Cherry70379912017-08-01 14:10:11 -070057#include <android-base/stringprintf.h>
Tom Cherryccf23532017-03-28 16:40:41 -070058#include <android-base/strings.h>
Tom Cherry70379912017-08-01 14:10:11 -070059#include <android-base/unique_fd.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070060#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070061#include <cutils/android_reboot.h>
Tom Cherryccf23532017-03-28 16:40:41 -070062#include <fs_mgr.h>
Eric Biggersf05da4a2018-10-23 13:10:33 -070063#include <fscrypt/fscrypt.h>
tangjie12b26bdf2023-04-13 17:15:23 +080064#include <libdm/dm.h>
65#include <libdm/loop_control.h>
David Anderson0e330f12019-01-03 18:16:56 -080066#include <libgsi/libgsi.h>
Kiyoung Kime4d3f212019-12-16 14:31:04 +090067#include <logwrap/logwrap.h>
Oli Landc516722020-01-03 10:04:31 +000068#include <private/android_filesystem_config.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070069#include <selinux/android.h>
70#include <selinux/label.h>
71#include <selinux/selinux.h>
Wei Wang02628f32017-08-16 11:34:50 -070072#include <system/thread_defs.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070073
Tom Cherry7fd3bc22018-02-13 15:36:14 -080074#include "action_manager.h"
Deyao Ren238e9092022-07-21 23:05:13 +000075#include "apex_init_util.h"
Tom Cherryb7349902015-08-26 11:43:36 -070076#include "bootchart.h"
Nikita Ioffec0df1872019-11-13 21:47:06 +000077#include "builtin_arguments.h"
Paul Crowley052f31c2019-08-26 10:33:17 -070078#include "fscrypt_init_extensions.h"
Tom Cherrybac32992015-07-31 12:45:25 -070079#include "init.h"
Jiyong Park68660412019-01-16 23:00:59 +090080#include "mount_namespace.h"
Tom Cherry67dee622017-07-27 12:54:48 -070081#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070082#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070083#include "reboot.h"
Tom Cherry7ac013d2017-08-25 10:39:25 -070084#include "rlimit_parser.h"
Vic Yang92c236e2019-05-28 15:58:35 -070085#include "selabel.h"
Joel Galenson4b591f12017-11-27 14:45:26 -080086#include "selinux.h"
Tom Cherrybac32992015-07-31 12:45:25 -070087#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070088#include "service_list.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070089#include "subcontext.h"
Tom Cherrybac32992015-07-31 12:45:25 -070090#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070091
Paul Crowley0b8b2302016-12-19 13:03:47 -080092using namespace std::literals::string_literals;
93
Tom Cherrycf80b6d2019-01-07 14:25:31 -080094using android::base::Basename;
Jiyong Parkd185d4a2021-12-11 10:45:20 +090095using android::base::ResultError;
Tom Cherryc88d8f92019-08-19 15:21:25 -070096using android::base::SetProperty;
Wei Wang49d25982020-11-18 15:56:14 -080097using android::base::Split;
Tom Cherry1ab3dfc2019-04-22 17:46:37 -070098using android::base::StartsWith;
Tom Cherry7896e7a2019-09-04 15:26:52 -070099using android::base::StringPrintf;
Tom Cherry70379912017-08-01 14:10:11 -0700100using android::base::unique_fd;
Tom Cherrya3530e62019-01-30 13:25:35 -0800101using android::fs_mgr::Fstab;
102using android::fs_mgr::ReadFstabFromFile;
Tom Cherry70379912017-08-01 14:10:11 -0700103
Nick Kralevichbc609542015-01-31 21:39:46 -0800104#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
105
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700106namespace android {
107namespace init {
108
Tom Cherryd17c3792019-07-30 10:51:59 -0700109// There are many legacy paths in rootdir/init.rc that will virtually never exist on a new
110// device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there
111// are 81 such failures on cuttlefish. Instead of spamming the log reporting them, we do not
112// report such failures unless we're running at the DEBUG log level.
113class ErrorIgnoreEnoent {
114 public:
115 ErrorIgnoreEnoent()
116 : ignore_error_(errno == ENOENT &&
117 android::base::GetMinimumLogSeverity() > android::base::DEBUG) {}
118 explicit ErrorIgnoreEnoent(int errno_to_append)
119 : error_(errno_to_append),
120 ignore_error_(errno_to_append == ENOENT &&
121 android::base::GetMinimumLogSeverity() > android::base::DEBUG) {}
122
123 template <typename T>
Jiyong Park705abe22021-12-14 22:55:34 +0900124 operator android::base::expected<T, ResultError<android::base::Errno>>() {
Tom Cherryd17c3792019-07-30 10:51:59 -0700125 if (ignore_error_) {
126 return {};
127 }
128 return error_;
129 }
130
131 template <typename T>
132 ErrorIgnoreEnoent& operator<<(T&& t) {
133 error_ << t;
134 return *this;
135 }
136
137 private:
Jiyong Parkd185d4a2021-12-11 10:45:20 +0900138 Error<> error_;
Tom Cherryd17c3792019-07-30 10:51:59 -0700139 bool ignore_error_;
140};
141
142inline ErrorIgnoreEnoent ErrnoErrorIgnoreEnoent() {
143 return ErrorIgnoreEnoent(errno);
144}
145
Tom Cherry172c83f2019-06-26 14:44:37 -0700146std::vector<std::string> late_import_paths;
147
Elliott Hughes9605a942016-11-10 17:43:47 -0800148static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800149
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700150static Result<void> reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyc73b2152018-04-13 17:38:57 +0000151 LOG(ERROR) << "Rebooting into recovery";
Paul Crowleyaf8be582016-05-10 08:52:06 -0700152 std::string err;
153 if (!write_bootloader_message(options, &err)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700154 return Error() << "Failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700155 }
Tom Cherry18278d22019-11-12 16:21:20 -0800156 trigger_shutdown("reboot,recovery");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700157 return {};
Yusuke Sato0df08272015-07-08 14:57:07 -0700158}
159
Tom Cherry911b9b12017-07-27 16:20:58 -0700160template <typename F>
161static void ForEachServiceInClass(const std::string& classname, F function) {
162 for (const auto& service : ServiceList::GetInstance()) {
163 if (service->classnames().count(classname)) std::invoke(function, service);
164 }
165}
166
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700167static Result<void> do_class_start(const BuiltinArguments& args) {
Daniel Rosenbergca00b0e2018-11-27 22:08:02 -0800168 // 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 +0200169 if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700170 return {};
Tom Cherry911b9b12017-07-27 16:20:58 -0700171 // Starting a class does not start services which are explicitly disabled.
172 // They must be started individually.
Tom Cherry20acdef2017-10-03 13:15:03 -0700173 for (const auto& service : ServiceList::GetInstance()) {
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200174 if (service->classnames().count(args[1])) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900175 if (auto result = service->StartIfNotDisabled(); !result.ok()) {
Tom Cherry20acdef2017-10-03 13:15:03 -0700176 LOG(ERROR) << "Could not start service '" << service->name()
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200177 << "' as part of class '" << args[1] << "': " << result.error();
Tom Cherry20acdef2017-10-03 13:15:03 -0700178 }
179 }
180 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700181 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182}
183
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700184static Result<void> do_class_stop(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700185 ForEachServiceInClass(args[1], &Service::Stop);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700186 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187}
188
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700189static Result<void> do_class_reset(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700190 ForEachServiceInClass(args[1], &Service::Reset);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700191 return {};
Ken Sumrall752923c2010-12-03 16:33:31 -0800192}
193
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700194static Result<void> do_class_restart(const BuiltinArguments& args) {
Daniel Rosenbergca00b0e2018-11-27 22:08:02 -0800195 // Do not restart a class if it has a property persist.dont_start_class.CLASS set to 1.
196 if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700197 return {};
David Anderson2285b522021-11-09 18:26:39 -0800198
199 std::string classname;
200
201 CHECK(args.size() == 2 || args.size() == 3);
202
203 bool only_enabled = false;
204 if (args.size() == 3) {
205 if (args[1] != "--only-enabled") {
206 return Error() << "Unexpected argument: " << args[1];
207 }
208 only_enabled = true;
209 classname = args[2];
210 } else if (args.size() == 2) {
211 classname = args[1];
212 }
213
214 for (const auto& service : ServiceList::GetInstance()) {
215 if (!service->classnames().count(classname)) {
216 continue;
217 }
218 if (only_enabled && !service->IsEnabled()) {
219 continue;
220 }
221 service->Restart();
222 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700223 return {};
Steven Moreland2b63d542017-03-10 14:04:37 -0800224}
225
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700226static Result<void> do_domainname(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900227 if (auto result = WriteFile("/proc/sys/kernel/domainname", args[1]); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700228 return Error() << "Unable to write to /proc/sys/kernel/domainname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700229 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700230 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231}
232
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700233static Result<void> do_enable(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700234 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700235 if (!svc) return Error() << "Could not find service";
236
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900237 if (auto result = svc->Enable(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700238 return Error() << "Could not enable service: " << result.error();
239 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700240
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700241 return {};
JP Abgrall3beec7e2014-05-02 21:14:29 -0700242}
243
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700244static Result<void> do_exec(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700245 auto service = Service::MakeTemporaryOneshotService(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900246 if (!service.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -0700247 return Error() << "Could not create exec service: " << service.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700248 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900249 if (auto result = (*service)->ExecStart(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700250 return Error() << "Could not start exec service: " << result.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700251 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700252
Tom Cherry4772f1d2019-07-30 09:34:41 -0700253 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700254 return {};
Tom Cherryb27004a2017-03-27 16:27:30 -0700255}
256
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700257static Result<void> do_exec_background(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700258 auto service = Service::MakeTemporaryOneshotService(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900259 if (!service.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -0700260 return Error() << "Could not create exec background service: " << service.error();
Tom Cherry3631c542017-09-18 12:16:27 -0700261 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900262 if (auto result = (*service)->Start(); !result.ok()) {
Tom Cherry3631c542017-09-18 12:16:27 -0700263 return Error() << "Could not start exec background service: " << result.error();
264 }
265
Tom Cherry4772f1d2019-07-30 09:34:41 -0700266 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700267 return {};
Tom Cherry3631c542017-09-18 12:16:27 -0700268}
269
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700270static Result<void> do_exec_start(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700271 Service* service = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700272 if (!service) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700273 return Error() << "Service not found";
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700274 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700275
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900276 if (auto result = service->ExecStart(); !result.ok()) {
Tom Cherry76af7e62017-08-22 16:13:59 -0700277 return Error() << "Could not start exec service: " << result.error();
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700278 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700279
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700280 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281}
282
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700283static Result<void> do_export(const BuiltinArguments& args) {
Tom Cherry6de21f12017-08-22 15:41:03 -0700284 if (setenv(args[1].c_str(), args[2].c_str(), 1) == -1) {
285 return ErrnoError() << "setenv() failed";
Tom Cherry7fa62c52017-08-01 13:50:23 -0700286 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700287 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288}
289
Artur Satayev500946b2021-02-15 20:48:49 +0000290static Result<void> do_load_exports(const BuiltinArguments& args) {
291 auto file_contents = ReadFile(args[1]);
292 if (!file_contents.ok()) {
293 return Error() << "Could not read input file '" << args[1]
294 << "': " << file_contents.error();
295 }
296
297 auto lines = Split(*file_contents, "\n");
298 for (const auto& line : lines) {
299 if (line.empty()) {
300 continue;
301 }
302
303 auto env = Split(line, " ");
304
305 if (env.size() != 3) {
306 return ErrnoError() << "Expected a line as `export <name> <value>`, found: `" << line
307 << "`";
308 }
309
310 if (env[0] != "export") {
311 return ErrnoError() << "Unknown action: '" << env[0] << "', expected 'export'";
312 }
313
314 if (setenv(env[1].c_str(), env[2].c_str(), 1) == -1) {
315 return ErrnoError() << "Failed to export '" << line << "' from " << args[1];
316 }
317 }
318
319 return {};
320}
321
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700322static Result<void> do_hostname(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900323 if (auto result = WriteFile("/proc/sys/kernel/hostname", args[1]); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700324 return Error() << "Unable to write to /proc/sys/kernel/hostname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700325 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700326 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327}
328
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700329static Result<void> do_ifup(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700330 struct ifreq ifr;
331
332 strlcpy(ifr.ifr_name, args[1].c_str(), IFNAMSIZ);
333
Tom Cherry247ffbf2019-07-08 15:09:36 -0700334 unique_fd s(TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)));
Tom Cherry7fa62c52017-08-01 13:50:23 -0700335 if (s < 0) return ErrnoError() << "opening socket failed";
336
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800337 if (ioctl(s.get(), SIOCGIFFLAGS, &ifr) < 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700338 return ErrnoError() << "ioctl(..., SIOCGIFFLAGS, ...) failed";
339 }
340
341 ifr.ifr_flags |= IFF_UP;
342
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800343 if (ioctl(s.get(), SIOCSIFFLAGS, &ifr) < 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700344 return ErrnoError() << "ioctl(..., SIOCSIFFLAGS, ...) failed";
345 }
346
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700347 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348}
349
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700350static Result<void> do_insmod(const BuiltinArguments& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800351 int flags = 0;
352 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800353
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800354 if (!(*it).compare("-f")) {
355 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
356 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800357 }
358
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800359 std::string filename = *it++;
360 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
Tom Cherry7fa62c52017-08-01 13:50:23 -0700361
362 unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
363 if (fd == -1) return ErrnoError() << "open(\"" << filename << "\") failed";
364
365 int rc = syscall(__NR_finit_module, fd.get(), options.c_str(), flags);
366 if (rc == -1) return ErrnoError() << "finit_module for \"" << filename << "\" failed";
367
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700368 return {};
The Android Open Source Project35237d12008-12-17 18:08:08 -0800369}
370
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700371static Result<void> do_interface_restart(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700372 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
373 if (!svc) return Error() << "interface " << args[1] << " not found";
374 svc->Restart();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700375 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700376}
377
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700378static Result<void> do_interface_start(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700379 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
380 if (!svc) return Error() << "interface " << args[1] << " not found";
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900381 if (auto result = svc->Start(); !result.ok()) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700382 return Error() << "Could not start interface: " << result.error();
383 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700384 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700385}
386
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700387static Result<void> do_interface_stop(const BuiltinArguments& args) {
Steven Moreland612d7a42018-05-08 11:21:37 -0700388 Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
389 if (!svc) return Error() << "interface " << args[1] << " not found";
390 svc->Stop();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700391 return {};
Steven Moreland612d7a42018-05-08 11:21:37 -0700392}
393
Oli Lan13e51e72019-11-19 18:08:45 +0000394static Result<void> make_dir_with_options(const MkdirOptions& options) {
Paul Crowley68258e82019-10-28 07:55:03 -0700395 std::string ref_basename;
Oli Lan13e51e72019-11-19 18:08:45 +0000396 if (options.ref_option == "ref") {
Paul Crowley68258e82019-10-28 07:55:03 -0700397 ref_basename = fscrypt_key_ref;
Oli Lan13e51e72019-11-19 18:08:45 +0000398 } else if (options.ref_option == "per_boot_ref") {
Paul Crowley68258e82019-10-28 07:55:03 -0700399 ref_basename = fscrypt_key_per_boot_ref;
400 } else {
Oli Lan13e51e72019-11-19 18:08:45 +0000401 return Error() << "Unknown key option: '" << options.ref_option << "'";
Paul Crowley1b4e7322019-08-25 12:18:43 -0700402 }
Paul Crowley68258e82019-10-28 07:55:03 -0700403
Paul Crowley1b4e7322019-08-25 12:18:43 -0700404 struct stat mstat;
Oli Lan13e51e72019-11-19 18:08:45 +0000405 if (lstat(options.target.c_str(), &mstat) != 0) {
Paul Crowley1b4e7322019-08-25 12:18:43 -0700406 if (errno != ENOENT) {
Oli Lan13e51e72019-11-19 18:08:45 +0000407 return ErrnoError() << "lstat() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700408 }
Oli Lan13e51e72019-11-19 18:08:45 +0000409 if (!make_dir(options.target, options.mode)) {
410 return ErrnoErrorIgnoreEnoent() << "mkdir() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700411 }
Oli Lan13e51e72019-11-19 18:08:45 +0000412 if (lstat(options.target.c_str(), &mstat) != 0) {
413 return ErrnoError() << "lstat() failed on new " << options.target;
Benoit Goby5c8574b2012-08-14 15:43:46 -0700414 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 }
Paul Crowley1b4e7322019-08-25 12:18:43 -0700416 if (!S_ISDIR(mstat.st_mode)) {
Oli Lan13e51e72019-11-19 18:08:45 +0000417 return Error() << "Not a directory on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700418 }
Oli Lan13e51e72019-11-19 18:08:45 +0000419 bool needs_chmod = (mstat.st_mode & ~S_IFMT) != options.mode;
420 if ((options.uid != static_cast<uid_t>(-1) && options.uid != mstat.st_uid) ||
421 (options.gid != static_cast<gid_t>(-1) && options.gid != mstat.st_gid)) {
422 if (lchown(options.target.c_str(), options.uid, options.gid) == -1) {
423 return ErrnoError() << "lchown failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700424 }
425 // chown may have cleared S_ISUID and S_ISGID, chmod again
426 needs_chmod = true;
427 }
428 if (needs_chmod) {
Oli Lan13e51e72019-11-19 18:08:45 +0000429 if (fchmodat(AT_FDCWD, options.target.c_str(), options.mode, AT_SYMLINK_NOFOLLOW) == -1) {
430 return ErrnoError() << "fchmodat() failed on " << options.target;
Paul Crowley1b4e7322019-08-25 12:18:43 -0700431 }
432 }
Eric Biggers893b1ae2022-06-15 18:52:39 +0000433 if (IsFbeEnabled()) {
Oli Lan13e51e72019-11-19 18:08:45 +0000434 if (!FscryptSetDirectoryPolicy(ref_basename, options.fscrypt_action, options.target)) {
Paul Crowleyc73b2152018-04-13 17:38:57 +0000435 return reboot_into_recovery(
Oli Lan13e51e72019-11-19 18:08:45 +0000436 {"--prompt_and_wipe_data", "--reason=set_policy_failed:"s + options.target});
Paul Crowleyaf8be582016-05-10 08:52:06 -0700437 }
438 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700439 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700440}
441
Oli Lan13e51e72019-11-19 18:08:45 +0000442// mkdir <path> [mode] [owner] [group] [<option> ...]
443static Result<void> do_mkdir(const BuiltinArguments& args) {
444 auto options = ParseMkdir(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900445 if (!options.ok()) return options.error();
Oli Lan13e51e72019-11-19 18:08:45 +0000446 return make_dir_with_options(*options);
447}
448
Alex Light68ab20f2016-06-23 11:11:39 -0700449/* umount <path> */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700450static Result<void> do_umount(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700451 if (umount(args[1].c_str()) < 0) {
452 return ErrnoError() << "umount() failed";
453 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700454 return {};
Alex Light68ab20f2016-06-23 11:11:39 -0700455}
456
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457static struct {
458 const char *name;
459 unsigned flag;
460} mount_flags[] = {
461 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200462 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700463 { "nosuid", MS_NOSUID },
464 { "nodev", MS_NODEV },
465 { "nodiratime", MS_NODIRATIME },
466 { "ro", MS_RDONLY },
467 { "rw", 0 },
468 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700469 { "bind", MS_BIND },
470 { "rec", MS_REC },
471 { "unbindable", MS_UNBINDABLE },
472 { "private", MS_PRIVATE },
473 { "slave", MS_SLAVE },
474 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475 { "defaults", 0 },
476 { 0, 0 },
477};
478
479/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700480static Result<void> do_mount(const BuiltinArguments& args) {
Tom Cherry70379912017-08-01 14:10:11 -0700481 const char* options = nullptr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482 unsigned flags = 0;
Tom Cherry70379912017-08-01 14:10:11 -0700483 bool wait = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484
Tom Cherry70379912017-08-01 14:10:11 -0700485 for (size_t na = 4; na < args.size(); na++) {
486 size_t i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700488 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489 flags |= mount_flags[i].flag;
490 break;
491 }
492 }
493
Colin Crosscd0f1732010-04-19 17:10:24 -0700494 if (!mount_flags[i].name) {
Tom Cherry70379912017-08-01 14:10:11 -0700495 if (!args[na].compare("wait")) {
496 wait = true;
497 // If our last argument isn't a flag, wolf it up as an option string.
498 } else if (na + 1 == args.size()) {
Tom Cherry96f67312015-07-30 13:52:55 -0700499 options = args[na].c_str();
Tom Cherry70379912017-08-01 14:10:11 -0700500 }
Colin Crosscd0f1732010-04-19 17:10:24 -0700501 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700502 }
503
Tom Cherry70379912017-08-01 14:10:11 -0700504 const char* system = args[1].c_str();
505 const char* source = args[2].c_str();
506 const char* target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000507
Tom Cherry70379912017-08-01 14:10:11 -0700508 if (android::base::StartsWith(source, "loop@")) {
509 int mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
tangjie12b26bdf2023-04-13 17:15:23 +0800510 const char* file_path = source + strlen("loop@");
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000511
tangjie12b26bdf2023-04-13 17:15:23 +0800512 // Open source file
513 if (wait) {
514 wait_for_file(file_path, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000515 }
516
tangjie12b26bdf2023-04-13 17:15:23 +0800517 unique_fd fd(TEMP_FAILURE_RETRY(open(file_path, mode | O_CLOEXEC)));
518 if (fd < 0) {
519 return ErrnoError() << "open(" << file_path << ", " << mode << ") failed";
520 }
521
522 // Allocate loop device and attach it to file_path.
523 android::dm::LoopControl loop_control;
524 std::string loop_device;
525 if (!loop_control.Attach(fd.get(), 5s, &loop_device)) {
526 return ErrnoError() << "loop_control.Attach " << file_path << " failed";
527 }
528
529 if (mount(loop_device.c_str(), target, system, flags, options) < 0) {
530 loop_control.Detach(loop_device);
531 return ErrnoError() << "mount() failed";
532 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000533 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700534 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800535 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000536 if (mount(source, target, system, flags, options) < 0) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700537 return ErrnoErrorIgnoreEnoent() << "mount() failed";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000538 }
539
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800541
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700542 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700543}
544
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800545/* Imports .rc files from the specified paths. Default ones are applied if none is given.
546 *
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700547 * rc_paths: list of paths to rc files to import
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800548 */
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700549static void import_late(const std::vector<std::string>& rc_paths) {
Tom Cherry67dee622017-07-27 12:54:48 -0700550 auto& action_manager = ActionManager::GetInstance();
Tom Cherry911b9b12017-07-27 16:20:58 -0700551 auto& service_list = ServiceList::GetInstance();
552 Parser parser = CreateParser(action_manager, service_list);
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700553 if (rc_paths.empty()) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900554 // Fallbacks for partitions on which early mount isn't enabled.
Tom Cherry67dee622017-07-27 12:54:48 -0700555 for (const auto& path : late_import_paths) {
556 parser.ParseConfig(path);
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900557 }
Tom Cherry67dee622017-07-27 12:54:48 -0700558 late_import_paths.clear();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800559 } else {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700560 for (const auto& rc_path : rc_paths) {
561 parser.ParseConfig(rc_path);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800562 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700563 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700564
565 // Turning this on and letting the INFO logging be discarded adds 0.2s to
566 // Nexus 9 boot time, so it's disabled by default.
Tom Cherry30a6f272017-04-19 15:31:58 -0700567 if (false) DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700568}
569
Wei Wangd61a7e22016-08-23 11:58:09 -0700570/* Queue event based on fs_mgr return code.
571 *
572 * code: return code of fs_mgr_mount_all
573 *
574 * This function might request a reboot, in which case it will
575 * not return.
576 *
577 * return code is processed based on input code
578 */
Eric Biggersab74dbb2023-07-10 17:58:33 +0000579static Result<void> queue_fs_event(int code) {
Eric Biggerse5b5e372021-11-08 16:38:54 -0800580 if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Tom Cherryc88d8f92019-08-19 15:21:25 -0700581 SetProperty("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700582 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700583 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700584 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700585 /* Setup a wipe via recovery, and reboot into recovery */
David Anderson0e330f12019-01-03 18:16:56 -0800586 if (android::gsi::IsGsiRunning()) {
587 return Error() << "cannot wipe within GSI";
588 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700589 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800590 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
Paul Crowleyc73b2152018-04-13 17:38:57 +0000591 return reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700592 /* If reboot worked, there is no return. */
Eric Biggersab74dbb2023-07-10 17:58:33 +0000593 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED ||
594 code == FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED ||
595 code == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
Tom Cherryc88d8f92019-08-19 15:21:25 -0700596 SetProperty("ro.crypto.state", "encrypted");
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700597
Paul Crowleyc6846962018-01-30 09:56:03 -0800598 // Although encrypted, vold has already set the device up, so we do not need to
599 // do anything different from the nonencrypted case.
600 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700601 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700602 } else if (code > 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700603 Error() << "fs_mgr_mount_all() returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700604 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700605 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700606
Tom Cherry7fa62c52017-08-01 13:50:23 -0700607 return Error() << "Invalid code: " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700608}
609
Nikita Ioffe12a36072019-10-23 20:11:32 +0100610static int initial_mount_fstab_return_code = -1;
611
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700612/* <= Q: mount_all <fstab> [ <path> ]* [--<options>]*
613 * >= R: mount_all [ <fstab> ] [--<options>]*
Wei Wangd61a7e22016-08-23 11:58:09 -0700614 *
615 * This function might request a reboot, in which case it will
616 * not return.
617 */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700618static Result<void> do_mount_all(const BuiltinArguments& args) {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700619 auto mount_all = ParseMountAll(args.args);
620 if (!mount_all.ok()) return mount_all.error();
Wei Wangd61a7e22016-08-23 11:58:09 -0700621
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700622 const char* prop_post_fix = "default";
623 bool queue_event = true;
624 if (mount_all->mode == MOUNT_MODE_EARLY) {
625 prop_post_fix = "early";
626 queue_event = false;
627 } else if (mount_all->mode == MOUNT_MODE_LATE) {
628 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700629 }
630
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700631 std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
Tom Cherryede0d532017-07-06 14:20:11 -0700632 android::base::Timer t;
Tom Cherry990483d2019-04-17 12:55:50 -0700633
634 Fstab fstab;
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700635 if (mount_all->fstab_path.empty()) {
636 if (!ReadDefaultFstab(&fstab)) {
637 return Error() << "Could not read default fstab";
638 }
639 } else {
640 if (!ReadFstabFromFile(mount_all->fstab_path, &fstab)) {
641 return Error() << "Could not read fstab";
642 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700643 }
Nikita Ioffe12a36072019-10-23 20:11:32 +0100644
Nikita Ioffe9ede7ec2020-09-07 10:27:25 +0100645 auto mount_fstab_result = fs_mgr_mount_all(&fstab, mount_all->mode);
Tom Cherryc88d8f92019-08-19 15:21:25 -0700646 SetProperty(prop_name, std::to_string(t.duration().count()));
Wei Wangd61a7e22016-08-23 11:58:09 -0700647
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700648 if (mount_all->import_rc) {
649 import_late(mount_all->rc_paths);
Wei Wangd61a7e22016-08-23 11:58:09 -0700650 }
651
Nikita Ioffe9ede7ec2020-09-07 10:27:25 +0100652 if (mount_fstab_result.userdata_mounted) {
653 // This call to fs_mgr_mount_all mounted userdata. Keep the result in
654 // order for userspace reboot to correctly remount userdata.
655 LOG(INFO) << "Userdata mounted using "
656 << (mount_all->fstab_path.empty() ? "(default fstab)" : mount_all->fstab_path)
657 << " result : " << mount_fstab_result.code;
658 initial_mount_fstab_return_code = mount_fstab_result.code;
659 }
660
Wei Wangd61a7e22016-08-23 11:58:09 -0700661 if (queue_event) {
662 /* queue_fs_event will queue event based on mount_fstab return code
663 * and return processed return code*/
Eric Biggersab74dbb2023-07-10 17:58:33 +0000664 auto queue_fs_result = queue_fs_event(mount_fstab_result.code);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900665 if (!queue_fs_result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700666 return Error() << "queue_fs_event() failed: " << queue_fs_result.error();
667 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700668 }
669
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700670 return {};
Wei Wangd61a7e22016-08-23 11:58:09 -0700671}
672
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700673/* umount_all [ <fstab> ] */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700674static Result<void> do_umount_all(const BuiltinArguments& args) {
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700675 auto umount_all = ParseUmountAll(args.args);
676 if (!umount_all.ok()) return umount_all.error();
677
Tom Cherry990483d2019-04-17 12:55:50 -0700678 Fstab fstab;
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -0700679 if (umount_all->empty()) {
680 if (!ReadDefaultFstab(&fstab)) {
681 return Error() << "Could not read default fstab";
682 }
683 } else {
684 if (!ReadFstabFromFile(*umount_all, &fstab)) {
685 return Error() << "Could not read fstab";
686 }
Tom Cherry990483d2019-04-17 12:55:50 -0700687 }
688
Tom Cherry3707d322019-08-15 13:07:24 -0700689 if (auto result = fs_mgr_umount_all(&fstab); result != 0) {
690 return Error() << "umount_fstab() failed " << result;
Yifan Hong402633d2019-04-09 10:11:34 -0700691 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700692 return {};
Yifan Hong402633d2019-04-09 10:11:34 -0700693}
694
Alistair Delvade28a862020-06-08 11:04:53 -0700695/* swapon_all [ <fstab> ] */
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700696static Result<void> do_swapon_all(const BuiltinArguments& args) {
Alistair Delvade28a862020-06-08 11:04:53 -0700697 auto swapon_all = ParseSwaponAll(args.args);
698 if (!swapon_all.ok()) return swapon_all.error();
699
Tom Cherry30554572018-11-30 15:21:04 -0800700 Fstab fstab;
Alistair Delvade28a862020-06-08 11:04:53 -0700701 if (swapon_all->empty()) {
702 if (!ReadDefaultFstab(&fstab)) {
703 return Error() << "Could not read default fstab";
704 }
705 } else {
706 if (!ReadFstabFromFile(*swapon_all, &fstab)) {
707 return Error() << "Could not read fstab '" << *swapon_all << "'";
708 }
Tom Cherry30554572018-11-30 15:21:04 -0800709 }
Ken Sumralla76baaa2013-07-09 18:42:09 -0700710
Tom Cherry3707d322019-08-15 13:07:24 -0700711 if (!fs_mgr_swapon_all(fstab)) {
712 return Error() << "fs_mgr_swapon_all() failed";
Tom Cherry30554572018-11-30 15:21:04 -0800713 }
Ken Sumralla76baaa2013-07-09 18:42:09 -0700714
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700715 return {};
Ken Sumralla76baaa2013-07-09 18:42:09 -0700716}
717
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700718static Result<void> do_setprop(const BuiltinArguments& args) {
Tom Cherry1ab3dfc2019-04-22 17:46:37 -0700719 if (StartsWith(args[1], "ctl.")) {
720 return Error()
721 << "Cannot set ctl. properties from init; call the Service functions directly";
722 }
723 if (args[1] == kRestoreconProperty) {
724 return Error() << "Cannot set '" << kRestoreconProperty
725 << "' from init; use the restorecon builtin directly";
726 }
727
Tom Cherryc88d8f92019-08-19 15:21:25 -0700728 SetProperty(args[1], args[2]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700729 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700730}
731
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700732static Result<void> do_setrlimit(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700733 auto rlimit = ParseRlimit(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900734 if (!rlimit.ok()) return rlimit.error();
Tom Cherry7fa62c52017-08-01 13:50:23 -0700735
Tom Cherry7ac013d2017-08-25 10:39:25 -0700736 if (setrlimit(rlimit->first, &rlimit->second) == -1) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700737 return ErrnoError() << "setrlimit failed";
738 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700739 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700740}
741
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700742static Result<void> do_start(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700743 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700744 if (!svc) return Error() << "service " << args[1] << " not found";
Jooyung Hand6b65fb2023-08-22 09:59:26 +0900745 errno = 0;
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900746 if (auto result = svc->Start(); !result.ok()) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700747 return ErrorIgnoreEnoent() << "Could not start service: " << result.error();
Tom Cherry76af7e62017-08-22 16:13:59 -0700748 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700749 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700750}
751
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700752static Result<void> do_stop(const BuiltinArguments& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700753 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry7fa62c52017-08-01 13:50:23 -0700754 if (!svc) return Error() << "service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700755 svc->Stop();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700756 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700757}
758
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700759static Result<void> do_restart(const BuiltinArguments& args) {
David Anderson6d7c7a22021-12-03 15:48:16 -0800760 bool only_if_running = false;
761 if (args.size() == 3) {
762 if (args[1] == "--only-if-running") {
763 only_if_running = true;
764 } else {
765 return Error() << "Unknown argument to restart: " << args[1];
766 }
767 }
768
769 const auto& classname = args[args.size() - 1];
770 Service* svc = ServiceList::GetInstance().FindService(classname);
771 if (!svc) return Error() << "service " << classname << " not found";
772 if (only_if_running && !svc->IsRunning()) {
773 return {};
774 }
Tom Cherrybac32992015-07-31 12:45:25 -0700775 svc->Restart();
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700776 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777}
778
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700779static Result<void> do_trigger(const BuiltinArguments& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700780 ActionManager::GetInstance().QueueEventTrigger(args[1]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700781 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700782}
783
Joel Galenson4b591f12017-11-27 14:45:26 -0800784static int MakeSymlink(const std::string& target, const std::string& linkpath) {
785 std::string secontext;
786 // Passing 0 for mode should work.
787 if (SelabelLookupFileContext(linkpath, 0, &secontext) && !secontext.empty()) {
788 setfscreatecon(secontext.c_str());
789 }
790
791 int rc = symlink(target.c_str(), linkpath.c_str());
792
793 if (!secontext.empty()) {
794 int save_errno = errno;
795 setfscreatecon(nullptr);
796 errno = save_errno;
797 }
798
799 return rc;
800}
801
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700802static Result<void> do_symlink(const BuiltinArguments& args) {
Joel Galenson4b591f12017-11-27 14:45:26 -0800803 if (MakeSymlink(args[1], args[2]) < 0) {
Tom Cherry68f2a462017-08-22 16:15:26 -0700804 // The symlink builtin is often used to create symlinks for older devices to be backwards
805 // compatible with new paths, therefore we skip reporting this error.
Tom Cherryd17c3792019-07-30 10:51:59 -0700806 return ErrnoErrorIgnoreEnoent() << "symlink() failed";
Elliott Hughesda46b392016-10-11 17:09:00 -0700807 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700808 return {};
The Android Open Source Project35237d12008-12-17 18:08:08 -0800809}
810
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700811static Result<void> do_rm(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700812 if (unlink(args[1].c_str()) < 0) {
813 return ErrnoError() << "unlink() failed";
814 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700815 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700816}
817
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700818static Result<void> do_rmdir(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700819 if (rmdir(args[1].c_str()) < 0) {
820 return ErrnoError() << "rmdir() failed";
821 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700822 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700823}
824
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700825static Result<void> do_sysclktz(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700826 struct timezone tz = {};
827 if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) {
828 return Error() << "Unable to parse mins_west_of_gmt";
829 }
830
831 if (settimeofday(nullptr, &tz) == -1) {
832 return ErrnoError() << "settimeofday() failed";
833 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700834 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700835}
836
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700837static Result<void> do_verity_update_state(const BuiltinArguments& args) {
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800838 int mode;
839 if (!fs_mgr_load_verity_state(&mode)) {
840 return Error() << "fs_mgr_load_verity_state() failed";
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700841 }
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800842
843 Fstab fstab;
844 if (!ReadDefaultFstab(&fstab)) {
845 return Error() << "Failed to read default fstab";
846 }
847
848 for (const auto& entry : fstab) {
849 if (!fs_mgr_is_verity_enabled(entry)) {
850 continue;
851 }
852
853 // To be consistent in vboot 1.0 and vboot 2.0 (AVB), use "system" for the partition even
854 // for system as root, so it has property [partition.system.verified].
855 std::string partition = entry.mount_point == "/" ? "system" : Basename(entry.mount_point);
Tom Cherryc88d8f92019-08-19 15:21:25 -0700856 SetProperty("partition." + partition + ".verified", std::to_string(mode));
Tianjie327237d2021-01-20 19:02:34 -0800857
Tianjie10bec652021-08-30 16:10:09 -0700858 auto hashtree_info = fs_mgr_get_hashtree_info(entry);
859 if (hashtree_info) {
860 SetProperty("partition." + partition + ".verified.hash_alg", hashtree_info->algorithm);
861 SetProperty("partition." + partition + ".verified.root_digest",
862 hashtree_info->root_digest);
Nathan Huckleberry997d7382022-10-21 20:55:49 +0000863 SetProperty("partition." + partition + ".verified.check_at_most_once",
864 hashtree_info->check_at_most_once ? "1" : "0");
Tianjie327237d2021-01-20 19:02:34 -0800865 }
Tom Cherrycf80b6d2019-01-07 14:25:31 -0800866 }
867
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700868 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869}
870
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700871static Result<void> do_write(const BuiltinArguments& args) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900872 if (auto result = WriteFile(args[1], args[2]); !result.ok()) {
Tom Cherryd17c3792019-07-30 10:51:59 -0700873 return ErrorIgnoreEnoent()
874 << "Unable to write to file '" << args[1] << "': " << result.error();
Tom Cherry7fa62c52017-08-01 13:50:23 -0700875 }
876
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700877 return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -0700878}
879
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700880static Result<void> readahead_file(const std::string& filename, bool fully) {
Tom Cherry247ffbf2019-07-08 15:09:36 -0700881 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_CLOEXEC)));
Wei Wang02628f32017-08-16 11:34:50 -0700882 if (fd == -1) {
883 return ErrnoError() << "Error opening file";
884 }
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800885 if (posix_fadvise(fd.get(), 0, 0, POSIX_FADV_WILLNEED)) {
Wei Wang02628f32017-08-16 11:34:50 -0700886 return ErrnoError() << "Error posix_fadvise file";
887 }
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800888 if (readahead(fd.get(), 0, std::numeric_limits<size_t>::max())) {
Wei Wang02628f32017-08-16 11:34:50 -0700889 return ErrnoError() << "Error readahead file";
890 }
891 if (fully) {
892 char buf[BUFSIZ];
893 ssize_t n;
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800894 while ((n = TEMP_FAILURE_RETRY(read(fd.get(), &buf[0], sizeof(buf)))) > 0) {
Wei Wang02628f32017-08-16 11:34:50 -0700895 }
896 if (n != 0) {
897 return ErrnoError() << "Error reading file";
898 }
899 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700900 return {};
Wei Wang02628f32017-08-16 11:34:50 -0700901}
902
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700903static Result<void> do_readahead(const BuiltinArguments& args) {
Wei Wang542aae42017-08-04 13:22:36 -0700904 struct stat sb;
905
906 if (stat(args[1].c_str(), &sb)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700907 return ErrnoError() << "Error opening " << args[1];
Wei Wang542aae42017-08-04 13:22:36 -0700908 }
909
Wei Wang02628f32017-08-16 11:34:50 -0700910 bool readfully = false;
911 if (args.size() == 3 && args[2] == "--fully") {
912 readfully = true;
913 }
Wei Wang542aae42017-08-04 13:22:36 -0700914 // We will do readahead in a forked process in order not to block init
915 // since it may block while it reads the
916 // filesystem metadata needed to locate the requested blocks. This
917 // occurs frequently with ext[234] on large files using indirect blocks
918 // instead of extents, giving the appearance that the call blocks until
919 // the requested data has been read.
920 pid_t pid = fork();
921 if (pid == 0) {
Wei Wang02628f32017-08-16 11:34:50 -0700922 if (setpriority(PRIO_PROCESS, 0, static_cast<int>(ANDROID_PRIORITY_LOWEST)) != 0) {
923 PLOG(WARNING) << "setpriority failed";
924 }
925 if (android_set_ioprio(0, IoSchedClass_IDLE, 7)) {
926 PLOG(WARNING) << "ioprio_get failed";
927 }
Wei Wang542aae42017-08-04 13:22:36 -0700928 android::base::Timer t;
929 if (S_ISREG(sb.st_mode)) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900930 if (auto result = readahead_file(args[1], readfully); !result.ok()) {
Wei Wang02628f32017-08-16 11:34:50 -0700931 LOG(WARNING) << "Unable to readahead '" << args[1] << "': " << result.error();
Wei Wang542aae42017-08-04 13:22:36 -0700932 _exit(EXIT_FAILURE);
933 }
934 } else if (S_ISDIR(sb.st_mode)) {
935 char* paths[] = {const_cast<char*>(args[1].data()), nullptr};
936 std::unique_ptr<FTS, decltype(&fts_close)> fts(
937 fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr), fts_close);
938 if (!fts) {
939 PLOG(ERROR) << "Error opening directory: " << args[1];
940 _exit(EXIT_FAILURE);
941 }
942 // Traverse the entire hierarchy and do readahead
943 for (FTSENT* ftsent = fts_read(fts.get()); ftsent != nullptr;
944 ftsent = fts_read(fts.get())) {
945 if (ftsent->fts_info & FTS_F) {
Wei Wang02628f32017-08-16 11:34:50 -0700946 const std::string filename = ftsent->fts_accpath;
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900947 if (auto result = readahead_file(filename, readfully); !result.ok()) {
Wei Wang02628f32017-08-16 11:34:50 -0700948 LOG(WARNING)
949 << "Unable to readahead '" << filename << "': " << result.error();
Wei Wang542aae42017-08-04 13:22:36 -0700950 }
951 }
952 }
953 }
Wei Wang02628f32017-08-16 11:34:50 -0700954 LOG(INFO) << "Readahead " << args[1] << " took " << t << " asynchronously";
Wei Wang542aae42017-08-04 13:22:36 -0700955 _exit(0);
956 } else if (pid < 0) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700957 return ErrnoError() << "Fork failed";
Wei Wang542aae42017-08-04 13:22:36 -0700958 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700959 return {};
Wei Wang542aae42017-08-04 13:22:36 -0700960}
961
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700962static Result<void> do_copy(const BuiltinArguments& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700963 auto file_contents = ReadFile(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900964 if (!file_contents.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700965 return Error() << "Could not read input file '" << args[1] << "': " << file_contents.error();
San Mehat7c44fe52009-08-26 16:39:25 -0700966 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900967 if (auto result = WriteFile(args[2], *file_contents); !result.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700968 return Error() << "Could not write to output file '" << args[2] << "': " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700969 }
Tom Cherry7fa62c52017-08-01 13:50:23 -0700970
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700971 return {};
San Mehat7c44fe52009-08-26 16:39:25 -0700972}
973
Wei Wang49d25982020-11-18 15:56:14 -0800974static Result<void> do_copy_per_line(const BuiltinArguments& args) {
975 std::string file_contents;
976 if (!android::base::ReadFileToString(args[1], &file_contents, true)) {
977 return Error() << "Could not read input file '" << args[1] << "'";
978 }
979 auto lines = Split(file_contents, "\n");
980 for (const auto& line : lines) {
981 auto result = WriteFile(args[2], line);
982 if (!result.ok()) {
983 LOG(VERBOSE) << "Could not write to output file '" << args[2] << "' with '" << line
984 << "' : " << result.error();
985 }
986 }
987
988 return {};
989}
990
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700991static Result<void> do_chown(const BuiltinArguments& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700992 auto uid = DecodeUid(args[1]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900993 if (!uid.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -0700994 return Error() << "Unable to decode UID for '" << args[1] << "': " << uid.error();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700995 }
Tom Cherry517e1f12017-05-04 17:40:33 -0700996
997 // GID is optional and pushes the index of path out by one if specified.
998 const std::string& path = (args.size() == 4) ? args[3] : args[2];
Tom Cherry62ca6632017-08-03 12:54:07 -0700999 Result<gid_t> gid = -1;
Tom Cherry517e1f12017-05-04 17:40:33 -07001000
1001 if (args.size() == 4) {
Tom Cherry62ca6632017-08-03 12:54:07 -07001002 gid = DecodeUid(args[2]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001003 if (!gid.ok()) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001004 return Error() << "Unable to decode GID for '" << args[2] << "': " << gid.error();
Tom Cherry517e1f12017-05-04 17:40:33 -07001005 }
1006 }
1007
Tom Cherry7fa62c52017-08-01 13:50:23 -07001008 if (lchown(path.c_str(), *uid, *gid) == -1) {
Tom Cherryd17c3792019-07-30 10:51:59 -07001009 return ErrnoErrorIgnoreEnoent() << "lchown() failed";
Tom Cherry7fa62c52017-08-01 13:50:23 -07001010 }
Tom Cherry517e1f12017-05-04 17:40:33 -07001011
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001012 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013}
1014
1015static mode_t get_mode(const char *s) {
1016 mode_t mode = 0;
1017 while (*s) {
1018 if (*s >= '0' && *s <= '7') {
1019 mode = (mode<<3) | (*s-'0');
1020 } else {
1021 return -1;
1022 }
1023 s++;
1024 }
1025 return mode;
1026}
1027
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001028static Result<void> do_chmod(const BuiltinArguments& args) {
Tom Cherry96f67312015-07-30 13:52:55 -07001029 mode_t mode = get_mode(args[1].c_str());
1030 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
Tom Cherryd17c3792019-07-30 10:51:59 -07001031 return ErrnoErrorIgnoreEnoent() << "fchmodat() failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001032 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001033 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001034}
1035
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001036static Result<void> do_restorecon(const BuiltinArguments& args) {
Tom Cherry4772f1d2019-07-30 09:34:41 -07001037 auto restorecon_info = ParseRestorecon(args.args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001038 if (!restorecon_info.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -07001039 return restorecon_info.error();
1040 }
1041
1042 const auto& [flag, paths] = *restorecon_info;
1043
Stephen Smalley726e8f72013-10-09 16:02:09 -04001044 int ret = 0;
Tom Cherry4772f1d2019-07-30 09:34:41 -07001045 for (const auto& path : paths) {
1046 if (selinux_android_restorecon(path.c_str(), flag) < 0) {
1047 ret = errno;
Paul Lawrencea8d84342016-11-14 15:40:18 -08001048 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001049 }
Tom Cherry7fa62c52017-08-01 13:50:23 -07001050
Tom Cherryd17c3792019-07-30 10:51:59 -07001051 if (ret) return ErrnoErrorIgnoreEnoent() << "selinux_android_restorecon() failed";
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001052 return {};
Stephen Smalley726e8f72013-10-09 16:02:09 -04001053}
1054
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001055static Result<void> do_restorecon_recursive(const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001056 std::vector<std::string> non_const_args(args.args);
Paul Lawrencea8d84342016-11-14 15:40:18 -08001057 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
Bart Van Assche3dfb8bc2023-02-22 10:01:33 -08001058 return do_restorecon({.args = std::move(non_const_args), .context = args.context});
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001059}
1060
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001061static Result<void> do_loglevel(const BuiltinArguments& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001062 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -07001063 int log_level = -1;
1064 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001065 android::base::LogSeverity severity;
1066 switch (log_level) {
1067 case 7: severity = android::base::DEBUG; break;
1068 case 6: severity = android::base::INFO; break;
1069 case 5:
1070 case 4: severity = android::base::WARNING; break;
1071 case 3: severity = android::base::ERROR; break;
1072 case 2:
1073 case 1:
1074 case 0: severity = android::base::FATAL; break;
1075 default:
Tom Cherry7fa62c52017-08-01 13:50:23 -07001076 return Error() << "invalid log level " << log_level;
Riley Andrews1bbef882014-06-26 13:55:03 -07001077 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -07001078 android::base::SetMinimumLogSeverity(severity);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001079 return {};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001080}
1081
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001082static Result<void> do_load_persist_props(const BuiltinArguments& args) {
Tom Cherry1ab3dfc2019-04-22 17:46:37 -07001083 SendLoadPersistentPropertiesMessage();
1084
1085 start_waiting_for_property("ro.persistent_properties.ready", "true");
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001086 return {};
Ken Sumrallc5c51032011-03-08 17:01:29 -08001087}
1088
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001089static Result<void> do_load_system_props(const BuiltinArguments& args) {
Jiyong Park3b316ee2019-01-13 02:42:31 +09001090 LOG(INFO) << "deprecated action `load_system_props` called.";
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001091 return {};
Riley Andrewse4b7b292014-06-16 15:06:21 -07001092}
1093
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001094static Result<void> do_wait(const BuiltinArguments& args) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001095 auto timeout = kCommandRetryTimeout;
1096 if (args.size() == 3) {
Mark Salyzynffa52e92020-05-14 09:20:30 -07001097 double timeout_double;
1098 if (!android::base::ParseDouble(args[2], &timeout_double, 0)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001099 return Error() << "failed to parse timeout";
Elliott Hughesda46b392016-10-11 17:09:00 -07001100 }
Mark Salyzynffa52e92020-05-14 09:20:30 -07001101 timeout = std::chrono::duration_cast<std::chrono::nanoseconds>(
1102 std::chrono::duration<double>(timeout_double));
Elliott Hughesda46b392016-10-11 17:09:00 -07001103 }
Tom Cherry7fa62c52017-08-01 13:50:23 -07001104
1105 if (wait_for_file(args[1].c_str(), timeout) != 0) {
1106 return Error() << "wait_for_file() failed";
1107 }
1108
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001109 return {};
Colin Crosscd0f1732010-04-19 17:10:24 -07001110}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001111
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001112static Result<void> do_wait_for_prop(const BuiltinArguments& args) {
Wei Wang132ac312017-01-25 16:27:03 -08001113 const char* name = args[1].c_str();
1114 const char* value = args[2].c_str();
1115 size_t value_len = strlen(value);
1116
Tom Cherryde6bd502018-02-13 16:50:08 -08001117 if (!IsLegalPropertyName(name)) {
1118 return Error() << "IsLegalPropertyName(" << name << ") failed";
Wei Wang132ac312017-01-25 16:27:03 -08001119 }
1120 if (value_len >= PROP_VALUE_MAX) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001121 return Error() << "value too long";
Wei Wang132ac312017-01-25 16:27:03 -08001122 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -08001123 if (!start_waiting_for_property(name, value)) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001124 return Error() << "already waiting for a property";
Wei Wang132ac312017-01-25 16:27:03 -08001125 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001126 return {};
Wei Wang132ac312017-01-25 16:27:03 -08001127}
1128
Paul Crowley749af8c2015-05-28 17:35:06 +01001129static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -07001130 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +01001131}
1132
Tom Cherry7896e7a2019-09-04 15:26:52 -07001133static Result<void> ExecWithFunctionOnFailure(const std::vector<std::string>& args,
1134 std::function<void(const std::string&)> function) {
1135 auto service = Service::MakeTemporaryOneshotService(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001136 if (!service.ok()) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001137 function("MakeTemporaryOneshotService failed: " + service.error().message());
Paul Crowleyc73b2152018-04-13 17:38:57 +00001138 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001139 (*service)->AddReapCallback([function](const siginfo_t& siginfo) {
Paul Crowleyc73b2152018-04-13 17:38:57 +00001140 if (siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001141 function(StringPrintf("Exec service failed, status %d", siginfo.si_status));
Paul Crowleyc73b2152018-04-13 17:38:57 +00001142 }
1143 });
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001144 if (auto result = (*service)->ExecStart(); !result.ok()) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001145 function("ExecStart failed: " + result.error().message());
Paul Crowleyc73b2152018-04-13 17:38:57 +00001146 }
Tom Cherry4772f1d2019-07-30 09:34:41 -07001147 ServiceList::GetInstance().AddService(std::move(*service));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001148 return {};
Paul Crowleyc73b2152018-04-13 17:38:57 +00001149}
1150
Tom Cherry7896e7a2019-09-04 15:26:52 -07001151static Result<void> ExecVdcRebootOnFailure(const std::string& vdc_arg) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001152 bool should_reboot_into_recovery = true;
Tom Cherry7896e7a2019-09-04 15:26:52 -07001153 auto reboot_reason = vdc_arg + "_failed";
Nikita Ioffe05506f02020-01-15 14:43:55 +00001154 if (android::sysprop::InitProperties::userspace_reboot_in_progress().value_or(false)) {
1155 should_reboot_into_recovery = false;
Nikita Ioffe4a787d92020-01-15 23:23:13 +00001156 reboot_reason = "userspace_failed," + vdc_arg;
Nikita Ioffe05506f02020-01-15 14:43:55 +00001157 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001158
Nikita Ioffe05506f02020-01-15 14:43:55 +00001159 auto reboot = [reboot_reason, should_reboot_into_recovery](const std::string& message) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001160 // TODO (b/122850122): support this in gsi
Nikita Ioffe05506f02020-01-15 14:43:55 +00001161 if (should_reboot_into_recovery) {
Eric Biggers893b1ae2022-06-15 18:52:39 +00001162 if (IsFbeEnabled() && !android::gsi::IsGsiRunning()) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001163 LOG(ERROR) << message << ": Rebooting into recovery, reason: " << reboot_reason;
1164 if (auto result = reboot_into_recovery(
1165 {"--prompt_and_wipe_data", "--reason="s + reboot_reason});
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001166 !result.ok()) {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001167 LOG(FATAL) << "Could not reboot into recovery: " << result.error();
1168 }
1169 } else {
1170 LOG(ERROR) << "Failure (reboot suppressed): " << reboot_reason;
Tom Cherry7896e7a2019-09-04 15:26:52 -07001171 }
1172 } else {
Nikita Ioffe05506f02020-01-15 14:43:55 +00001173 LOG(ERROR) << message << ": rebooting, reason: " << reboot_reason;
1174 trigger_shutdown("reboot," + reboot_reason);
Tom Cherry7896e7a2019-09-04 15:26:52 -07001175 }
1176 };
1177
1178 std::vector<std::string> args = {"exec", "/system/bin/vdc", "--wait", "cryptfs", vdc_arg};
1179 return ExecWithFunctionOnFailure(args, reboot);
1180}
1181
Nikita Ioffe12a36072019-10-23 20:11:32 +01001182static Result<void> do_remount_userdata(const BuiltinArguments& args) {
1183 if (initial_mount_fstab_return_code == -1) {
1184 return Error() << "Calling remount_userdata too early";
1185 }
1186 Fstab fstab;
1187 if (!ReadDefaultFstab(&fstab)) {
1188 // TODO(b/135984674): should we reboot here?
1189 return Error() << "Failed to read fstab";
1190 }
1191 // TODO(b/135984674): check that fstab contains /data.
1192 if (auto rc = fs_mgr_remount_userdata_into_checkpointing(&fstab); rc < 0) {
Gavin Corkery8c922562020-05-11 14:10:09 +01001193 std::string proc_mounts_output;
1194 android::base::ReadFileToString("/proc/mounts", &proc_mounts_output, true);
1195 android::base::WriteStringToFile(proc_mounts_output,
1196 "/metadata/userspacereboot/mount_info.txt");
Nikita Ioffe4a787d92020-01-15 23:23:13 +00001197 trigger_shutdown("reboot,mount_userdata_failed");
Nikita Ioffe12a36072019-10-23 20:11:32 +01001198 }
Eric Biggersab74dbb2023-07-10 17:58:33 +00001199 if (auto result = queue_fs_event(initial_mount_fstab_return_code); !result.ok()) {
Nikita Ioffe12a36072019-10-23 20:11:32 +01001200 return Error() << "queue_fs_event() failed: " << result.error();
1201 }
1202 return {};
1203}
1204
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001205static Result<void> do_installkey(const BuiltinArguments& args) {
1206 if (!is_file_crypto()) return {};
Tom Cherry7fa62c52017-08-01 13:50:23 -07001207
Eric Biggersf05da4a2018-10-23 13:10:33 -07001208 auto unencrypted_dir = args[1] + fscrypt_unencrypted_folder;
Tom Cherryc3692b32017-08-10 12:22:44 -07001209 if (!make_dir(unencrypted_dir, 0700) && errno != EEXIST) {
Tom Cherry7fa62c52017-08-01 13:50:23 -07001210 return ErrnoError() << "Failed to create " << unencrypted_dir;
Janis Danisevskis9cc51722017-03-29 14:50:01 -07001211 }
Tom Cherry7896e7a2019-09-04 15:26:52 -07001212 return ExecVdcRebootOnFailure("enablefilecrypto");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001213}
Paul Crowley749af8c2015-05-28 17:35:06 +01001214
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001215static Result<void> do_init_user0(const BuiltinArguments& args) {
Tom Cherry7896e7a2019-09-04 15:26:52 -07001216 return ExecVdcRebootOnFailure("init_user0");
Paul Crowley59497452016-02-01 16:37:13 +00001217}
1218
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001219static Result<void> do_mark_post_data(const BuiltinArguments& args) {
Martijn Coenen70788f92019-04-23 16:26:01 +02001220 ServiceList::GetInstance().MarkPostData();
1221
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001222 return {};
Martijn Coenen70788f92019-04-23 16:26:01 +02001223}
1224
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001225static Result<void> GenerateLinkerConfiguration() {
Kiyoung Kim03b9bca2020-11-30 14:49:36 +09001226 const char* linkerconfig_binary = "/apex/com.android.runtime/bin/linkerconfig";
Kiyoung Kim3b2dbe92019-12-30 18:44:41 +09001227 const char* linkerconfig_target = "/linkerconfig";
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001228 const char* arguments[] = {linkerconfig_binary, "--target", linkerconfig_target};
1229
1230 if (logwrap_fork_execvp(arraysize(arguments), arguments, nullptr, false, LOG_KLOG, false,
1231 nullptr) != 0) {
1232 return ErrnoError() << "failed to execute linkerconfig";
1233 }
1234
Kiyoung Kim0cbee0d2021-03-02 16:45:27 +09001235 auto current_mount_ns = GetCurrentMountNamespace();
1236 if (!current_mount_ns.ok()) {
1237 return current_mount_ns.error();
1238 }
1239 if (*current_mount_ns == NS_DEFAULT) {
1240 SetDefaultMountNamespaceReady();
1241 }
1242
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001243 LOG(INFO) << "linkerconfig generated " << linkerconfig_target
1244 << " with mounted APEX modules info";
1245
1246 return {};
1247}
1248
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001249static Result<void> MountLinkerConfigForDefaultNamespace() {
1250 // No need to mount linkerconfig for default mount namespace if the path does not exist (which
1251 // would mean it is already mounted)
1252 if (access("/linkerconfig/default", 0) != 0) {
1253 return {};
1254 }
1255
1256 if (mount("/linkerconfig/default", "/linkerconfig", nullptr, MS_BIND | MS_REC, nullptr) != 0) {
1257 return ErrnoError() << "Failed to mount linker configuration for default mount namespace.";
1258 }
1259
1260 return {};
1261}
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001262static Result<void> do_update_linker_config(const BuiltinArguments&) {
Shikha Malhotra720694d2021-07-30 12:35:27 +00001263 return GenerateLinkerConfiguration();
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001264}
1265
Oli Lan13e51e72019-11-19 18:08:45 +00001266/*
1267 * Creates a directory under /data/misc/apexdata/ for each APEX.
1268 */
Jooyung Han2982f092023-08-11 14:14:37 +09001269static void create_apex_data_dirs() {
1270 for (const auto& name : GetApexListFrom("/apex")) {
1271 auto path = "/data/misc/apexdata/" + name;
Oli Lan90c523b2020-01-17 11:41:04 +00001272 auto options = MkdirOptions{path, 0771, AID_ROOT, AID_SYSTEM, FscryptAction::kNone, "ref"};
Jooyung Han2982f092023-08-11 14:14:37 +09001273 auto result = make_dir_with_options(options);
1274 if (!result.ok()) {
1275 LOG(ERROR) << result.error();
1276 }
Oli Lan13e51e72019-11-19 18:08:45 +00001277 }
Oli Lan13e51e72019-11-19 18:08:45 +00001278}
1279
1280static Result<void> do_perform_apex_config(const BuiltinArguments& args) {
Jooyung Han5c4217c2023-08-10 13:12:53 +09001281 bool bootstrap = false;
1282 if (args.size() == 2) {
1283 if (args[1] != "--bootstrap") {
1284 return Error() << "Unexpected argument: " << args[1];
1285 }
1286 bootstrap = true;
Oli Lan13e51e72019-11-19 18:08:45 +00001287 }
Jooyung Han5c4217c2023-08-10 13:12:53 +09001288
1289 if (!bootstrap) {
Jooyung Han2982f092023-08-11 14:14:37 +09001290 create_apex_data_dirs();
Jooyung Han5c4217c2023-08-10 13:12:53 +09001291 }
1292
Jooyung Han55ef3d62023-08-10 18:27:22 +09001293 auto parse_result = ParseRcScriptsFromAllApexes(bootstrap);
1294 if (!parse_result.ok()) {
1295 return parse_result.error();
Oli Lan13e51e72019-11-19 18:08:45 +00001296 }
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001297
1298 auto update_linker_config = do_update_linker_config(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +09001299 if (!update_linker_config.ok()) {
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001300 return update_linker_config.error();
1301 }
1302
Jooyung Han5c4217c2023-08-10 13:12:53 +09001303 if (!bootstrap) {
Jooyung Han646e0012023-08-21 17:34:37 +09001304 ServiceList::GetInstance().StartDelayedServices();
Jooyung Han5c4217c2023-08-10 13:12:53 +09001305 }
Oli Lan13e51e72019-11-19 18:08:45 +00001306 return {};
1307}
1308
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001309static Result<void> do_enter_default_mount_ns(const BuiltinArguments& args) {
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001310 if (auto result = SwitchToMountNamespaceIfNeeded(NS_DEFAULT); !result.ok()) {
1311 return result.error();
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001312 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +09001313 if (auto result = MountLinkerConfigForDefaultNamespace(); !result.ok()) {
1314 return result.error();
1315 }
1316 LOG(INFO) << "Switched to default mount namespace";
1317 return {};
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001318}
1319
terryguan43997542024-09-05 17:35:41 -07001320static Result<void> do_swapoff(const BuiltinArguments& args) {
1321 if (!swapoff(args[1].c_str())) {
1322 return ErrnoError() << "swapoff() failed";
1323 }
1324 return {};
1325}
1326
Tom Cherryde6bd502018-02-13 16:50:08 -08001327// Builtin-function-map start
Tom Cherryd52a5b32019-07-22 16:05:36 -07001328const BuiltinFunctionMap& GetBuiltinFunctionMap() {
Tom Cherryb7349902015-08-26 11:43:36 -07001329 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -07001330 // clang-format off
Tom Cherryd52a5b32019-07-22 16:05:36 -07001331 static const BuiltinFunctionMap builtin_functions = {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001332 {"bootchart", {1, 1, {false, do_bootchart}}},
1333 {"chmod", {2, 2, {true, do_chmod}}},
1334 {"chown", {2, 3, {true, do_chown}}},
1335 {"class_reset", {1, 1, {false, do_class_reset}}},
David Anderson2285b522021-11-09 18:26:39 -08001336 {"class_restart", {1, 2, {false, do_class_restart}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001337 {"class_start", {1, 1, {false, do_class_start}}},
1338 {"class_stop", {1, 1, {false, do_class_stop}}},
1339 {"copy", {2, 2, {true, do_copy}}},
Wei Wang49d25982020-11-18 15:56:14 -08001340 {"copy_per_line", {2, 2, {true, do_copy_per_line}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001341 {"domainname", {1, 1, {true, do_domainname}}},
1342 {"enable", {1, 1, {false, do_enable}}},
1343 {"exec", {1, kMax, {false, do_exec}}},
1344 {"exec_background", {1, kMax, {false, do_exec_background}}},
1345 {"exec_start", {1, 1, {false, do_exec_start}}},
1346 {"export", {2, 2, {false, do_export}}},
1347 {"hostname", {1, 1, {true, do_hostname}}},
1348 {"ifup", {1, 1, {true, do_ifup}}},
1349 {"init_user0", {0, 0, {false, do_init_user0}}},
1350 {"insmod", {1, kMax, {true, do_insmod}}},
1351 {"installkey", {1, 1, {false, do_installkey}}},
Steven Moreland612d7a42018-05-08 11:21:37 -07001352 {"interface_restart", {1, 1, {false, do_interface_restart}}},
1353 {"interface_start", {1, 1, {false, do_interface_start}}},
1354 {"interface_stop", {1, 1, {false, do_interface_stop}}},
Artur Satayev500946b2021-02-15 20:48:49 +00001355 {"load_exports", {1, 1, {false, do_load_exports}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001356 {"load_persist_props", {0, 0, {false, do_load_persist_props}}},
1357 {"load_system_props", {0, 0, {false, do_load_system_props}}},
1358 {"loglevel", {1, 1, {false, do_loglevel}}},
Martijn Coenen70788f92019-04-23 16:26:01 +02001359 {"mark_post_data", {0, 0, {false, do_mark_post_data}}},
Paul Crowley68258e82019-10-28 07:55:03 -07001360 {"mkdir", {1, 6, {true, do_mkdir}}},
Tom Cherry880d5662018-02-05 08:01:54 -08001361 // TODO: Do mount operations in vendor_init.
1362 // mount_all is currently too complex to run in vendor_init as it queues action triggers,
1363 // imports rc scripts, etc. It should be simplified and run in vendor_init context.
1364 // mount and umount are run in the same context as mount_all for symmetry.
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -07001365 {"mount_all", {0, kMax, {false, do_mount_all}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001366 {"mount", {3, kMax, {false, do_mount}}},
Jooyung Han5c4217c2023-08-10 13:12:53 +09001367 {"perform_apex_config", {0, 1, {false, do_perform_apex_config}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001368 {"umount", {1, 1, {false, do_umount}}},
Alistair Delvaa2cc1eb2020-05-20 16:24:00 -07001369 {"umount_all", {0, 1, {false, do_umount_all}}},
Kiyoung Kime4d3f212019-12-16 14:31:04 +09001370 {"update_linker_config", {0, 0, {false, do_update_linker_config}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001371 {"readahead", {1, 2, {true, do_readahead}}},
Nikita Ioffe12a36072019-10-23 20:11:32 +01001372 {"remount_userdata", {0, 0, {false, do_remount_userdata}}},
David Anderson6d7c7a22021-12-03 15:48:16 -08001373 {"restart", {1, 2, {false, do_restart}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001374 {"restorecon", {1, kMax, {true, do_restorecon}}},
1375 {"restorecon_recursive", {1, kMax, {true, do_restorecon_recursive}}},
1376 {"rm", {1, 1, {true, do_rm}}},
1377 {"rmdir", {1, 1, {true, do_rmdir}}},
Tom Cherry32228482018-01-18 16:14:25 -08001378 {"setprop", {2, 2, {true, do_setprop}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001379 {"setrlimit", {3, 3, {false, do_setrlimit}}},
1380 {"start", {1, 1, {false, do_start}}},
1381 {"stop", {1, 1, {false, do_stop}}},
Alistair Delvade28a862020-06-08 11:04:53 -07001382 {"swapon_all", {0, 1, {false, do_swapon_all}}},
terryguan43997542024-09-05 17:35:41 -07001383 {"swapoff", {1, 1, {false, do_swapoff}}},
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +09001384 {"enter_default_mount_ns", {0, 0, {false, do_enter_default_mount_ns}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001385 {"symlink", {2, 2, {true, do_symlink}}},
1386 {"sysclktz", {1, 1, {false, do_sysclktz}}},
1387 {"trigger", {1, 1, {false, do_trigger}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001388 {"verity_update_state", {0, 0, {false, do_verity_update_state}}},
1389 {"wait", {1, 2, {true, do_wait}}},
Tom Cherryfa3e52c2017-10-17 10:43:52 -07001390 {"wait_for_prop", {2, 2, {false, do_wait_for_prop}}},
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001391 {"write", {2, 2, {true, do_write}}},
Tom Cherryb7349902015-08-26 11:43:36 -07001392 };
Tom Cherryb27004a2017-03-27 16:27:30 -07001393 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -07001394 return builtin_functions;
1395}
Tom Cherryde6bd502018-02-13 16:50:08 -08001396// Builtin-function-map end
Tom Cherry81f5d3e2017-06-22 12:53:17 -07001397
1398} // namespace init
1399} // namespace android