blob: 7418aaf50d096ed2c49141e4906b1402a4c33a9c [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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
Jeff Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Jeff Sharkey11c2d382017-09-11 10:32:01 -060019#include "model/Disk.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070020#include "VolumeManager.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070021#include "NetlinkManager.h"
Jeff Sharkey068c6be2017-09-06 13:47:40 -060022#include "VoldNativeService.h"
Paul Crowleye2ee1522017-09-26 14:05:26 -070023#include "VoldUtil.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070024#include "cryptfs.h"
25#include "sehandle.h"
26
Elliott Hughes7e128fb2015-12-04 15:50:53 -080027#include <android-base/logging.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060028#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080029#include <android-base/stringprintf.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070030#include <cutils/klog.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060031#include <utils/Trace.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070032
San Mehatf1b736b2009-10-10 17:22:08 -070033#include <stdio.h>
34#include <stdlib.h>
35#include <errno.h>
36#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070037#include <sys/stat.h>
38#include <sys/types.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070039#include <getopt.h>
San Mehat3578c412009-10-12 14:51:52 -070040#include <fcntl.h>
Wei Wang2edbe282017-03-06 17:27:05 -080041#include <dirent.h>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080042#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070043
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -070044static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
45 bool* has_reserved);
Wei Wang2edbe282017-03-06 17:27:05 -080046static void coldboot(const char *path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070047static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070048
Stephen Smalley684e6622014-09-30 10:29:24 -040049struct selabel_handle *sehandle;
50
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051using android::base::StringPrintf;
52
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070053int main(int argc, char** argv) {
Paul Crowley0fd26262018-01-30 09:48:19 -080054 atrace_set_tracing_enabled(false);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070055 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Jeff Sharkey5bad3782015-04-18 16:15:10 -070056 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
San Mehatf1b736b2009-10-10 17:22:08 -070057
Paul Crowley0fd26262018-01-30 09:48:19 -080058 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
59
Jeff Sharkey67b8c492017-09-21 17:08:43 -060060 ATRACE_BEGIN("main");
61
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070062
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070063 LOG(VERBOSE) << "Detected support for:"
Dan Pasanend1952662015-10-27 22:52:37 -050064 << (android::vold::IsFilesystemSupported("exfat") ? " exfat" : "")
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070065 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
66 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
Dan Pasanend1952662015-10-27 22:52:37 -050067 << (android::vold::IsFilesystemSupported("ntfs") ? " ntfs" : "")
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070068 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
69
San Mehatf1b736b2009-10-10 17:22:08 -070070 VolumeManager *vm;
San Mehatf1b736b2009-10-10 17:22:08 -070071 NetlinkManager *nm;
72
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070073 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080074
Stephen Smalley684e6622014-09-30 10:29:24 -040075 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070076 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040077 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070078 }
Stephen Smalley684e6622014-09-30 10:29:24 -040079
San Mehata2677e42009-12-13 10:40:18 -080080 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070081
Ken Sumrallb9738ea2013-03-19 19:42:30 -070082 /* For when cryptfs checks and mounts an encrypted filesystem */
83 klog_set_level(6);
84
San Mehatf1b736b2009-10-10 17:22:08 -070085 /* Create our singleton managers */
86 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070087 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070088 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070089 }
San Mehatf1b736b2009-10-10 17:22:08 -070090
91 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070092 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070093 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070094 }
San Mehata2677e42009-12-13 10:40:18 -080095
Jeff Sharkey3472e522017-10-06 18:02:53 -060096 if (android::base::GetBoolProperty("vold.debug", false)) {
Jeff Sharkeyb0667872015-04-29 08:57:18 -070097 vm->setDebug(true);
98 }
99
San Mehatf1b736b2009-10-10 17:22:08 -0700100 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700101 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700102 exit(1);
103 }
104
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800105 bool has_adoptable;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600106 bool has_quota;
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700107 bool has_reserved;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800108
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700109 if (process_config(vm, &has_adoptable, &has_quota, &has_reserved)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700110 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700111 }
112
Paul Crowley31888052017-09-27 10:56:54 -0700113 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
Paul Crowley0fd26262018-01-30 09:48:19 -0800120 LOG(DEBUG) << "VoldNativeService::start() completed OK";
121
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600122 ATRACE_BEGIN("NetlinkManager::start");
San Mehatf1b736b2009-10-10 17:22:08 -0700123 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700124 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700125 exit(1);
126 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600127 ATRACE_END();
San Mehatf1b736b2009-10-10 17:22:08 -0700128
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800129 // This call should go after listeners are started to avoid
130 // a deadlock between vold and init (see b/34278978 for details)
Jeff Sharkey3472e522017-10-06 18:02:53 -0600131 android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0");
132 android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0");
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700133 android::base::SetProperty("vold.has_reserved", has_reserved ? "1" : "0");
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800134
Wei Wang2edbe282017-03-06 17:27:05 -0800135 // 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");
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600139
140 ATRACE_END();
141
Jeff Sharkey93396c12017-10-18 16:52:48 -0600142 android::IPCThreadState::self()->joinThreadPool();
143 LOG(INFO) << "vold shutting down";
San Mehatf1b736b2009-10-10 17:22:08 -0700144
San Mehatf1b736b2009-10-10 17:22:08 -0700145 exit(0);
146}
147
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700148static 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
Wei Wang2edbe282017-03-06 17:27:05 -0800172static 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
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600193 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Wei Wang2edbe282017-03-06 17:27:05 -0800194 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
207static void coldboot(const char *path) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600208 ATRACE_NAME("coldboot");
Wei Wang2edbe282017-03-06 17:27:05 -0800209 DIR *d = opendir(path);
210 if(d) {
211 do_coldboot(d, 0);
212 closedir(d);
213 }
214}
215
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700216static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
217 bool* has_reserved) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600218 ATRACE_NAME("process_config");
219
Paul Crowleye2ee1522017-09-26 14:05:26 -0700220 fstab_default = fs_mgr_read_fstab_default();
221 if (!fstab_default) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800222 PLOG(ERROR) << "Failed to open default fstab";
San Mehatf1b736b2009-10-10 17:22:08 -0700223 return -1;
224 }
225
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800226 /* Loop through entries looking for ones that vold manages */
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800227 *has_adoptable = false;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600228 *has_quota = false;
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700229 *has_reserved = false;
Paul Crowleye2ee1522017-09-26 14:05:26 -0700230 for (int i = 0; i < fstab_default->num_entries; i++) {
231 auto rec = &fstab_default->recs[i];
232 if (fs_mgr_is_quota(rec)) {
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600233 *has_quota = true;
234 }
Jeff Sharkey53d5d7c2018-01-08 10:43:00 -0700235 if (rec->reserved_size > 0) {
236 *has_reserved = true;
237 }
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600238
Paul Crowleye2ee1522017-09-26 14:05:26 -0700239 if (fs_mgr_is_voldmanaged(rec)) {
Paul Crowleye2ee1522017-09-26 14:05:26 -0700240 std::string sysPattern(rec->blk_device);
Tom Marshalld18795d2015-11-05 11:20:54 -0800241 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 }
Paul Crowleye2ee1522017-09-26 14:05:26 -0700249 std::string nickname(rec->label);
Tom Marshallaf693242015-11-04 15:44:44 -0800250 int partnum = rec->partnum;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700251 int flags = 0;
252
Paul Crowleye2ee1522017-09-26 14:05:26 -0700253 if (fs_mgr_is_encryptable(rec)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700254 flags |= android::vold::Disk::Flags::kAdoptable;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800255 *has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700256 }
Paul Crowleye2ee1522017-09-26 14:05:26 -0700257 if (fs_mgr_is_noemulatedsd(rec)
Jeff Sharkey3472e522017-10-06 18:02:53 -0600258 || android::base::GetBoolProperty("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700259 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700260 }
Tom Marshallaf693242015-11-04 15:44:44 -0800261 if (fs_mgr_is_nonremovable(rec)) {
262 flags |= android::vold::Disk::Flags::kNonRemovable;
263 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700264
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700265 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
Tom Marshalld18795d2015-11-05 11:20:54 -0800266 new VolumeManager::DiskSource(sysPattern, nickname, partnum, flags,
267 fstype, mntopts)));
San Mehatf1b736b2009-10-10 17:22:08 -0700268 }
269 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700270 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700271}