blob: f317f3d26b6dfe95c439d3b2a7c3aa21d495dcc1 [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"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070023#include "cryptfs.h"
24#include "sehandle.h"
25
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/logging.h>
27#include <android-base/stringprintf.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070028#include <cutils/klog.h>
29#include <cutils/properties.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060030#include <utils/Trace.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070031
San Mehatf1b736b2009-10-10 17:22:08 -070032#include <stdio.h>
33#include <stdlib.h>
34#include <errno.h>
35#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070036#include <sys/stat.h>
37#include <sys/types.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070038#include <getopt.h>
San Mehat3578c412009-10-12 14:51:52 -070039#include <fcntl.h>
Wei Wang2edbe282017-03-06 17:27:05 -080040#include <dirent.h>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080041#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070042
Jeff Sharkey95a92f92017-07-17 13:57:18 -060043static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota);
Wei Wang2edbe282017-03-06 17:27:05 -080044static void coldboot(const char *path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070045static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070046
Ken Sumrall56ad03c2013-02-13 13:00:19 -080047struct fstab *fstab;
48
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) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070054 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Jeff Sharkey5bad3782015-04-18 16:15:10 -070055 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
San Mehatf1b736b2009-10-10 17:22:08 -070056
Jeff Sharkey67b8c492017-09-21 17:08:43 -060057 ATRACE_BEGIN("main");
58
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070059 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
60
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070061 LOG(VERBOSE) << "Detected support for:"
62 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
63 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
64 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
65
San Mehatf1b736b2009-10-10 17:22:08 -070066 VolumeManager *vm;
San Mehatf1b736b2009-10-10 17:22:08 -070067 NetlinkManager *nm;
68
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070069 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080070
Stephen Smalley684e6622014-09-30 10:29:24 -040071 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070072 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040073 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070074 }
Stephen Smalley684e6622014-09-30 10:29:24 -040075
San Mehata2677e42009-12-13 10:40:18 -080076 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070077
Ken Sumrallb9738ea2013-03-19 19:42:30 -070078 /* For when cryptfs checks and mounts an encrypted filesystem */
79 klog_set_level(6);
80
San Mehatf1b736b2009-10-10 17:22:08 -070081 /* Create our singleton managers */
82 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070083 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070084 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070085 }
San Mehatf1b736b2009-10-10 17:22:08 -070086
87 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070088 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070089 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070090 }
San Mehata2677e42009-12-13 10:40:18 -080091
Jeff Sharkeyb0667872015-04-29 08:57:18 -070092 if (property_get_bool("vold.debug", false)) {
93 vm->setDebug(true);
94 }
95
San Mehatf1b736b2009-10-10 17:22:08 -070096 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070097 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070098 exit(1);
99 }
100
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600101 ATRACE_BEGIN("VoldNativeService::start");
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600102 if (android::vold::VoldNativeService::start() != android::OK) {
103 LOG(ERROR) << "Unable to start VoldNativeService";
104 exit(1);
105 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600106 ATRACE_END();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600107
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800108 bool has_adoptable;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600109 bool has_quota;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800110
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600111 if (process_config(vm, &has_adoptable, &has_quota)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700112 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700113 }
114
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600115 ATRACE_BEGIN("NetlinkManager::start");
San Mehatf1b736b2009-10-10 17:22:08 -0700116 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700117 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700118 exit(1);
119 }
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600120 ATRACE_END();
San Mehatf1b736b2009-10-10 17:22:08 -0700121
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800122 // This call should go after listeners are started to avoid
123 // a deadlock between vold and init (see b/34278978 for details)
124 property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600125 property_set("vold.has_quota", has_quota ? "1" : "0");
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800126
Wei Wang2edbe282017-03-06 17:27:05 -0800127 // Do coldboot here so it won't block booting,
128 // also the cold boot is needed in case we have flash drive
129 // connected before Vold launched
130 coldboot("/sys/block");
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600131
132 ATRACE_END();
133
San Mehatf1b736b2009-10-10 17:22:08 -0700134 // Eventually we'll become the monitoring thread
135 while(1) {
Wei Wang6b455c22017-01-20 11:52:33 -0800136 pause();
San Mehatf1b736b2009-10-10 17:22:08 -0700137 }
138
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700139 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700140 exit(0);
141}
142
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700143static void parse_args(int argc, char** argv) {
144 static struct option opts[] = {
145 {"blkid_context", required_argument, 0, 'b' },
146 {"blkid_untrusted_context", required_argument, 0, 'B' },
147 {"fsck_context", required_argument, 0, 'f' },
148 {"fsck_untrusted_context", required_argument, 0, 'F' },
149 };
150
151 int c;
152 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
153 switch (c) {
154 case 'b': android::vold::sBlkidContext = optarg; break;
155 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
156 case 'f': android::vold::sFsckContext = optarg; break;
157 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
158 }
159 }
160
161 CHECK(android::vold::sBlkidContext != nullptr);
162 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
163 CHECK(android::vold::sFsckContext != nullptr);
164 CHECK(android::vold::sFsckUntrustedContext != nullptr);
165}
166
Wei Wang2edbe282017-03-06 17:27:05 -0800167static void do_coldboot(DIR *d, int lvl) {
168 struct dirent *de;
169 int dfd, fd;
170
171 dfd = dirfd(d);
172
173 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
174 if(fd >= 0) {
175 write(fd, "add\n", 4);
176 close(fd);
177 }
178
179 while((de = readdir(d))) {
180 DIR *d2;
181
182 if (de->d_name[0] == '.')
183 continue;
184
185 if (de->d_type != DT_DIR && lvl > 0)
186 continue;
187
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600188 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Wei Wang2edbe282017-03-06 17:27:05 -0800189 if(fd < 0)
190 continue;
191
192 d2 = fdopendir(fd);
193 if(d2 == 0)
194 close(fd);
195 else {
196 do_coldboot(d2, lvl + 1);
197 closedir(d2);
198 }
199 }
200}
201
202static void coldboot(const char *path) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600203 ATRACE_NAME("coldboot");
Wei Wang2edbe282017-03-06 17:27:05 -0800204 DIR *d = opendir(path);
205 if(d) {
206 do_coldboot(d, 0);
207 closedir(d);
208 }
209}
210
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600211static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota) {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600212 ATRACE_NAME("process_config");
213
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800214 fstab = fs_mgr_read_fstab_default();
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800215 if (!fstab) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800216 PLOG(ERROR) << "Failed to open default fstab";
San Mehatf1b736b2009-10-10 17:22:08 -0700217 return -1;
218 }
219
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800220 /* Loop through entries looking for ones that vold manages */
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800221 *has_adoptable = false;
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600222 *has_quota = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223 for (int i = 0; i < fstab->num_entries; i++) {
Jeff Sharkey95a92f92017-07-17 13:57:18 -0600224 if (fs_mgr_is_quota(&fstab->recs[i])) {
225 *has_quota = true;
226 }
227
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800228 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800229 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
231 continue;
San Mehata2677e42009-12-13 10:40:18 -0800232 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700233
234 std::string sysPattern(fstab->recs[i].blk_device);
235 std::string nickname(fstab->recs[i].label);
236 int flags = 0;
237
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800238 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239 flags |= android::vold::Disk::Flags::kAdoptable;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800240 *has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700241 }
Jeff Sharkey65427f12015-05-19 15:54:15 -0700242 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
243 || property_get_bool("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700244 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700245 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700246
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700247 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
248 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700249 }
250 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700251 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700252}