blob: 01a2168fa3cfc8d64a40f19a42e6fb8f78bce0bd [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 Sharkey95a92f92017-07-17 13:57:18 -060044static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota);
Wei Wang2edbe282017-03-06 17:27:05 -080045static void coldboot(const char *path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070046static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070047
Stephen Smalley684e6622014-09-30 10:29:24 -040048struct selabel_handle *sehandle;
49
Jeff Sharkey36801cc2015-03-13 16:09:20 -070050using android::base::StringPrintf;
51
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070052int main(int argc, char** argv) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070053 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Jeff Sharkey5bad3782015-04-18 16:15:10 -070054 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
San Mehatf1b736b2009-10-10 17:22:08 -070055
Jeff Sharkey67b8c492017-09-21 17:08:43 -060056 ATRACE_BEGIN("main");
57
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070058 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
59
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070060 LOG(VERBOSE) << "Detected support for:"
61 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
62 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
63 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
64
San Mehatf1b736b2009-10-10 17:22:08 -070065 VolumeManager *vm;
San Mehatf1b736b2009-10-10 17:22:08 -070066 NetlinkManager *nm;
67
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070068 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080069
Stephen Smalley684e6622014-09-30 10:29:24 -040070 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070071 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040072 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070073 }
Stephen Smalley684e6622014-09-30 10:29:24 -040074
San Mehata2677e42009-12-13 10:40:18 -080075 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070076
Ken Sumrallb9738ea2013-03-19 19:42:30 -070077 /* For when cryptfs checks and mounts an encrypted filesystem */
78 klog_set_level(6);
79
San Mehatf1b736b2009-10-10 17:22:08 -070080 /* Create our singleton managers */
81 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070082 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070083 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070084 }
San Mehatf1b736b2009-10-10 17:22:08 -070085
86 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070087 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070088 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070089 }
San Mehata2677e42009-12-13 10:40:18 -080090
Jeff Sharkey3472e522017-10-06 18:02:53 -060091 if (android::base::GetBoolProperty("vold.debug", false)) {
Jeff Sharkeyb0667872015-04-29 08:57:18 -070092 vm->setDebug(true);
93 }
94
San Mehatf1b736b2009-10-10 17:22:08 -070095 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070096 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070097 exit(1);
98 }
99
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800100 bool has_adoptable;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600101 bool has_quota;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800102
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600103 if (process_config(vm, &has_adoptable, &has_quota)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700104 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700105 }
106
Paul Crowley31888052017-09-27 10:56:54 -0700107 ATRACE_BEGIN("VoldNativeService::start");
108 if (android::vold::VoldNativeService::start() != android::OK) {
109 LOG(ERROR) << "Unable to start VoldNativeService";
110 exit(1);
111 }
112 ATRACE_END();
113
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600114 ATRACE_BEGIN("NetlinkManager::start");
San Mehatf1b736b2009-10-10 17:22:08 -0700115 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700116 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700117 exit(1);
118 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600119 ATRACE_END();
San Mehatf1b736b2009-10-10 17:22:08 -0700120
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800121 // This call should go after listeners are started to avoid
122 // a deadlock between vold and init (see b/34278978 for details)
Jeff Sharkey3472e522017-10-06 18:02:53 -0600123 android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0");
124 android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0");
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800125
Wei Wang2edbe282017-03-06 17:27:05 -0800126 // Do coldboot here so it won't block booting,
127 // also the cold boot is needed in case we have flash drive
128 // connected before Vold launched
129 coldboot("/sys/block");
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600130
131 ATRACE_END();
132
Jeff Sharkey93396c12017-10-18 16:52:48 -0600133 android::IPCThreadState::self()->joinThreadPool();
134 LOG(INFO) << "vold shutting down";
San Mehatf1b736b2009-10-10 17:22:08 -0700135
San Mehatf1b736b2009-10-10 17:22:08 -0700136 exit(0);
137}
138
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700139static void parse_args(int argc, char** argv) {
140 static struct option opts[] = {
141 {"blkid_context", required_argument, 0, 'b' },
142 {"blkid_untrusted_context", required_argument, 0, 'B' },
143 {"fsck_context", required_argument, 0, 'f' },
144 {"fsck_untrusted_context", required_argument, 0, 'F' },
145 };
146
147 int c;
148 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
149 switch (c) {
150 case 'b': android::vold::sBlkidContext = optarg; break;
151 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
152 case 'f': android::vold::sFsckContext = optarg; break;
153 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
154 }
155 }
156
157 CHECK(android::vold::sBlkidContext != nullptr);
158 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
159 CHECK(android::vold::sFsckContext != nullptr);
160 CHECK(android::vold::sFsckUntrustedContext != nullptr);
161}
162
Wei Wang2edbe282017-03-06 17:27:05 -0800163static void do_coldboot(DIR *d, int lvl) {
164 struct dirent *de;
165 int dfd, fd;
166
167 dfd = dirfd(d);
168
169 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
170 if(fd >= 0) {
171 write(fd, "add\n", 4);
172 close(fd);
173 }
174
175 while((de = readdir(d))) {
176 DIR *d2;
177
178 if (de->d_name[0] == '.')
179 continue;
180
181 if (de->d_type != DT_DIR && lvl > 0)
182 continue;
183
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600184 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Wei Wang2edbe282017-03-06 17:27:05 -0800185 if(fd < 0)
186 continue;
187
188 d2 = fdopendir(fd);
189 if(d2 == 0)
190 close(fd);
191 else {
192 do_coldboot(d2, lvl + 1);
193 closedir(d2);
194 }
195 }
196}
197
198static void coldboot(const char *path) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600199 ATRACE_NAME("coldboot");
Wei Wang2edbe282017-03-06 17:27:05 -0800200 DIR *d = opendir(path);
201 if(d) {
202 do_coldboot(d, 0);
203 closedir(d);
204 }
205}
206
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600207static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600208 ATRACE_NAME("process_config");
209
Paul Crowleye2ee1522017-09-26 14:05:26 -0700210 fstab_default = fs_mgr_read_fstab_default();
211 if (!fstab_default) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800212 PLOG(ERROR) << "Failed to open default fstab";
San Mehatf1b736b2009-10-10 17:22:08 -0700213 return -1;
214 }
215
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800216 /* Loop through entries looking for ones that vold manages */
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800217 *has_adoptable = false;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600218 *has_quota = false;
Paul Crowleye2ee1522017-09-26 14:05:26 -0700219 for (int i = 0; i < fstab_default->num_entries; i++) {
220 auto rec = &fstab_default->recs[i];
221 if (fs_mgr_is_quota(rec)) {
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600222 *has_quota = true;
223 }
224
Paul Crowleye2ee1522017-09-26 14:05:26 -0700225 if (fs_mgr_is_voldmanaged(rec)) {
226 if (fs_mgr_is_nonremovable(rec)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
228 continue;
San Mehata2677e42009-12-13 10:40:18 -0800229 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230
Paul Crowleye2ee1522017-09-26 14:05:26 -0700231 std::string sysPattern(rec->blk_device);
232 std::string nickname(rec->label);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700233 int flags = 0;
234
Paul Crowleye2ee1522017-09-26 14:05:26 -0700235 if (fs_mgr_is_encryptable(rec)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236 flags |= android::vold::Disk::Flags::kAdoptable;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800237 *has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700238 }
Paul Crowleye2ee1522017-09-26 14:05:26 -0700239 if (fs_mgr_is_noemulatedsd(rec)
Jeff Sharkey3472e522017-10-06 18:02:53 -0600240 || android::base::GetBoolProperty("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700242 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700243
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700244 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
245 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700246 }
247 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700248 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700249}