blob: 8a3f84588a87e069a0f0d3a70896f59692536e76 [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"
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070036#include "cutils/sockets.h"
San Mehatf1b736b2009-10-10 17:22:08 -070037
Jeff Sharkey36801cc2015-03-13 16:09:20 -070038#include "Disk.h"
San Mehatf1b736b2009-10-10 17:22:08 -070039#include "VolumeManager.h"
40#include "CommandListener.h"
41#include "NetlinkManager.h"
San Mehatae10b912009-10-12 14:57:05 -070042#include "DirectVolume.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070043#include "cryptfs.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040044#include "sehandle.h"
San Mehatf1b736b2009-10-10 17:22:08 -070045
46static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070047static void coldboot(const char *path);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070048static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070049
Jeff Sharkey36801cc2015-03-13 16:09:20 -070050//#define DEBUG_FSTAB "/data/local/tmp/fstab.debug"
51
Ken Sumrall56ad03c2013-02-13 13:00:19 -080052struct fstab *fstab;
53
Stephen Smalley684e6622014-09-30 10:29:24 -040054struct selabel_handle *sehandle;
55
Jeff Sharkey36801cc2015-03-13 16:09:20 -070056using android::base::StringPrintf;
57
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070058int main(int argc, char** argv) {
59 SLOGI("Vold 2.1 (the revenge) firing up");
60
Jeff Sharkey36801cc2015-03-13 16:09:20 -070061 setenv("ANDROID_LOG_TAGS", "*:v", 1);
62 android::base::InitLogging(argv);
San Mehatf1b736b2009-10-10 17:22:08 -070063
64 VolumeManager *vm;
65 CommandListener *cl;
66 NetlinkManager *nm;
67
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070068 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080069
Stephen Smalley684e6622014-09-30 10:29:24 -040070 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070071 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040072 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070073 }
Stephen Smalley684e6622014-09-30 10:29:24 -040074
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070075 // Quickly throw a CLOEXEC on the socket we just inherited from init
76 fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
77
San Mehata2677e42009-12-13 10:40:18 -080078 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070079
Ken Sumrallb9738ea2013-03-19 19:42:30 -070080 /* For when cryptfs checks and mounts an encrypted filesystem */
81 klog_set_level(6);
82
San Mehatf1b736b2009-10-10 17:22:08 -070083 /* Create our singleton managers */
84 if (!(vm = VolumeManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070085 SLOGE("Unable to create VolumeManager");
San Mehatf1b736b2009-10-10 17:22:08 -070086 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070087 }
San Mehatf1b736b2009-10-10 17:22:08 -070088
89 if (!(nm = NetlinkManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070090 SLOGE("Unable to create NetlinkManager");
San Mehatf1b736b2009-10-10 17:22:08 -070091 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070092 }
San Mehata2677e42009-12-13 10:40:18 -080093
San Mehatf1b736b2009-10-10 17:22:08 -070094 cl = new CommandListener();
95 vm->setBroadcaster((SocketListener *) cl);
96 nm->setBroadcaster((SocketListener *) cl);
97
98 if (vm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070099 SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700100 exit(1);
101 }
102
103 if (process_config(vm)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700104 SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700105 }
106
107 if (nm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700108 SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700109 exit(1);
110 }
111
San Mehat3578c412009-10-12 14:51:52 -0700112 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800113// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700114
San Mehatf1b736b2009-10-10 17:22:08 -0700115 /*
116 * Now that we're up, we can respond to commands
117 */
118 if (cl->startListener()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700119 SLOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700120 exit(1);
121 }
122
123 // Eventually we'll become the monitoring thread
124 while(1) {
125 sleep(1000);
126 }
127
San Mehat97ac40e2010-03-24 10:24:19 -0700128 SLOGI("Vold exiting");
San Mehatf1b736b2009-10-10 17:22:08 -0700129 exit(0);
130}
131
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700132static void parse_args(int argc, char** argv) {
133 static struct option opts[] = {
134 {"blkid_context", required_argument, 0, 'b' },
135 {"blkid_untrusted_context", required_argument, 0, 'B' },
136 {"fsck_context", required_argument, 0, 'f' },
137 {"fsck_untrusted_context", required_argument, 0, 'F' },
138 };
139
140 int c;
141 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
142 switch (c) {
143 case 'b': android::vold::sBlkidContext = optarg; break;
144 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
145 case 'f': android::vold::sFsckContext = optarg; break;
146 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
147 }
148 }
149
150 CHECK(android::vold::sBlkidContext != nullptr);
151 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
152 CHECK(android::vold::sFsckContext != nullptr);
153 CHECK(android::vold::sFsckUntrustedContext != nullptr);
154}
155
156static void do_coldboot(DIR *d, int lvl) {
San Mehat3578c412009-10-12 14:51:52 -0700157 struct dirent *de;
158 int dfd, fd;
159
160 dfd = dirfd(d);
161
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -0700162 fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
San Mehat3578c412009-10-12 14:51:52 -0700163 if(fd >= 0) {
164 write(fd, "add\n", 4);
165 close(fd);
166 }
167
168 while((de = readdir(d))) {
169 DIR *d2;
170
171 if (de->d_name[0] == '.')
172 continue;
173
174 if (de->d_type != DT_DIR && lvl > 0)
175 continue;
176
177 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
178 if(fd < 0)
179 continue;
180
181 d2 = fdopendir(fd);
182 if(d2 == 0)
183 close(fd);
184 else {
185 do_coldboot(d2, lvl + 1);
186 closedir(d2);
187 }
188 }
189}
190
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700191static void coldboot(const char *path) {
San Mehat3578c412009-10-12 14:51:52 -0700192 DIR *d = opendir(path);
193 if(d) {
194 do_coldboot(d, 0);
195 closedir(d);
196 }
197}
198
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700199static int process_config(VolumeManager *vm) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700200 char hardware[PROPERTY_VALUE_MAX];
201 property_get("ro.hardware", hardware, "");
202 std::string fstab_filename(StringPrintf("/fstab.%s", hardware));
Ken Sumrall29d8da82011-05-18 17:20:07 -0700203
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700204#ifdef DEBUG_FSTAB
205 if (access(DEBUG_FSTAB, R_OK) == 0) {
206 LOG(DEBUG) << "Found debug fstab; switching!";
207 fstab_filename = DEBUG_FSTAB;
208 }
209#endif
Ken Sumrall29d8da82011-05-18 17:20:07 -0700210
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700211 fstab = fs_mgr_read_fstab(fstab_filename.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800212 if (!fstab) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700213 PLOG(ERROR) << "Failed to open " << fstab_filename;
San Mehatf1b736b2009-10-10 17:22:08 -0700214 return -1;
215 }
216
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800217 /* Loop through entries looking for ones that vold manages */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800219 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800220 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
222 continue;
San Mehata2677e42009-12-13 10:40:18 -0800223 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224
225 std::string sysPattern(fstab->recs[i].blk_device);
226 std::string nickname(fstab->recs[i].label);
227 int flags = 0;
228
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800229 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230 flags |= android::vold::Disk::Flags::kAdoptable;
San Mehatf1b736b2009-10-10 17:22:08 -0700231 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])) {
233 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 }
240
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700242}