blob: accaa012a10058808b00995c712b7fb9ace0f3d8 [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
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070019#include "NetlinkManager.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060020#include "VoldNativeService.h"
Paul Crowleye2ee1522017-09-26 14:05:26 -070021#include "VoldUtil.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070022#include "VolumeManager.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070023#include "cryptfs.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070024#include "model/Disk.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070025#include "sehandle.h"
26
Elliott Hughes7e128fb2015-12-04 15:50:53 -080027#include <android-base/logging.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060028#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080029#include <android-base/stringprintf.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070030#include <cutils/klog.h>
Yifan Hong024a1242018-08-10 13:50:46 -070031#include <hidl/HidlTransportSupport.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060032#include <utils/Trace.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070033
Paul Crowley14c8c072018-09-18 13:30:21 -070034#include <dirent.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <fs_mgr.h>
38#include <getopt.h>
San Mehatf1b736b2009-10-10 17:22:08 -070039#include <stdio.h>
40#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070041#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070042#include <sys/stat.h>
43#include <sys/types.h>
San Mehatf1b736b2009-10-10 17:22:08 -070044
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -070045static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
46 bool* has_reserved);
Paul Crowley14c8c072018-09-18 13:30:21 -070047static void coldboot(const char* path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070048static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070049
Paul Crowley14c8c072018-09-18 13:30:21 -070050struct selabel_handle* sehandle;
Stephen Smalley684e6622014-09-30 10:29:24 -040051
Jeff Sharkey36801cc2015-03-13 16:09:20 -070052using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080053using android::fs_mgr::ReadDefaultFstab;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070054
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070055int main(int argc, char** argv) {
Paul Crowley0fd26262018-01-30 09:48:19 -080056 atrace_set_tracing_enabled(false);
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -070057 setenv("ANDROID_LOG_TAGS", "*:d", 1);
Jeff Sharkey5bad3782015-04-18 16:15:10 -070058 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
San Mehatf1b736b2009-10-10 17:22:08 -070059
Paul Crowley0fd26262018-01-30 09:48:19 -080060 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
61
Jeff Sharkey67b8c492017-09-21 17:08:43 -060062 ATRACE_BEGIN("main");
63
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -070064 LOG(DEBUG) << "Detected support for:"
65 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
66 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
67 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070068
Paul Crowley14c8c072018-09-18 13:30:21 -070069 VolumeManager* vm;
70 NetlinkManager* nm;
San Mehatf1b736b2009-10-10 17:22:08 -070071
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070072 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080073
Stephen Smalley684e6622014-09-30 10:29:24 -040074 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070075 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040076 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070077 }
Stephen Smalley684e6622014-09-30 10:29:24 -040078
San Mehata2677e42009-12-13 10:40:18 -080079 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070080
Ken Sumrallb9738ea2013-03-19 19:42:30 -070081 /* For when cryptfs checks and mounts an encrypted filesystem */
82 klog_set_level(6);
83
San Mehatf1b736b2009-10-10 17:22:08 -070084 /* Create our singleton managers */
85 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070086 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070087 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070088 }
San Mehatf1b736b2009-10-10 17:22:08 -070089
90 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070091 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070092 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070093 }
San Mehata2677e42009-12-13 10:40:18 -080094
Jeff Sharkey3472e522017-10-06 18:02:53 -060095 if (android::base::GetBoolProperty("vold.debug", false)) {
Jeff Sharkeyb0667872015-04-29 08:57:18 -070096 vm->setDebug(true);
97 }
98
San Mehatf1b736b2009-10-10 17:22:08 -070099 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700100 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700101 exit(1);
102 }
103
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800104 bool has_adoptable;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600105 bool has_quota;
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700106 bool has_reserved;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800107
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700108 if (process_config(vm, &has_adoptable, &has_quota, &has_reserved)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700109 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700110 }
111
Yifan Hong024a1242018-08-10 13:50:46 -0700112 android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
113
Paul Crowley31888052017-09-27 10:56:54 -0700114 ATRACE_BEGIN("VoldNativeService::start");
115 if (android::vold::VoldNativeService::start() != android::OK) {
116 LOG(ERROR) << "Unable to start VoldNativeService";
117 exit(1);
118 }
119 ATRACE_END();
120
Paul Crowley0fd26262018-01-30 09:48:19 -0800121 LOG(DEBUG) << "VoldNativeService::start() completed OK";
122
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600123 ATRACE_BEGIN("NetlinkManager::start");
San Mehatf1b736b2009-10-10 17:22:08 -0700124 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700125 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700126 exit(1);
127 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600128 ATRACE_END();
San Mehatf1b736b2009-10-10 17:22:08 -0700129
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800130 // This call should go after listeners are started to avoid
131 // a deadlock between vold and init (see b/34278978 for details)
Jeff Sharkey3472e522017-10-06 18:02:53 -0600132 android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0");
133 android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0");
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700134 android::base::SetProperty("vold.has_reserved", has_reserved ? "1" : "0");
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800135
Wei Wang2edbe282017-03-06 17:27:05 -0800136 // Do coldboot here so it won't block booting,
137 // also the cold boot is needed in case we have flash drive
138 // connected before Vold launched
139 coldboot("/sys/block");
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600140
141 ATRACE_END();
142
Jeff Sharkey93396c12017-10-18 16:52:48 -0600143 android::IPCThreadState::self()->joinThreadPool();
144 LOG(INFO) << "vold shutting down";
San Mehatf1b736b2009-10-10 17:22:08 -0700145
San Mehatf1b736b2009-10-10 17:22:08 -0700146 exit(0);
147}
148
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700149static void parse_args(int argc, char** argv) {
150 static struct option opts[] = {
Paul Crowley14c8c072018-09-18 13:30:21 -0700151 {"blkid_context", required_argument, 0, 'b'},
152 {"blkid_untrusted_context", required_argument, 0, 'B'},
153 {"fsck_context", required_argument, 0, 'f'},
154 {"fsck_untrusted_context", required_argument, 0, 'F'},
Eric Biggersf5ef40d2019-04-03 16:32:24 -0700155 {nullptr, 0, nullptr, 0},
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700156 };
157
158 int c;
159 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
160 switch (c) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700161 // clang-format off
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700162 case 'b': android::vold::sBlkidContext = optarg; break;
163 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
164 case 'f': android::vold::sFsckContext = optarg; break;
165 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
Paul Crowley14c8c072018-09-18 13:30:21 -0700166 // clang-format on
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700167 }
168 }
169
170 CHECK(android::vold::sBlkidContext != nullptr);
171 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
172 CHECK(android::vold::sFsckContext != nullptr);
173 CHECK(android::vold::sFsckUntrustedContext != nullptr);
174}
175
Paul Crowley14c8c072018-09-18 13:30:21 -0700176static void do_coldboot(DIR* d, int lvl) {
177 struct dirent* de;
Wei Wang2edbe282017-03-06 17:27:05 -0800178 int dfd, fd;
179
180 dfd = dirfd(d);
181
182 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
Paul Crowley14c8c072018-09-18 13:30:21 -0700183 if (fd >= 0) {
Wei Wang2edbe282017-03-06 17:27:05 -0800184 write(fd, "add\n", 4);
185 close(fd);
186 }
187
Paul Crowley14c8c072018-09-18 13:30:21 -0700188 while ((de = readdir(d))) {
189 DIR* d2;
Wei Wang2edbe282017-03-06 17:27:05 -0800190
Paul Crowley14c8c072018-09-18 13:30:21 -0700191 if (de->d_name[0] == '.') continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800192
Paul Crowley14c8c072018-09-18 13:30:21 -0700193 if (de->d_type != DT_DIR && lvl > 0) continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800194
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600195 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Paul Crowley14c8c072018-09-18 13:30:21 -0700196 if (fd < 0) continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800197
198 d2 = fdopendir(fd);
Paul Crowley14c8c072018-09-18 13:30:21 -0700199 if (d2 == 0)
Wei Wang2edbe282017-03-06 17:27:05 -0800200 close(fd);
201 else {
202 do_coldboot(d2, lvl + 1);
203 closedir(d2);
204 }
205 }
206}
207
Paul Crowley14c8c072018-09-18 13:30:21 -0700208static void coldboot(const char* path) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600209 ATRACE_NAME("coldboot");
Paul Crowley14c8c072018-09-18 13:30:21 -0700210 DIR* d = opendir(path);
211 if (d) {
Wei Wang2edbe282017-03-06 17:27:05 -0800212 do_coldboot(d, 0);
213 closedir(d);
214 }
215}
216
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700217static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
218 bool* has_reserved) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600219 ATRACE_NAME("process_config");
220
Tom Cherry4c5bde22019-01-29 14:34:01 -0800221 if (!ReadDefaultFstab(&fstab_default)) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800222 PLOG(ERROR) << "Failed to open default fstab";
San Mehatf1b736b2009-10-10 17:22:08 -0700223 return -1;
224 }
225
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800226 /* Loop through entries looking for ones that vold manages */
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800227 *has_adoptable = false;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600228 *has_quota = false;
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700229 *has_reserved = false;
David Anderson0d71c4b2019-02-05 17:32:05 -0800230 for (auto& entry : fstab_default) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800231 if (entry.fs_mgr_flags.quota) {
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600232 *has_quota = true;
233 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800234 if (entry.reserved_size > 0) {
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700235 *has_reserved = true;
236 }
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600237
David Anderson0d71c4b2019-02-05 17:32:05 -0800238 /* Make sure logical partitions have an updated blk_device. */
239 if (entry.fs_mgr_flags.logical && !fs_mgr_update_logical_partition(&entry)) {
240 PLOG(FATAL) << "could not find logical partition " << entry.blk_device;
241 }
242
Tom Cherry4c5bde22019-01-29 14:34:01 -0800243 if (entry.fs_mgr_flags.vold_managed) {
244 if (entry.fs_mgr_flags.nonremovable) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700245 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
246 continue;
San Mehata2677e42009-12-13 10:40:18 -0800247 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700248
Tom Cherry4c5bde22019-01-29 14:34:01 -0800249 std::string sysPattern(entry.blk_device);
250 std::string nickname(entry.label);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700251 int flags = 0;
252
Tom Cherry4c5bde22019-01-29 14:34:01 -0800253 if (entry.is_encryptable()) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700254 flags |= android::vold::Disk::Flags::kAdoptable;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800255 *has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700256 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800257 if (entry.fs_mgr_flags.no_emulated_sd ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700258 android::base::GetBoolProperty("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700259 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700260 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700261
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
Paul Crowley14c8c072018-09-18 13:30:21 -0700263 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700264 }
265 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700266 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700267}