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.
18 #include "VolumeManager.h"
19 #include "CommandListener.h"
20 #include "CryptCommandListener.h"
21 #include "NetlinkManager.h"
25 #include <android-base/logging.h>
26 #include <android-base/stringprintf.h>
27 #include <cutils/klog.h>
28 #include <cutils/properties.h>
29 #include <cutils/sockets.h>
36 #include <sys/types.h>
42 static int process_config(VolumeManager *vm);
43 static void coldboot(const char *path);
44 static void parse_args(int argc, char** argv);
48 struct selabel_handle *sehandle;
50 using android::base::StringPrintf;
52 int main(int argc, char** argv) {
53 setenv("ANDROID_LOG_TAGS", "*:v", 1);
54 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
56 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
58 LOG(VERBOSE) << "Detected support for:"
59 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
60 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
61 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
65 CryptCommandListener *ccl;
68 parse_args(argc, argv);
70 sehandle = selinux_android_file_context_handle();
72 selinux_android_set_sehandle(sehandle);
75 // Quickly throw a CLOEXEC on the socket we just inherited from init
76 fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
77 fcntl(android_get_control_socket("cryptd"), F_SETFD, FD_CLOEXEC);
79 mkdir("/dev/block/vold", 0755);
81 /* For when cryptfs checks and mounts an encrypted filesystem */
84 /* Create our singleton managers */
85 if (!(vm = VolumeManager::Instance())) {
86 LOG(ERROR) << "Unable to create VolumeManager";
90 if (!(nm = NetlinkManager::Instance())) {
91 LOG(ERROR) << "Unable to create NetlinkManager";
95 if (property_get_bool("vold.debug", false)) {
99 cl = new CommandListener();
100 ccl = new CryptCommandListener();
101 vm->setBroadcaster((SocketListener *) cl);
102 nm->setBroadcaster((SocketListener *) cl);
105 PLOG(ERROR) << "Unable to start VolumeManager";
109 if (process_config(vm)) {
110 PLOG(ERROR) << "Error reading configuration... continuing anyways";
114 PLOG(ERROR) << "Unable to start NetlinkManager";
118 coldboot("/sys/block");
119 // coldboot("/sys/class/switch");
122 * Now that we're up, we can respond to commands
124 if (cl->startListener()) {
125 PLOG(ERROR) << "Unable to start CommandListener";
129 if (ccl->startListener()) {
130 PLOG(ERROR) << "Unable to start CryptCommandListener";
134 // Eventually we'll become the monitoring thread
139 LOG(ERROR) << "Vold exiting";
143 static void parse_args(int argc, char** argv) {
144 static struct option opts[] = {
145 {"blkid_context", required_argument, 0, 'b' },
146 {"blkid_untrusted_context", required_argument, 0, 'B' },
147 {"fsck_context", required_argument, 0, 'f' },
148 {"fsck_untrusted_context", required_argument, 0, 'F' },
152 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
154 case 'b': android::vold::sBlkidContext = optarg; break;
155 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
156 case 'f': android::vold::sFsckContext = optarg; break;
157 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
161 CHECK(android::vold::sBlkidContext != nullptr);
162 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
163 CHECK(android::vold::sFsckContext != nullptr);
164 CHECK(android::vold::sFsckUntrustedContext != nullptr);
167 static void do_coldboot(DIR *d, int lvl) {
173 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
175 write(fd, "add\n", 4);
179 while((de = readdir(d))) {
182 if (de->d_name[0] == '.')
185 if (de->d_type != DT_DIR && lvl > 0)
188 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
196 do_coldboot(d2, lvl + 1);
202 static void coldboot(const char *path) {
203 DIR *d = opendir(path);
210 static int process_config(VolumeManager *vm) {
211 std::string path(android::vold::DefaultFstabPath());
212 fstab = fs_mgr_read_fstab(path.c_str());
214 PLOG(ERROR) << "Failed to open default fstab " << path;
218 /* Loop through entries looking for ones that vold manages */
219 bool has_adoptable = false;
220 for (int i = 0; i < fstab->num_entries; i++) {
221 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
222 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
223 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
227 std::string sysPattern(fstab->recs[i].blk_device);
228 std::string nickname(fstab->recs[i].label);
231 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
232 flags |= android::vold::Disk::Flags::kAdoptable;
233 has_adoptable = true;
235 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
236 || property_get_bool("vold.debug.default_primary", false)) {
237 flags |= android::vold::Disk::Flags::kDefaultPrimary;
240 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
241 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
244 property_set("vold.has_adoptable", has_adoptable ? "1" : "0");