OSDN Git Service

Detect factory reset and deleteAllKeys
[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/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 typedef struct vold_configs {
45     bool has_adoptable : 1;
46     bool has_quota : 1;
47     bool has_reserved : 1;
48     bool has_compress : 1;
49 } VoldConfigs;
50
51 static int process_config(VolumeManager* vm, VoldConfigs* configs);
52 static void coldboot(const char* path);
53 static void parse_args(int argc, char** argv);
54
55 struct selabel_handle* sehandle;
56
57 using android::base::StringPrintf;
58 using android::fs_mgr::ReadDefaultFstab;
59
60 int main(int argc, char** argv) {
61     atrace_set_tracing_enabled(false);
62     setenv("ANDROID_LOG_TAGS", "*:d", 1);  // Do not submit with verbose logs enabled
63     android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
64
65     LOG(INFO) << "Vold 3.0 (the awakening) firing up";
66
67     ATRACE_BEGIN("main");
68
69     LOG(DEBUG) << "Detected support for:"
70                << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
71                << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
72                << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
73
74     VolumeManager* vm;
75     NetlinkManager* nm;
76
77     parse_args(argc, argv);
78
79     sehandle = selinux_android_file_context_handle();
80     if (sehandle) {
81         selinux_android_set_sehandle(sehandle);
82     }
83
84     mkdir("/dev/block/vold", 0755);
85
86     /* For when cryptfs checks and mounts an encrypted filesystem */
87     klog_set_level(6);
88
89     /* Create our singleton managers */
90     if (!(vm = VolumeManager::Instance())) {
91         LOG(ERROR) << "Unable to create VolumeManager";
92         exit(1);
93     }
94
95     if (!(nm = NetlinkManager::Instance())) {
96         LOG(ERROR) << "Unable to create NetlinkManager";
97         exit(1);
98     }
99
100     if (android::base::GetBoolProperty("vold.debug", false)) {
101         vm->setDebug(true);
102     }
103
104     if (vm->start()) {
105         PLOG(ERROR) << "Unable to start VolumeManager";
106         exit(1);
107     }
108
109     VoldConfigs configs = {};
110     if (process_config(vm, &configs)) {
111         PLOG(ERROR) << "Error reading configuration... continuing anyways";
112     }
113
114     android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
115
116     ATRACE_BEGIN("VoldNativeService::start");
117     if (android::vold::VoldNativeService::start() != android::OK) {
118         LOG(ERROR) << "Unable to start VoldNativeService";
119         exit(1);
120     }
121     ATRACE_END();
122
123     LOG(DEBUG) << "VoldNativeService::start() completed OK";
124
125     ATRACE_BEGIN("NetlinkManager::start");
126     if (nm->start()) {
127         PLOG(ERROR) << "Unable to start NetlinkManager";
128         exit(1);
129     }
130     ATRACE_END();
131
132     // This call should go after listeners are started to avoid
133     // a deadlock between vold and init (see b/34278978 for details)
134     android::base::SetProperty("vold.has_adoptable", configs.has_adoptable ? "1" : "0");
135     android::base::SetProperty("vold.has_quota", configs.has_quota ? "1" : "0");
136     android::base::SetProperty("vold.has_reserved", configs.has_reserved ? "1" : "0");
137     android::base::SetProperty("vold.has_compress", configs.has_compress ? "1" : "0");
138
139     // Do coldboot here so it won't block booting,
140     // also the cold boot is needed in case we have flash drive
141     // connected before Vold launched
142     coldboot("/sys/block");
143
144     ATRACE_END();
145
146     android::IPCThreadState::self()->joinThreadPool();
147     LOG(INFO) << "vold shutting down";
148
149     exit(0);
150 }
151
152 static void parse_args(int argc, char** argv) {
153     static struct option opts[] = {
154         {"blkid_context", required_argument, 0, 'b'},
155         {"blkid_untrusted_context", required_argument, 0, 'B'},
156         {"fsck_context", required_argument, 0, 'f'},
157         {"fsck_untrusted_context", required_argument, 0, 'F'},
158         {nullptr, 0, nullptr, 0},
159     };
160
161     int c;
162     while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
163         switch (c) {
164             // clang-format off
165         case 'b': android::vold::sBlkidContext = optarg; break;
166         case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
167         case 'f': android::vold::sFsckContext = optarg; break;
168         case 'F': android::vold::sFsckUntrustedContext = optarg; break;
169                 // clang-format on
170         }
171     }
172
173     CHECK(android::vold::sBlkidContext != nullptr);
174     CHECK(android::vold::sBlkidUntrustedContext != nullptr);
175     CHECK(android::vold::sFsckContext != nullptr);
176     CHECK(android::vold::sFsckUntrustedContext != nullptr);
177 }
178
179 static void do_coldboot(DIR* d, int lvl) {
180     struct dirent* de;
181     int dfd, fd;
182
183     dfd = dirfd(d);
184
185     fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
186     if (fd >= 0) {
187         write(fd, "add\n", 4);
188         close(fd);
189     }
190
191     while ((de = readdir(d))) {
192         DIR* d2;
193
194         if (de->d_name[0] == '.') continue;
195
196         if (de->d_type != DT_DIR && lvl > 0) continue;
197
198         fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
199         if (fd < 0) continue;
200
201         d2 = fdopendir(fd);
202         if (d2 == 0)
203             close(fd);
204         else {
205             do_coldboot(d2, lvl + 1);
206             closedir(d2);
207         }
208     }
209 }
210
211 static void coldboot(const char* path) {
212     ATRACE_NAME("coldboot");
213     DIR* d = opendir(path);
214     if (d) {
215         do_coldboot(d, 0);
216         closedir(d);
217     }
218 }
219
220 static int process_config(VolumeManager* vm, VoldConfigs* configs) {
221     ATRACE_NAME("process_config");
222
223     if (!ReadDefaultFstab(&fstab_default)) {
224         PLOG(ERROR) << "Failed to open default fstab";
225         return -1;
226     }
227
228     /* Loop through entries looking for ones that vold manages */
229     configs->has_adoptable = false;
230     configs->has_quota = false;
231     configs->has_reserved = false;
232     configs->has_compress = false;
233     for (auto& entry : fstab_default) {
234         if (entry.fs_mgr_flags.quota) {
235             configs->has_quota = true;
236         }
237         if (entry.reserved_size > 0) {
238             configs->has_reserved = true;
239         }
240         if (entry.fs_mgr_flags.fs_compress) {
241             configs->has_compress = true;
242         }
243
244         /* Make sure logical partitions have an updated blk_device. */
245         if (entry.fs_mgr_flags.logical && !fs_mgr_update_logical_partition(&entry) &&
246             !entry.fs_mgr_flags.no_fail) {
247             PLOG(FATAL) << "could not find logical partition " << entry.blk_device;
248         }
249
250         if (entry.fs_mgr_flags.vold_managed) {
251             if (entry.fs_mgr_flags.nonremovable) {
252                 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
253                 continue;
254             }
255
256             std::string sysPattern(entry.blk_device);
257             std::string nickname(entry.label);
258             int flags = 0;
259
260             if (entry.is_encryptable()) {
261                 flags |= android::vold::Disk::Flags::kAdoptable;
262                 configs->has_adoptable = true;
263             }
264             if (entry.fs_mgr_flags.no_emulated_sd ||
265                 android::base::GetBoolProperty("vold.debug.default_primary", false)) {
266                 flags |= android::vold::Disk::Flags::kDefaultPrimary;
267             }
268
269             vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
270                 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
271         }
272     }
273     return 0;
274 }