blob: 4810d436a552801a6772e8d67fe780b13b7138c2 [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"
Paul Lawrenced0b42952015-06-03 14:19:51 -070020#include "CryptCommandListener.h"
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070021#include "NetlinkManager.h"
22#include "cryptfs.h"
23#include "sehandle.h"
24
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070027#include <cutils/klog.h>
28#include <cutils/properties.h>
29#include <cutils/sockets.h>
30
San Mehatf1b736b2009-10-10 17:22:08 -070031#include <stdio.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070035#include <sys/stat.h>
36#include <sys/types.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070037#include <getopt.h>
San Mehat3578c412009-10-12 14:51:52 -070038#include <fcntl.h>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080039#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070040
Dimitry Ivanovc976e732017-01-19 12:48:27 -080041static int process_config(VolumeManager *vm, bool* has_adoptable);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070042static void parse_args(int argc, char** argv);
San Mehatf1b736b2009-10-10 17:22:08 -070043
Ken Sumrall56ad03c2013-02-13 13:00:19 -080044struct fstab *fstab;
45
Stephen Smalley684e6622014-09-30 10:29:24 -040046struct selabel_handle *sehandle;
47
Jeff Sharkey36801cc2015-03-13 16:09:20 -070048using android::base::StringPrintf;
49
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070050int main(int argc, char** argv) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Jeff Sharkey5bad3782015-04-18 16:15:10 -070052 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
San Mehatf1b736b2009-10-10 17:22:08 -070053
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070054 LOG(INFO) << "Vold 3.0 (the awakening) firing up";
55
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070056 LOG(VERBOSE) << "Detected support for:"
57 << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
58 << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
59 << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
60
San Mehatf1b736b2009-10-10 17:22:08 -070061 VolumeManager *vm;
62 CommandListener *cl;
Paul Lawrenced0b42952015-06-03 14:19:51 -070063 CryptCommandListener *ccl;
San Mehatf1b736b2009-10-10 17:22:08 -070064 NetlinkManager *nm;
65
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070066 parse_args(argc, argv);
San Mehata2677e42009-12-13 10:40:18 -080067
Stephen Smalley684e6622014-09-30 10:29:24 -040068 sehandle = selinux_android_file_context_handle();
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070069 if (sehandle) {
Stephen Smalley684e6622014-09-30 10:29:24 -040070 selinux_android_set_sehandle(sehandle);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070071 }
Stephen Smalley684e6622014-09-30 10:29:24 -040072
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070073 // Quickly throw a CLOEXEC on the socket we just inherited from init
74 fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
Paul Lawrenced0b42952015-06-03 14:19:51 -070075 fcntl(android_get_control_socket("cryptd"), F_SETFD, FD_CLOEXEC);
Jeff Sharkeyf7e86ea2015-04-01 23:07:19 -070076
San Mehata2677e42009-12-13 10:40:18 -080077 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070078
Ken Sumrallb9738ea2013-03-19 19:42:30 -070079 /* For when cryptfs checks and mounts an encrypted filesystem */
80 klog_set_level(6);
81
San Mehatf1b736b2009-10-10 17:22:08 -070082 /* Create our singleton managers */
83 if (!(vm = VolumeManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070084 LOG(ERROR) << "Unable to create VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -070085 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070086 }
San Mehatf1b736b2009-10-10 17:22:08 -070087
88 if (!(nm = NetlinkManager::Instance())) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070089 LOG(ERROR) << "Unable to create NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -070090 exit(1);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070091 }
San Mehata2677e42009-12-13 10:40:18 -080092
Jeff Sharkeyb0667872015-04-29 08:57:18 -070093 if (property_get_bool("vold.debug", false)) {
94 vm->setDebug(true);
95 }
96
San Mehatf1b736b2009-10-10 17:22:08 -070097 cl = new CommandListener();
Paul Lawrenced0b42952015-06-03 14:19:51 -070098 ccl = new CryptCommandListener();
San Mehatf1b736b2009-10-10 17:22:08 -070099 vm->setBroadcaster((SocketListener *) cl);
100 nm->setBroadcaster((SocketListener *) cl);
101
102 if (vm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700103 PLOG(ERROR) << "Unable to start VolumeManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700104 exit(1);
105 }
106
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800107 bool has_adoptable;
108
109 if (process_config(vm, &has_adoptable)) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700110 PLOG(ERROR) << "Error reading configuration... continuing anyways";
San Mehatf1b736b2009-10-10 17:22:08 -0700111 }
112
113 if (nm->start()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700114 PLOG(ERROR) << "Unable to start NetlinkManager";
San Mehatf1b736b2009-10-10 17:22:08 -0700115 exit(1);
116 }
117
118 /*
119 * Now that we're up, we can respond to commands
120 */
121 if (cl->startListener()) {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700122 PLOG(ERROR) << "Unable to start CommandListener";
San Mehatf1b736b2009-10-10 17:22:08 -0700123 exit(1);
124 }
125
Paul Lawrenced0b42952015-06-03 14:19:51 -0700126 if (ccl->startListener()) {
127 PLOG(ERROR) << "Unable to start CryptCommandListener";
128 exit(1);
129 }
130
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800131 // This call should go after listeners are started to avoid
132 // a deadlock between vold and init (see b/34278978 for details)
133 property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
134
San Mehatf1b736b2009-10-10 17:22:08 -0700135 // Eventually we'll become the monitoring thread
136 while(1) {
Wei Wang6b455c22017-01-20 11:52:33 -0800137 pause();
San Mehatf1b736b2009-10-10 17:22:08 -0700138 }
139
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700140 LOG(ERROR) << "Vold exiting";
San Mehatf1b736b2009-10-10 17:22:08 -0700141 exit(0);
142}
143
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700144static void parse_args(int argc, char** argv) {
145 static struct option opts[] = {
146 {"blkid_context", required_argument, 0, 'b' },
147 {"blkid_untrusted_context", required_argument, 0, 'B' },
148 {"fsck_context", required_argument, 0, 'f' },
149 {"fsck_untrusted_context", required_argument, 0, 'F' },
150 };
151
152 int c;
153 while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
154 switch (c) {
155 case 'b': android::vold::sBlkidContext = optarg; break;
156 case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
157 case 'f': android::vold::sFsckContext = optarg; break;
158 case 'F': android::vold::sFsckUntrustedContext = optarg; break;
159 }
160 }
161
162 CHECK(android::vold::sBlkidContext != nullptr);
163 CHECK(android::vold::sBlkidUntrustedContext != nullptr);
164 CHECK(android::vold::sFsckContext != nullptr);
165 CHECK(android::vold::sFsckUntrustedContext != nullptr);
166}
167
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800168static int process_config(VolumeManager *vm, bool* has_adoptable) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700169 std::string path(android::vold::DefaultFstabPath());
170 fstab = fs_mgr_read_fstab(path.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800171 if (!fstab) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700172 PLOG(ERROR) << "Failed to open default fstab " << path;
San Mehatf1b736b2009-10-10 17:22:08 -0700173 return -1;
174 }
175
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800176 /* Loop through entries looking for ones that vold manages */
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800177 *has_adoptable = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800179 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800180 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700181 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
182 continue;
San Mehata2677e42009-12-13 10:40:18 -0800183 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184
185 std::string sysPattern(fstab->recs[i].blk_device);
186 std::string nickname(fstab->recs[i].label);
187 int flags = 0;
188
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800189 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 flags |= android::vold::Disk::Flags::kAdoptable;
Dimitry Ivanovc976e732017-01-19 12:48:27 -0800191 *has_adoptable = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700192 }
Jeff Sharkey65427f12015-05-19 15:54:15 -0700193 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
194 || property_get_bool("vold.debug.default_primary", false)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700196 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700197
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700198 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
199 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700200 }
201 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700202 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700203}