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("ext4") ? " ext4" : "")
65 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
66 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
71 parse_args(argc, argv);
73 sehandle = selinux_android_file_context_handle();
75 selinux_android_set_sehandle(sehandle);
78 mkdir("/dev/block/vold", 0755);
80 /* For when cryptfs checks and mounts an encrypted filesystem */
83 /* Create our singleton managers */
84 if (!(vm = VolumeManager::Instance())) {
85 LOG(ERROR) << "Unable to create VolumeManager";
89 if (!(nm = NetlinkManager::Instance())) {
90 LOG(ERROR) << "Unable to create NetlinkManager";
94 if (android::base::GetBoolProperty("vold.debug", false)) {
99 PLOG(ERROR) << "Unable to start VolumeManager";
107 if (process_config(vm, &has_adoptable, &has_quota, &has_reserved)) {
108 PLOG(ERROR) << "Error reading configuration... continuing anyways";
111 ATRACE_BEGIN("VoldNativeService::start");
112 if (android::vold::VoldNativeService::start() != android::OK) {
113 LOG(ERROR) << "Unable to start VoldNativeService";
118 LOG(DEBUG) << "VoldNativeService::start() completed OK";
120 ATRACE_BEGIN("NetlinkManager::start");
122 PLOG(ERROR) << "Unable to start NetlinkManager";
127 // This call should go after listeners are started to avoid
128 // a deadlock between vold and init (see b/34278978 for details)
129 android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0");
130 android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0");
131 android::base::SetProperty("vold.has_reserved", has_reserved ? "1" : "0");
133 // Do coldboot here so it won't block booting,
134 // also the cold boot is needed in case we have flash drive
135 // connected before Vold launched
136 coldboot("/sys/block");
140 android::IPCThreadState::self()->joinThreadPool();
141 LOG(INFO) << "vold shutting down";
146 static void parse_args(int argc, char** argv) {
147 static struct option opts[] = {
148 {"blkid_context", required_argument, 0, 'b' },
149 {"blkid_untrusted_context", required_argument, 0, 'B' },
150 {"fsck_context", required_argument, 0, 'f' },
151 {"fsck_untrusted_context", required_argument, 0, 'F' },
155 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
157 case 'b': android::vold::sBlkidContext = optarg; break;
158 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
159 case 'f': android::vold::sFsckContext = optarg; break;
160 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
164 CHECK(android::vold::sBlkidContext != nullptr);
165 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
166 CHECK(android::vold::sFsckContext != nullptr);
167 CHECK(android::vold::sFsckUntrustedContext != nullptr);
170 static void do_coldboot(DIR *d, int lvl) {
176 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
178 write(fd, "add\n", 4);
182 while((de = readdir(d))) {
185 if (de->d_name[0] == '.')
188 if (de->d_type != DT_DIR && lvl > 0)
191 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
199 do_coldboot(d2, lvl + 1);
205 static void coldboot(const char *path) {
206 ATRACE_NAME("coldboot");
207 DIR *d = opendir(path);
214 static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
215 bool* has_reserved) {
216 ATRACE_NAME("process_config");
218 fstab_default = fs_mgr_read_fstab_default();
219 if (!fstab_default) {
220 PLOG(ERROR) << "Failed to open default fstab";
224 /* Loop through entries looking for ones that vold manages */
225 *has_adoptable = false;
227 *has_reserved = false;
228 for (int i = 0; i < fstab_default->num_entries; i++) {
229 auto rec = &fstab_default->recs[i];
230 if (fs_mgr_is_quota(rec)) {
233 if (rec->reserved_size > 0) {
234 *has_reserved = true;
237 if (fs_mgr_is_voldmanaged(rec)) {
238 if (fs_mgr_is_nonremovable(rec)) {
239 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
243 std::string sysPattern(rec->blk_device);
244 std::string nickname(rec->label);
247 if (fs_mgr_is_encryptable(rec)) {
248 flags |= android::vold::Disk::Flags::kAdoptable;
249 *has_adoptable = true;
251 if (fs_mgr_is_noemulatedsd(rec)
252 || android::base::GetBoolProperty("vold.debug.default_primary", false)) {
253 flags |= android::vold::Disk::Flags::kDefaultPrimary;
256 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
257 new VolumeManager::DiskSource(sysPattern, nickname, flags)));