blob: 35e99dfcf7c089ba52f5b324599555fbf389fb7a [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
Jeff Sharkey36801cc2015-03-13 16:09:20 -070045//#define DEBUG_FSTAB "/data/local/tmp/fstab.debug"
46
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);
55 android::base::InitLogging(argv);
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
San Mehatf1b736b2009-10-10 17:22:08 -070059 VolumeManager *vm;
60 CommandListener *cl;
61 NetlinkManager *nm;
62
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070063 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080064
Stephen Smalley684e6622014-09-30 10:29:24 -040065 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070066 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040067 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070068 }
Stephen Smalley684e6622014-09-30 10:29:24 -040069
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070070 // Quickly throw a CLOEXEC on the socket we just inherited from init
71 fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
72
San Mehata2677e42009-12-13 10:40:18 -080073 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070074
Ken Sumrallb9738ea2013-03-19 19:42:30 -070075 /* For when cryptfs checks and mounts an encrypted filesystem */
76 klog_set_level(6);
77
San Mehatf1b736b2009-10-10 17:22:08 -070078 /* Create our singleton managers */
79 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070080 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070081 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070082 }
San Mehatf1b736b2009-10-10 17:22:08 -070083
84 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070085 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070086 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070087 }
San Mehata2677e42009-12-13 10:40:18 -080088
San Mehatf1b736b2009-10-10 17:22:08 -070089 cl = new CommandListener();
90 vm->setBroadcaster((SocketListener *) cl);
91 nm->setBroadcaster((SocketListener *) cl);
92
93 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070094 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070095 exit(1);
96 }
97
98 if (process_config(vm)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070099 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700100 }
101
102 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700103 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700104 exit(1);
105 }
106
San Mehat3578c412009-10-12 14:51:52 -0700107 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800108// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700109
San Mehatf1b736b2009-10-10 17:22:08 -0700110 /*
111 * Now that we're up, we can respond to commands
112 */
113 if (cl->startListener()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700114 PLOG(ERROR) << "Unable to start CommandListener";
San Mehatf1b736b2009-10-10 17:22:08 -0700115 exit(1);
116 }
117
118 // Eventually we'll become the monitoring thread
119 while(1) {
120 sleep(1000);
121 }
122
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700123 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700124 exit(0);
125}
126
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700127static void parse_args(int argc, char** argv) {
128 static struct option opts[] = {
129 {"blkid_context", required_argument, 0, 'b' },
130 {"blkid_untrusted_context", required_argument, 0, 'B' },
131 {"fsck_context", required_argument, 0, 'f' },
132 {"fsck_untrusted_context", required_argument, 0, 'F' },
133 };
134
135 int c;
136 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
137 switch (c) {
138 case 'b': android::vold::sBlkidContext = optarg; break;
139 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
140 case 'f': android::vold::sFsckContext = optarg; break;
141 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
142 }
143 }
144
145 CHECK(android::vold::sBlkidContext != nullptr);
146 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
147 CHECK(android::vold::sFsckContext != nullptr);
148 CHECK(android::vold::sFsckUntrustedContext != nullptr);
149}
150
151static void do_coldboot(DIR *d, int lvl) {
San Mehat3578c412009-10-12 14:51:52 -0700152 struct dirent *de;
153 int dfd, fd;
154
155 dfd = dirfd(d);
156
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -0700157 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
San Mehat3578c412009-10-12 14:51:52 -0700158 if(fd >= 0) {
159 write(fd, "add\n", 4);
160 close(fd);
161 }
162
163 while((de = readdir(d))) {
164 DIR *d2;
165
166 if (de->d_name[0] == '.')
167 continue;
168
169 if (de->d_type != DT_DIR && lvl > 0)
170 continue;
171
172 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
173 if(fd < 0)
174 continue;
175
176 d2 = fdopendir(fd);
177 if(d2 == 0)
178 close(fd);
179 else {
180 do_coldboot(d2, lvl + 1);
181 closedir(d2);
182 }
183 }
184}
185
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700186static void coldboot(const char *path) {
San Mehat3578c412009-10-12 14:51:52 -0700187 DIR *d = opendir(path);
188 if(d) {
189 do_coldboot(d, 0);
190 closedir(d);
191 }
192}
193
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700194static int process_config(VolumeManager *vm) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195 char hardware[PROPERTY_VALUE_MAX];
196 property_get("ro.hardware", hardware, "");
197 std::string fstab_filename(StringPrintf("/fstab.%s", hardware));
Ken Sumrall29d8da82011-05-18 17:20:07 -0700198
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700199#ifdef DEBUG_FSTAB
200 if (access(DEBUG_FSTAB, R_OK) == 0) {
201 LOG(DEBUG) << "Found debug fstab; switching!";
202 fstab_filename = DEBUG_FSTAB;
203 }
204#endif
Ken Sumrall29d8da82011-05-18 17:20:07 -0700205
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700206 fstab = fs_mgr_read_fstab(fstab_filename.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800207 if (!fstab) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208 PLOG(ERROR) << "Failed to open " << fstab_filename;
San Mehatf1b736b2009-10-10 17:22:08 -0700209 return -1;
210 }
211
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800212 /* Loop through entries looking for ones that vold manages */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700213 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800214 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800215 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700216 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
217 continue;
San Mehata2677e42009-12-13 10:40:18 -0800218 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700219
220 std::string sysPattern(fstab->recs[i].blk_device);
221 std::string nickname(fstab->recs[i].label);
222 int flags = 0;
223
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800224 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700225 flags |= android::vold::Disk::Flags::kAdoptable;
San Mehatf1b736b2009-10-10 17:22:08 -0700226 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])) {
228 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700229 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700230
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700231 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
232 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700233 }
234 }
235
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700237}