2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
19 #include "NetlinkManager.h"
20 #include "VoldNativeService.h"
22 #include "VolumeManager.h"
23 #include "model/Disk.h"
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28 #include <android-base/properties.h>
29 #include <android-base/stringprintf.h>
30 #include <cutils/klog.h>
31 #include <hidl/HidlTransportSupport.h>
32 #include <utils/Trace.h>
43 #include <sys/types.h>
45 typedef struct vold_configs {
46 bool has_adoptable : 1;
48 bool has_reserved : 1;
49 bool has_compress : 1;
52 static int process_config(VolumeManager* vm, VoldConfigs* configs);
53 static void coldboot(const char* path);
54 static void parse_args(int argc, char** argv);
56 struct selabel_handle* sehandle;
58 using android::base::StringPrintf;
59 using android::fs_mgr::ReadDefaultFstab;
61 int main(int argc, char** argv) {
62 atrace_set_tracing_enabled(false);
63 setenv("ANDROID_LOG_TAGS", "*:d", 1); // Do not submit with verbose logs enabled
64 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
66 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
70 LOG(DEBUG) << "Detected support for:"
71 << (android::vold::IsFilesystemSupported("exfat") ? " exfat" : "")
72 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
73 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
74 << (android::vold::IsFilesystemSupported("ntfs") ? " ntfs" : "")
75 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
80 parse_args(argc, argv);
82 sehandle = selinux_android_file_context_handle();
84 selinux_android_set_sehandle(sehandle);
87 mkdir("/dev/block/vold", 0755);
89 /* For when cryptfs checks and mounts an encrypted filesystem */
92 /* Create our singleton managers */
93 if (!(vm = VolumeManager::Instance())) {
94 LOG(ERROR) << "Unable to create VolumeManager";
98 if (!(nm = NetlinkManager::Instance())) {
99 LOG(ERROR) << "Unable to create NetlinkManager";
103 if (android::base::GetBoolProperty("vold.debug", false)) {
108 PLOG(ERROR) << "Unable to start VolumeManager";
112 VoldConfigs configs = {};
113 if (process_config(vm, &configs)) {
114 PLOG(ERROR) << "Error reading configuration... continuing anyways";
117 android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
119 ATRACE_BEGIN("VoldNativeService::start");
120 if (android::vold::VoldNativeService::start() != android::OK) {
121 LOG(ERROR) << "Unable to start VoldNativeService";
126 LOG(DEBUG) << "VoldNativeService::start() completed OK";
128 ATRACE_BEGIN("NetlinkManager::start");
130 PLOG(ERROR) << "Unable to start NetlinkManager";
135 // This call should go after listeners are started to avoid
136 // a deadlock between vold and init (see b/34278978 for details)
137 android::base::SetProperty("vold.has_adoptable", configs.has_adoptable ? "1" : "0");
138 android::base::SetProperty("vold.has_quota", configs.has_quota ? "1" : "0");
139 android::base::SetProperty("vold.has_reserved", configs.has_reserved ? "1" : "0");
140 android::base::SetProperty("vold.has_compress", configs.has_compress ? "1" : "0");
142 // Do coldboot here so it won't block booting,
143 // also the cold boot is needed in case we have flash drive
144 // connected before Vold launched
145 coldboot("/sys/block");
149 android::IPCThreadState::self()->joinThreadPool();
150 LOG(INFO) << "vold shutting down";
155 static void parse_args(int argc, char** argv) {
156 static struct option opts[] = {
157 {"blkid_context", required_argument, 0, 'b'},
158 {"blkid_untrusted_context", required_argument, 0, 'B'},
159 {"fsck_context", required_argument, 0, 'f'},
160 {"fsck_untrusted_context", required_argument, 0, 'F'},
161 {nullptr, 0, nullptr, 0},
165 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
168 case 'b': android::vold::sBlkidContext = optarg; break;
169 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
170 case 'f': android::vold::sFsckContext = optarg; break;
171 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
176 CHECK(android::vold::sBlkidContext != nullptr);
177 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
178 CHECK(android::vold::sFsckContext != nullptr);
179 CHECK(android::vold::sFsckUntrustedContext != nullptr);
182 static void do_coldboot(DIR* d, int lvl) {
188 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
190 write(fd, "add\n", 4);
194 while ((de = readdir(d))) {
197 if (de->d_name[0] == '.') continue;
199 if (de->d_type != DT_DIR && lvl > 0) continue;
201 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
202 if (fd < 0) continue;
208 do_coldboot(d2, lvl + 1);
214 static void coldboot(const char* path) {
215 ATRACE_NAME("coldboot");
216 DIR* d = opendir(path);
223 static int process_config(VolumeManager* vm, VoldConfigs* configs) {
224 ATRACE_NAME("process_config");
226 if (!ReadDefaultFstab(&fstab_default)) {
227 PLOG(ERROR) << "Failed to open default fstab";
231 /* Loop through entries looking for ones that vold manages */
232 configs->has_adoptable = false;
233 configs->has_quota = false;
234 configs->has_reserved = false;
235 configs->has_compress = false;
236 for (auto& entry : fstab_default) {
237 if (entry.fs_mgr_flags.quota) {
238 configs->has_quota = true;
240 if (entry.reserved_size > 0) {
241 configs->has_reserved = true;
243 if (entry.fs_mgr_flags.fs_compress) {
244 configs->has_compress = true;
247 /* Make sure logical partitions have an updated blk_device. */
248 if (entry.fs_mgr_flags.logical && !fs_mgr_update_logical_partition(&entry) &&
249 !entry.fs_mgr_flags.no_fail) {
250 PLOG(FATAL) << "could not find logical partition " << entry.blk_device;
253 if (entry.fs_mgr_flags.vold_managed) {
254 std::string sysPattern(entry.blk_device);
255 std::string nickname(entry.label);
258 if (entry.is_encryptable()) {
259 flags |= android::vold::Disk::Flags::kAdoptable;
260 configs->has_adoptable = true;
262 if (entry.fs_mgr_flags.no_emulated_sd ||
263 android::base::GetBoolProperty("vold.debug.default_primary", false)) {
264 flags |= android::vold::Disk::Flags::kDefaultPrimary;
266 if (entry.fs_mgr_flags.nonremovable) {
267 flags |= android::vold::Disk::Flags::kNonRemovable;
270 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
271 new VolumeManager::DiskSource(sysPattern, nickname, entry.partnum,
272 flags, entry.fs_type, entry.fs_options)));
277 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
278 size_t pos = cmdline.find("SDCARD=");
279 if (pos != std::string::npos) {
280 std::string sdcard = cmdline.substr(pos + 7);
281 sdcard = sdcard.substr(0, sdcard.find_first_of(" \n"));
282 if (!sdcard.empty()) {
284 if (access(std::string("/sys/block/" + sdcard).c_str(), X_OK)) { // not a disk
285 auto d = std::find_if_not(sdcard.rbegin(), sdcard.rend(), ::isdigit);
286 pos = std::distance(d, sdcard.rend());
287 if (pos != sdcard.length()) {
288 partnum = std::stoi(sdcard.substr(pos));
289 sdcard = sdcard.substr(0, pos);
290 if (sdcard.find("mmcblk") != std::string::npos) {
291 // exclude the last 'p'
292 sdcard = sdcard.substr(0, pos - 1);
296 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
297 new VolumeManager::DiskSource("/devices/*/" + sdcard, sdcard,
298 partnum, android::vold::Disk::Flags::kAdoptable, "auto", "")));
299 configs->has_adoptable = true;
300 LOG(INFO) << "Add SDCARD=" << sdcard << " partnum=" << partnum;