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