OSDN Git Service

vold3: support the old SDCARD=xxx function
[android-x86/system-vold.git] / main.cpp
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/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>
33
34 #include <dirent.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <fs_mgr.h>
38 #include <getopt.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44
45 typedef struct vold_configs {
46     bool has_adoptable : 1;
47     bool has_quota : 1;
48     bool has_reserved : 1;
49     bool has_compress : 1;
50 } VoldConfigs;
51
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);
55
56 struct selabel_handle* sehandle;
57
58 using android::base::StringPrintf;
59 using android::fs_mgr::ReadDefaultFstab;
60
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));
65
66     LOG(INFO) << "Vold 3.0 (the awakening) firing up";
67
68     ATRACE_BEGIN("main");
69
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" : "");
76
77     VolumeManager* vm;
78     NetlinkManager* nm;
79
80     parse_args(argc, argv);
81
82     sehandle = selinux_android_file_context_handle();
83     if (sehandle) {
84         selinux_android_set_sehandle(sehandle);
85     }
86
87     mkdir("/dev/block/vold", 0755);
88
89     /* For when cryptfs checks and mounts an encrypted filesystem */
90     klog_set_level(6);
91
92     /* Create our singleton managers */
93     if (!(vm = VolumeManager::Instance())) {
94         LOG(ERROR) << "Unable to create VolumeManager";
95         exit(1);
96     }
97
98     if (!(nm = NetlinkManager::Instance())) {
99         LOG(ERROR) << "Unable to create NetlinkManager";
100         exit(1);
101     }
102
103     if (android::base::GetBoolProperty("vold.debug", false)) {
104         vm->setDebug(true);
105     }
106
107     if (vm->start()) {
108         PLOG(ERROR) << "Unable to start VolumeManager";
109         exit(1);
110     }
111
112     VoldConfigs configs = {};
113     if (process_config(vm, &configs)) {
114         PLOG(ERROR) << "Error reading configuration... continuing anyways";
115     }
116
117     android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
118
119     ATRACE_BEGIN("VoldNativeService::start");
120     if (android::vold::VoldNativeService::start() != android::OK) {
121         LOG(ERROR) << "Unable to start VoldNativeService";
122         exit(1);
123     }
124     ATRACE_END();
125
126     LOG(DEBUG) << "VoldNativeService::start() completed OK";
127
128     ATRACE_BEGIN("NetlinkManager::start");
129     if (nm->start()) {
130         PLOG(ERROR) << "Unable to start NetlinkManager";
131         exit(1);
132     }
133     ATRACE_END();
134
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");
141
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");
146
147     ATRACE_END();
148
149     android::IPCThreadState::self()->joinThreadPool();
150     LOG(INFO) << "vold shutting down";
151
152     exit(0);
153 }
154
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},
162     };
163
164     int c;
165     while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
166         switch (c) {
167             // clang-format off
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;
172                 // clang-format on
173         }
174     }
175
176     CHECK(android::vold::sBlkidContext != nullptr);
177     CHECK(android::vold::sBlkidUntrustedContext != nullptr);
178     CHECK(android::vold::sFsckContext != nullptr);
179     CHECK(android::vold::sFsckUntrustedContext != nullptr);
180 }
181
182 static void do_coldboot(DIR* d, int lvl) {
183     struct dirent* de;
184     int dfd, fd;
185
186     dfd = dirfd(d);
187
188     fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
189     if (fd >= 0) {
190         write(fd, "add\n", 4);
191         close(fd);
192     }
193
194     while ((de = readdir(d))) {
195         DIR* d2;
196
197         if (de->d_name[0] == '.') continue;
198
199         if (de->d_type != DT_DIR && lvl > 0) continue;
200
201         fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
202         if (fd < 0) continue;
203
204         d2 = fdopendir(fd);
205         if (d2 == 0)
206             close(fd);
207         else {
208             do_coldboot(d2, lvl + 1);
209             closedir(d2);
210         }
211     }
212 }
213
214 static void coldboot(const char* path) {
215     ATRACE_NAME("coldboot");
216     DIR* d = opendir(path);
217     if (d) {
218         do_coldboot(d, 0);
219         closedir(d);
220     }
221 }
222
223 static int process_config(VolumeManager* vm, VoldConfigs* configs) {
224     ATRACE_NAME("process_config");
225
226     if (!ReadDefaultFstab(&fstab_default)) {
227         PLOG(ERROR) << "Failed to open default fstab";
228         return -1;
229     }
230
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;
239         }
240         if (entry.reserved_size > 0) {
241             configs->has_reserved = true;
242         }
243         if (entry.fs_mgr_flags.fs_compress) {
244             configs->has_compress = true;
245         }
246
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;
251         }
252
253         if (entry.fs_mgr_flags.vold_managed) {
254             std::string sysPattern(entry.blk_device);
255             std::string nickname(entry.label);
256             int flags = 0;
257
258             if (entry.is_encryptable()) {
259                 flags |= android::vold::Disk::Flags::kAdoptable;
260                 configs->has_adoptable = true;
261             }
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;
265             }
266             if (entry.fs_mgr_flags.nonremovable) {
267                 flags |= android::vold::Disk::Flags::kNonRemovable;
268             }
269
270             vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
271                 new VolumeManager::DiskSource(sysPattern, nickname, entry.partnum,
272                                 flags, entry.fs_type, entry.fs_options)));
273         }
274     }
275
276     std::string cmdline;
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()) {
283                 int partnum = -1;
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);
293                         }
294                     }
295                 }
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;
301             }
302         }
303     }
304
305     return 0;
306 }