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 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <dirent.h> |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 26 | #include <fs_mgr.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 27 | |
| 28 | #define LOG_TAG "Vold" |
| 29 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 30 | #include <base/logging.h> |
| 31 | #include <base/stringprintf.h> |
Ken Sumrall | b9738ea | 2013-03-19 19:42:30 -0700 | [diff] [blame] | 32 | #include "cutils/klog.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 33 | #include "cutils/log.h" |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 34 | #include "cutils/properties.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 36 | #include "Disk.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 37 | #include "VolumeManager.h" |
| 38 | #include "CommandListener.h" |
| 39 | #include "NetlinkManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 40 | #include "DirectVolume.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 41 | #include "cryptfs.h" |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 42 | #include "sehandle.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 43 | |
| 44 | static int process_config(VolumeManager *vm); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 45 | static void coldboot(const char *path); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 46 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 47 | //#define DEBUG_FSTAB "/data/local/tmp/fstab.debug" |
| 48 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 49 | struct fstab *fstab; |
| 50 | |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 51 | struct selabel_handle *sehandle; |
| 52 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 53 | using android::base::StringPrintf; |
| 54 | |
| 55 | int main(int argc, char* argv[]) { |
| 56 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 57 | android::base::InitLogging(argv); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 58 | |
| 59 | VolumeManager *vm; |
| 60 | CommandListener *cl; |
| 61 | NetlinkManager *nm; |
| 62 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 63 | SLOGI("Vold 2.1 (the revenge) firing up"); |
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(); |
| 66 | if (sehandle) |
| 67 | selinux_android_set_sehandle(sehandle); |
| 68 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 69 | mkdir("/dev/block/vold", 0755); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 70 | |
Ken Sumrall | b9738ea | 2013-03-19 19:42:30 -0700 | [diff] [blame] | 71 | /* For when cryptfs checks and mounts an encrypted filesystem */ |
| 72 | klog_set_level(6); |
| 73 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 74 | /* Create our singleton managers */ |
| 75 | if (!(vm = VolumeManager::Instance())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 76 | SLOGE("Unable to create VolumeManager"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 77 | exit(1); |
| 78 | }; |
| 79 | |
| 80 | if (!(nm = NetlinkManager::Instance())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 81 | SLOGE("Unable to create NetlinkManager"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 82 | exit(1); |
| 83 | }; |
| 84 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 85 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 86 | cl = new CommandListener(); |
| 87 | vm->setBroadcaster((SocketListener *) cl); |
| 88 | nm->setBroadcaster((SocketListener *) cl); |
| 89 | |
| 90 | if (vm->start()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 91 | SLOGE("Unable to start VolumeManager (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | if (process_config(vm)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 96 | SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if (nm->start()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 100 | SLOGE("Unable to start NetlinkManager (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 101 | exit(1); |
| 102 | } |
| 103 | |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 104 | coldboot("/sys/block"); |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 105 | // coldboot("/sys/class/switch"); |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 106 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 107 | /* |
| 108 | * Now that we're up, we can respond to commands |
| 109 | */ |
| 110 | if (cl->startListener()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 111 | SLOGE("Unable to start CommandListener (%s)", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 112 | exit(1); |
| 113 | } |
| 114 | |
| 115 | // Eventually we'll become the monitoring thread |
| 116 | while(1) { |
| 117 | sleep(1000); |
| 118 | } |
| 119 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 120 | SLOGI("Vold exiting"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 121 | exit(0); |
| 122 | } |
| 123 | |
San Mehat | 3578c41 | 2009-10-12 14:51:52 -0700 | [diff] [blame] | 124 | static void do_coldboot(DIR *d, int lvl) |
| 125 | { |
| 126 | struct dirent *de; |
| 127 | int dfd, fd; |
| 128 | |
| 129 | dfd = dirfd(d); |
| 130 | |
| 131 | fd = openat(dfd, "uevent", O_WRONLY); |
| 132 | if(fd >= 0) { |
| 133 | write(fd, "add\n", 4); |
| 134 | close(fd); |
| 135 | } |
| 136 | |
| 137 | while((de = readdir(d))) { |
| 138 | DIR *d2; |
| 139 | |
| 140 | if (de->d_name[0] == '.') |
| 141 | continue; |
| 142 | |
| 143 | if (de->d_type != DT_DIR && lvl > 0) |
| 144 | continue; |
| 145 | |
| 146 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 147 | if(fd < 0) |
| 148 | continue; |
| 149 | |
| 150 | d2 = fdopendir(fd); |
| 151 | if(d2 == 0) |
| 152 | close(fd); |
| 153 | else { |
| 154 | do_coldboot(d2, lvl + 1); |
| 155 | closedir(d2); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void coldboot(const char *path) |
| 161 | { |
| 162 | DIR *d = opendir(path); |
| 163 | if(d) { |
| 164 | do_coldboot(d, 0); |
| 165 | closedir(d); |
| 166 | } |
| 167 | } |
| 168 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 169 | static int process_config(VolumeManager *vm) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 170 | { |
| 171 | char hardware[PROPERTY_VALUE_MAX]; |
| 172 | property_get("ro.hardware", hardware, ""); |
| 173 | std::string fstab_filename(StringPrintf("/fstab.%s", hardware)); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 174 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 175 | #ifdef DEBUG_FSTAB |
| 176 | if (access(DEBUG_FSTAB, R_OK) == 0) { |
| 177 | LOG(DEBUG) << "Found debug fstab; switching!"; |
| 178 | fstab_filename = DEBUG_FSTAB; |
| 179 | } |
| 180 | #endif |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 181 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 182 | fstab = fs_mgr_read_fstab(fstab_filename.c_str()); |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 183 | if (!fstab) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 184 | PLOG(ERROR) << "Failed to open " << fstab_filename; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 188 | /* Loop through entries looking for ones that vold manages */ |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 189 | for (int i = 0; i < fstab->num_entries; i++) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 190 | if (fs_mgr_is_voldmanaged(&fstab->recs[i])) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 191 | if (fs_mgr_is_nonremovable(&fstab->recs[i])) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 192 | LOG(WARNING) << "nonremovable no longer supported; ignoring volume"; |
| 193 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 194 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 195 | |
| 196 | std::string sysPattern(fstab->recs[i].blk_device); |
| 197 | std::string nickname(fstab->recs[i].label); |
| 198 | int flags = 0; |
| 199 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 200 | if (fs_mgr_is_encryptable(&fstab->recs[i])) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 201 | flags |= android::vold::Disk::Flags::kAdoptable; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 202 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 203 | if (fs_mgr_is_noemulatedsd(&fstab->recs[i])) { |
| 204 | flags |= android::vold::Disk::Flags::kDefaultPrimary; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 205 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 206 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 207 | vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>( |
| 208 | new VolumeManager::DiskSource(sysPattern, nickname, flags))); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame^] | 212 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 213 | } |