blob: b127bb06c3724582dd4668d0bd626bbeec5fe4f0 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <errno.h>
20#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070021#include <sys/stat.h>
22#include <sys/types.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070023#include <getopt.h>
San Mehat3578c412009-10-12 14:51:52 -070024
25#include <fcntl.h>
26#include <dirent.h>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080027#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070028
29#define LOG_TAG "Vold"
30
Jeff Sharkey36801cc2015-03-13 16:09:20 -070031#include <base/logging.h>
32#include <base/stringprintf.h>
Ken Sumrallb9738ea2013-03-19 19:42:30 -070033#include "cutils/klog.h"
San Mehatf1b736b2009-10-10 17:22:08 -070034#include "cutils/log.h"
Ken Sumrall56ad03c2013-02-13 13:00:19 -080035#include "cutils/properties.h"
San Mehatf1b736b2009-10-10 17:22:08 -070036
Jeff Sharkey36801cc2015-03-13 16:09:20 -070037#include "Disk.h"
San Mehatf1b736b2009-10-10 17:22:08 -070038#include "VolumeManager.h"
39#include "CommandListener.h"
40#include "NetlinkManager.h"
San Mehatae10b912009-10-12 14:57:05 -070041#include "DirectVolume.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070042#include "cryptfs.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040043#include "sehandle.h"
San Mehatf1b736b2009-10-10 17:22:08 -070044
45static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070046static void coldboot(const char *path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070047static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070048
Jeff Sharkey36801cc2015-03-13 16:09:20 -070049//#define DEBUG_FSTAB "/data/local/tmp/fstab.debug"
50
Ken Sumrall56ad03c2013-02-13 13:00:19 -080051struct fstab *fstab;
52
Stephen Smalley684e6622014-09-30 10:29:24 -040053struct selabel_handle *sehandle;
54
Jeff Sharkey36801cc2015-03-13 16:09:20 -070055using android::base::StringPrintf;
56
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070057int main(int argc, char** argv) {
58 SLOGI("Vold 2.1 (the revenge) firing up");
59
Jeff Sharkey36801cc2015-03-13 16:09:20 -070060 setenv("ANDROID_LOG_TAGS", "*:v", 1);
61 android::base::InitLogging(argv);
San Mehatf1b736b2009-10-10 17:22:08 -070062
63 VolumeManager *vm;
64 CommandListener *cl;
65 NetlinkManager *nm;
66
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070067 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080068
Stephen Smalley684e6622014-09-30 10:29:24 -040069 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070070 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040071 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070072 }
Stephen Smalley684e6622014-09-30 10:29:24 -040073
San Mehata2677e42009-12-13 10:40:18 -080074 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070075
Ken Sumrallb9738ea2013-03-19 19:42:30 -070076 /* For when cryptfs checks and mounts an encrypted filesystem */
77 klog_set_level(6);
78
San Mehatf1b736b2009-10-10 17:22:08 -070079 /* Create our singleton managers */
80 if (!(vm = VolumeManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070081 SLOGE("Unable to create VolumeManager");
San Mehatf1b736b2009-10-10 17:22:08 -070082 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070083 }
San Mehatf1b736b2009-10-10 17:22:08 -070084
85 if (!(nm = NetlinkManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070086 SLOGE("Unable to create NetlinkManager");
San Mehatf1b736b2009-10-10 17:22:08 -070087 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070088 }
San Mehata2677e42009-12-13 10:40:18 -080089
San Mehatf1b736b2009-10-10 17:22:08 -070090 cl = new CommandListener();
91 vm->setBroadcaster((SocketListener *) cl);
92 nm->setBroadcaster((SocketListener *) cl);
93
94 if (vm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070095 SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070096 exit(1);
97 }
98
99 if (process_config(vm)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700100 SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700101 }
102
103 if (nm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700104 SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700105 exit(1);
106 }
107
San Mehat3578c412009-10-12 14:51:52 -0700108 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800109// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700110
San Mehatf1b736b2009-10-10 17:22:08 -0700111 /*
112 * Now that we're up, we can respond to commands
113 */
114 if (cl->startListener()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700115 SLOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700116 exit(1);
117 }
118
119 // Eventually we'll become the monitoring thread
120 while(1) {
121 sleep(1000);
122 }
123
San Mehat97ac40e2010-03-24 10:24:19 -0700124 SLOGI("Vold exiting");
San Mehatf1b736b2009-10-10 17:22:08 -0700125 exit(0);
126}
127
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700128static void parse_args(int argc, char** argv) {
129 static struct option opts[] = {
130 {"blkid_context", required_argument, 0, 'b' },
131 {"blkid_untrusted_context", required_argument, 0, 'B' },
132 {"fsck_context", required_argument, 0, 'f' },
133 {"fsck_untrusted_context", required_argument, 0, 'F' },
134 };
135
136 int c;
137 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
138 switch (c) {
139 case 'b': android::vold::sBlkidContext = optarg; break;
140 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
141 case 'f': android::vold::sFsckContext = optarg; break;
142 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
143 }
144 }
145
146 CHECK(android::vold::sBlkidContext != nullptr);
147 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
148 CHECK(android::vold::sFsckContext != nullptr);
149 CHECK(android::vold::sFsckUntrustedContext != nullptr);
150}
151
152static void do_coldboot(DIR *d, int lvl) {
San Mehat3578c412009-10-12 14:51:52 -0700153 struct dirent *de;
154 int dfd, fd;
155
156 dfd = dirfd(d);
157
158 fd = openat(dfd, "uevent", O_WRONLY);
159 if(fd >= 0) {
160 write(fd, "add\n", 4);
161 close(fd);
162 }
163
164 while((de = readdir(d))) {
165 DIR *d2;
166
167 if (de->d_name[0] == '.')
168 continue;
169
170 if (de->d_type != DT_DIR && lvl > 0)
171 continue;
172
173 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
174 if(fd < 0)
175 continue;
176
177 d2 = fdopendir(fd);
178 if(d2 == 0)
179 close(fd);
180 else {
181 do_coldboot(d2, lvl + 1);
182 closedir(d2);
183 }
184 }
185}
186
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700187static void coldboot(const char *path) {
San Mehat3578c412009-10-12 14:51:52 -0700188 DIR *d = opendir(path);
189 if(d) {
190 do_coldboot(d, 0);
191 closedir(d);
192 }
193}
194
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700195static int process_config(VolumeManager *vm) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700196 char hardware[PROPERTY_VALUE_MAX];
197 property_get("ro.hardware", hardware, "");
198 std::string fstab_filename(StringPrintf("/fstab.%s", hardware));
Ken Sumrall29d8da82011-05-18 17:20:07 -0700199
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700200#ifdef DEBUG_FSTAB
201 if (access(DEBUG_FSTAB, R_OK) == 0) {
202 LOG(DEBUG) << "Found debug fstab; switching!";
203 fstab_filename = DEBUG_FSTAB;
204 }
205#endif
Ken Sumrall29d8da82011-05-18 17:20:07 -0700206
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700207 fstab = fs_mgr_read_fstab(fstab_filename.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800208 if (!fstab) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700209 PLOG(ERROR) << "Failed to open " << fstab_filename;
San Mehatf1b736b2009-10-10 17:22:08 -0700210 return -1;
211 }
212
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800213 /* Loop through entries looking for ones that vold manages */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800215 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800216 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700217 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
218 continue;
San Mehata2677e42009-12-13 10:40:18 -0800219 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700220
221 std::string sysPattern(fstab->recs[i].blk_device);
222 std::string nickname(fstab->recs[i].label);
223 int flags = 0;
224
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800225 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700226 flags |= android::vold::Disk::Flags::kAdoptable;
San Mehatf1b736b2009-10-10 17:22:08 -0700227 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700228 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])) {
229 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700230 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700231
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
233 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700234 }
235 }
236
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700237 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700238}