blob: bdce76eda35625a58d09bb27f87704928a99c737 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Eric Biggersfb486662022-03-22 00:33:52 +000019#include "FsCrypt.h"
David Anderson156d9d22021-09-21 17:21:57 -070020#include "MetadataCrypt.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070021#include "NetlinkManager.h"
Weston Carvalho6ab20652024-12-03 15:21:45 -060022#include "VendorVoldNativeService.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060023#include "VoldNativeService.h"
Paul Crowleye2ee1522017-09-26 14:05:26 -070024#include "VoldUtil.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070025#include "VolumeManager.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070026#include "model/Disk.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070027#include "sehandle.h"
28
Elliott Hughes7e128fb2015-12-04 15:50:53 -080029#include <android-base/logging.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060030#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080031#include <android-base/stringprintf.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070032#include <cutils/klog.h>
Yifan Hong024a1242018-08-10 13:50:46 -070033#include <hidl/HidlTransportSupport.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060034#include <utils/Trace.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070035
Paul Crowley14c8c072018-09-18 13:30:21 -070036#include <dirent.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <fs_mgr.h>
40#include <getopt.h>
San Mehatf1b736b2009-10-10 17:22:08 -070041#include <stdio.h>
42#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070043#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070044#include <sys/stat.h>
45#include <sys/types.h>
San Mehatf1b736b2009-10-10 17:22:08 -070046
Jaegeuk Kimc52f6722020-04-06 15:58:41 -070047typedef struct vold_configs {
48 bool has_adoptable : 1;
49 bool has_quota : 1;
50 bool has_reserved : 1;
51 bool has_compress : 1;
52} VoldConfigs;
53
54static int process_config(VolumeManager* vm, VoldConfigs* configs);
Paul Crowley14c8c072018-09-18 13:30:21 -070055static void coldboot(const char* path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070056static void parse_args(int argc, char** argv);
David Anderson52de78e2021-11-05 19:26:56 -070057static void VoldLogger(android::base::LogId log_buffer_id, android::base::LogSeverity severity,
58 const char* tag, const char* file, unsigned int line, const char* message);
San Mehatf1b736b2009-10-10 17:22:08 -070059
Paul Crowley14c8c072018-09-18 13:30:21 -070060struct selabel_handle* sehandle;
David Anderson52de78e2021-11-05 19:26:56 -070061android::base::LogdLogger logd_logger(android::base::SYSTEM);
Stephen Smalley684e6622014-09-30 10:29:24 -040062
Jeff Sharkey36801cc2015-03-13 16:09:20 -070063using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080064using android::fs_mgr::ReadDefaultFstab;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070065
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070066int main(int argc, char** argv) {
Paul Crowley0fd26262018-01-30 09:48:19 -080067 atrace_set_tracing_enabled(false);
Sudheer Shanka40ab6742018-09-18 13:07:45 -070068 setenv("ANDROID_LOG_TAGS", "*:d", 1); // Do not submit with verbose logs enabled
David Anderson52de78e2021-11-05 19:26:56 -070069 android::base::InitLogging(argv, &VoldLogger);
San Mehatf1b736b2009-10-10 17:22:08 -070070
Paul Crowley0fd26262018-01-30 09:48:19 -080071 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
72
Jeff Sharkey67b8c492017-09-21 17:08:43 -060073 ATRACE_BEGIN("main");
74
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -070075 LOG(DEBUG) << "Detected support for:"
76 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
77 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
Alfred Piccionifc4934f2023-02-02 09:46:30 +000078 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070079
Paul Crowley14c8c072018-09-18 13:30:21 -070080 VolumeManager* vm;
81 NetlinkManager* nm;
San Mehatf1b736b2009-10-10 17:22:08 -070082
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070083 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080084
Stephen Smalley684e6622014-09-30 10:29:24 -040085 sehandle = selinux_android_file_context_handle();
Eric Biggersc7c4f5a2023-03-01 20:08:59 +000086 if (!sehandle) {
87 LOG(ERROR) << "Failed to get SELinux file contexts handle";
88 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070089 }
Eric Biggersc7c4f5a2023-03-01 20:08:59 +000090 selinux_android_set_sehandle(sehandle);
Stephen Smalley684e6622014-09-30 10:29:24 -040091
San Mehata2677e42009-12-13 10:40:18 -080092 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070093
Ken Sumrallb9738ea2013-03-19 19:42:30 -070094 /* For when cryptfs checks and mounts an encrypted filesystem */
95 klog_set_level(6);
96
San Mehatf1b736b2009-10-10 17:22:08 -070097 /* Create our singleton managers */
98 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070099 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700100 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700101 }
San Mehatf1b736b2009-10-10 17:22:08 -0700102
103 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700104 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700105 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700106 }
San Mehata2677e42009-12-13 10:40:18 -0800107
Jeff Sharkey3472e522017-10-06 18:02:53 -0600108 if (android::base::GetBoolProperty("vold.debug", false)) {
Jeff Sharkeyb0667872015-04-29 08:57:18 -0700109 vm->setDebug(true);
110 }
111
San Mehatf1b736b2009-10-10 17:22:08 -0700112 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700113 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700114 exit(1);
115 }
116
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700117 VoldConfigs configs = {};
118 if (process_config(vm, &configs)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700119 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700120 }
121
Yifan Hong024a1242018-08-10 13:50:46 -0700122 android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
123
Paul Crowley31888052017-09-27 10:56:54 -0700124 ATRACE_BEGIN("VoldNativeService::start");
125 if (android::vold::VoldNativeService::start() != android::OK) {
126 LOG(ERROR) << "Unable to start VoldNativeService";
127 exit(1);
128 }
129 ATRACE_END();
Paul Crowley0fd26262018-01-30 09:48:19 -0800130 LOG(DEBUG) << "VoldNativeService::start() completed OK";
131
Weston Carvalho6ab20652024-12-03 15:21:45 -0600132 ATRACE_BEGIN("VendorVoldNativeService::try_start");
133 if (android::vold::VendorVoldNativeService::try_start() != android::OK) {
134 LOG(ERROR) << "Unable to start VendorVoldNativeService";
135 exit(1);
136 }
137 ATRACE_END();
138 LOG(DEBUG) << "VendorVoldNativeService::try_start() completed OK";
139
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600140 ATRACE_BEGIN("NetlinkManager::start");
San Mehatf1b736b2009-10-10 17:22:08 -0700141 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700142 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700143 exit(1);
144 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600145 ATRACE_END();
San Mehatf1b736b2009-10-10 17:22:08 -0700146
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800147 // This call should go after listeners are started to avoid
148 // a deadlock between vold and init (see b/34278978 for details)
Jaegeuk Kim4ea573a2020-04-23 13:43:12 -0700149 android::base::SetProperty("vold.has_adoptable", configs.has_adoptable ? "1" : "0");
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700150 android::base::SetProperty("vold.has_quota", configs.has_quota ? "1" : "0");
151 android::base::SetProperty("vold.has_reserved", configs.has_reserved ? "1" : "0");
152 android::base::SetProperty("vold.has_compress", configs.has_compress ? "1" : "0");
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800153
Wei Wang2edbe282017-03-06 17:27:05 -0800154 // Do coldboot here so it won't block booting,
155 // also the cold boot is needed in case we have flash drive
156 // connected before Vold launched
157 coldboot("/sys/block");
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600158
159 ATRACE_END();
160
Jeff Sharkey93396c12017-10-18 16:52:48 -0600161 android::IPCThreadState::self()->joinThreadPool();
162 LOG(INFO) << "vold shutting down";
San Mehatf1b736b2009-10-10 17:22:08 -0700163
San Mehatf1b736b2009-10-10 17:22:08 -0700164 exit(0);
165}
166
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700167static void parse_args(int argc, char** argv) {
168 static struct option opts[] = {
Paul Crowley14c8c072018-09-18 13:30:21 -0700169 {"blkid_context", required_argument, 0, 'b'},
170 {"blkid_untrusted_context", required_argument, 0, 'B'},
171 {"fsck_context", required_argument, 0, 'f'},
172 {"fsck_untrusted_context", required_argument, 0, 'F'},
Eric Biggersf5ef40d2019-04-03 16:32:24 -0700173 {nullptr, 0, nullptr, 0},
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700174 };
175
176 int c;
177 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
178 switch (c) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700179 // clang-format off
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700180 case 'b': android::vold::sBlkidContext = optarg; break;
181 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
182 case 'f': android::vold::sFsckContext = optarg; break;
183 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
Paul Crowley14c8c072018-09-18 13:30:21 -0700184 // clang-format on
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700185 }
186 }
187
188 CHECK(android::vold::sBlkidContext != nullptr);
189 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
190 CHECK(android::vold::sFsckContext != nullptr);
191 CHECK(android::vold::sFsckUntrustedContext != nullptr);
192}
193
Paul Crowley14c8c072018-09-18 13:30:21 -0700194static void do_coldboot(DIR* d, int lvl) {
195 struct dirent* de;
Wei Wang2edbe282017-03-06 17:27:05 -0800196 int dfd, fd;
197
198 dfd = dirfd(d);
199
200 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
Paul Crowley14c8c072018-09-18 13:30:21 -0700201 if (fd >= 0) {
Wei Wang2edbe282017-03-06 17:27:05 -0800202 write(fd, "add\n", 4);
203 close(fd);
204 }
205
Paul Crowley14c8c072018-09-18 13:30:21 -0700206 while ((de = readdir(d))) {
207 DIR* d2;
Wei Wang2edbe282017-03-06 17:27:05 -0800208
Paul Crowley14c8c072018-09-18 13:30:21 -0700209 if (de->d_name[0] == '.') continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800210
Paul Crowley14c8c072018-09-18 13:30:21 -0700211 if (de->d_type != DT_DIR && lvl > 0) continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800212
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600213 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Paul Crowley14c8c072018-09-18 13:30:21 -0700214 if (fd < 0) continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800215
216 d2 = fdopendir(fd);
Paul Crowley14c8c072018-09-18 13:30:21 -0700217 if (d2 == 0)
Wei Wang2edbe282017-03-06 17:27:05 -0800218 close(fd);
219 else {
220 do_coldboot(d2, lvl + 1);
221 closedir(d2);
222 }
223 }
224}
225
Paul Crowley14c8c072018-09-18 13:30:21 -0700226static void coldboot(const char* path) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600227 ATRACE_NAME("coldboot");
Paul Crowley14c8c072018-09-18 13:30:21 -0700228 DIR* d = opendir(path);
229 if (d) {
Wei Wang2edbe282017-03-06 17:27:05 -0800230 do_coldboot(d, 0);
231 closedir(d);
232 }
233}
234
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700235static int process_config(VolumeManager* vm, VoldConfigs* configs) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600236 ATRACE_NAME("process_config");
237
Tom Cherry4c5bde22019-01-29 14:34:01 -0800238 if (!ReadDefaultFstab(&fstab_default)) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800239 PLOG(ERROR) << "Failed to open default fstab";
San Mehatf1b736b2009-10-10 17:22:08 -0700240 return -1;
241 }
242
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800243 /* Loop through entries looking for ones that vold manages */
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700244 configs->has_adoptable = false;
245 configs->has_quota = false;
246 configs->has_reserved = false;
247 configs->has_compress = false;
David Anderson0d71c4b2019-02-05 17:32:05 -0800248 for (auto& entry : fstab_default) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800249 if (entry.fs_mgr_flags.quota) {
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700250 configs->has_quota = true;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600251 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800252 if (entry.reserved_size > 0) {
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700253 configs->has_reserved = true;
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700254 }
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -0800255 if (entry.fs_mgr_flags.fs_compress) {
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700256 configs->has_compress = true;
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -0800257 }
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600258
David Anderson0d71c4b2019-02-05 17:32:05 -0800259 /* Make sure logical partitions have an updated blk_device. */
P.Adarsh Reddy167c2a62020-04-16 21:46:08 +0530260 if (entry.fs_mgr_flags.logical && !fs_mgr_update_logical_partition(&entry) &&
261 !entry.fs_mgr_flags.no_fail) {
David Anderson0d71c4b2019-02-05 17:32:05 -0800262 PLOG(FATAL) << "could not find logical partition " << entry.blk_device;
263 }
264
Eric Biggersf14f8622022-03-17 23:06:45 +0000265 if (entry.mount_point == "/data" && !entry.metadata_key_dir.empty()) {
David Anderson156d9d22021-09-21 17:21:57 -0700266 // Pre-populate userdata dm-devices since the uevents are asynchronous (b/198405417).
267 android::vold::defaultkey_precreate_dm_device();
268 }
269
Tom Cherry4c5bde22019-01-29 14:34:01 -0800270 if (entry.fs_mgr_flags.vold_managed) {
271 if (entry.fs_mgr_flags.nonremovable) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700272 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
273 continue;
San Mehata2677e42009-12-13 10:40:18 -0800274 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700275
Tom Cherry4c5bde22019-01-29 14:34:01 -0800276 std::string sysPattern(entry.blk_device);
277 std::string nickname(entry.label);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700278 int flags = 0;
279
Tom Cherry4c5bde22019-01-29 14:34:01 -0800280 if (entry.is_encryptable()) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700281 flags |= android::vold::Disk::Flags::kAdoptable;
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700282 configs->has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700283 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800284 if (entry.fs_mgr_flags.no_emulated_sd ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700285 android::base::GetBoolProperty("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700286 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700287 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700288
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700289 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
Paul Crowley14c8c072018-09-18 13:30:21 -0700290 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700291 }
292 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700293 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700294}
David Anderson52de78e2021-11-05 19:26:56 -0700295
296static void VoldLogger(android::base::LogId log_buffer_id, android::base::LogSeverity severity,
297 const char* tag, const char* file, unsigned int line, const char* message) {
298 logd_logger(log_buffer_id, severity, tag, file, line, message);
299
Eric Biggersfb486662022-03-22 00:33:52 +0000300 if (severity >= android::base::WARNING) {
301 static bool early_boot_done = false;
David Anderson52de78e2021-11-05 19:26:56 -0700302
Eric Biggersfb486662022-03-22 00:33:52 +0000303 // If metadata encryption setup (fscrypt_mount_metadata_encrypted) or
304 // basic FBE setup (fscrypt_init_user0) fails, then the boot will fail
305 // before adb can be started, so logcat won't be available. To allow
306 // debugging these early boot failures, log early errors and warnings to
307 // the kernel log. This allows diagnosing failures via the serial log,
308 // or via last dmesg/"fastboot oem dmesg" on devices that support it.
David Anderson52de78e2021-11-05 19:26:56 -0700309 //
Eric Biggersfb486662022-03-22 00:33:52 +0000310 // As a very quick-and-dirty test for whether /data has been mounted,
311 // check whether /data/misc/vold exists.
312 if (!early_boot_done) {
313 if (access("/data/misc/vold", F_OK) == 0 && fscrypt_init_user0_done) {
314 early_boot_done = true;
315 return;
316 }
317 android::base::KernelLogger(log_buffer_id, severity, tag, file, line, message);
David Anderson52de78e2021-11-05 19:26:56 -0700318 }
David Anderson52de78e2021-11-05 19:26:56 -0700319 }
320}