OSDN Git Service

vold: Accept Linux GPT partitions on external SD cards
[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 "model/Disk.h"
20 #include "VolumeManager.h"
21 #include "NetlinkManager.h"
22 #include "VoldNativeService.h"
23 #include "VoldUtil.h"
24 #include "cryptfs.h"
25 #include "sehandle.h"
26
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>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <getopt.h>
40 #include <fcntl.h>
41 #include <dirent.h>
42 #include <fs_mgr.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
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));
57
58     LOG(INFO) << "Vold 3.0 (the awakening) firing up";
59
60     ATRACE_BEGIN("main");
61
62
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" : "");
69
70     VolumeManager *vm;
71     NetlinkManager *nm;
72
73     parse_args(argc, argv);
74
75     sehandle = selinux_android_file_context_handle();
76     if (sehandle) {
77         selinux_android_set_sehandle(sehandle);
78     }
79
80     mkdir("/dev/block/vold", 0755);
81
82     /* For when cryptfs checks and mounts an encrypted filesystem */
83     klog_set_level(6);
84
85     /* Create our singleton managers */
86     if (!(vm = VolumeManager::Instance())) {
87         LOG(ERROR) << "Unable to create VolumeManager";
88         exit(1);
89     }
90
91     if (!(nm = NetlinkManager::Instance())) {
92         LOG(ERROR) << "Unable to create NetlinkManager";
93         exit(1);
94     }
95
96     if (android::base::GetBoolProperty("vold.debug", false)) {
97         vm->setDebug(true);
98     }
99
100     if (vm->start()) {
101         PLOG(ERROR) << "Unable to start VolumeManager";
102         exit(1);
103     }
104
105     bool has_adoptable;
106     bool has_quota;
107     bool has_reserved;
108
109     if (process_config(vm, &has_adoptable, &has_quota, &has_reserved)) {
110         PLOG(ERROR) << "Error reading configuration... continuing anyways";
111     }
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
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     };
155
156     int c;
157     while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
158         switch (c) {
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;
163         }
164     }
165
166     CHECK(android::vold::sBlkidContext != nullptr);
167     CHECK(android::vold::sBlkidUntrustedContext != nullptr);
168     CHECK(android::vold::sFsckContext != nullptr);
169     CHECK(android::vold::sFsckUntrustedContext != nullptr);
170 }
171
172 static void do_coldboot(DIR *d, int lvl) {
173     struct dirent *de;
174     int dfd, fd;
175
176     dfd = dirfd(d);
177
178     fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
179     if(fd >= 0) {
180         write(fd, "add\n", 4);
181         close(fd);
182     }
183
184     while((de = readdir(d))) {
185         DIR *d2;
186
187         if (de->d_name[0] == '.')
188             continue;
189
190         if (de->d_type != DT_DIR && lvl > 0)
191             continue;
192
193         fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
194         if(fd < 0)
195             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
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
216 static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
217                           bool* has_reserved) {
218     ATRACE_NAME("process_config");
219
220     fstab_default = fs_mgr_read_fstab_default();
221     if (!fstab_default) {
222         PLOG(ERROR) << "Failed to open default fstab";
223         return -1;
224     }
225
226     /* Loop through entries looking for ones that vold manages */
227     *has_adoptable = false;
228     *has_quota = 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)) {
233             *has_quota = true;
234         }
235         if (rec->reserved_size > 0) {
236             *has_reserved = true;
237         }
238
239         if (fs_mgr_is_voldmanaged(rec)) {
240             std::string sysPattern(rec->blk_device);
241             std::string fstype;
242             if (rec->fs_type) {
243                 fstype = rec->fs_type;
244             }
245             std::string mntopts;
246             if (rec->fs_options) {
247                 mntopts = rec->fs_options;
248             }
249             std::string nickname(rec->label);
250             int partnum = rec->partnum;
251             int flags = 0;
252
253             if (fs_mgr_is_encryptable(rec)) {
254                 flags |= android::vold::Disk::Flags::kAdoptable;
255                 *has_adoptable = true;
256             }
257             if (fs_mgr_is_noemulatedsd(rec)
258                     || android::base::GetBoolProperty("vold.debug.default_primary", false)) {
259                 flags |= android::vold::Disk::Flags::kDefaultPrimary;
260             }
261             if (fs_mgr_is_nonremovable(rec)) {
262                 flags |= android::vold::Disk::Flags::kNonRemovable;
263             }
264
265             vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
266                     new VolumeManager::DiskSource(sysPattern, nickname, partnum, flags,
267                                     fstype, mntopts)));
268         }
269     }
270     return 0;
271 }