blob: fb8ffc4e31b2ba8bfff5042beb126782a37c8bc4 [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"
20#include "NetlinkManager.h"
21#include "cryptfs.h"
22#include "sehandle.h"
23
24#include <base/logging.h>
25#include <base/stringprintf.h>
26#include <cutils/klog.h>
27#include <cutils/properties.h>
28#include <cutils/sockets.h>
29
San Mehatf1b736b2009-10-10 17:22:08 -070030#include <stdio.h>
31#include <stdlib.h>
32#include <errno.h>
33#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070034#include <sys/stat.h>
35#include <sys/types.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070036#include <getopt.h>
San Mehat3578c412009-10-12 14:51:52 -070037#include <fcntl.h>
38#include <dirent.h>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080039#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070040
San Mehatf1b736b2009-10-10 17:22:08 -070041static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070042static void coldboot(const char *path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070043static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070044
Ken Sumrall56ad03c2013-02-13 13:00:19 -080045struct fstab *fstab;
46
Stephen Smalley684e6622014-09-30 10:29:24 -040047struct selabel_handle *sehandle;
48
Jeff Sharkey36801cc2015-03-13 16:09:20 -070049using android::base::StringPrintf;
50
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070051int main(int argc, char** argv) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070052 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Jeff Sharkey5bad3782015-04-18 16:15:10 -070053 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
San Mehatf1b736b2009-10-10 17:22:08 -070054
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070055 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
56
San Mehatf1b736b2009-10-10 17:22:08 -070057 VolumeManager *vm;
58 CommandListener *cl;
59 NetlinkManager *nm;
60
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070061 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080062
Stephen Smalley684e6622014-09-30 10:29:24 -040063 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070064 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040065 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070066 }
Stephen Smalley684e6622014-09-30 10:29:24 -040067
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070068 // Quickly throw a CLOEXEC on the socket we just inherited from init
69 fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
70
San Mehata2677e42009-12-13 10:40:18 -080071 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070072
Ken Sumrallb9738ea2013-03-19 19:42:30 -070073 /* For when cryptfs checks and mounts an encrypted filesystem */
74 klog_set_level(6);
75
San Mehatf1b736b2009-10-10 17:22:08 -070076 /* Create our singleton managers */
77 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070078 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070079 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070080 }
San Mehatf1b736b2009-10-10 17:22:08 -070081
82 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070083 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070084 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070085 }
San Mehata2677e42009-12-13 10:40:18 -080086
Jeff Sharkeyb0667872015-04-29 08:57:18 -070087 if (property_get_bool("vold.debug", false)) {
88 vm->setDebug(true);
89 }
90
San Mehatf1b736b2009-10-10 17:22:08 -070091 cl = new CommandListener();
92 vm->setBroadcaster((SocketListener *) cl);
93 nm->setBroadcaster((SocketListener *) cl);
94
95 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070096 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070097 exit(1);
98 }
99
100 if (process_config(vm)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700101 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700102 }
103
104 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700105 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700106 exit(1);
107 }
108
San Mehat3578c412009-10-12 14:51:52 -0700109 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800110// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700111
San Mehatf1b736b2009-10-10 17:22:08 -0700112 /*
113 * Now that we're up, we can respond to commands
114 */
115 if (cl->startListener()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700116 PLOG(ERROR) << "Unable to start CommandListener";
San Mehatf1b736b2009-10-10 17:22:08 -0700117 exit(1);
118 }
119
120 // Eventually we'll become the monitoring thread
121 while(1) {
122 sleep(1000);
123 }
124
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700125 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700126 exit(0);
127}
128
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700129static void parse_args(int argc, char** argv) {
130 static struct option opts[] = {
131 {"blkid_context", required_argument, 0, 'b' },
132 {"blkid_untrusted_context", required_argument, 0, 'B' },
133 {"fsck_context", required_argument, 0, 'f' },
134 {"fsck_untrusted_context", required_argument, 0, 'F' },
135 };
136
137 int c;
138 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
139 switch (c) {
140 case 'b': android::vold::sBlkidContext = optarg; break;
141 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
142 case 'f': android::vold::sFsckContext = optarg; break;
143 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
144 }
145 }
146
147 CHECK(android::vold::sBlkidContext != nullptr);
148 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
149 CHECK(android::vold::sFsckContext != nullptr);
150 CHECK(android::vold::sFsckUntrustedContext != nullptr);
151}
152
153static void do_coldboot(DIR *d, int lvl) {
San Mehat3578c412009-10-12 14:51:52 -0700154 struct dirent *de;
155 int dfd, fd;
156
157 dfd = dirfd(d);
158
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -0700159 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
San Mehat3578c412009-10-12 14:51:52 -0700160 if(fd >= 0) {
161 write(fd, "add\n", 4);
162 close(fd);
163 }
164
165 while((de = readdir(d))) {
166 DIR *d2;
167
168 if (de->d_name[0] == '.')
169 continue;
170
171 if (de->d_type != DT_DIR && lvl > 0)
172 continue;
173
174 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
175 if(fd < 0)
176 continue;
177
178 d2 = fdopendir(fd);
179 if(d2 == 0)
180 close(fd);
181 else {
182 do_coldboot(d2, lvl + 1);
183 closedir(d2);
184 }
185 }
186}
187
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700188static void coldboot(const char *path) {
San Mehat3578c412009-10-12 14:51:52 -0700189 DIR *d = opendir(path);
190 if(d) {
191 do_coldboot(d, 0);
192 closedir(d);
193 }
194}
195
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700196static int process_config(VolumeManager *vm) {
Jeff Sharkeye44a41a2015-05-13 13:53:07 -0700197 bool has_adoptable = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700198 char hardware[PROPERTY_VALUE_MAX];
199 property_get("ro.hardware", hardware, "");
200 std::string fstab_filename(StringPrintf("/fstab.%s", hardware));
Ken Sumrall29d8da82011-05-18 17:20:07 -0700201
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700202#ifdef DEBUG_FSTAB
203 if (access(DEBUG_FSTAB, R_OK) == 0) {
204 LOG(DEBUG) << "Found debug fstab; switching!";
205 fstab_filename = DEBUG_FSTAB;
206 }
207#endif
Ken Sumrall29d8da82011-05-18 17:20:07 -0700208
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700209 fstab = fs_mgr_read_fstab(fstab_filename.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800210 if (!fstab) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700211 PLOG(ERROR) << "Failed to open " << fstab_filename;
San Mehatf1b736b2009-10-10 17:22:08 -0700212 return -1;
213 }
214
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800215 /* Loop through entries looking for ones that vold manages */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700216 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800217 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800218 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700219 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
220 continue;
San Mehata2677e42009-12-13 10:40:18 -0800221 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700222
223 std::string sysPattern(fstab->recs[i].blk_device);
224 std::string nickname(fstab->recs[i].label);
225 int flags = 0;
226
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800227 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700228 flags |= android::vold::Disk::Flags::kAdoptable;
Jeff Sharkeye44a41a2015-05-13 13:53:07 -0700229 has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700230 }
Jeff Sharkey65427f12015-05-19 15:54:15 -0700231 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
232 || property_get_bool("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700233 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700234 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700235
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
237 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700238 }
239 }
Jeff Sharkeye44a41a2015-05-13 13:53:07 -0700240 property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700242}