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