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 "model/Disk.h"
20 #include "VolumeManager.h"
21 #include "NetlinkManager.h"
22 #include "VoldNativeService.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 <utils/Trace.h>
38 #include <sys/types.h>
44 static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
46 static void coldboot(const char *path);
47 static void parse_args(int argc, char** argv);
49 struct selabel_handle *sehandle;
51 using android::base::StringPrintf;
53 int main(int argc, char** argv) {
54 atrace_set_tracing_enabled(false);
55 setenv("ANDROID_LOG_TAGS", "*:v", 1);
56 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
58 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
63 LOG(VERBOSE) << "Detected support for:"
64 << (android::vold::IsFilesystemSupported("exfat") ? " exfat" : "")
65 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
66 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
67 << (android::vold::IsFilesystemSupported("ntfs") ? " ntfs" : "")
68 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
73 parse_args(argc, argv);
75 sehandle = selinux_android_file_context_handle();
77 selinux_android_set_sehandle(sehandle);
80 mkdir("/dev/block/vold", 0755);
82 /* For when cryptfs checks and mounts an encrypted filesystem */
85 /* Create our singleton managers */
86 if (!(vm = VolumeManager::Instance())) {
87 LOG(ERROR) << "Unable to create VolumeManager";
91 if (!(nm = NetlinkManager::Instance())) {
92 LOG(ERROR) << "Unable to create NetlinkManager";
96 if (android::base::GetBoolProperty("vold.debug", false)) {
101 PLOG(ERROR) << "Unable to start VolumeManager";
109 if (process_config(vm, &has_adoptable, &has_quota, &has_reserved)) {
110 PLOG(ERROR) << "Error reading configuration... continuing anyways";
113 ATRACE_BEGIN("VoldNativeService::start");
114 if (android::vold::VoldNativeService::start() != android::OK) {
115 LOG(ERROR) << "Unable to start VoldNativeService";
120 LOG(DEBUG) << "VoldNativeService::start() completed OK";
122 ATRACE_BEGIN("NetlinkManager::start");
124 PLOG(ERROR) << "Unable to start NetlinkManager";
129 // This call should go after listeners are started to avoid
130 // a deadlock between vold and init (see b/34278978 for details)
131 android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0");
132 android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0");
133 android::base::SetProperty("vold.has_reserved", has_reserved ? "1" : "0");
135 // Do coldboot here so it won't block booting,
136 // also the cold boot is needed in case we have flash drive
137 // connected before Vold launched
138 coldboot("/sys/block");
142 android::IPCThreadState::self()->joinThreadPool();
143 LOG(INFO) << "vold shutting down";
148 static void parse_args(int argc, char** argv) {
149 static struct option opts[] = {
150 {"blkid_context", required_argument, 0, 'b' },
151 {"blkid_untrusted_context", required_argument, 0, 'B' },
152 {"fsck_context", required_argument, 0, 'f' },
153 {"fsck_untrusted_context", required_argument, 0, 'F' },
157 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
159 case 'b': android::vold::sBlkidContext = optarg; break;
160 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
161 case 'f': android::vold::sFsckContext = optarg; break;
162 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
166 CHECK(android::vold::sBlkidContext != nullptr);
167 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
168 CHECK(android::vold::sFsckContext != nullptr);
169 CHECK(android::vold::sFsckUntrustedContext != nullptr);
172 static void do_coldboot(DIR *d, int lvl) {
178 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
180 write(fd, "add\n", 4);
184 while((de = readdir(d))) {
187 if (de->d_name[0] == '.')
190 if (de->d_type != DT_DIR && lvl > 0)
193 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
201 do_coldboot(d2, lvl + 1);
207 static void coldboot(const char *path) {
208 ATRACE_NAME("coldboot");
209 DIR *d = opendir(path);
216 static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
217 bool* has_reserved) {
218 ATRACE_NAME("process_config");
220 fstab_default = fs_mgr_read_fstab_default();
221 if (!fstab_default) {
222 PLOG(ERROR) << "Failed to open default fstab";
226 /* Loop through entries looking for ones that vold manages */
227 *has_adoptable = false;
229 *has_reserved = false;
230 for (int i = 0; i < fstab_default->num_entries; i++) {
231 auto rec = &fstab_default->recs[i];
232 if (fs_mgr_is_quota(rec)) {
235 if (rec->reserved_size > 0) {
236 *has_reserved = true;
239 if (fs_mgr_is_voldmanaged(rec)) {
240 std::string sysPattern(rec->blk_device);
243 fstype = rec->fs_type;
246 if (rec->fs_options) {
247 mntopts = rec->fs_options;
249 std::string nickname(rec->label);
250 int partnum = rec->partnum;
253 if (fs_mgr_is_encryptable(rec)) {
254 flags |= android::vold::Disk::Flags::kAdoptable;
255 *has_adoptable = true;
257 if (fs_mgr_is_noemulatedsd(rec)
258 || android::base::GetBoolProperty("vold.debug.default_primary", false)) {
259 flags |= android::vold::Disk::Flags::kDefaultPrimary;
261 if (fs_mgr_is_nonremovable(rec)) {
262 flags |= android::vold::Disk::Flags::kNonRemovable;
265 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
266 new VolumeManager::DiskSource(sysPattern, nickname, partnum, flags,