San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 17 | #include "Disk.h" |
| 18 | #include "VolumeManager.h" |
| 19 | #include "CommandListener.h" |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 20 | #include "CryptCommandListener.h" |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 21 | #include "NetlinkManager.h" |
| 22 | #include "cryptfs.h" |
| 23 | #include "sehandle.h" |
| 24 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 27 | #include <cutils/klog.h> |
| 28 | #include <cutils/properties.h> |
| 29 | #include <cutils/sockets.h> |
| 30 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 37 | #include <getopt.h> |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 38 | #include <fcntl.h> |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 39 | #include <fs_mgr.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 41 | static int process_config(VolumeManager *vm, bool* has_adoptable); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 42 | static void parse_args(int argc, char** argv); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 43 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 44 | struct fstab *fstab; |
| 45 | |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 46 | struct selabel_handle *sehandle; |
| 47 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 48 | using android::base::StringPrintf; |
| 49 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 50 | int main(int argc, char** argv) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 51 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
Jeff Sharkey | 5bad378 | 2015-04-18 16:15:10 -0700 | [diff] [blame] | 52 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 54 | LOG(INFO) << "Vold 3.0 (the awakening) firing up"; |
| 55 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 56 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 61 | VolumeManager *vm; |
| 62 | CommandListener *cl; |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 63 | CryptCommandListener *ccl; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 64 | NetlinkManager *nm; |
| 65 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 66 | parse_args(argc, argv); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 67 | |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 68 | sehandle = selinux_android_file_context_handle(); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 69 | if (sehandle) { |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 70 | selinux_android_set_sehandle(sehandle); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 71 | } |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 72 | |
Jeff Sharkey | f7e86ea | 2015-04-01 23:07:19 -0700 | [diff] [blame] | 73 | // Quickly throw a CLOEXEC on the socket we just inherited from init |
| 74 | fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC); |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 75 | fcntl(android_get_control_socket("cryptd"), F_SETFD, FD_CLOEXEC); |
Jeff Sharkey | f7e86ea | 2015-04-01 23:07:19 -0700 | [diff] [blame] | 76 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 77 | mkdir("/dev/block/vold", 0755); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 78 | |
Ken Sumrall | b9738ea | 2013-03-19 19:42:30 -0700 | [diff] [blame] | 79 | /* For when cryptfs checks and mounts an encrypted filesystem */ |
| 80 | klog_set_level(6); |
| 81 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 82 | /* Create our singleton managers */ |
| 83 | if (!(vm = VolumeManager::Instance())) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 84 | LOG(ERROR) << "Unable to create VolumeManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 85 | exit(1); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 86 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 87 | |
| 88 | if (!(nm = NetlinkManager::Instance())) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 89 | LOG(ERROR) << "Unable to create NetlinkManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 90 | exit(1); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 91 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 92 | |
Jeff Sharkey | b066787 | 2015-04-29 08:57:18 -0700 | [diff] [blame] | 93 | if (property_get_bool("vold.debug", false)) { |
| 94 | vm->setDebug(true); |
| 95 | } |
| 96 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 97 | cl = new CommandListener(); |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 98 | ccl = new CryptCommandListener(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 99 | vm->setBroadcaster((SocketListener *) cl); |
| 100 | nm->setBroadcaster((SocketListener *) cl); |
| 101 | |
| 102 | if (vm->start()) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 103 | PLOG(ERROR) << "Unable to start VolumeManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 104 | exit(1); |
| 105 | } |
| 106 | |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 107 | bool has_adoptable; |
| 108 | |
| 109 | if (process_config(vm, &has_adoptable)) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 110 | PLOG(ERROR) << "Error reading configuration... continuing anyways"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (nm->start()) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 114 | PLOG(ERROR) << "Unable to start NetlinkManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 115 | exit(1); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Now that we're up, we can respond to commands |
| 120 | */ |
| 121 | if (cl->startListener()) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 122 | PLOG(ERROR) << "Unable to start CommandListener"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 123 | exit(1); |
| 124 | } |
| 125 | |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 126 | if (ccl->startListener()) { |
| 127 | PLOG(ERROR) << "Unable to start CryptCommandListener"; |
| 128 | exit(1); |
| 129 | } |
| 130 | |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 131 | // 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 135 | // Eventually we'll become the monitoring thread |
| 136 | while(1) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 137 | pause(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 140 | LOG(ERROR) << "Vold exiting"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 141 | exit(0); |
| 142 | } |
| 143 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 144 | static 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 Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 168 | static int process_config(VolumeManager *vm, bool* has_adoptable) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 169 | std::string path(android::vold::DefaultFstabPath()); |
| 170 | fstab = fs_mgr_read_fstab(path.c_str()); |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 171 | if (!fstab) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 172 | PLOG(ERROR) << "Failed to open default fstab " << path; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 173 | return -1; |
| 174 | } |
| 175 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 176 | /* Loop through entries looking for ones that vold manages */ |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 177 | *has_adoptable = false; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 178 | for (int i = 0; i < fstab->num_entries; i++) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 179 | if (fs_mgr_is_voldmanaged(&fstab->recs[i])) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 180 | if (fs_mgr_is_nonremovable(&fstab->recs[i])) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 181 | LOG(WARNING) << "nonremovable no longer supported; ignoring volume"; |
| 182 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 183 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 184 | |
| 185 | std::string sysPattern(fstab->recs[i].blk_device); |
| 186 | std::string nickname(fstab->recs[i].label); |
| 187 | int flags = 0; |
| 188 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 189 | if (fs_mgr_is_encryptable(&fstab->recs[i])) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 190 | flags |= android::vold::Disk::Flags::kAdoptable; |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 191 | *has_adoptable = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 192 | } |
Jeff Sharkey | 65427f1 | 2015-05-19 15:54:15 -0700 | [diff] [blame] | 193 | if (fs_mgr_is_noemulatedsd(&fstab->recs[i]) |
| 194 | || property_get_bool("vold.debug.default_primary", false)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 195 | flags |= android::vold::Disk::Flags::kDefaultPrimary; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 196 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 198 | vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>( |
| 199 | new VolumeManager::DiskSource(sysPattern, nickname, flags))); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 202 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 203 | } |