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