The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 17 | #include "builtins.h" |
| 18 | |
Tom Cherry | 3041a51 | 2019-05-21 17:48:33 -0700 | [diff] [blame] | 19 | #include <android/api-level.h> |
Mark Salyzyn | a98cc9c | 2016-04-05 13:43:40 -0700 | [diff] [blame] | 20 | #include <dirent.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 23 | #include <fts.h> |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 24 | #include <glob.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 25 | #include <linux/loop.h> |
| 26 | #include <linux/module.h> |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 27 | #include <mntent.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 28 | #include <net/if.h> |
Mark Salyzyn | ad575e0 | 2016-04-05 08:10:25 -0700 | [diff] [blame] | 29 | #include <sched.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 30 | #include <signal.h> |
Ray Essick | 35ffd69 | 2021-10-07 15:48:11 -0700 | [diff] [blame] | 31 | #include <stdint.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 32 | #include <stdio.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | #include <stdlib.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 34 | #include <string.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | #include <sys/mount.h> |
| 36 | #include <sys/resource.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 39 | #include <sys/syscall.h> |
Tom Cherry | f57c0bf | 2017-04-07 13:46:21 -0700 | [diff] [blame] | 40 | #include <sys/system_properties.h> |
Elliott Hughes | 3d74d7a | 2015-01-29 21:31:23 -0800 | [diff] [blame] | 41 | #include <sys/time.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 42 | #include <sys/types.h> |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 43 | #include <sys/wait.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 44 | #include <unistd.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 45 | |
Ray Essick | 35ffd69 | 2021-10-07 15:48:11 -0700 | [diff] [blame] | 46 | #include <map> |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 47 | #include <memory> |
| 48 | |
Nikita Ioffe | 23dbd6d | 2019-11-14 01:21:24 +0000 | [diff] [blame] | 49 | #include <InitProperties.sysprop.h> |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 50 | #include <android-base/chrono_utils.h> |
Mark Salyzyn | a98cc9c | 2016-04-05 13:43:40 -0700 | [diff] [blame] | 51 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 52 | #include <android-base/logging.h> |
Mark Salyzyn | ffa52e9 | 2020-05-14 09:20:30 -0700 | [diff] [blame] | 53 | #include <android-base/parsedouble.h> |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 54 | #include <android-base/parseint.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 55 | #include <android-base/properties.h> |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 56 | #include <android-base/stringprintf.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 57 | #include <android-base/strings.h> |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 58 | #include <android-base/unique_fd.h> |
Yabin Cui | 0b1252c | 2016-06-24 18:28:03 -0700 | [diff] [blame] | 59 | #include <bootloader_message/bootloader_message.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 60 | #include <cutils/android_reboot.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 61 | #include <fs_mgr.h> |
Eric Biggers | f05da4a | 2018-10-23 13:10:33 -0700 | [diff] [blame] | 62 | #include <fscrypt/fscrypt.h> |
David Anderson | 0e330f1 | 2019-01-03 18:16:56 -0800 | [diff] [blame] | 63 | #include <libgsi/libgsi.h> |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 64 | #include <logwrap/logwrap.h> |
Oli Lan | dc51672 | 2020-01-03 10:04:31 +0000 | [diff] [blame] | 65 | #include <private/android_filesystem_config.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 66 | #include <selinux/android.h> |
| 67 | #include <selinux/label.h> |
| 68 | #include <selinux/selinux.h> |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 69 | #include <system/thread_defs.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 70 | |
Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 71 | #include "action_manager.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 72 | #include "bootchart.h" |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 73 | #include "builtin_arguments.h" |
Paul Crowley | 052f31c | 2019-08-26 10:33:17 -0700 | [diff] [blame] | 74 | #include "fscrypt_init_extensions.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 75 | #include "init.h" |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 76 | #include "mount_namespace.h" |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 77 | #include "parser.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 78 | #include "property_service.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 79 | #include "reboot.h" |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 80 | #include "rlimit_parser.h" |
Vic Yang | 92c236e | 2019-05-28 15:58:35 -0700 | [diff] [blame] | 81 | #include "selabel.h" |
Joel Galenson | 4b591f1 | 2017-11-27 14:45:26 -0800 | [diff] [blame] | 82 | #include "selinux.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 83 | #include "service.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 84 | #include "service_list.h" |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 85 | #include "subcontext.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 86 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 87 | |
Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 88 | using namespace std::literals::string_literals; |
| 89 | |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 90 | using android::base::Basename; |
Jiyong Park | d185d4a | 2021-12-11 10:45:20 +0900 | [diff] [blame^] | 91 | using android::base::ResultError; |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 92 | using android::base::SetProperty; |
Wei Wang | 49d2598 | 2020-11-18 15:56:14 -0800 | [diff] [blame] | 93 | using android::base::Split; |
Tom Cherry | 1ab3dfc | 2019-04-22 17:46:37 -0700 | [diff] [blame] | 94 | using android::base::StartsWith; |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 95 | using android::base::StringPrintf; |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 96 | using android::base::unique_fd; |
Tom Cherry | a3530e6 | 2019-01-30 13:25:35 -0800 | [diff] [blame] | 97 | using android::fs_mgr::Fstab; |
| 98 | using android::fs_mgr::ReadFstabFromFile; |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 99 | |
Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 100 | #define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW |
| 101 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 102 | namespace android { |
| 103 | namespace init { |
| 104 | |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 105 | // There are many legacy paths in rootdir/init.rc that will virtually never exist on a new |
| 106 | // device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there |
| 107 | // are 81 such failures on cuttlefish. Instead of spamming the log reporting them, we do not |
| 108 | // report such failures unless we're running at the DEBUG log level. |
| 109 | class ErrorIgnoreEnoent { |
| 110 | public: |
| 111 | ErrorIgnoreEnoent() |
| 112 | : ignore_error_(errno == ENOENT && |
| 113 | android::base::GetMinimumLogSeverity() > android::base::DEBUG) {} |
| 114 | explicit ErrorIgnoreEnoent(int errno_to_append) |
| 115 | : error_(errno_to_append), |
| 116 | ignore_error_(errno_to_append == ENOENT && |
| 117 | android::base::GetMinimumLogSeverity() > android::base::DEBUG) {} |
| 118 | |
| 119 | template <typename T> |
Jiyong Park | d185d4a | 2021-12-11 10:45:20 +0900 | [diff] [blame^] | 120 | operator android::base::expected<T, ResultError<int>>() { |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 121 | if (ignore_error_) { |
| 122 | return {}; |
| 123 | } |
| 124 | return error_; |
| 125 | } |
| 126 | |
| 127 | template <typename T> |
| 128 | ErrorIgnoreEnoent& operator<<(T&& t) { |
| 129 | error_ << t; |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | private: |
Jiyong Park | d185d4a | 2021-12-11 10:45:20 +0900 | [diff] [blame^] | 134 | Error<> error_; |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 135 | bool ignore_error_; |
| 136 | }; |
| 137 | |
| 138 | inline ErrorIgnoreEnoent ErrnoErrorIgnoreEnoent() { |
| 139 | return ErrorIgnoreEnoent(errno); |
| 140 | } |
| 141 | |
Tom Cherry | 172c83f | 2019-06-26 14:44:37 -0700 | [diff] [blame] | 142 | std::vector<std::string> late_import_paths; |
| 143 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 144 | static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 145 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 146 | static Result<void> reboot_into_recovery(const std::vector<std::string>& options) { |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 147 | LOG(ERROR) << "Rebooting into recovery"; |
Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 148 | std::string err; |
| 149 | if (!write_bootloader_message(options, &err)) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 150 | return Error() << "Failed to set bootloader message: " << err; |
Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 151 | } |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 152 | trigger_shutdown("reboot,recovery"); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 153 | return {}; |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 156 | template <typename F> |
| 157 | static void ForEachServiceInClass(const std::string& classname, F function) { |
| 158 | for (const auto& service : ServiceList::GetInstance()) { |
| 159 | if (service->classnames().count(classname)) std::invoke(function, service); |
| 160 | } |
| 161 | } |
| 162 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 163 | static Result<void> do_class_start(const BuiltinArguments& args) { |
Daniel Rosenberg | ca00b0e | 2018-11-27 22:08:02 -0800 | [diff] [blame] | 164 | // Do not start a class if it has a property persist.dont_start_class.CLASS set to 1. |
Martijn Coenen | acc45aa | 2019-05-15 22:04:13 +0200 | [diff] [blame] | 165 | if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false)) |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 166 | return {}; |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 167 | // Starting a class does not start services which are explicitly disabled. |
| 168 | // They must be started individually. |
Tom Cherry | 20acdef | 2017-10-03 13:15:03 -0700 | [diff] [blame] | 169 | for (const auto& service : ServiceList::GetInstance()) { |
Martijn Coenen | acc45aa | 2019-05-15 22:04:13 +0200 | [diff] [blame] | 170 | if (service->classnames().count(args[1])) { |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 171 | if (auto result = service->StartIfNotDisabled(); !result.ok()) { |
Tom Cherry | 20acdef | 2017-10-03 13:15:03 -0700 | [diff] [blame] | 172 | LOG(ERROR) << "Could not start service '" << service->name() |
Martijn Coenen | acc45aa | 2019-05-15 22:04:13 +0200 | [diff] [blame] | 173 | << "' as part of class '" << args[1] << "': " << result.error(); |
Tom Cherry | 20acdef | 2017-10-03 13:15:03 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 177 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 180 | static Result<void> do_class_stop(const BuiltinArguments& args) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 181 | ForEachServiceInClass(args[1], &Service::Stop); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 182 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 185 | static Result<void> do_class_reset(const BuiltinArguments& args) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 186 | ForEachServiceInClass(args[1], &Service::Reset); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 187 | return {}; |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 190 | static Result<void> do_class_restart(const BuiltinArguments& args) { |
Daniel Rosenberg | ca00b0e | 2018-11-27 22:08:02 -0800 | [diff] [blame] | 191 | // Do not restart a class if it has a property persist.dont_start_class.CLASS set to 1. |
| 192 | if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false)) |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 193 | return {}; |
David Anderson | 2285b52 | 2021-11-09 18:26:39 -0800 | [diff] [blame] | 194 | |
| 195 | std::string classname; |
| 196 | |
| 197 | CHECK(args.size() == 2 || args.size() == 3); |
| 198 | |
| 199 | bool only_enabled = false; |
| 200 | if (args.size() == 3) { |
| 201 | if (args[1] != "--only-enabled") { |
| 202 | return Error() << "Unexpected argument: " << args[1]; |
| 203 | } |
| 204 | only_enabled = true; |
| 205 | classname = args[2]; |
| 206 | } else if (args.size() == 2) { |
| 207 | classname = args[1]; |
| 208 | } |
| 209 | |
| 210 | for (const auto& service : ServiceList::GetInstance()) { |
| 211 | if (!service->classnames().count(classname)) { |
| 212 | continue; |
| 213 | } |
| 214 | if (only_enabled && !service->IsEnabled()) { |
| 215 | continue; |
| 216 | } |
| 217 | service->Restart(); |
| 218 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 219 | return {}; |
Steven Moreland | 2b63d54 | 2017-03-10 14:04:37 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 222 | static Result<void> do_domainname(const BuiltinArguments& args) { |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 223 | if (auto result = WriteFile("/proc/sys/kernel/domainname", args[1]); !result.ok()) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 224 | return Error() << "Unable to write to /proc/sys/kernel/domainname: " << result.error(); |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 225 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 226 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 229 | static Result<void> do_enable(const BuiltinArguments& args) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 230 | Service* svc = ServiceList::GetInstance().FindService(args[1]); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 231 | if (!svc) return Error() << "Could not find service"; |
| 232 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 233 | if (auto result = svc->Enable(); !result.ok()) { |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 234 | return Error() << "Could not enable service: " << result.error(); |
| 235 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 236 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 237 | return {}; |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 240 | static Result<void> do_exec(const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 241 | auto service = Service::MakeTemporaryOneshotService(args.args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 242 | if (!service.ok()) { |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 243 | return Error() << "Could not create exec service: " << service.error(); |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 244 | } |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 245 | if (auto result = (*service)->ExecStart(); !result.ok()) { |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 246 | return Error() << "Could not start exec service: " << result.error(); |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 247 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 248 | |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 249 | ServiceList::GetInstance().AddService(std::move(*service)); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 250 | return {}; |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 253 | static Result<void> do_exec_background(const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 254 | auto service = Service::MakeTemporaryOneshotService(args.args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 255 | if (!service.ok()) { |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 256 | return Error() << "Could not create exec background service: " << service.error(); |
Tom Cherry | 3631c54 | 2017-09-18 12:16:27 -0700 | [diff] [blame] | 257 | } |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 258 | if (auto result = (*service)->Start(); !result.ok()) { |
Tom Cherry | 3631c54 | 2017-09-18 12:16:27 -0700 | [diff] [blame] | 259 | return Error() << "Could not start exec background service: " << result.error(); |
| 260 | } |
| 261 | |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 262 | ServiceList::GetInstance().AddService(std::move(*service)); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 263 | return {}; |
Tom Cherry | 3631c54 | 2017-09-18 12:16:27 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 266 | static Result<void> do_exec_start(const BuiltinArguments& args) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 267 | Service* service = ServiceList::GetInstance().FindService(args[1]); |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 268 | if (!service) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 269 | return Error() << "Service not found"; |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 270 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 271 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 272 | if (auto result = service->ExecStart(); !result.ok()) { |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 273 | return Error() << "Could not start exec service: " << result.error(); |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 274 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 275 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 276 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 279 | static Result<void> do_export(const BuiltinArguments& args) { |
Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 280 | if (setenv(args[1].c_str(), args[2].c_str(), 1) == -1) { |
| 281 | return ErrnoError() << "setenv() failed"; |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 282 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 283 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Artur Satayev | 500946b | 2021-02-15 20:48:49 +0000 | [diff] [blame] | 286 | static Result<void> do_load_exports(const BuiltinArguments& args) { |
| 287 | auto file_contents = ReadFile(args[1]); |
| 288 | if (!file_contents.ok()) { |
| 289 | return Error() << "Could not read input file '" << args[1] |
| 290 | << "': " << file_contents.error(); |
| 291 | } |
| 292 | |
| 293 | auto lines = Split(*file_contents, "\n"); |
| 294 | for (const auto& line : lines) { |
| 295 | if (line.empty()) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | auto env = Split(line, " "); |
| 300 | |
| 301 | if (env.size() != 3) { |
| 302 | return ErrnoError() << "Expected a line as `export <name> <value>`, found: `" << line |
| 303 | << "`"; |
| 304 | } |
| 305 | |
| 306 | if (env[0] != "export") { |
| 307 | return ErrnoError() << "Unknown action: '" << env[0] << "', expected 'export'"; |
| 308 | } |
| 309 | |
| 310 | if (setenv(env[1].c_str(), env[2].c_str(), 1) == -1) { |
| 311 | return ErrnoError() << "Failed to export '" << line << "' from " << args[1]; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return {}; |
| 316 | } |
| 317 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 318 | static Result<void> do_hostname(const BuiltinArguments& args) { |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 319 | if (auto result = WriteFile("/proc/sys/kernel/hostname", args[1]); !result.ok()) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 320 | return Error() << "Unable to write to /proc/sys/kernel/hostname: " << result.error(); |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 321 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 322 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 325 | static Result<void> do_ifup(const BuiltinArguments& args) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 326 | struct ifreq ifr; |
| 327 | |
| 328 | strlcpy(ifr.ifr_name, args[1].c_str(), IFNAMSIZ); |
| 329 | |
Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 330 | unique_fd s(TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0))); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 331 | if (s < 0) return ErrnoError() << "opening socket failed"; |
| 332 | |
| 333 | if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) { |
| 334 | return ErrnoError() << "ioctl(..., SIOCGIFFLAGS, ...) failed"; |
| 335 | } |
| 336 | |
| 337 | ifr.ifr_flags |= IFF_UP; |
| 338 | |
| 339 | if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) { |
| 340 | return ErrnoError() << "ioctl(..., SIOCSIFFLAGS, ...) failed"; |
| 341 | } |
| 342 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 343 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 346 | static Result<void> do_insmod(const BuiltinArguments& args) { |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 347 | int flags = 0; |
| 348 | auto it = args.begin() + 1; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 349 | |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 350 | if (!(*it).compare("-f")) { |
| 351 | flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS; |
| 352 | it++; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 355 | std::string filename = *it++; |
| 356 | std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' '); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 357 | |
| 358 | unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); |
| 359 | if (fd == -1) return ErrnoError() << "open(\"" << filename << "\") failed"; |
| 360 | |
| 361 | int rc = syscall(__NR_finit_module, fd.get(), options.c_str(), flags); |
| 362 | if (rc == -1) return ErrnoError() << "finit_module for \"" << filename << "\" failed"; |
| 363 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 364 | return {}; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 367 | static Result<void> do_interface_restart(const BuiltinArguments& args) { |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 368 | Service* svc = ServiceList::GetInstance().FindInterface(args[1]); |
| 369 | if (!svc) return Error() << "interface " << args[1] << " not found"; |
| 370 | svc->Restart(); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 371 | return {}; |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 374 | static Result<void> do_interface_start(const BuiltinArguments& args) { |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 375 | Service* svc = ServiceList::GetInstance().FindInterface(args[1]); |
| 376 | if (!svc) return Error() << "interface " << args[1] << " not found"; |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 377 | if (auto result = svc->Start(); !result.ok()) { |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 378 | return Error() << "Could not start interface: " << result.error(); |
| 379 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 380 | return {}; |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 383 | static Result<void> do_interface_stop(const BuiltinArguments& args) { |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 384 | Service* svc = ServiceList::GetInstance().FindInterface(args[1]); |
| 385 | if (!svc) return Error() << "interface " << args[1] << " not found"; |
| 386 | svc->Stop(); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 387 | return {}; |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 390 | static Result<void> make_dir_with_options(const MkdirOptions& options) { |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 391 | std::string ref_basename; |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 392 | if (options.ref_option == "ref") { |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 393 | ref_basename = fscrypt_key_ref; |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 394 | } else if (options.ref_option == "per_boot_ref") { |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 395 | ref_basename = fscrypt_key_per_boot_ref; |
| 396 | } else { |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 397 | return Error() << "Unknown key option: '" << options.ref_option << "'"; |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 398 | } |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 399 | |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 400 | struct stat mstat; |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 401 | if (lstat(options.target.c_str(), &mstat) != 0) { |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 402 | if (errno != ENOENT) { |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 403 | return ErrnoError() << "lstat() failed on " << options.target; |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 404 | } |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 405 | if (!make_dir(options.target, options.mode)) { |
| 406 | return ErrnoErrorIgnoreEnoent() << "mkdir() failed on " << options.target; |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 407 | } |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 408 | if (lstat(options.target.c_str(), &mstat) != 0) { |
| 409 | return ErrnoError() << "lstat() failed on new " << options.target; |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 410 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 411 | } |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 412 | if (!S_ISDIR(mstat.st_mode)) { |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 413 | return Error() << "Not a directory on " << options.target; |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 414 | } |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 415 | bool needs_chmod = (mstat.st_mode & ~S_IFMT) != options.mode; |
| 416 | if ((options.uid != static_cast<uid_t>(-1) && options.uid != mstat.st_uid) || |
| 417 | (options.gid != static_cast<gid_t>(-1) && options.gid != mstat.st_gid)) { |
| 418 | if (lchown(options.target.c_str(), options.uid, options.gid) == -1) { |
| 419 | return ErrnoError() << "lchown failed on " << options.target; |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 420 | } |
| 421 | // chown may have cleared S_ISUID and S_ISGID, chmod again |
| 422 | needs_chmod = true; |
| 423 | } |
| 424 | if (needs_chmod) { |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 425 | if (fchmodat(AT_FDCWD, options.target.c_str(), options.mode, AT_SYMLINK_NOFOLLOW) == -1) { |
| 426 | return ErrnoError() << "fchmodat() failed on " << options.target; |
Paul Crowley | 1b4e732 | 2019-08-25 12:18:43 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
Eric Biggers | f05da4a | 2018-10-23 13:10:33 -0700 | [diff] [blame] | 429 | if (fscrypt_is_native()) { |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 430 | if (!FscryptSetDirectoryPolicy(ref_basename, options.fscrypt_action, options.target)) { |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 431 | return reboot_into_recovery( |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 432 | {"--prompt_and_wipe_data", "--reason=set_policy_failed:"s + options.target}); |
Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 435 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 438 | // mkdir <path> [mode] [owner] [group] [<option> ...] |
| 439 | static Result<void> do_mkdir(const BuiltinArguments& args) { |
| 440 | auto options = ParseMkdir(args.args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 441 | if (!options.ok()) return options.error(); |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 442 | return make_dir_with_options(*options); |
| 443 | } |
| 444 | |
Alex Light | 68ab20f | 2016-06-23 11:11:39 -0700 | [diff] [blame] | 445 | /* umount <path> */ |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 446 | static Result<void> do_umount(const BuiltinArguments& args) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 447 | if (umount(args[1].c_str()) < 0) { |
| 448 | return ErrnoError() << "umount() failed"; |
| 449 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 450 | return {}; |
Alex Light | 68ab20f | 2016-06-23 11:11:39 -0700 | [diff] [blame] | 451 | } |
| 452 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 453 | static struct { |
| 454 | const char *name; |
| 455 | unsigned flag; |
| 456 | } mount_flags[] = { |
| 457 | { "noatime", MS_NOATIME }, |
Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 458 | { "noexec", MS_NOEXEC }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | { "nosuid", MS_NOSUID }, |
| 460 | { "nodev", MS_NODEV }, |
| 461 | { "nodiratime", MS_NODIRATIME }, |
| 462 | { "ro", MS_RDONLY }, |
| 463 | { "rw", 0 }, |
| 464 | { "remount", MS_REMOUNT }, |
Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 465 | { "bind", MS_BIND }, |
| 466 | { "rec", MS_REC }, |
| 467 | { "unbindable", MS_UNBINDABLE }, |
| 468 | { "private", MS_PRIVATE }, |
| 469 | { "slave", MS_SLAVE }, |
| 470 | { "shared", MS_SHARED }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 471 | { "defaults", 0 }, |
| 472 | { 0, 0 }, |
| 473 | }; |
| 474 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 475 | #define DATA_MNT_POINT "/data" |
| 476 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 477 | /* mount <type> <device> <path> <flags ...> <options> */ |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 478 | static Result<void> do_mount(const BuiltinArguments& args) { |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 479 | const char* options = nullptr; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 480 | unsigned flags = 0; |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 481 | bool wait = false; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 483 | for (size_t na = 4; na < args.size(); na++) { |
| 484 | size_t i; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 485 | for (i = 0; mount_flags[i].name; i++) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 486 | if (!args[na].compare(mount_flags[i].name)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 487 | flags |= mount_flags[i].flag; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 492 | if (!mount_flags[i].name) { |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 493 | if (!args[na].compare("wait")) { |
| 494 | wait = true; |
| 495 | // If our last argument isn't a flag, wolf it up as an option string. |
| 496 | } else if (na + 1 == args.size()) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 497 | options = args[na].c_str(); |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 498 | } |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 499 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 502 | const char* system = args[1].c_str(); |
| 503 | const char* source = args[2].c_str(); |
| 504 | const char* target = args[3].c_str(); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 505 | |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 506 | if (android::base::StartsWith(source, "loop@")) { |
| 507 | int mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; |
| 508 | unique_fd fd(TEMP_FAILURE_RETRY(open(source + 5, mode | O_CLOEXEC))); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 509 | if (fd < 0) return ErrnoError() << "open(" << source + 5 << ", " << mode << ") failed"; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 510 | |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 511 | for (size_t n = 0;; n++) { |
| 512 | std::string tmp = android::base::StringPrintf("/dev/block/loop%zu", n); |
| 513 | unique_fd loop(TEMP_FAILURE_RETRY(open(tmp.c_str(), mode | O_CLOEXEC))); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 514 | if (loop < 0) return ErrnoError() << "open(" << tmp << ", " << mode << ") failed"; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 515 | |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 516 | loop_info info; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 517 | /* if it is a blank loop device */ |
| 518 | if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { |
| 519 | /* if it becomes our loop device */ |
Tom Cherry | 7037991 | 2017-08-01 14:10:11 -0700 | [diff] [blame] | 520 | if (ioctl(loop, LOOP_SET_FD, fd.get()) >= 0) { |
| 521 | if (mount(tmp.c_str(), target, system, flags, options) < 0) { |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 522 | ioctl(loop, LOOP_CLR_FD, 0); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 523 | return ErrnoError() << "mount() failed"; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 524 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 525 | return {}; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 530 | return Error() << "out of loopback devices"; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 531 | } else { |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 532 | if (wait) |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 533 | wait_for_file(source, kCommandRetryTimeout); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 534 | if (mount(source, target, system, flags, options) < 0) { |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 535 | return ErrnoErrorIgnoreEnoent() << "mount() failed"; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 536 | } |
| 537 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | } |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 539 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 540 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 543 | /* Imports .rc files from the specified paths. Default ones are applied if none is given. |
| 544 | * |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 545 | * rc_paths: list of paths to rc files to import |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 546 | */ |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 547 | static void import_late(const std::vector<std::string>& rc_paths) { |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 548 | auto& action_manager = ActionManager::GetInstance(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 549 | auto& service_list = ServiceList::GetInstance(); |
| 550 | Parser parser = CreateParser(action_manager, service_list); |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 551 | if (rc_paths.empty()) { |
Jaekyun Seok | 4ec72cc | 2017-02-22 20:37:57 +0900 | [diff] [blame] | 552 | // Fallbacks for partitions on which early mount isn't enabled. |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 553 | for (const auto& path : late_import_paths) { |
| 554 | parser.ParseConfig(path); |
Jaekyun Seok | 4ec72cc | 2017-02-22 20:37:57 +0900 | [diff] [blame] | 555 | } |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 556 | late_import_paths.clear(); |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 557 | } else { |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 558 | for (const auto& rc_path : rc_paths) { |
| 559 | parser.ParseConfig(rc_path); |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 560 | } |
Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 561 | } |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 562 | |
| 563 | // Turning this on and letting the INFO logging be discarded adds 0.2s to |
| 564 | // Nexus 9 boot time, so it's disabled by default. |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 565 | if (false) DumpState(); |
Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 568 | /* Queue event based on fs_mgr return code. |
| 569 | * |
| 570 | * code: return code of fs_mgr_mount_all |
| 571 | * |
| 572 | * This function might request a reboot, in which case it will |
| 573 | * not return. |
| 574 | * |
| 575 | * return code is processed based on input code |
| 576 | */ |
Nikita Ioffe | 63898aa | 2019-11-08 12:24:02 +0000 | [diff] [blame] | 577 | static Result<void> queue_fs_event(int code, bool userdata_remount) { |
Eric Biggers | e5b5e37 | 2021-11-08 16:38:54 -0800 | [diff] [blame] | 578 | if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) { |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 579 | SetProperty("ro.crypto.state", "unsupported"); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 580 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 581 | return {}; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 582 | } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 583 | /* Setup a wipe via recovery, and reboot into recovery */ |
David Anderson | 0e330f1 | 2019-01-03 18:16:56 -0800 | [diff] [blame] | 584 | if (android::gsi::IsGsiRunning()) { |
| 585 | return Error() << "cannot wipe within GSI"; |
| 586 | } |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 587 | PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery."; |
Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 588 | const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" }; |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 589 | return reboot_into_recovery(options); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 590 | /* If reboot worked, there is no return. */ |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 591 | } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) { |
Nikita Ioffe | 1f40c94 | 2019-12-04 17:47:37 +0000 | [diff] [blame] | 592 | if (!FscryptInstallKeyring()) { |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 593 | return Error() << "FscryptInstallKeyring() failed"; |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 594 | } |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 595 | SetProperty("ro.crypto.state", "encrypted"); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 596 | |
| 597 | // Although encrypted, we have device key, so we do not need to |
| 598 | // do anything different from the nonencrypted case. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 599 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 600 | return {}; |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 601 | } else if (code == FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED) { |
Nikita Ioffe | 1f40c94 | 2019-12-04 17:47:37 +0000 | [diff] [blame] | 602 | if (!FscryptInstallKeyring()) { |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 603 | return Error() << "FscryptInstallKeyring() failed"; |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 604 | } |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 605 | SetProperty("ro.crypto.state", "encrypted"); |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 606 | |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 607 | // Although encrypted, vold has already set the device up, so we do not need to |
| 608 | // do anything different from the nonencrypted case. |
| 609 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 610 | return {}; |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 611 | } else if (code == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) { |
Nikita Ioffe | 1f40c94 | 2019-12-04 17:47:37 +0000 | [diff] [blame] | 612 | if (!FscryptInstallKeyring()) { |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 613 | return Error() << "FscryptInstallKeyring() failed"; |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 614 | } |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 615 | SetProperty("ro.crypto.state", "encrypted"); |
Paul Lawrence | 9dbe97b | 2017-04-21 12:41:48 -0700 | [diff] [blame] | 616 | |
Paul Crowley | c684696 | 2018-01-30 09:56:03 -0800 | [diff] [blame] | 617 | // Although encrypted, vold has already set the device up, so we do not need to |
| 618 | // do anything different from the nonencrypted case. |
| 619 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 620 | return {}; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 621 | } else if (code > 0) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 622 | Error() << "fs_mgr_mount_all() returned unexpected error " << code; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 623 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 624 | /* else ... < 0: error */ |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 625 | |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 626 | return Error() << "Invalid code: " << code; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 629 | static int initial_mount_fstab_return_code = -1; |
| 630 | |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 631 | /* <= Q: mount_all <fstab> [ <path> ]* [--<options>]* |
| 632 | * >= R: mount_all [ <fstab> ] [--<options>]* |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 633 | * |
| 634 | * This function might request a reboot, in which case it will |
| 635 | * not return. |
| 636 | */ |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 637 | static Result<void> do_mount_all(const BuiltinArguments& args) { |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 638 | auto mount_all = ParseMountAll(args.args); |
| 639 | if (!mount_all.ok()) return mount_all.error(); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 640 | |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 641 | const char* prop_post_fix = "default"; |
| 642 | bool queue_event = true; |
| 643 | if (mount_all->mode == MOUNT_MODE_EARLY) { |
| 644 | prop_post_fix = "early"; |
| 645 | queue_event = false; |
| 646 | } else if (mount_all->mode == MOUNT_MODE_LATE) { |
| 647 | prop_post_fix = "late"; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 650 | std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 651 | android::base::Timer t; |
Tom Cherry | 990483d | 2019-04-17 12:55:50 -0700 | [diff] [blame] | 652 | |
| 653 | Fstab fstab; |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 654 | if (mount_all->fstab_path.empty()) { |
| 655 | if (!ReadDefaultFstab(&fstab)) { |
| 656 | return Error() << "Could not read default fstab"; |
| 657 | } |
| 658 | } else { |
| 659 | if (!ReadFstabFromFile(mount_all->fstab_path, &fstab)) { |
| 660 | return Error() << "Could not read fstab"; |
| 661 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 662 | } |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 663 | |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 664 | auto mount_fstab_result = fs_mgr_mount_all(&fstab, mount_all->mode); |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 665 | SetProperty(prop_name, std::to_string(t.duration().count())); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 666 | |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 667 | if (mount_all->import_rc) { |
| 668 | import_late(mount_all->rc_paths); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 671 | if (mount_fstab_result.userdata_mounted) { |
| 672 | // This call to fs_mgr_mount_all mounted userdata. Keep the result in |
| 673 | // order for userspace reboot to correctly remount userdata. |
| 674 | LOG(INFO) << "Userdata mounted using " |
| 675 | << (mount_all->fstab_path.empty() ? "(default fstab)" : mount_all->fstab_path) |
| 676 | << " result : " << mount_fstab_result.code; |
| 677 | initial_mount_fstab_return_code = mount_fstab_result.code; |
| 678 | } |
| 679 | |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 680 | if (queue_event) { |
| 681 | /* queue_fs_event will queue event based on mount_fstab return code |
| 682 | * and return processed return code*/ |
Nikita Ioffe | 9ede7ec | 2020-09-07 10:27:25 +0100 | [diff] [blame] | 683 | auto queue_fs_result = queue_fs_event(mount_fstab_result.code, false); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 684 | if (!queue_fs_result.ok()) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 685 | return Error() << "queue_fs_event() failed: " << queue_fs_result.error(); |
| 686 | } |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 689 | return {}; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 692 | /* umount_all [ <fstab> ] */ |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 693 | static Result<void> do_umount_all(const BuiltinArguments& args) { |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 694 | auto umount_all = ParseUmountAll(args.args); |
| 695 | if (!umount_all.ok()) return umount_all.error(); |
| 696 | |
Tom Cherry | 990483d | 2019-04-17 12:55:50 -0700 | [diff] [blame] | 697 | Fstab fstab; |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 698 | if (umount_all->empty()) { |
| 699 | if (!ReadDefaultFstab(&fstab)) { |
| 700 | return Error() << "Could not read default fstab"; |
| 701 | } |
| 702 | } else { |
| 703 | if (!ReadFstabFromFile(*umount_all, &fstab)) { |
| 704 | return Error() << "Could not read fstab"; |
| 705 | } |
Tom Cherry | 990483d | 2019-04-17 12:55:50 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Tom Cherry | 3707d32 | 2019-08-15 13:07:24 -0700 | [diff] [blame] | 708 | if (auto result = fs_mgr_umount_all(&fstab); result != 0) { |
| 709 | return Error() << "umount_fstab() failed " << result; |
Yifan Hong | 402633d | 2019-04-09 10:11:34 -0700 | [diff] [blame] | 710 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 711 | return {}; |
Yifan Hong | 402633d | 2019-04-09 10:11:34 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Alistair Delva | de28a86 | 2020-06-08 11:04:53 -0700 | [diff] [blame] | 714 | /* swapon_all [ <fstab> ] */ |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 715 | static Result<void> do_swapon_all(const BuiltinArguments& args) { |
Alistair Delva | de28a86 | 2020-06-08 11:04:53 -0700 | [diff] [blame] | 716 | auto swapon_all = ParseSwaponAll(args.args); |
| 717 | if (!swapon_all.ok()) return swapon_all.error(); |
| 718 | |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 719 | Fstab fstab; |
Alistair Delva | de28a86 | 2020-06-08 11:04:53 -0700 | [diff] [blame] | 720 | if (swapon_all->empty()) { |
| 721 | if (!ReadDefaultFstab(&fstab)) { |
| 722 | return Error() << "Could not read default fstab"; |
| 723 | } |
| 724 | } else { |
| 725 | if (!ReadFstabFromFile(*swapon_all, &fstab)) { |
| 726 | return Error() << "Could not read fstab '" << *swapon_all << "'"; |
| 727 | } |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 728 | } |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 729 | |
Tom Cherry | 3707d32 | 2019-08-15 13:07:24 -0700 | [diff] [blame] | 730 | if (!fs_mgr_swapon_all(fstab)) { |
| 731 | return Error() << "fs_mgr_swapon_all() failed"; |
Tom Cherry | 3055457 | 2018-11-30 15:21:04 -0800 | [diff] [blame] | 732 | } |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 733 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 734 | return {}; |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 737 | static Result<void> do_setprop(const BuiltinArguments& args) { |
Tom Cherry | 1ab3dfc | 2019-04-22 17:46:37 -0700 | [diff] [blame] | 738 | if (StartsWith(args[1], "ctl.")) { |
| 739 | return Error() |
| 740 | << "Cannot set ctl. properties from init; call the Service functions directly"; |
| 741 | } |
| 742 | if (args[1] == kRestoreconProperty) { |
| 743 | return Error() << "Cannot set '" << kRestoreconProperty |
| 744 | << "' from init; use the restorecon builtin directly"; |
| 745 | } |
| 746 | |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 747 | SetProperty(args[1], args[2]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 748 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 751 | static Result<void> do_setrlimit(const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 752 | auto rlimit = ParseRlimit(args.args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 753 | if (!rlimit.ok()) return rlimit.error(); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 754 | |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 755 | if (setrlimit(rlimit->first, &rlimit->second) == -1) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 756 | return ErrnoError() << "setrlimit failed"; |
| 757 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 758 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 761 | static Result<void> do_start(const BuiltinArguments& args) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 762 | Service* svc = ServiceList::GetInstance().FindService(args[1]); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 763 | if (!svc) return Error() << "service " << args[1] << " not found"; |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 764 | if (auto result = svc->Start(); !result.ok()) { |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 765 | return ErrorIgnoreEnoent() << "Could not start service: " << result.error(); |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 766 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 767 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 770 | static Result<void> do_stop(const BuiltinArguments& args) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 771 | Service* svc = ServiceList::GetInstance().FindService(args[1]); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 772 | if (!svc) return Error() << "service " << args[1] << " not found"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 773 | svc->Stop(); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 774 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 777 | static Result<void> do_restart(const BuiltinArguments& args) { |
David Anderson | 6d7c7a2 | 2021-12-03 15:48:16 -0800 | [diff] [blame] | 778 | bool only_if_running = false; |
| 779 | if (args.size() == 3) { |
| 780 | if (args[1] == "--only-if-running") { |
| 781 | only_if_running = true; |
| 782 | } else { |
| 783 | return Error() << "Unknown argument to restart: " << args[1]; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | const auto& classname = args[args.size() - 1]; |
| 788 | Service* svc = ServiceList::GetInstance().FindService(classname); |
| 789 | if (!svc) return Error() << "service " << classname << " not found"; |
| 790 | if (only_if_running && !svc->IsRunning()) { |
| 791 | return {}; |
| 792 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 793 | svc->Restart(); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 794 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 797 | static Result<void> do_trigger(const BuiltinArguments& args) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 798 | ActionManager::GetInstance().QueueEventTrigger(args[1]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 799 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Joel Galenson | 4b591f1 | 2017-11-27 14:45:26 -0800 | [diff] [blame] | 802 | static int MakeSymlink(const std::string& target, const std::string& linkpath) { |
| 803 | std::string secontext; |
| 804 | // Passing 0 for mode should work. |
| 805 | if (SelabelLookupFileContext(linkpath, 0, &secontext) && !secontext.empty()) { |
| 806 | setfscreatecon(secontext.c_str()); |
| 807 | } |
| 808 | |
| 809 | int rc = symlink(target.c_str(), linkpath.c_str()); |
| 810 | |
| 811 | if (!secontext.empty()) { |
| 812 | int save_errno = errno; |
| 813 | setfscreatecon(nullptr); |
| 814 | errno = save_errno; |
| 815 | } |
| 816 | |
| 817 | return rc; |
| 818 | } |
| 819 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 820 | static Result<void> do_symlink(const BuiltinArguments& args) { |
Joel Galenson | 4b591f1 | 2017-11-27 14:45:26 -0800 | [diff] [blame] | 821 | if (MakeSymlink(args[1], args[2]) < 0) { |
Tom Cherry | 68f2a46 | 2017-08-22 16:15:26 -0700 | [diff] [blame] | 822 | // The symlink builtin is often used to create symlinks for older devices to be backwards |
| 823 | // compatible with new paths, therefore we skip reporting this error. |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 824 | return ErrnoErrorIgnoreEnoent() << "symlink() failed"; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 825 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 826 | return {}; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 827 | } |
| 828 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 829 | static Result<void> do_rm(const BuiltinArguments& args) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 830 | if (unlink(args[1].c_str()) < 0) { |
| 831 | return ErrnoError() << "unlink() failed"; |
| 832 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 833 | return {}; |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 836 | static Result<void> do_rmdir(const BuiltinArguments& args) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 837 | if (rmdir(args[1].c_str()) < 0) { |
| 838 | return ErrnoError() << "rmdir() failed"; |
| 839 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 840 | return {}; |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 843 | static Result<void> do_sysclktz(const BuiltinArguments& args) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 844 | struct timezone tz = {}; |
| 845 | if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) { |
| 846 | return Error() << "Unable to parse mins_west_of_gmt"; |
| 847 | } |
| 848 | |
| 849 | if (settimeofday(nullptr, &tz) == -1) { |
| 850 | return ErrnoError() << "settimeofday() failed"; |
| 851 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 852 | return {}; |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 855 | static Result<void> do_verity_update_state(const BuiltinArguments& args) { |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 856 | int mode; |
| 857 | if (!fs_mgr_load_verity_state(&mode)) { |
| 858 | return Error() << "fs_mgr_load_verity_state() failed"; |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 859 | } |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 860 | |
| 861 | Fstab fstab; |
| 862 | if (!ReadDefaultFstab(&fstab)) { |
| 863 | return Error() << "Failed to read default fstab"; |
| 864 | } |
| 865 | |
| 866 | for (const auto& entry : fstab) { |
| 867 | if (!fs_mgr_is_verity_enabled(entry)) { |
| 868 | continue; |
| 869 | } |
| 870 | |
| 871 | // To be consistent in vboot 1.0 and vboot 2.0 (AVB), use "system" for the partition even |
| 872 | // for system as root, so it has property [partition.system.verified]. |
| 873 | std::string partition = entry.mount_point == "/" ? "system" : Basename(entry.mount_point); |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 874 | SetProperty("partition." + partition + ".verified", std::to_string(mode)); |
Tianjie | 327237d | 2021-01-20 19:02:34 -0800 | [diff] [blame] | 875 | |
Tianjie | 10bec65 | 2021-08-30 16:10:09 -0700 | [diff] [blame] | 876 | auto hashtree_info = fs_mgr_get_hashtree_info(entry); |
| 877 | if (hashtree_info) { |
| 878 | SetProperty("partition." + partition + ".verified.hash_alg", hashtree_info->algorithm); |
| 879 | SetProperty("partition." + partition + ".verified.root_digest", |
| 880 | hashtree_info->root_digest); |
Tianjie | 327237d | 2021-01-20 19:02:34 -0800 | [diff] [blame] | 881 | } |
Tom Cherry | cf80b6d | 2019-01-07 14:25:31 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 884 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 887 | static Result<void> do_write(const BuiltinArguments& args) { |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 888 | if (auto result = WriteFile(args[1], args[2]); !result.ok()) { |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 889 | return ErrorIgnoreEnoent() |
| 890 | << "Unable to write to file '" << args[1] << "': " << result.error(); |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 893 | return {}; |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 896 | static Result<void> readahead_file(const std::string& filename, bool fully) { |
Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 897 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_CLOEXEC))); |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 898 | if (fd == -1) { |
| 899 | return ErrnoError() << "Error opening file"; |
| 900 | } |
| 901 | if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED)) { |
| 902 | return ErrnoError() << "Error posix_fadvise file"; |
| 903 | } |
| 904 | if (readahead(fd, 0, std::numeric_limits<size_t>::max())) { |
| 905 | return ErrnoError() << "Error readahead file"; |
| 906 | } |
| 907 | if (fully) { |
| 908 | char buf[BUFSIZ]; |
| 909 | ssize_t n; |
| 910 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 911 | } |
| 912 | if (n != 0) { |
| 913 | return ErrnoError() << "Error reading file"; |
| 914 | } |
| 915 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 916 | return {}; |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 919 | static Result<void> do_readahead(const BuiltinArguments& args) { |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 920 | struct stat sb; |
| 921 | |
| 922 | if (stat(args[1].c_str(), &sb)) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 923 | return ErrnoError() << "Error opening " << args[1]; |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 926 | bool readfully = false; |
| 927 | if (args.size() == 3 && args[2] == "--fully") { |
| 928 | readfully = true; |
| 929 | } |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 930 | // We will do readahead in a forked process in order not to block init |
| 931 | // since it may block while it reads the |
| 932 | // filesystem metadata needed to locate the requested blocks. This |
| 933 | // occurs frequently with ext[234] on large files using indirect blocks |
| 934 | // instead of extents, giving the appearance that the call blocks until |
| 935 | // the requested data has been read. |
| 936 | pid_t pid = fork(); |
| 937 | if (pid == 0) { |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 938 | if (setpriority(PRIO_PROCESS, 0, static_cast<int>(ANDROID_PRIORITY_LOWEST)) != 0) { |
| 939 | PLOG(WARNING) << "setpriority failed"; |
| 940 | } |
| 941 | if (android_set_ioprio(0, IoSchedClass_IDLE, 7)) { |
| 942 | PLOG(WARNING) << "ioprio_get failed"; |
| 943 | } |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 944 | android::base::Timer t; |
| 945 | if (S_ISREG(sb.st_mode)) { |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 946 | if (auto result = readahead_file(args[1], readfully); !result.ok()) { |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 947 | LOG(WARNING) << "Unable to readahead '" << args[1] << "': " << result.error(); |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 948 | _exit(EXIT_FAILURE); |
| 949 | } |
| 950 | } else if (S_ISDIR(sb.st_mode)) { |
| 951 | char* paths[] = {const_cast<char*>(args[1].data()), nullptr}; |
| 952 | std::unique_ptr<FTS, decltype(&fts_close)> fts( |
| 953 | fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr), fts_close); |
| 954 | if (!fts) { |
| 955 | PLOG(ERROR) << "Error opening directory: " << args[1]; |
| 956 | _exit(EXIT_FAILURE); |
| 957 | } |
| 958 | // Traverse the entire hierarchy and do readahead |
| 959 | for (FTSENT* ftsent = fts_read(fts.get()); ftsent != nullptr; |
| 960 | ftsent = fts_read(fts.get())) { |
| 961 | if (ftsent->fts_info & FTS_F) { |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 962 | const std::string filename = ftsent->fts_accpath; |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 963 | if (auto result = readahead_file(filename, readfully); !result.ok()) { |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 964 | LOG(WARNING) |
| 965 | << "Unable to readahead '" << filename << "': " << result.error(); |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | } |
| 969 | } |
Wei Wang | 02628f3 | 2017-08-16 11:34:50 -0700 | [diff] [blame] | 970 | LOG(INFO) << "Readahead " << args[1] << " took " << t << " asynchronously"; |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 971 | _exit(0); |
| 972 | } else if (pid < 0) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 973 | return ErrnoError() << "Fork failed"; |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 974 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 975 | return {}; |
Wei Wang | 542aae4 | 2017-08-04 13:22:36 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 978 | static Result<void> do_copy(const BuiltinArguments& args) { |
Tom Cherry | 62ca663 | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 979 | auto file_contents = ReadFile(args[1]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 980 | if (!file_contents.ok()) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 981 | return Error() << "Could not read input file '" << args[1] << "': " << file_contents.error(); |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 982 | } |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 983 | if (auto result = WriteFile(args[2], *file_contents); !result.ok()) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 984 | return Error() << "Could not write to output file '" << args[2] << "': " << result.error(); |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 985 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 986 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 987 | return {}; |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Wei Wang | 49d2598 | 2020-11-18 15:56:14 -0800 | [diff] [blame] | 990 | static Result<void> do_copy_per_line(const BuiltinArguments& args) { |
| 991 | std::string file_contents; |
| 992 | if (!android::base::ReadFileToString(args[1], &file_contents, true)) { |
| 993 | return Error() << "Could not read input file '" << args[1] << "'"; |
| 994 | } |
| 995 | auto lines = Split(file_contents, "\n"); |
| 996 | for (const auto& line : lines) { |
| 997 | auto result = WriteFile(args[2], line); |
| 998 | if (!result.ok()) { |
| 999 | LOG(VERBOSE) << "Could not write to output file '" << args[2] << "' with '" << line |
| 1000 | << "' : " << result.error(); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | return {}; |
| 1005 | } |
| 1006 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1007 | static Result<void> do_chown(const BuiltinArguments& args) { |
Tom Cherry | 62ca663 | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1008 | auto uid = DecodeUid(args[1]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1009 | if (!uid.ok()) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1010 | return Error() << "Unable to decode UID for '" << args[1] << "': " << uid.error(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1011 | } |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1012 | |
| 1013 | // GID is optional and pushes the index of path out by one if specified. |
| 1014 | const std::string& path = (args.size() == 4) ? args[3] : args[2]; |
Tom Cherry | 62ca663 | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1015 | Result<gid_t> gid = -1; |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1016 | |
| 1017 | if (args.size() == 4) { |
Tom Cherry | 62ca663 | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1018 | gid = DecodeUid(args[2]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1019 | if (!gid.ok()) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1020 | return Error() << "Unable to decode GID for '" << args[2] << "': " << gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1024 | if (lchown(path.c_str(), *uid, *gid) == -1) { |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 1025 | return ErrnoErrorIgnoreEnoent() << "lchown() failed"; |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1026 | } |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1027 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1028 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | static mode_t get_mode(const char *s) { |
| 1032 | mode_t mode = 0; |
| 1033 | while (*s) { |
| 1034 | if (*s >= '0' && *s <= '7') { |
| 1035 | mode = (mode<<3) | (*s-'0'); |
| 1036 | } else { |
| 1037 | return -1; |
| 1038 | } |
| 1039 | s++; |
| 1040 | } |
| 1041 | return mode; |
| 1042 | } |
| 1043 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1044 | static Result<void> do_chmod(const BuiltinArguments& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 1045 | mode_t mode = get_mode(args[1].c_str()); |
| 1046 | if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) { |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 1047 | return ErrnoErrorIgnoreEnoent() << "fchmodat() failed"; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1048 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1049 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1052 | static Result<void> do_restorecon(const BuiltinArguments& args) { |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 1053 | auto restorecon_info = ParseRestorecon(args.args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1054 | if (!restorecon_info.ok()) { |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 1055 | return restorecon_info.error(); |
| 1056 | } |
| 1057 | |
| 1058 | const auto& [flag, paths] = *restorecon_info; |
| 1059 | |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 1060 | int ret = 0; |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 1061 | for (const auto& path : paths) { |
| 1062 | if (selinux_android_restorecon(path.c_str(), flag) < 0) { |
| 1063 | ret = errno; |
Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 1064 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 1065 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1066 | |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 1067 | if (ret) return ErrnoErrorIgnoreEnoent() << "selinux_android_restorecon() failed"; |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1068 | return {}; |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1071 | static Result<void> do_restorecon_recursive(const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1072 | std::vector<std::string> non_const_args(args.args); |
Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 1073 | non_const_args.insert(std::next(non_const_args.begin()), "--recursive"); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1074 | return do_restorecon({std::move(non_const_args), args.context}); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 1075 | } |
| 1076 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1077 | static Result<void> do_loglevel(const BuiltinArguments& args) { |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 1078 | // TODO: support names instead/as well? |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 1079 | int log_level = -1; |
| 1080 | android::base::ParseInt(args[1], &log_level); |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 1081 | android::base::LogSeverity severity; |
| 1082 | switch (log_level) { |
| 1083 | case 7: severity = android::base::DEBUG; break; |
| 1084 | case 6: severity = android::base::INFO; break; |
| 1085 | case 5: |
| 1086 | case 4: severity = android::base::WARNING; break; |
| 1087 | case 3: severity = android::base::ERROR; break; |
| 1088 | case 2: |
| 1089 | case 1: |
| 1090 | case 0: severity = android::base::FATAL; break; |
| 1091 | default: |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1092 | return Error() << "invalid log level " << log_level; |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 1093 | } |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 1094 | android::base::SetMinimumLogSeverity(severity); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1095 | return {}; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1098 | static Result<void> do_load_persist_props(const BuiltinArguments& args) { |
Tom Cherry | 1ab3dfc | 2019-04-22 17:46:37 -0700 | [diff] [blame] | 1099 | SendLoadPersistentPropertiesMessage(); |
| 1100 | |
| 1101 | start_waiting_for_property("ro.persistent_properties.ready", "true"); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1102 | return {}; |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1105 | static Result<void> do_load_system_props(const BuiltinArguments& args) { |
Jiyong Park | 3b316ee | 2019-01-13 02:42:31 +0900 | [diff] [blame] | 1106 | LOG(INFO) << "deprecated action `load_system_props` called."; |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1107 | return {}; |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1110 | static Result<void> do_wait(const BuiltinArguments& args) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1111 | auto timeout = kCommandRetryTimeout; |
| 1112 | if (args.size() == 3) { |
Mark Salyzyn | ffa52e9 | 2020-05-14 09:20:30 -0700 | [diff] [blame] | 1113 | double timeout_double; |
| 1114 | if (!android::base::ParseDouble(args[2], &timeout_double, 0)) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1115 | return Error() << "failed to parse timeout"; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 1116 | } |
Mark Salyzyn | ffa52e9 | 2020-05-14 09:20:30 -0700 | [diff] [blame] | 1117 | timeout = std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 1118 | std::chrono::duration<double>(timeout_double)); |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 1119 | } |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1120 | |
| 1121 | if (wait_for_file(args[1].c_str(), timeout) != 0) { |
| 1122 | return Error() << "wait_for_file() failed"; |
| 1123 | } |
| 1124 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1125 | return {}; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 1126 | } |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1127 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1128 | static Result<void> do_wait_for_prop(const BuiltinArguments& args) { |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 1129 | const char* name = args[1].c_str(); |
| 1130 | const char* value = args[2].c_str(); |
| 1131 | size_t value_len = strlen(value); |
| 1132 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 1133 | if (!IsLegalPropertyName(name)) { |
| 1134 | return Error() << "IsLegalPropertyName(" << name << ") failed"; |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 1135 | } |
| 1136 | if (value_len >= PROP_VALUE_MAX) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1137 | return Error() << "value too long"; |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 1138 | } |
Wei Wang | 2d0fdaa | 2017-02-02 10:52:39 -0800 | [diff] [blame] | 1139 | if (!start_waiting_for_property(name, value)) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1140 | return Error() << "already waiting for a property"; |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 1141 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1142 | return {}; |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 1145 | static bool is_file_crypto() { |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 1146 | return android::base::GetProperty("ro.crypto.type", "") == "file"; |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1149 | static Result<void> ExecWithFunctionOnFailure(const std::vector<std::string>& args, |
| 1150 | std::function<void(const std::string&)> function) { |
| 1151 | auto service = Service::MakeTemporaryOneshotService(args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1152 | if (!service.ok()) { |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1153 | function("MakeTemporaryOneshotService failed: " + service.error().message()); |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 1154 | } |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1155 | (*service)->AddReapCallback([function](const siginfo_t& siginfo) { |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 1156 | if (siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) { |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1157 | function(StringPrintf("Exec service failed, status %d", siginfo.si_status)); |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 1158 | } |
| 1159 | }); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1160 | if (auto result = (*service)->ExecStart(); !result.ok()) { |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1161 | function("ExecStart failed: " + result.error().message()); |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 1162 | } |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 1163 | ServiceList::GetInstance().AddService(std::move(*service)); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1164 | return {}; |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1167 | static Result<void> ExecVdcRebootOnFailure(const std::string& vdc_arg) { |
Nikita Ioffe | 05506f0 | 2020-01-15 14:43:55 +0000 | [diff] [blame] | 1168 | bool should_reboot_into_recovery = true; |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1169 | auto reboot_reason = vdc_arg + "_failed"; |
Nikita Ioffe | 05506f0 | 2020-01-15 14:43:55 +0000 | [diff] [blame] | 1170 | if (android::sysprop::InitProperties::userspace_reboot_in_progress().value_or(false)) { |
| 1171 | should_reboot_into_recovery = false; |
Nikita Ioffe | 4a787d9 | 2020-01-15 23:23:13 +0000 | [diff] [blame] | 1172 | reboot_reason = "userspace_failed," + vdc_arg; |
Nikita Ioffe | 05506f0 | 2020-01-15 14:43:55 +0000 | [diff] [blame] | 1173 | } |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1174 | |
Nikita Ioffe | 05506f0 | 2020-01-15 14:43:55 +0000 | [diff] [blame] | 1175 | auto reboot = [reboot_reason, should_reboot_into_recovery](const std::string& message) { |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1176 | // TODO (b/122850122): support this in gsi |
Nikita Ioffe | 05506f0 | 2020-01-15 14:43:55 +0000 | [diff] [blame] | 1177 | if (should_reboot_into_recovery) { |
| 1178 | if (fscrypt_is_native() && !android::gsi::IsGsiRunning()) { |
| 1179 | LOG(ERROR) << message << ": Rebooting into recovery, reason: " << reboot_reason; |
| 1180 | if (auto result = reboot_into_recovery( |
| 1181 | {"--prompt_and_wipe_data", "--reason="s + reboot_reason}); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1182 | !result.ok()) { |
Nikita Ioffe | 05506f0 | 2020-01-15 14:43:55 +0000 | [diff] [blame] | 1183 | LOG(FATAL) << "Could not reboot into recovery: " << result.error(); |
| 1184 | } |
| 1185 | } else { |
| 1186 | LOG(ERROR) << "Failure (reboot suppressed): " << reboot_reason; |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1187 | } |
| 1188 | } else { |
Nikita Ioffe | 05506f0 | 2020-01-15 14:43:55 +0000 | [diff] [blame] | 1189 | LOG(ERROR) << message << ": rebooting, reason: " << reboot_reason; |
| 1190 | trigger_shutdown("reboot," + reboot_reason); |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1191 | } |
| 1192 | }; |
| 1193 | |
| 1194 | std::vector<std::string> args = {"exec", "/system/bin/vdc", "--wait", "cryptfs", vdc_arg}; |
| 1195 | return ExecWithFunctionOnFailure(args, reboot); |
| 1196 | } |
| 1197 | |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1198 | static Result<void> do_remount_userdata(const BuiltinArguments& args) { |
| 1199 | if (initial_mount_fstab_return_code == -1) { |
| 1200 | return Error() << "Calling remount_userdata too early"; |
| 1201 | } |
| 1202 | Fstab fstab; |
| 1203 | if (!ReadDefaultFstab(&fstab)) { |
| 1204 | // TODO(b/135984674): should we reboot here? |
| 1205 | return Error() << "Failed to read fstab"; |
| 1206 | } |
| 1207 | // TODO(b/135984674): check that fstab contains /data. |
| 1208 | if (auto rc = fs_mgr_remount_userdata_into_checkpointing(&fstab); rc < 0) { |
Gavin Corkery | 8c92256 | 2020-05-11 14:10:09 +0100 | [diff] [blame] | 1209 | std::string proc_mounts_output; |
| 1210 | android::base::ReadFileToString("/proc/mounts", &proc_mounts_output, true); |
| 1211 | android::base::WriteStringToFile(proc_mounts_output, |
| 1212 | "/metadata/userspacereboot/mount_info.txt"); |
Nikita Ioffe | 4a787d9 | 2020-01-15 23:23:13 +0000 | [diff] [blame] | 1213 | trigger_shutdown("reboot,mount_userdata_failed"); |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1214 | } |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1215 | if (auto result = queue_fs_event(initial_mount_fstab_return_code, true); !result.ok()) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1216 | return Error() << "queue_fs_event() failed: " << result.error(); |
| 1217 | } |
| 1218 | return {}; |
| 1219 | } |
| 1220 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1221 | static Result<void> do_installkey(const BuiltinArguments& args) { |
| 1222 | if (!is_file_crypto()) return {}; |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1223 | |
Eric Biggers | f05da4a | 2018-10-23 13:10:33 -0700 | [diff] [blame] | 1224 | auto unencrypted_dir = args[1] + fscrypt_unencrypted_folder; |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 1225 | if (!make_dir(unencrypted_dir, 0700) && errno != EEXIST) { |
Tom Cherry | 7fa62c5 | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 1226 | return ErrnoError() << "Failed to create " << unencrypted_dir; |
Janis Danisevskis | 9cc5172 | 2017-03-29 14:50:01 -0700 | [diff] [blame] | 1227 | } |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1228 | return ExecVdcRebootOnFailure("enablefilecrypto"); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 1229 | } |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 1230 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1231 | static Result<void> do_init_user0(const BuiltinArguments& args) { |
Tom Cherry | 7896e7a | 2019-09-04 15:26:52 -0700 | [diff] [blame] | 1232 | return ExecVdcRebootOnFailure("init_user0"); |
Paul Crowley | 5949745 | 2016-02-01 16:37:13 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1235 | static Result<void> do_mark_post_data(const BuiltinArguments& args) { |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 1236 | ServiceList::GetInstance().MarkPostData(); |
| 1237 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1238 | return {}; |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1241 | static Result<void> GenerateLinkerConfiguration() { |
Kiyoung Kim | 03b9bca | 2020-11-30 14:49:36 +0900 | [diff] [blame] | 1242 | const char* linkerconfig_binary = "/apex/com.android.runtime/bin/linkerconfig"; |
Kiyoung Kim | 3b2dbe9 | 2019-12-30 18:44:41 +0900 | [diff] [blame] | 1243 | const char* linkerconfig_target = "/linkerconfig"; |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1244 | const char* arguments[] = {linkerconfig_binary, "--target", linkerconfig_target}; |
| 1245 | |
| 1246 | if (logwrap_fork_execvp(arraysize(arguments), arguments, nullptr, false, LOG_KLOG, false, |
| 1247 | nullptr) != 0) { |
| 1248 | return ErrnoError() << "failed to execute linkerconfig"; |
| 1249 | } |
| 1250 | |
Kiyoung Kim | 0cbee0d | 2021-03-02 16:45:27 +0900 | [diff] [blame] | 1251 | auto current_mount_ns = GetCurrentMountNamespace(); |
| 1252 | if (!current_mount_ns.ok()) { |
| 1253 | return current_mount_ns.error(); |
| 1254 | } |
| 1255 | if (*current_mount_ns == NS_DEFAULT) { |
| 1256 | SetDefaultMountNamespaceReady(); |
| 1257 | } |
| 1258 | |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1259 | LOG(INFO) << "linkerconfig generated " << linkerconfig_target |
| 1260 | << " with mounted APEX modules info"; |
| 1261 | |
| 1262 | return {}; |
| 1263 | } |
| 1264 | |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 1265 | static Result<void> MountLinkerConfigForDefaultNamespace() { |
| 1266 | // No need to mount linkerconfig for default mount namespace if the path does not exist (which |
| 1267 | // would mean it is already mounted) |
| 1268 | if (access("/linkerconfig/default", 0) != 0) { |
| 1269 | return {}; |
| 1270 | } |
| 1271 | |
| 1272 | if (mount("/linkerconfig/default", "/linkerconfig", nullptr, MS_BIND | MS_REC, nullptr) != 0) { |
| 1273 | return ErrnoError() << "Failed to mount linker configuration for default mount namespace."; |
| 1274 | } |
| 1275 | |
| 1276 | return {}; |
| 1277 | } |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1278 | static Result<void> do_update_linker_config(const BuiltinArguments&) { |
Shikha Malhotra | 720694d | 2021-07-30 12:35:27 +0000 | [diff] [blame] | 1279 | return GenerateLinkerConfiguration(); |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1280 | } |
| 1281 | |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 1282 | static Result<void> parse_apex_configs() { |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1283 | glob_t glob_result; |
Ray Essick | 35ffd69 | 2021-10-07 15:48:11 -0700 | [diff] [blame] | 1284 | static constexpr char glob_pattern[] = "/apex/*/etc/*rc"; |
Jiyong Park | f169f72 | 2018-11-16 16:23:20 +0900 | [diff] [blame] | 1285 | const int ret = glob(glob_pattern, GLOB_MARK, nullptr, &glob_result); |
| 1286 | if (ret != 0 && ret != GLOB_NOMATCH) { |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1287 | globfree(&glob_result); |
| 1288 | return Error() << "glob pattern '" << glob_pattern << "' failed"; |
| 1289 | } |
| 1290 | std::vector<std::string> configs; |
Nikita Ioffe | 091c4d1 | 2019-12-05 12:35:19 +0000 | [diff] [blame] | 1291 | Parser parser = CreateServiceOnlyParser(ServiceList::GetInstance(), true); |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1292 | for (size_t i = 0; i < glob_result.gl_pathc; i++) { |
Jiyong Park | d2217b5 | 2019-06-07 19:18:46 +0900 | [diff] [blame] | 1293 | std::string path = glob_result.gl_pathv[i]; |
| 1294 | // Filter-out /apex/<name>@<ver> paths. The paths are bind-mounted to |
| 1295 | // /apex/<name> paths, so unless we filter them out, we will parse the |
| 1296 | // same file twice. |
| 1297 | std::vector<std::string> paths = android::base::Split(path, "/"); |
Jooyung Han | 9c4fa66 | 2019-07-05 11:22:18 +0900 | [diff] [blame] | 1298 | if (paths.size() >= 3 && paths[2].find('@') != std::string::npos) { |
Jiyong Park | d2217b5 | 2019-06-07 19:18:46 +0900 | [diff] [blame] | 1299 | continue; |
| 1300 | } |
Ray Essick | 35ffd69 | 2021-10-07 15:48:11 -0700 | [diff] [blame] | 1301 | // Filter directories |
| 1302 | if (path.back() == '/') { |
| 1303 | continue; |
| 1304 | } |
Jiyong Park | d2217b5 | 2019-06-07 19:18:46 +0900 | [diff] [blame] | 1305 | configs.push_back(path); |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1306 | } |
| 1307 | globfree(&glob_result); |
| 1308 | |
Ray Essick | 35ffd69 | 2021-10-07 15:48:11 -0700 | [diff] [blame] | 1309 | // Compare all files /apex/path.#rc and /apex/path.rc with the same "/apex/path" prefix, |
| 1310 | // choosing the one with the highest # that doesn't exceed the system's SDK. |
| 1311 | // (.rc == .0rc for ranking purposes) |
| 1312 | // |
| 1313 | int active_sdk = android::base::GetIntProperty("ro.build.version.sdk", INT_MAX); |
| 1314 | |
| 1315 | std::map<std::string, std::pair<std::string, int>> script_map; |
| 1316 | |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1317 | for (const auto& c : configs) { |
Ray Essick | 35ffd69 | 2021-10-07 15:48:11 -0700 | [diff] [blame] | 1318 | int sdk = 0; |
| 1319 | const std::vector<std::string> parts = android::base::Split(c, "."); |
| 1320 | std::string base; |
| 1321 | if (parts.size() < 2) { |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1322 | continue; |
| 1323 | } |
Ray Essick | 35ffd69 | 2021-10-07 15:48:11 -0700 | [diff] [blame] | 1324 | |
| 1325 | // parts[size()-1], aka the suffix, should be "rc" or "#rc" |
| 1326 | // any other pattern gets discarded |
| 1327 | |
| 1328 | const auto& suffix = parts[parts.size() - 1]; |
| 1329 | if (suffix == "rc") { |
| 1330 | sdk = 0; |
| 1331 | } else { |
| 1332 | char trailer[9] = {0}; |
| 1333 | int r = sscanf(suffix.c_str(), "%d%8s", &sdk, trailer); |
| 1334 | if (r != 2) { |
| 1335 | continue; |
| 1336 | } |
| 1337 | if (strlen(trailer) > 2 || strcmp(trailer, "rc") != 0) { |
| 1338 | continue; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | if (sdk < 0 || sdk > active_sdk) { |
| 1343 | continue; |
| 1344 | } |
| 1345 | |
| 1346 | base = parts[0]; |
| 1347 | for (unsigned int i = 1; i < parts.size() - 1; i++) { |
| 1348 | base = base + "." + parts[i]; |
| 1349 | } |
| 1350 | |
| 1351 | // is this preferred over what we already have |
| 1352 | auto it = script_map.find(base); |
| 1353 | if (it == script_map.end() || it->second.second < sdk) { |
| 1354 | script_map[base] = std::make_pair(c, sdk); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | bool success = true; |
| 1359 | for (const auto& m : script_map) { |
| 1360 | success &= parser.ParseConfigFile(m.second.first); |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1361 | } |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 1362 | ServiceList::GetInstance().MarkServicesUpdate(); |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1363 | if (success) { |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1364 | return {}; |
Jiyong Park | c240440 | 2018-11-08 17:14:35 +0900 | [diff] [blame] | 1365 | } else { |
| 1366 | return Error() << "Could not parse apex configs"; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 1370 | /* |
| 1371 | * Creates a directory under /data/misc/apexdata/ for each APEX. |
| 1372 | */ |
| 1373 | static Result<void> create_apex_data_dirs() { |
| 1374 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/apex"), closedir); |
| 1375 | if (!dirp) { |
| 1376 | return ErrnoError() << "Unable to open apex directory"; |
| 1377 | } |
| 1378 | struct dirent* entry; |
| 1379 | while ((entry = readdir(dirp.get())) != nullptr) { |
| 1380 | if (entry->d_type != DT_DIR) continue; |
| 1381 | |
| 1382 | const char* name = entry->d_name; |
| 1383 | // skip any starting with "." |
| 1384 | if (name[0] == '.') continue; |
| 1385 | |
| 1386 | if (strchr(name, '@') != nullptr) continue; |
| 1387 | |
| 1388 | auto path = "/data/misc/apexdata/" + std::string(name); |
Oli Lan | 90c523b | 2020-01-17 11:41:04 +0000 | [diff] [blame] | 1389 | auto options = MkdirOptions{path, 0771, AID_ROOT, AID_SYSTEM, FscryptAction::kNone, "ref"}; |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 1390 | make_dir_with_options(options); |
| 1391 | } |
| 1392 | return {}; |
| 1393 | } |
| 1394 | |
| 1395 | static Result<void> do_perform_apex_config(const BuiltinArguments& args) { |
| 1396 | auto create_dirs = create_apex_data_dirs(); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1397 | if (!create_dirs.ok()) { |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 1398 | return create_dirs.error(); |
| 1399 | } |
| 1400 | auto parse_configs = parse_apex_configs(); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1401 | if (!parse_configs.ok()) { |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 1402 | return parse_configs.error(); |
| 1403 | } |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1404 | |
| 1405 | auto update_linker_config = do_update_linker_config(args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 1406 | if (!update_linker_config.ok()) { |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1407 | return update_linker_config.error(); |
| 1408 | } |
| 1409 | |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 1410 | return {}; |
| 1411 | } |
| 1412 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 1413 | static Result<void> do_enter_default_mount_ns(const BuiltinArguments& args) { |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 1414 | if (auto result = SwitchToMountNamespaceIfNeeded(NS_DEFAULT); !result.ok()) { |
| 1415 | return result.error(); |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 1416 | } |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 1417 | if (auto result = MountLinkerConfigForDefaultNamespace(); !result.ok()) { |
| 1418 | return result.error(); |
| 1419 | } |
| 1420 | LOG(INFO) << "Switched to default mount namespace"; |
| 1421 | return {}; |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 1422 | } |
| 1423 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 1424 | // Builtin-function-map start |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 1425 | const BuiltinFunctionMap& GetBuiltinFunctionMap() { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1426 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 1427 | // clang-format off |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 1428 | static const BuiltinFunctionMap builtin_functions = { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1429 | {"bootchart", {1, 1, {false, do_bootchart}}}, |
| 1430 | {"chmod", {2, 2, {true, do_chmod}}}, |
| 1431 | {"chown", {2, 3, {true, do_chown}}}, |
| 1432 | {"class_reset", {1, 1, {false, do_class_reset}}}, |
David Anderson | 2285b52 | 2021-11-09 18:26:39 -0800 | [diff] [blame] | 1433 | {"class_restart", {1, 2, {false, do_class_restart}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1434 | {"class_start", {1, 1, {false, do_class_start}}}, |
| 1435 | {"class_stop", {1, 1, {false, do_class_stop}}}, |
| 1436 | {"copy", {2, 2, {true, do_copy}}}, |
Wei Wang | 49d2598 | 2020-11-18 15:56:14 -0800 | [diff] [blame] | 1437 | {"copy_per_line", {2, 2, {true, do_copy_per_line}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1438 | {"domainname", {1, 1, {true, do_domainname}}}, |
| 1439 | {"enable", {1, 1, {false, do_enable}}}, |
| 1440 | {"exec", {1, kMax, {false, do_exec}}}, |
| 1441 | {"exec_background", {1, kMax, {false, do_exec_background}}}, |
| 1442 | {"exec_start", {1, 1, {false, do_exec_start}}}, |
| 1443 | {"export", {2, 2, {false, do_export}}}, |
| 1444 | {"hostname", {1, 1, {true, do_hostname}}}, |
| 1445 | {"ifup", {1, 1, {true, do_ifup}}}, |
| 1446 | {"init_user0", {0, 0, {false, do_init_user0}}}, |
| 1447 | {"insmod", {1, kMax, {true, do_insmod}}}, |
| 1448 | {"installkey", {1, 1, {false, do_installkey}}}, |
Steven Moreland | 612d7a4 | 2018-05-08 11:21:37 -0700 | [diff] [blame] | 1449 | {"interface_restart", {1, 1, {false, do_interface_restart}}}, |
| 1450 | {"interface_start", {1, 1, {false, do_interface_start}}}, |
| 1451 | {"interface_stop", {1, 1, {false, do_interface_stop}}}, |
Artur Satayev | 500946b | 2021-02-15 20:48:49 +0000 | [diff] [blame] | 1452 | {"load_exports", {1, 1, {false, do_load_exports}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1453 | {"load_persist_props", {0, 0, {false, do_load_persist_props}}}, |
| 1454 | {"load_system_props", {0, 0, {false, do_load_system_props}}}, |
| 1455 | {"loglevel", {1, 1, {false, do_loglevel}}}, |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 1456 | {"mark_post_data", {0, 0, {false, do_mark_post_data}}}, |
Paul Crowley | 68258e8 | 2019-10-28 07:55:03 -0700 | [diff] [blame] | 1457 | {"mkdir", {1, 6, {true, do_mkdir}}}, |
Tom Cherry | 880d566 | 2018-02-05 08:01:54 -0800 | [diff] [blame] | 1458 | // TODO: Do mount operations in vendor_init. |
| 1459 | // mount_all is currently too complex to run in vendor_init as it queues action triggers, |
| 1460 | // imports rc scripts, etc. It should be simplified and run in vendor_init context. |
| 1461 | // mount and umount are run in the same context as mount_all for symmetry. |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 1462 | {"mount_all", {0, kMax, {false, do_mount_all}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1463 | {"mount", {3, kMax, {false, do_mount}}}, |
Oli Lan | 13e51e7 | 2019-11-19 18:08:45 +0000 | [diff] [blame] | 1464 | {"perform_apex_config", {0, 0, {false, do_perform_apex_config}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1465 | {"umount", {1, 1, {false, do_umount}}}, |
Alistair Delva | a2cc1eb | 2020-05-20 16:24:00 -0700 | [diff] [blame] | 1466 | {"umount_all", {0, 1, {false, do_umount_all}}}, |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 1467 | {"update_linker_config", {0, 0, {false, do_update_linker_config}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1468 | {"readahead", {1, 2, {true, do_readahead}}}, |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 1469 | {"remount_userdata", {0, 0, {false, do_remount_userdata}}}, |
David Anderson | 6d7c7a2 | 2021-12-03 15:48:16 -0800 | [diff] [blame] | 1470 | {"restart", {1, 2, {false, do_restart}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1471 | {"restorecon", {1, kMax, {true, do_restorecon}}}, |
| 1472 | {"restorecon_recursive", {1, kMax, {true, do_restorecon_recursive}}}, |
| 1473 | {"rm", {1, 1, {true, do_rm}}}, |
| 1474 | {"rmdir", {1, 1, {true, do_rmdir}}}, |
Tom Cherry | 3222848 | 2018-01-18 16:14:25 -0800 | [diff] [blame] | 1475 | {"setprop", {2, 2, {true, do_setprop}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1476 | {"setrlimit", {3, 3, {false, do_setrlimit}}}, |
| 1477 | {"start", {1, 1, {false, do_start}}}, |
| 1478 | {"stop", {1, 1, {false, do_stop}}}, |
Alistair Delva | de28a86 | 2020-06-08 11:04:53 -0700 | [diff] [blame] | 1479 | {"swapon_all", {0, 1, {false, do_swapon_all}}}, |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 1480 | {"enter_default_mount_ns", {0, 0, {false, do_enter_default_mount_ns}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1481 | {"symlink", {2, 2, {true, do_symlink}}}, |
| 1482 | {"sysclktz", {1, 1, {false, do_sysclktz}}}, |
| 1483 | {"trigger", {1, 1, {false, do_trigger}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1484 | {"verity_update_state", {0, 0, {false, do_verity_update_state}}}, |
| 1485 | {"wait", {1, 2, {true, do_wait}}}, |
Tom Cherry | fa3e52c | 2017-10-17 10:43:52 -0700 | [diff] [blame] | 1486 | {"wait_for_prop", {2, 2, {false, do_wait_for_prop}}}, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1487 | {"write", {2, 2, {true, do_write}}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1488 | }; |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 1489 | // clang-format on |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1490 | return builtin_functions; |
| 1491 | } |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 1492 | // Builtin-function-map end |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 1493 | |
| 1494 | } // namespace init |
| 1495 | } // namespace android |