blob: a49386f1ed9cf99f8b353851cd35a8bb95ed79d5 [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 Sharkey9f18fe72015-04-01 23:32:18 -070017#include "Disk.h"
18#include "VolumeManager.h"
19#include "CommandListener.h"
Paul Lawrenced0b42952015-06-03 14:19:51 -070020#include "CryptCommandListener.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070021#include "NetlinkManager.h"
22#include "cryptfs.h"
23#include "sehandle.h"
24
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070027#include <cutils/klog.h>
28#include <cutils/properties.h>
29#include <cutils/sockets.h>
30
San Mehatf1b736b2009-10-10 17:22:08 -070031#include <stdio.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070035#include <sys/stat.h>
36#include <sys/types.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070037#include <getopt.h>
San Mehat3578c412009-10-12 14:51:52 -070038#include <fcntl.h>
Wei Wang2edbe282017-03-06 17:27:05 -080039#include <dirent.h>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080040#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070041
Dimitry Ivanovc976e732017-01-19 12:48:27 -080042static int process_config(VolumeManager *vm, bool* has_adoptable);
Wei Wang2edbe282017-03-06 17:27:05 -080043static void coldboot(const char *path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070044static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070045
Ken Sumrall56ad03c2013-02-13 13:00:19 -080046struct fstab *fstab;
47
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 Sharkey9f18fe72015-04-01 23:32:18 -070056 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
57
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070058 LOG(VERBOSE) << "Detected support for:"
Dan Pasanend2509612015-10-27 22:52:37 -050059 << (android::vold::IsFilesystemSupported("exfat") ? " exfat" : "")
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070060 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
61 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
Dan Pasanend2509612015-10-27 22:52:37 -050062 << (android::vold::IsFilesystemSupported("ntfs") ? " ntfs" : "")
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070063 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
64
San Mehatf1b736b2009-10-10 17:22:08 -070065 VolumeManager *vm;
66 CommandListener *cl;
Paul Lawrenced0b42952015-06-03 14:19:51 -070067 CryptCommandListener *ccl;
San Mehatf1b736b2009-10-10 17:22:08 -070068 NetlinkManager *nm;
69
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070070 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080071
Stephen Smalley684e6622014-09-30 10:29:24 -040072 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070073 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040074 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070075 }
Stephen Smalley684e6622014-09-30 10:29:24 -040076
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070077 // Quickly throw a CLOEXEC on the socket we just inherited from init
78 fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
Paul Lawrenced0b42952015-06-03 14:19:51 -070079 fcntl(android_get_control_socket("cryptd"), F_SETFD, FD_CLOEXEC);
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070080
San Mehata2677e42009-12-13 10:40:18 -080081 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070082
Ken Sumrallb9738ea2013-03-19 19:42:30 -070083 /* For when cryptfs checks and mounts an encrypted filesystem */
84 klog_set_level(6);
85
San Mehatf1b736b2009-10-10 17:22:08 -070086 /* Create our singleton managers */
87 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070088 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070089 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070090 }
San Mehatf1b736b2009-10-10 17:22:08 -070091
92 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070093 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070094 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070095 }
San Mehata2677e42009-12-13 10:40:18 -080096
Jeff Sharkeyb0667872015-04-29 08:57:18 -070097 if (property_get_bool("vold.debug", false)) {
98 vm->setDebug(true);
99 }
100
San Mehatf1b736b2009-10-10 17:22:08 -0700101 cl = new CommandListener();
Paul Lawrenced0b42952015-06-03 14:19:51 -0700102 ccl = new CryptCommandListener();
San Mehatf1b736b2009-10-10 17:22:08 -0700103 vm->setBroadcaster((SocketListener *) cl);
104 nm->setBroadcaster((SocketListener *) cl);
105
106 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700107 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700108 exit(1);
109 }
110
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800111 bool has_adoptable;
112
113 if (process_config(vm, &has_adoptable)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700114 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700115 }
116
117 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700118 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700119 exit(1);
120 }
121
122 /*
123 * Now that we're up, we can respond to commands
124 */
125 if (cl->startListener()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700126 PLOG(ERROR) << "Unable to start CommandListener";
San Mehatf1b736b2009-10-10 17:22:08 -0700127 exit(1);
128 }
129
Paul Lawrenced0b42952015-06-03 14:19:51 -0700130 if (ccl->startListener()) {
131 PLOG(ERROR) << "Unable to start CryptCommandListener";
132 exit(1);
133 }
134
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800135 // This call should go after listeners are started to avoid
136 // a deadlock between vold and init (see b/34278978 for details)
137 property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
138
Wei Wang2edbe282017-03-06 17:27:05 -0800139 // 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");
San Mehatf1b736b2009-10-10 17:22:08 -0700143 // Eventually we'll become the monitoring thread
144 while(1) {
Wei Wang6b455c22017-01-20 11:52:33 -0800145 pause();
San Mehatf1b736b2009-10-10 17:22:08 -0700146 }
147
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700148 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700149 exit(0);
150}
151
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700152static 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 };
159
160 int c;
161 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
162 switch (c) {
163 case 'b': android::vold::sBlkidContext = optarg; break;
164 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
165 case 'f': android::vold::sFsckContext = optarg; break;
166 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
167 }
168 }
169
170 CHECK(android::vold::sBlkidContext != nullptr);
171 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
172 CHECK(android::vold::sFsckContext != nullptr);
173 CHECK(android::vold::sFsckUntrustedContext != nullptr);
174}
175
Wei Wang2edbe282017-03-06 17:27:05 -0800176static void do_coldboot(DIR *d, int lvl) {
177 struct dirent *de;
178 int dfd, fd;
179
180 dfd = dirfd(d);
181
182 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
183 if(fd >= 0) {
184 write(fd, "add\n", 4);
185 close(fd);
186 }
187
188 while((de = readdir(d))) {
189 DIR *d2;
190
191 if (de->d_name[0] == '.')
192 continue;
193
194 if (de->d_type != DT_DIR && lvl > 0)
195 continue;
196
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600197 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Wei Wang2edbe282017-03-06 17:27:05 -0800198 if(fd < 0)
199 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
211static void coldboot(const char *path) {
212 DIR *d = opendir(path);
213 if(d) {
214 do_coldboot(d, 0);
215 closedir(d);
216 }
217}
218
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800219static int process_config(VolumeManager *vm, bool* has_adoptable) {
Bowgo Tsaie8fb6c32017-03-09 23:11:33 +0800220 fstab = fs_mgr_read_fstab_default();
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800221 if (!fstab) {
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 Sharkey36801cc2015-03-13 16:09:20 -0700228 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800229 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230 std::string sysPattern(fstab->recs[i].blk_device);
Tom Marshalld4246122015-11-05 11:20:54 -0800231 std::string fstype;
232 if (fstab->recs[i].fs_type) {
233 fstype = fstab->recs[i].fs_type;
234 }
235 std::string mntopts;
236 if (fstab->recs[i].fs_options) {
237 mntopts = fstab->recs[i].fs_options;
238 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239 std::string nickname(fstab->recs[i].label);
Tom Marshall35681042015-11-04 15:44:44 -0800240 int partnum = fstab->recs[i].partnum;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 int flags = 0;
242
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800243 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700244 flags |= android::vold::Disk::Flags::kAdoptable;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800245 *has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700246 }
Jeff Sharkey65427f12015-05-19 15:54:15 -0700247 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
248 || property_get_bool("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700249 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700250 }
Tom Marshall35681042015-11-04 15:44:44 -0800251 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
252 flags |= android::vold::Disk::Flags::kNonRemovable;
253 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700254
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700255 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
Tom Marshalld4246122015-11-05 11:20:54 -0800256 new VolumeManager::DiskSource(sysPattern, nickname, partnum, flags,
257 fstype, mntopts)));
San Mehatf1b736b2009-10-10 17:22:08 -0700258 }
259 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700260 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700261}