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 | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER |
| 18 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 19 | #include "model/Disk.h" |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 20 | #include "VolumeManager.h" |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 21 | #include "NetlinkManager.h" |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 22 | #include "VoldNativeService.h" |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 23 | #include "VoldUtil.h" |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 24 | #include "cryptfs.h" |
| 25 | #include "sehandle.h" |
| 26 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 28 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 30 | #include <cutils/klog.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 31 | #include <utils/Trace.h> |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 32 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 39 | #include <getopt.h> |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 40 | #include <fcntl.h> |
Wei Wang | 2edbe28 | 2017-03-06 17:27:05 -0800 | [diff] [blame] | 41 | #include <dirent.h> |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 42 | #include <fs_mgr.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 43 | |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 44 | static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota); |
Wei Wang | 2edbe28 | 2017-03-06 17:27:05 -0800 | [diff] [blame] | 45 | static void coldboot(const char *path); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 46 | static void parse_args(int argc, char** argv); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 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 | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 56 | ATRACE_BEGIN("main"); |
| 57 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 58 | LOG(INFO) << "Vold 3.0 (the awakening) firing up"; |
| 59 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 60 | LOG(VERBOSE) << "Detected support for:" |
| 61 | << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "") |
| 62 | << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "") |
| 63 | << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : ""); |
| 64 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 65 | VolumeManager *vm; |
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 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 75 | mkdir("/dev/block/vold", 0755); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 76 | |
Ken Sumrall | b9738ea | 2013-03-19 19:42:30 -0700 | [diff] [blame] | 77 | /* For when cryptfs checks and mounts an encrypted filesystem */ |
| 78 | klog_set_level(6); |
| 79 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 80 | /* Create our singleton managers */ |
| 81 | if (!(vm = VolumeManager::Instance())) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 82 | LOG(ERROR) << "Unable to create VolumeManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 83 | exit(1); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 84 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 85 | |
| 86 | if (!(nm = NetlinkManager::Instance())) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 87 | LOG(ERROR) << "Unable to create NetlinkManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 88 | exit(1); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 89 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 90 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 91 | if (android::base::GetBoolProperty("vold.debug", false)) { |
Jeff Sharkey | b066787 | 2015-04-29 08:57:18 -0700 | [diff] [blame] | 92 | vm->setDebug(true); |
| 93 | } |
| 94 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 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 | |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 100 | bool has_adoptable; |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 101 | bool has_quota; |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 102 | |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 103 | if (process_config(vm, &has_adoptable, &has_quota)) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 104 | PLOG(ERROR) << "Error reading configuration... continuing anyways"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Paul Crowley | 3188805 | 2017-09-27 10:56:54 -0700 | [diff] [blame] | 107 | ATRACE_BEGIN("VoldNativeService::start"); |
| 108 | if (android::vold::VoldNativeService::start() != android::OK) { |
| 109 | LOG(ERROR) << "Unable to start VoldNativeService"; |
| 110 | exit(1); |
| 111 | } |
| 112 | ATRACE_END(); |
| 113 | |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 114 | ATRACE_BEGIN("NetlinkManager::start"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 115 | if (nm->start()) { |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 116 | PLOG(ERROR) << "Unable to start NetlinkManager"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 117 | exit(1); |
| 118 | } |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 119 | ATRACE_END(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 120 | |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 121 | // This call should go after listeners are started to avoid |
| 122 | // a deadlock between vold and init (see b/34278978 for details) |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 123 | android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0"); |
| 124 | android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0"); |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 125 | |
Wei Wang | 2edbe28 | 2017-03-06 17:27:05 -0800 | [diff] [blame] | 126 | // Do coldboot here so it won't block booting, |
| 127 | // also the cold boot is needed in case we have flash drive |
| 128 | // connected before Vold launched |
| 129 | coldboot("/sys/block"); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 130 | |
| 131 | ATRACE_END(); |
| 132 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 133 | // Eventually we'll become the monitoring thread |
| 134 | while(1) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 135 | pause(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 138 | LOG(ERROR) << "Vold exiting"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 139 | exit(0); |
| 140 | } |
| 141 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 142 | static void parse_args(int argc, char** argv) { |
| 143 | static struct option opts[] = { |
| 144 | {"blkid_context", required_argument, 0, 'b' }, |
| 145 | {"blkid_untrusted_context", required_argument, 0, 'B' }, |
| 146 | {"fsck_context", required_argument, 0, 'f' }, |
| 147 | {"fsck_untrusted_context", required_argument, 0, 'F' }, |
| 148 | }; |
| 149 | |
| 150 | int c; |
| 151 | while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) { |
| 152 | switch (c) { |
| 153 | case 'b': android::vold::sBlkidContext = optarg; break; |
| 154 | case 'B': android::vold::sBlkidUntrustedContext = optarg; break; |
| 155 | case 'f': android::vold::sFsckContext = optarg; break; |
| 156 | case 'F': android::vold::sFsckUntrustedContext = optarg; break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | CHECK(android::vold::sBlkidContext != nullptr); |
| 161 | CHECK(android::vold::sBlkidUntrustedContext != nullptr); |
| 162 | CHECK(android::vold::sFsckContext != nullptr); |
| 163 | CHECK(android::vold::sFsckUntrustedContext != nullptr); |
| 164 | } |
| 165 | |
Wei Wang | 2edbe28 | 2017-03-06 17:27:05 -0800 | [diff] [blame] | 166 | static void do_coldboot(DIR *d, int lvl) { |
| 167 | struct dirent *de; |
| 168 | int dfd, fd; |
| 169 | |
| 170 | dfd = dirfd(d); |
| 171 | |
| 172 | fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC); |
| 173 | if(fd >= 0) { |
| 174 | write(fd, "add\n", 4); |
| 175 | close(fd); |
| 176 | } |
| 177 | |
| 178 | while((de = readdir(d))) { |
| 179 | DIR *d2; |
| 180 | |
| 181 | if (de->d_name[0] == '.') |
| 182 | continue; |
| 183 | |
| 184 | if (de->d_type != DT_DIR && lvl > 0) |
| 185 | continue; |
| 186 | |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 187 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
Wei Wang | 2edbe28 | 2017-03-06 17:27:05 -0800 | [diff] [blame] | 188 | if(fd < 0) |
| 189 | continue; |
| 190 | |
| 191 | d2 = fdopendir(fd); |
| 192 | if(d2 == 0) |
| 193 | close(fd); |
| 194 | else { |
| 195 | do_coldboot(d2, lvl + 1); |
| 196 | closedir(d2); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void coldboot(const char *path) { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 202 | ATRACE_NAME("coldboot"); |
Wei Wang | 2edbe28 | 2017-03-06 17:27:05 -0800 | [diff] [blame] | 203 | DIR *d = opendir(path); |
| 204 | if(d) { |
| 205 | do_coldboot(d, 0); |
| 206 | closedir(d); |
| 207 | } |
| 208 | } |
| 209 | |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 210 | static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota) { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 211 | ATRACE_NAME("process_config"); |
| 212 | |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 213 | fstab_default = fs_mgr_read_fstab_default(); |
| 214 | if (!fstab_default) { |
Bowgo Tsai | e8fb6c3 | 2017-03-09 23:11:33 +0800 | [diff] [blame] | 215 | PLOG(ERROR) << "Failed to open default fstab"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 219 | /* Loop through entries looking for ones that vold manages */ |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 220 | *has_adoptable = false; |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 221 | *has_quota = false; |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 222 | for (int i = 0; i < fstab_default->num_entries; i++) { |
| 223 | auto rec = &fstab_default->recs[i]; |
| 224 | if (fs_mgr_is_quota(rec)) { |
Jeff Sharkey | 95a92f9 | 2017-07-17 13:57:18 -0600 | [diff] [blame] | 225 | *has_quota = true; |
| 226 | } |
| 227 | |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 228 | if (fs_mgr_is_voldmanaged(rec)) { |
| 229 | if (fs_mgr_is_nonremovable(rec)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 230 | LOG(WARNING) << "nonremovable no longer supported; ignoring volume"; |
| 231 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 232 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 233 | |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 234 | std::string sysPattern(rec->blk_device); |
| 235 | std::string nickname(rec->label); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 236 | int flags = 0; |
| 237 | |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 238 | if (fs_mgr_is_encryptable(rec)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 239 | flags |= android::vold::Disk::Flags::kAdoptable; |
Dimitry Ivanov | c976e73 | 2017-01-19 12:48:27 -0800 | [diff] [blame] | 240 | *has_adoptable = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 241 | } |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 242 | if (fs_mgr_is_noemulatedsd(rec) |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 243 | || android::base::GetBoolProperty("vold.debug.default_primary", false)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 244 | flags |= android::vold::Disk::Flags::kDefaultPrimary; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 245 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 246 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 247 | vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>( |
| 248 | new VolumeManager::DiskSource(sysPattern, nickname, flags))); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 251 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 252 | } |