blob: 67a3344f5b92bf30c225947e4fdeef07ddb4af72 [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"
Paul Crowley14c8c072018-09-18 13:30:21 -070023#include "model/Disk.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070024#include "sehandle.h"
25
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/logging.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060027#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080028#include <android-base/stringprintf.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070029#include <cutils/klog.h>
Yifan Hong024a1242018-08-10 13:50:46 -070030#include <hidl/HidlTransportSupport.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060031#include <utils/Trace.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070032
Paul Crowley14c8c072018-09-18 13:30:21 -070033#include <dirent.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <fs_mgr.h>
37#include <getopt.h>
San Mehatf1b736b2009-10-10 17:22:08 -070038#include <stdio.h>
39#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070040#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070041#include <sys/stat.h>
42#include <sys/types.h>
San Mehatf1b736b2009-10-10 17:22:08 -070043
Jaegeuk Kimc52f6722020-04-06 15:58:41 -070044typedef struct vold_configs {
45 bool has_adoptable : 1;
46 bool has_quota : 1;
47 bool has_reserved : 1;
48 bool has_compress : 1;
49} VoldConfigs;
50
51static int process_config(VolumeManager* vm, VoldConfigs* configs);
Paul Crowley14c8c072018-09-18 13:30:21 -070052static void coldboot(const char* path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070053static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070054
Paul Crowley14c8c072018-09-18 13:30:21 -070055struct selabel_handle* sehandle;
Stephen Smalley684e6622014-09-30 10:29:24 -040056
Jeff Sharkey36801cc2015-03-13 16:09:20 -070057using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080058using android::fs_mgr::ReadDefaultFstab;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070059
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070060int main(int argc, char** argv) {
Paul Crowley0fd26262018-01-30 09:48:19 -080061 atrace_set_tracing_enabled(false);
Sudheer Shanka40ab6742018-09-18 13:07:45 -070062 setenv("ANDROID_LOG_TAGS", "*:d", 1); // Do not submit with verbose logs enabled
Jeff Sharkey5bad3782015-04-18 16:15:10 -070063 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
San Mehatf1b736b2009-10-10 17:22:08 -070064
Paul Crowley0fd26262018-01-30 09:48:19 -080065 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
66
Jeff Sharkey67b8c492017-09-21 17:08:43 -060067 ATRACE_BEGIN("main");
68
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -070069 LOG(DEBUG) << "Detected support for:"
70 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
71 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
72 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070073
Paul Crowley14c8c072018-09-18 13:30:21 -070074 VolumeManager* vm;
75 NetlinkManager* nm;
San Mehatf1b736b2009-10-10 17:22:08 -070076
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070077 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080078
Stephen Smalley684e6622014-09-30 10:29:24 -040079 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070080 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040081 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070082 }
Stephen Smalley684e6622014-09-30 10:29:24 -040083
San Mehata2677e42009-12-13 10:40:18 -080084 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070085
Ken Sumrallb9738ea2013-03-19 19:42:30 -070086 /* For when cryptfs checks and mounts an encrypted filesystem */
87 klog_set_level(6);
88
San Mehatf1b736b2009-10-10 17:22:08 -070089 /* Create our singleton managers */
90 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070091 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070092 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070093 }
San Mehatf1b736b2009-10-10 17:22:08 -070094
95 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070096 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070097 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070098 }
San Mehata2677e42009-12-13 10:40:18 -080099
Jeff Sharkey3472e522017-10-06 18:02:53 -0600100 if (android::base::GetBoolProperty("vold.debug", false)) {
Jeff Sharkeyb0667872015-04-29 08:57:18 -0700101 vm->setDebug(true);
102 }
103
San Mehatf1b736b2009-10-10 17:22:08 -0700104 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700105 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700106 exit(1);
107 }
108
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700109 VoldConfigs configs = {};
110 if (process_config(vm, &configs)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700111 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700112 }
113
Yifan Hong024a1242018-08-10 13:50:46 -0700114 android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
115
Paul Crowley31888052017-09-27 10:56:54 -0700116 ATRACE_BEGIN("VoldNativeService::start");
117 if (android::vold::VoldNativeService::start() != android::OK) {
118 LOG(ERROR) << "Unable to start VoldNativeService";
119 exit(1);
120 }
121 ATRACE_END();
122
Paul Crowley0fd26262018-01-30 09:48:19 -0800123 LOG(DEBUG) << "VoldNativeService::start() completed OK";
124
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600125 ATRACE_BEGIN("NetlinkManager::start");
San Mehatf1b736b2009-10-10 17:22:08 -0700126 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700127 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700128 exit(1);
129 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600130 ATRACE_END();
San Mehatf1b736b2009-10-10 17:22:08 -0700131
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800132 // This call should go after listeners are started to avoid
133 // a deadlock between vold and init (see b/34278978 for details)
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700134 android::base::SetProperty("vold.has_quota", configs.has_quota ? "1" : "0");
135 android::base::SetProperty("vold.has_reserved", configs.has_reserved ? "1" : "0");
136 android::base::SetProperty("vold.has_compress", configs.has_compress ? "1" : "0");
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800137
Wei Wang2edbe282017-03-06 17:27:05 -0800138 // Do coldboot here so it won't block booting,
139 // also the cold boot is needed in case we have flash drive
140 // connected before Vold launched
141 coldboot("/sys/block");
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600142
143 ATRACE_END();
144
Jeff Sharkey93396c12017-10-18 16:52:48 -0600145 android::IPCThreadState::self()->joinThreadPool();
146 LOG(INFO) << "vold shutting down";
San Mehatf1b736b2009-10-10 17:22:08 -0700147
San Mehatf1b736b2009-10-10 17:22:08 -0700148 exit(0);
149}
150
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700151static void parse_args(int argc, char** argv) {
152 static struct option opts[] = {
Paul Crowley14c8c072018-09-18 13:30:21 -0700153 {"blkid_context", required_argument, 0, 'b'},
154 {"blkid_untrusted_context", required_argument, 0, 'B'},
155 {"fsck_context", required_argument, 0, 'f'},
156 {"fsck_untrusted_context", required_argument, 0, 'F'},
Eric Biggersf5ef40d2019-04-03 16:32:24 -0700157 {nullptr, 0, nullptr, 0},
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700158 };
159
160 int c;
161 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
162 switch (c) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700163 // clang-format off
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700164 case 'b': android::vold::sBlkidContext = optarg; break;
165 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
166 case 'f': android::vold::sFsckContext = optarg; break;
167 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 // clang-format on
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700169 }
170 }
171
172 CHECK(android::vold::sBlkidContext != nullptr);
173 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
174 CHECK(android::vold::sFsckContext != nullptr);
175 CHECK(android::vold::sFsckUntrustedContext != nullptr);
176}
177
Paul Crowley14c8c072018-09-18 13:30:21 -0700178static void do_coldboot(DIR* d, int lvl) {
179 struct dirent* de;
Wei Wang2edbe282017-03-06 17:27:05 -0800180 int dfd, fd;
181
182 dfd = dirfd(d);
183
184 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
Paul Crowley14c8c072018-09-18 13:30:21 -0700185 if (fd >= 0) {
Wei Wang2edbe282017-03-06 17:27:05 -0800186 write(fd, "add\n", 4);
187 close(fd);
188 }
189
Paul Crowley14c8c072018-09-18 13:30:21 -0700190 while ((de = readdir(d))) {
191 DIR* d2;
Wei Wang2edbe282017-03-06 17:27:05 -0800192
Paul Crowley14c8c072018-09-18 13:30:21 -0700193 if (de->d_name[0] == '.') continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800194
Paul Crowley14c8c072018-09-18 13:30:21 -0700195 if (de->d_type != DT_DIR && lvl > 0) continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800196
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600197 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Paul Crowley14c8c072018-09-18 13:30:21 -0700198 if (fd < 0) continue;
Wei Wang2edbe282017-03-06 17:27:05 -0800199
200 d2 = fdopendir(fd);
Paul Crowley14c8c072018-09-18 13:30:21 -0700201 if (d2 == 0)
Wei Wang2edbe282017-03-06 17:27:05 -0800202 close(fd);
203 else {
204 do_coldboot(d2, lvl + 1);
205 closedir(d2);
206 }
207 }
208}
209
Paul Crowley14c8c072018-09-18 13:30:21 -0700210static void coldboot(const char* path) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600211 ATRACE_NAME("coldboot");
Paul Crowley14c8c072018-09-18 13:30:21 -0700212 DIR* d = opendir(path);
213 if (d) {
Wei Wang2edbe282017-03-06 17:27:05 -0800214 do_coldboot(d, 0);
215 closedir(d);
216 }
217}
218
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700219static int process_config(VolumeManager* vm, VoldConfigs* configs) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600220 ATRACE_NAME("process_config");
221
Tom Cherry4c5bde22019-01-29 14:34:01 -0800222 if (!ReadDefaultFstab(&fstab_default)) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800223 PLOG(ERROR) << "Failed to open default fstab";
San Mehatf1b736b2009-10-10 17:22:08 -0700224 return -1;
225 }
226
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800227 /* Loop through entries looking for ones that vold manages */
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700228 configs->has_adoptable = false;
229 configs->has_quota = false;
230 configs->has_reserved = false;
231 configs->has_compress = false;
David Anderson0d71c4b2019-02-05 17:32:05 -0800232 for (auto& entry : fstab_default) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800233 if (entry.fs_mgr_flags.quota) {
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700234 configs->has_quota = true;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600235 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800236 if (entry.reserved_size > 0) {
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700237 configs->has_reserved = true;
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700238 }
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -0800239 if (entry.fs_mgr_flags.fs_compress) {
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700240 configs->has_compress = true;
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -0800241 }
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600242
David Anderson0d71c4b2019-02-05 17:32:05 -0800243 /* Make sure logical partitions have an updated blk_device. */
244 if (entry.fs_mgr_flags.logical && !fs_mgr_update_logical_partition(&entry)) {
245 PLOG(FATAL) << "could not find logical partition " << entry.blk_device;
246 }
247
Tom Cherry4c5bde22019-01-29 14:34:01 -0800248 if (entry.fs_mgr_flags.vold_managed) {
249 if (entry.fs_mgr_flags.nonremovable) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700250 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
251 continue;
San Mehata2677e42009-12-13 10:40:18 -0800252 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700253
Tom Cherry4c5bde22019-01-29 14:34:01 -0800254 std::string sysPattern(entry.blk_device);
255 std::string nickname(entry.label);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700256 int flags = 0;
257
Tom Cherry4c5bde22019-01-29 14:34:01 -0800258 if (entry.is_encryptable()) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700259 flags |= android::vold::Disk::Flags::kAdoptable;
Jaegeuk Kimc52f6722020-04-06 15:58:41 -0700260 configs->has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700261 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800262 if (entry.fs_mgr_flags.no_emulated_sd ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700263 android::base::GetBoolProperty("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700264 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700265 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700266
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700267 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
Paul Crowley14c8c072018-09-18 13:30:21 -0700268 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700269 }
270 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700271 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700272}