• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18 
19 #include "NetlinkManager.h"
20 #include "VoldNativeService.h"
21 #include "VoldUtil.h"
22 #include "VolumeManager.h"
23 #include "model/Disk.h"
24 #include "sehandle.h"
25 
26 #include <android-base/logging.h>
27 #include <android-base/properties.h>
28 #include <android-base/stringprintf.h>
29 #include <cutils/klog.h>
30 #include <hidl/HidlTransportSupport.h>
31 #include <utils/Trace.h>
32 
33 #include <dirent.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <fs_mgr.h>
37 #include <getopt.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 
44 static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
45                           bool* has_reserved);
46 static void coldboot(const char* path);
47 static void parse_args(int argc, char** argv);
48 
49 struct selabel_handle* sehandle;
50 
51 using android::base::StringPrintf;
52 using android::fs_mgr::ReadDefaultFstab;
53 
main(int argc,char ** argv)54 int main(int argc, char** argv) {
55     atrace_set_tracing_enabled(false);
56     setenv("ANDROID_LOG_TAGS", "*:d", 1);  // Do not submit with verbose logs enabled
57     android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
58 
59     LOG(INFO) << "Vold 3.0 (the awakening) firing up";
60 
61     ATRACE_BEGIN("main");
62 
63     LOG(DEBUG) << "Detected support for:"
64                << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
65                << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
66                << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
67 
68     VolumeManager* vm;
69     NetlinkManager* nm;
70 
71     parse_args(argc, argv);
72 
73     sehandle = selinux_android_file_context_handle();
74     if (sehandle) {
75         selinux_android_set_sehandle(sehandle);
76     }
77 
78     mkdir("/dev/block/vold", 0755);
79 
80     /* For when cryptfs checks and mounts an encrypted filesystem */
81     klog_set_level(6);
82 
83     /* Create our singleton managers */
84     if (!(vm = VolumeManager::Instance())) {
85         LOG(ERROR) << "Unable to create VolumeManager";
86         exit(1);
87     }
88 
89     if (!(nm = NetlinkManager::Instance())) {
90         LOG(ERROR) << "Unable to create NetlinkManager";
91         exit(1);
92     }
93 
94     if (android::base::GetBoolProperty("vold.debug", false)) {
95         vm->setDebug(true);
96     }
97 
98     if (vm->start()) {
99         PLOG(ERROR) << "Unable to start VolumeManager";
100         exit(1);
101     }
102 
103     bool has_adoptable;
104     bool has_quota;
105     bool has_reserved;
106 
107     if (process_config(vm, &has_adoptable, &has_quota, &has_reserved)) {
108         PLOG(ERROR) << "Error reading configuration... continuing anyways";
109     }
110 
111     android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
112 
113     ATRACE_BEGIN("VoldNativeService::start");
114     if (android::vold::VoldNativeService::start() != android::OK) {
115         LOG(ERROR) << "Unable to start VoldNativeService";
116         exit(1);
117     }
118     ATRACE_END();
119 
120     LOG(DEBUG) << "VoldNativeService::start() completed OK";
121 
122     ATRACE_BEGIN("NetlinkManager::start");
123     if (nm->start()) {
124         PLOG(ERROR) << "Unable to start NetlinkManager";
125         exit(1);
126     }
127     ATRACE_END();
128 
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");
134 
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");
139 
140     ATRACE_END();
141 
142     android::IPCThreadState::self()->joinThreadPool();
143     LOG(INFO) << "vold shutting down";
144 
145     exit(0);
146 }
147 
parse_args(int argc,char ** argv)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'},
154         {nullptr, 0, nullptr, 0},
155     };
156 
157     int c;
158     while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
159         switch (c) {
160             // clang-format off
161         case 'b': android::vold::sBlkidContext = optarg; break;
162         case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
163         case 'f': android::vold::sFsckContext = optarg; break;
164         case 'F': android::vold::sFsckUntrustedContext = optarg; break;
165                 // clang-format on
166         }
167     }
168 
169     CHECK(android::vold::sBlkidContext != nullptr);
170     CHECK(android::vold::sBlkidUntrustedContext != nullptr);
171     CHECK(android::vold::sFsckContext != nullptr);
172     CHECK(android::vold::sFsckUntrustedContext != nullptr);
173 }
174 
do_coldboot(DIR * d,int lvl)175 static void do_coldboot(DIR* d, int lvl) {
176     struct dirent* de;
177     int dfd, fd;
178 
179     dfd = dirfd(d);
180 
181     fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
182     if (fd >= 0) {
183         write(fd, "add\n", 4);
184         close(fd);
185     }
186 
187     while ((de = readdir(d))) {
188         DIR* d2;
189 
190         if (de->d_name[0] == '.') continue;
191 
192         if (de->d_type != DT_DIR && lvl > 0) continue;
193 
194         fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
195         if (fd < 0) continue;
196 
197         d2 = fdopendir(fd);
198         if (d2 == 0)
199             close(fd);
200         else {
201             do_coldboot(d2, lvl + 1);
202             closedir(d2);
203         }
204     }
205 }
206 
coldboot(const char * path)207 static void coldboot(const char* path) {
208     ATRACE_NAME("coldboot");
209     DIR* d = opendir(path);
210     if (d) {
211         do_coldboot(d, 0);
212         closedir(d);
213     }
214 }
215 
process_config(VolumeManager * vm,bool * has_adoptable,bool * has_quota,bool * has_reserved)216 static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
217                           bool* has_reserved) {
218     ATRACE_NAME("process_config");
219 
220     if (!ReadDefaultFstab(&fstab_default)) {
221         PLOG(ERROR) << "Failed to open default fstab";
222         return -1;
223     }
224 
225     /* Loop through entries looking for ones that vold manages */
226     *has_adoptable = false;
227     *has_quota = false;
228     *has_reserved = false;
229     for (auto& entry : fstab_default) {
230         if (entry.fs_mgr_flags.quota) {
231             *has_quota = true;
232         }
233         if (entry.reserved_size > 0) {
234             *has_reserved = true;
235         }
236 
237         /* Make sure logical partitions have an updated blk_device. */
238         if (entry.fs_mgr_flags.logical && !fs_mgr_update_logical_partition(&entry)) {
239             PLOG(FATAL) << "could not find logical partition " << entry.blk_device;
240         }
241 
242         if (entry.fs_mgr_flags.vold_managed) {
243             if (entry.fs_mgr_flags.nonremovable) {
244                 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
245                 continue;
246             }
247 
248             std::string sysPattern(entry.blk_device);
249             std::string nickname(entry.label);
250             int flags = 0;
251 
252             if (entry.is_encryptable()) {
253                 flags |= android::vold::Disk::Flags::kAdoptable;
254                 *has_adoptable = true;
255             }
256             if (entry.fs_mgr_flags.no_emulated_sd ||
257                 android::base::GetBoolProperty("vold.debug.default_primary", false)) {
258                 flags |= android::vold::Disk::Flags::kDefaultPrimary;
259             }
260 
261             vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
262                 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
263         }
264     }
265     return 0;
266 }
267