blob: 537e0a0c34324cdb1a1131e72dd38d986eb30d66 [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
26#include <base/logging.h>
27#include <base/stringprintf.h>
28#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
Jeff Sharkeyc79fb892015-11-12 20:18:02 -0800100 // Prepare owner storage
101 e4crypt_prepare_user_storage(nullptr, 0);
102
San Mehatf1b736b2009-10-10 17:22:08 -0700103 cl = new CommandListener();
Paul Lawrenced0b42952015-06-03 14:19:51 -0700104 ccl = new CryptCommandListener();
San Mehatf1b736b2009-10-10 17:22:08 -0700105 vm->setBroadcaster((SocketListener *) cl);
106 nm->setBroadcaster((SocketListener *) cl);
107
108 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700109 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700110 exit(1);
111 }
112
113 if (process_config(vm)) {
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
San Mehat3578c412009-10-12 14:51:52 -0700122 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800123// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700124
San Mehatf1b736b2009-10-10 17:22:08 -0700125 /*
126 * Now that we're up, we can respond to commands
127 */
128 if (cl->startListener()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700129 PLOG(ERROR) << "Unable to start CommandListener";
San Mehatf1b736b2009-10-10 17:22:08 -0700130 exit(1);
131 }
132
Paul Lawrenced0b42952015-06-03 14:19:51 -0700133 if (ccl->startListener()) {
134 PLOG(ERROR) << "Unable to start CryptCommandListener";
135 exit(1);
136 }
137
San Mehatf1b736b2009-10-10 17:22:08 -0700138 // Eventually we'll become the monitoring thread
139 while(1) {
140 sleep(1000);
141 }
142
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700143 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700144 exit(0);
145}
146
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700147static void parse_args(int argc, char** argv) {
148 static struct option opts[] = {
149 {"blkid_context", required_argument, 0, 'b' },
150 {"blkid_untrusted_context", required_argument, 0, 'B' },
151 {"fsck_context", required_argument, 0, 'f' },
152 {"fsck_untrusted_context", required_argument, 0, 'F' },
153 };
154
155 int c;
156 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
157 switch (c) {
158 case 'b': android::vold::sBlkidContext = optarg; break;
159 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
160 case 'f': android::vold::sFsckContext = optarg; break;
161 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
162 }
163 }
164
165 CHECK(android::vold::sBlkidContext != nullptr);
166 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
167 CHECK(android::vold::sFsckContext != nullptr);
168 CHECK(android::vold::sFsckUntrustedContext != nullptr);
169}
170
171static void do_coldboot(DIR *d, int lvl) {
San Mehat3578c412009-10-12 14:51:52 -0700172 struct dirent *de;
173 int dfd, fd;
174
175 dfd = dirfd(d);
176
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -0700177 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
San Mehat3578c412009-10-12 14:51:52 -0700178 if(fd >= 0) {
179 write(fd, "add\n", 4);
180 close(fd);
181 }
182
183 while((de = readdir(d))) {
184 DIR *d2;
185
186 if (de->d_name[0] == '.')
187 continue;
188
189 if (de->d_type != DT_DIR && lvl > 0)
190 continue;
191
192 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
193 if(fd < 0)
194 continue;
195
196 d2 = fdopendir(fd);
197 if(d2 == 0)
198 close(fd);
199 else {
200 do_coldboot(d2, lvl + 1);
201 closedir(d2);
202 }
203 }
204}
205
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700206static void coldboot(const char *path) {
San Mehat3578c412009-10-12 14:51:52 -0700207 DIR *d = opendir(path);
208 if(d) {
209 do_coldboot(d, 0);
210 closedir(d);
211 }
212}
213
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700214static int process_config(VolumeManager *vm) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700215 std::string path(android::vold::DefaultFstabPath());
216 fstab = fs_mgr_read_fstab(path.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800217 if (!fstab) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700218 PLOG(ERROR) << "Failed to open default fstab " << path;
San Mehatf1b736b2009-10-10 17:22:08 -0700219 return -1;
220 }
221
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800222 /* Loop through entries looking for ones that vold manages */
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700223 bool has_adoptable = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800225 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800226 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
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
231 std::string sysPattern(fstab->recs[i].blk_device);
232 std::string nickname(fstab->recs[i].label);
233 int flags = 0;
234
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800235 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236 flags |= android::vold::Disk::Flags::kAdoptable;
Jeff Sharkeye44a41a2015-05-13 13:53:07 -0700237 has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700238 }
Jeff Sharkey65427f12015-05-19 15:54:15 -0700239 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
240 || property_get_bool("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 Sharkeye44a41a2015-05-13 13:53:07 -0700248 property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700249 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700250}