blob: 64cd0eeea56b7f675a1940a67860dbde79fb1f78 [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
San Mehatf1b736b2009-10-10 17:22:08 -070087 cl = new CommandListener();
88 vm->setBroadcaster((SocketListener *) cl);
89 nm->setBroadcaster((SocketListener *) cl);
90
91 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070092 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070093 exit(1);
94 }
95
96 if (process_config(vm)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070097 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -070098 }
99
100 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700101 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700102 exit(1);
103 }
104
San Mehat3578c412009-10-12 14:51:52 -0700105 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800106// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700107
San Mehatf1b736b2009-10-10 17:22:08 -0700108 /*
109 * Now that we're up, we can respond to commands
110 */
111 if (cl->startListener()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700112 PLOG(ERROR) << "Unable to start CommandListener";
San Mehatf1b736b2009-10-10 17:22:08 -0700113 exit(1);
114 }
115
116 // Eventually we'll become the monitoring thread
117 while(1) {
118 sleep(1000);
119 }
120
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700121 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700122 exit(0);
123}
124
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700125static void parse_args(int argc, char** argv) {
126 static struct option opts[] = {
127 {"blkid_context", required_argument, 0, 'b' },
128 {"blkid_untrusted_context", required_argument, 0, 'B' },
129 {"fsck_context", required_argument, 0, 'f' },
130 {"fsck_untrusted_context", required_argument, 0, 'F' },
131 };
132
133 int c;
134 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
135 switch (c) {
136 case 'b': android::vold::sBlkidContext = optarg; break;
137 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
138 case 'f': android::vold::sFsckContext = optarg; break;
139 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
140 }
141 }
142
143 CHECK(android::vold::sBlkidContext != nullptr);
144 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
145 CHECK(android::vold::sFsckContext != nullptr);
146 CHECK(android::vold::sFsckUntrustedContext != nullptr);
147}
148
149static void do_coldboot(DIR *d, int lvl) {
San Mehat3578c412009-10-12 14:51:52 -0700150 struct dirent *de;
151 int dfd, fd;
152
153 dfd = dirfd(d);
154
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -0700155 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
San Mehat3578c412009-10-12 14:51:52 -0700156 if(fd >= 0) {
157 write(fd, "add\n", 4);
158 close(fd);
159 }
160
161 while((de = readdir(d))) {
162 DIR *d2;
163
164 if (de->d_name[0] == '.')
165 continue;
166
167 if (de->d_type != DT_DIR && lvl > 0)
168 continue;
169
170 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
171 if(fd < 0)
172 continue;
173
174 d2 = fdopendir(fd);
175 if(d2 == 0)
176 close(fd);
177 else {
178 do_coldboot(d2, lvl + 1);
179 closedir(d2);
180 }
181 }
182}
183
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700184static void coldboot(const char *path) {
San Mehat3578c412009-10-12 14:51:52 -0700185 DIR *d = opendir(path);
186 if(d) {
187 do_coldboot(d, 0);
188 closedir(d);
189 }
190}
191
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700192static int process_config(VolumeManager *vm) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700193 char hardware[PROPERTY_VALUE_MAX];
194 property_get("ro.hardware", hardware, "");
195 std::string fstab_filename(StringPrintf("/fstab.%s", hardware));
Ken Sumrall29d8da82011-05-18 17:20:07 -0700196
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700197#ifdef DEBUG_FSTAB
198 if (access(DEBUG_FSTAB, R_OK) == 0) {
199 LOG(DEBUG) << "Found debug fstab; switching!";
200 fstab_filename = DEBUG_FSTAB;
201 }
202#endif
Ken Sumrall29d8da82011-05-18 17:20:07 -0700203
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700204 fstab = fs_mgr_read_fstab(fstab_filename.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800205 if (!fstab) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700206 PLOG(ERROR) << "Failed to open " << fstab_filename;
San Mehatf1b736b2009-10-10 17:22:08 -0700207 return -1;
208 }
209
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800210 /* Loop through entries looking for ones that vold manages */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700211 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800212 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800213 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
215 continue;
San Mehata2677e42009-12-13 10:40:18 -0800216 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700217
218 std::string sysPattern(fstab->recs[i].blk_device);
219 std::string nickname(fstab->recs[i].label);
220 int flags = 0;
221
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800222 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223 flags |= android::vold::Disk::Flags::kAdoptable;
San Mehatf1b736b2009-10-10 17:22:08 -0700224 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700225 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])) {
226 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700227 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700228
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700229 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
230 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700231 }
232 }
233
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700235}