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