blob: 6d94818a436d94da3a5eddce5f21eb9891165d73 [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 Sharkeyc79fb892015-11-12 20:18:02 -080021#include "Ext4Crypt.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070022#include "NetlinkManager.h"
23#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>
30#include <cutils/sockets.h>
31
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>
40#include <dirent.h>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080041#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070042
San Mehatf1b736b2009-10-10 17:22:08 -070043static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070044static 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 Sharkey9f18fe72015-04-01 23:32:18 -070057 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
58
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070059 LOG(VERBOSE) << "Detected support for:"
60 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
61 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
62 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
63
San Mehatf1b736b2009-10-10 17:22:08 -070064 VolumeManager *vm;
65 CommandListener *cl;
Paul Lawrenced0b42952015-06-03 14:19:51 -070066 CryptCommandListener *ccl;
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
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070076 // Quickly throw a CLOEXEC on the socket we just inherited from init
77 fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
Paul Lawrenced0b42952015-06-03 14:19:51 -070078 fcntl(android_get_control_socket("cryptd"), F_SETFD, FD_CLOEXEC);
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070079
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 Sharkeyb0667872015-04-29 08:57:18 -070096 if (property_get_bool("vold.debug", false)) {
97 vm->setDebug(true);
98 }
99
San Mehatf1b736b2009-10-10 17:22:08 -0700100 cl = new CommandListener();
Paul Lawrenced0b42952015-06-03 14:19:51 -0700101 ccl = new CryptCommandListener();
San Mehatf1b736b2009-10-10 17:22:08 -0700102 vm->setBroadcaster((SocketListener *) cl);
103 nm->setBroadcaster((SocketListener *) cl);
104
105 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700106 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700107 exit(1);
108 }
109
110 if (process_config(vm)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700111 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700112 }
113
114 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700115 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700116 exit(1);
117 }
118
San Mehat3578c412009-10-12 14:51:52 -0700119 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800120// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700121
San Mehatf1b736b2009-10-10 17:22:08 -0700122 /*
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
San Mehatf1b736b2009-10-10 17:22:08 -0700135 // Eventually we'll become the monitoring thread
136 while(1) {
137 sleep(1000);
138 }
139
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700140 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700141 exit(0);
142}
143
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700144static void parse_args(int argc, char** argv) {
145 static struct option opts[] = {
146 {"blkid_context", required_argument, 0, 'b' },
147 {"blkid_untrusted_context", required_argument, 0, 'B' },
148 {"fsck_context", required_argument, 0, 'f' },
149 {"fsck_untrusted_context", required_argument, 0, 'F' },
150 };
151
152 int c;
153 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
154 switch (c) {
155 case 'b': android::vold::sBlkidContext = optarg; break;
156 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
157 case 'f': android::vold::sFsckContext = optarg; break;
158 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
159 }
160 }
161
162 CHECK(android::vold::sBlkidContext != nullptr);
163 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
164 CHECK(android::vold::sFsckContext != nullptr);
165 CHECK(android::vold::sFsckUntrustedContext != nullptr);
166}
167
168static void do_coldboot(DIR *d, int lvl) {
San Mehat3578c412009-10-12 14:51:52 -0700169 struct dirent *de;
170 int dfd, fd;
171
172 dfd = dirfd(d);
173
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -0700174 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
San Mehat3578c412009-10-12 14:51:52 -0700175 if(fd >= 0) {
176 write(fd, "add\n", 4);
177 close(fd);
178 }
179
180 while((de = readdir(d))) {
181 DIR *d2;
182
183 if (de->d_name[0] == '.')
184 continue;
185
186 if (de->d_type != DT_DIR && lvl > 0)
187 continue;
188
189 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
190 if(fd < 0)
191 continue;
192
193 d2 = fdopendir(fd);
194 if(d2 == 0)
195 close(fd);
196 else {
197 do_coldboot(d2, lvl + 1);
198 closedir(d2);
199 }
200 }
201}
202
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700203static void coldboot(const char *path) {
San Mehat3578c412009-10-12 14:51:52 -0700204 DIR *d = opendir(path);
205 if(d) {
206 do_coldboot(d, 0);
207 closedir(d);
208 }
209}
210
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700211static int process_config(VolumeManager *vm) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700212 std::string path(android::vold::DefaultFstabPath());
213 fstab = fs_mgr_read_fstab(path.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800214 if (!fstab) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700215 PLOG(ERROR) << "Failed to open default fstab " << path;
San Mehatf1b736b2009-10-10 17:22:08 -0700216 return -1;
217 }
218
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800219 /* Loop through entries looking for ones that vold manages */
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700220 bool has_adoptable = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800222 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800223 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
225 continue;
San Mehata2677e42009-12-13 10:40:18 -0800226 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227
228 std::string sysPattern(fstab->recs[i].blk_device);
229 std::string nickname(fstab->recs[i].label);
230 int flags = 0;
231
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800232 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700233 flags |= android::vold::Disk::Flags::kAdoptable;
Jeff Sharkeye44a41a2015-05-13 13:53:07 -0700234 has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700235 }
Jeff Sharkey65427f12015-05-19 15:54:15 -0700236 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
237 || property_get_bool("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700238 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700239 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700240
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
242 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700243 }
244 }
Jeff Sharkeye44a41a2015-05-13 13:53:07 -0700245 property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700246 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700247}