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 | |
| 25 | #include <base/logging.h> |
| 26 | #include <base/stringprintf.h> |
| 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> |
| 39 | #include <dirent.h> |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 40 | #include <fs_mgr.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 41 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 42 | static int process_config(VolumeManager *vm); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 43 | static void coldboot(const char *path); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 44 | static void parse_args(int argc, char** argv); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 46 | struct fstab *fstab; |
| 47 | |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 48 | struct selabel_handle *sehandle; |
| 49 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 50 | using android::base::StringPrintf; |
| 51 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 52 | int main(int argc, char** argv) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 53 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
Jeff Sharkey | 5bad378 | 2015-04-18 16:15:10 -0700 | [diff] [blame] | 54 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 55 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 56 | LOG(INFO) << "Vold 3.0 (the awakening) firing up"; |
| 57 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 58 | LOG(VERBOSE) << "Detected support for:" |
| 59 | << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "") |
| 60 | << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "") |
| 61 | << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : ""); |
| 62 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 63 | VolumeManager *vm; |
| 64 | CommandListener *cl; |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 65 | CryptCommandListener *ccl; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 66 | NetlinkManager *nm; |
| 67 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 68 | parse_args(argc, argv); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 69 | |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 70 | sehandle = selinux_android_file_context_handle(); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 71 | if (sehandle) { |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 72 | selinux_android_set_sehandle(sehandle); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 73 | } |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 74 | |
Jeff Sharkey | f7e86ea | 2015-04-01 23:07:19 -0700 | [diff] [blame] | 75 | // Quickly throw a CLOEXEC on the socket we just inherited from init |
| 76 | fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC); |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 77 | fcntl(android_get_control_socket("cryptd"), F_SETFD, FD_CLOEXEC); |
Jeff Sharkey | f7e86ea | 2015-04-01 23:07:19 -0700 | [diff] [blame] | 78 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 79 | mkdir("/dev/block/vold", 0755); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 80 | |
Ken Sumrall | b9738ea | 2013-03-19 19:42:30 -0700 | [diff] [blame] | 81 | /* For when cryptfs checks and mounts an encrypted filesystem */ |
| 82 | klog_set_level(6); |
| 83 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 84 | /* Create our singleton managers */ |
| 85 | if (!(vm = VolumeManager::Instance())) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 86 | LOG(ERROR) << "Unable to create VolumeManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 87 | exit(1); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 88 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 89 | |
| 90 | if (!(nm = NetlinkManager::Instance())) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 91 | LOG(ERROR) << "Unable to create NetlinkManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 92 | exit(1); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 93 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 94 | |
Jeff Sharkey | b066787 | 2015-04-29 08:57:18 -0700 | [diff] [blame] | 95 | if (property_get_bool("vold.debug", false)) { |
| 96 | vm->setDebug(true); |
| 97 | } |
| 98 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 99 | cl = new CommandListener(); |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 100 | ccl = new CryptCommandListener(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 101 | vm->setBroadcaster((SocketListener *) cl); |
| 102 | nm->setBroadcaster((SocketListener *) cl); |
| 103 | |
| 104 | if (vm->start()) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 105 | PLOG(ERROR) << "Unable to start VolumeManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 106 | exit(1); |
| 107 | } |
| 108 | |
| 109 | if (process_config(vm)) { |
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 | |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 118 | coldboot("/sys/block"); |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 119 | // coldboot("/sys/class/switch"); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 120 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Now that we're up, we can respond to commands |
| 123 | */ |
| 124 | if (cl->startListener()) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 125 | PLOG(ERROR) << "Unable to start CommandListener"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 126 | exit(1); |
| 127 | } |
| 128 | |
Paul Lawrence | d0b4295 | 2015-06-03 14:19:51 -0700 | [diff] [blame] | 129 | if (ccl->startListener()) { |
| 130 | PLOG(ERROR) << "Unable to start CryptCommandListener"; |
| 131 | exit(1); |
| 132 | } |
| 133 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 134 | // Eventually we'll become the monitoring thread |
| 135 | while(1) { |
| 136 | sleep(1000); |
| 137 | } |
| 138 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 139 | LOG(ERROR) << "Vold exiting"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 140 | exit(0); |
| 141 | } |
| 142 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 143 | static void parse_args(int argc, char** argv) { |
| 144 | static struct option opts[] = { |
| 145 | {"blkid_context", required_argument, 0, 'b' }, |
| 146 | {"blkid_untrusted_context", required_argument, 0, 'B' }, |
| 147 | {"fsck_context", required_argument, 0, 'f' }, |
| 148 | {"fsck_untrusted_context", required_argument, 0, 'F' }, |
| 149 | }; |
| 150 | |
| 151 | int c; |
| 152 | while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) { |
| 153 | switch (c) { |
| 154 | case 'b': android::vold::sBlkidContext = optarg; break; |
| 155 | case 'B': android::vold::sBlkidUntrustedContext = optarg; break; |
| 156 | case 'f': android::vold::sFsckContext = optarg; break; |
| 157 | case 'F': android::vold::sFsckUntrustedContext = optarg; break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | CHECK(android::vold::sBlkidContext != nullptr); |
| 162 | CHECK(android::vold::sBlkidUntrustedContext != nullptr); |
| 163 | CHECK(android::vold::sFsckContext != nullptr); |
| 164 | CHECK(android::vold::sFsckUntrustedContext != nullptr); |
| 165 | } |
| 166 | |
| 167 | static void do_coldboot(DIR *d, int lvl) { |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 168 | struct dirent *de; |
| 169 | int dfd, fd; |
| 170 | |
| 171 | dfd = dirfd(d); |
| 172 | |
Jeff Sharkey | f7e86ea | 2015-04-01 23:07:19 -0700 | [diff] [blame] | 173 | fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 174 | if(fd >= 0) { |
| 175 | write(fd, "add\n", 4); |
| 176 | close(fd); |
| 177 | } |
| 178 | |
| 179 | while((de = readdir(d))) { |
| 180 | DIR *d2; |
| 181 | |
| 182 | if (de->d_name[0] == '.') |
| 183 | continue; |
| 184 | |
| 185 | if (de->d_type != DT_DIR && lvl > 0) |
| 186 | continue; |
| 187 | |
| 188 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 189 | if(fd < 0) |
| 190 | continue; |
| 191 | |
| 192 | d2 = fdopendir(fd); |
| 193 | if(d2 == 0) |
| 194 | close(fd); |
| 195 | else { |
| 196 | do_coldboot(d2, lvl + 1); |
| 197 | closedir(d2); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 202 | static void coldboot(const char *path) { |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 203 | DIR *d = opendir(path); |
| 204 | if(d) { |
| 205 | do_coldboot(d, 0); |
| 206 | closedir(d); |
| 207 | } |
| 208 | } |
| 209 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 210 | static int process_config(VolumeManager *vm) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 211 | std::string path(android::vold::DefaultFstabPath()); |
| 212 | fstab = fs_mgr_read_fstab(path.c_str()); |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 213 | if (!fstab) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 214 | PLOG(ERROR) << "Failed to open default fstab " << path; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 218 | /* Loop through entries looking for ones that vold manages */ |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 219 | bool has_adoptable = false; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 220 | for (int i = 0; i < fstab->num_entries; i++) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 221 | if (fs_mgr_is_voldmanaged(&fstab->recs[i])) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 222 | if (fs_mgr_is_nonremovable(&fstab->recs[i])) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 223 | LOG(WARNING) << "nonremovable no longer supported; ignoring volume"; |
| 224 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 225 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 226 | |
| 227 | std::string sysPattern(fstab->recs[i].blk_device); |
| 228 | std::string nickname(fstab->recs[i].label); |
| 229 | int flags = 0; |
| 230 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 231 | if (fs_mgr_is_encryptable(&fstab->recs[i])) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 232 | flags |= android::vold::Disk::Flags::kAdoptable; |
Jeff Sharkey | e44a41a | 2015-05-13 13:53:07 -0700 | [diff] [blame] | 233 | has_adoptable = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 234 | } |
Jeff Sharkey | 65427f1 | 2015-05-19 15:54:15 -0700 | [diff] [blame] | 235 | if (fs_mgr_is_noemulatedsd(&fstab->recs[i]) |
| 236 | || property_get_bool("vold.debug.default_primary", false)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 237 | flags |= android::vold::Disk::Flags::kDefaultPrimary; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 238 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 239 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 240 | vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>( |
| 241 | new VolumeManager::DiskSource(sysPattern, nickname, flags))); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
Jeff Sharkey | e44a41a | 2015-05-13 13:53:07 -0700 | [diff] [blame] | 244 | property_set("vold.has_adoptable", has_adoptable ? "1" : "0"); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 245 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 246 | } |