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 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 20 | #include <fts.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 21 | #include <mntent.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mount.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 29 | #include <sys/sysmacros.h> |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 30 | #include <sys/wait.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 31 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 32 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 33 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 34 | |
| 35 | #define LOG_TAG "Vold" |
| 36 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 37 | #include <openssl/md5.h> |
| 38 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 39 | #include <android-base/logging.h> |
Jeff Vander Stoep | a997db7 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 40 | #include <android-base/parseint.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
Tri Vo | e98adee | 2018-04-16 14:27:10 -0700 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 43 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 44 | #include <cutils/log.h> |
| 45 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 46 | #include <selinux/android.h> |
| 47 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 48 | #include <sysutils/NetlinkEvent.h> |
| 49 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> |
| 51 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 52 | #include "Benchmark.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 53 | #include "EmulatedVolume.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 54 | #include "VolumeManager.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 55 | #include "NetlinkManager.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 56 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 57 | #include "Loop.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 58 | #include "fs/Ext4.h" |
| 59 | #include "fs/Vfat.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 60 | #include "Utils.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 61 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 62 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 63 | #include "Asec.h" |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 64 | #include "VoldUtil.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 65 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 66 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 67 | #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" |
| 68 | |
Chih-Hung Hsieh | cc5d580 | 2016-05-11 15:05:05 -0700 | [diff] [blame] | 69 | #define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\ |
| 70 | + ((number) & (~((1U << (po2)) - 1)))) |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 71 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 72 | using android::base::StringPrintf; |
| 73 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Path to external storage where *only* root can access ASEC image files |
| 76 | */ |
| 77 | const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec"; |
| 78 | |
| 79 | /* |
| 80 | * Path to internal storage where *only* root can access ASEC image files |
| 81 | */ |
| 82 | const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec"; |
| 83 | |
| 84 | /* |
| 85 | * Path to where secure containers are mounted |
| 86 | */ |
| 87 | const char *VolumeManager::ASECDIR = "/mnt/asec"; |
| 88 | |
| 89 | /* |
| 90 | * Path to where OBBs are mounted |
| 91 | */ |
| 92 | const char *VolumeManager::LOOPDIR = "/mnt/obb"; |
| 93 | |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 94 | bool VolumeManager::shutting_down = false; |
| 95 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 96 | static const char* kPathUserMount = "/mnt/user"; |
| 97 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 98 | |
| 99 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 100 | |
| 101 | /* 512MiB is large enough for testing purposes */ |
| 102 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 103 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 104 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 105 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 106 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 107 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 108 | /* writes superblock at end of file or device given by name */ |
| 109 | static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 110 | int sbfd = open(name, O_RDWR | O_CLOEXEC); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 111 | if (sbfd < 0) { |
| 112 | SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno)); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 117 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
| 118 | close(sbfd); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 123 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
| 124 | close(sbfd); |
| 125 | return -1; |
| 126 | } |
| 127 | close(sbfd); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 131 | static unsigned long adjustSectorNumExt4(unsigned long numSectors) { |
Daniel Rosenberg | e9196fe | 2014-06-10 17:16:03 -0700 | [diff] [blame] | 132 | // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for |
| 133 | // preventing costly operations or unexpected ENOSPC error. |
| 134 | // Ext4::format() uses default block size without clustering. |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 135 | unsigned long clusterSectors = 4096 / 512; |
| 136 | unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0); |
Daniel Rosenberg | e9196fe | 2014-06-10 17:16:03 -0700 | [diff] [blame] | 137 | numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 138 | return ROUND_UP_POWER_OF_2(numSectors, 3); |
| 139 | } |
| 140 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 141 | static unsigned long adjustSectorNumFAT(unsigned long numSectors) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Add some headroom |
| 144 | */ |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 145 | unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 146 | numSectors += fatSize + 2; |
| 147 | /* |
| 148 | * FAT is aligned to 32 kb with 512b sectors. |
| 149 | */ |
| 150 | return ROUND_UP_POWER_OF_2(numSectors, 6); |
| 151 | } |
| 152 | |
| 153 | static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) { |
| 154 | if (Loop::lookupActive(idHash, buffer, len)) { |
| 155 | if (Loop::create(idHash, asecFileName, buffer, len)) { |
| 156 | SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno)); |
| 157 | return -1; |
| 158 | } |
| 159 | if (debug) { |
| 160 | SLOGD("New loop device created at %s", buffer); |
| 161 | } |
| 162 | } else { |
| 163 | if (debug) { |
| 164 | SLOGD("Found active loopback for %s at %s", asecFileName, buffer); |
| 165 | } |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 170 | static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , unsigned long numImgSectors, bool* createdDMDevice, bool debug) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 171 | if (strcmp(key, "none")) { |
| 172 | if (Devmapper::lookupActive(idHash, buffer, len)) { |
| 173 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, |
| 174 | buffer, len)) { |
| 175 | SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno)); |
| 176 | return -1; |
| 177 | } |
| 178 | if (debug) { |
| 179 | SLOGD("New devmapper instance created at %s", buffer); |
| 180 | } |
| 181 | } else { |
| 182 | if (debug) { |
| 183 | SLOGD("Found active devmapper for %s at %s", asecFileName, buffer); |
| 184 | } |
| 185 | } |
| 186 | *createdDMDevice = true; |
| 187 | } else { |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 188 | strlcpy(buffer, loopDevice, len); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 189 | *createdDMDevice = false; |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static void waitForDevMapper(const char *dmDevice) { |
| 195 | /* |
| 196 | * Wait for the device mapper node to be created. Sometimes it takes a |
| 197 | * while. Wait for up to 1 second. We could also inspect incoming uevents, |
| 198 | * but that would take more effort. |
| 199 | */ |
| 200 | int tries = 25; |
| 201 | while (tries--) { |
| 202 | if (!access(dmDevice, F_OK) || errno != ENOENT) { |
| 203 | break; |
| 204 | } |
| 205 | usleep(40 * 1000); |
| 206 | } |
| 207 | } |
| 208 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 209 | VolumeManager *VolumeManager::sInstance = NULL; |
| 210 | |
| 211 | VolumeManager *VolumeManager::Instance() { |
| 212 | if (!sInstance) |
| 213 | sInstance = new VolumeManager(); |
| 214 | return sInstance; |
| 215 | } |
| 216 | |
| 217 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 218 | mDebug = false; |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 219 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 220 | mBroadcaster = NULL; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 221 | mUmsSharingCount = 0; |
| 222 | mSavedDirtyRatio = -1; |
| 223 | // set dirty ratio to 0 when UMS is active |
| 224 | mUmsDirtyRatio = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 228 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 231 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 232 | static const char* digits = "0123456789abcdef"; |
| 233 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 234 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 235 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 236 | if (buffer == NULL) { |
| 237 | SLOGE("Destination buffer is NULL"); |
| 238 | errno = ESPIPE; |
| 239 | return NULL; |
| 240 | } else if (id == NULL) { |
| 241 | SLOGE("Source buffer is NULL"); |
| 242 | errno = ESPIPE; |
| 243 | return NULL; |
| 244 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 245 | SLOGE("Target hash buffer size < %d bytes (%zu)", |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 246 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 247 | errno = ESPIPE; |
| 248 | return NULL; |
| 249 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 250 | |
| 251 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 252 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 253 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 254 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 255 | *p++ = digits[sig[i] >> 4]; |
| 256 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 257 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 258 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 259 | |
| 260 | return buffer; |
| 261 | } |
| 262 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 263 | int VolumeManager::updateVirtualDisk() { |
| 264 | if (property_get_bool(kPropVirtualDisk, false)) { |
| 265 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 266 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 267 | } |
| 268 | |
| 269 | if (mVirtualDisk == nullptr) { |
| 270 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 271 | LOG(ERROR) << "Failed to create virtual disk"; |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | struct stat buf; |
| 276 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 277 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual", |
| 282 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
| 283 | disk->create(); |
| 284 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
| 285 | mDisks.push_back(mVirtualDisk); |
| 286 | } |
| 287 | } else { |
| 288 | if (mVirtualDisk != nullptr) { |
| 289 | dev_t device = mVirtualDisk->getDevice(); |
| 290 | |
| 291 | auto i = mDisks.begin(); |
| 292 | while (i != mDisks.end()) { |
| 293 | if ((*i)->getDevice() == device) { |
| 294 | (*i)->destroy(); |
| 295 | i = mDisks.erase(i); |
| 296 | } else { |
| 297 | ++i; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 302 | mVirtualDisk = nullptr; |
| 303 | } |
| 304 | |
| 305 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 306 | unlink(kPathVirtualDisk); |
| 307 | } |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 312 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 313 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 314 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 315 | } |
| 316 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 317 | int VolumeManager::start() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 318 | // Always start from a clean slate by unmounting everything in |
| 319 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 320 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 321 | |
| 322 | // Assume that we always have an emulated volume on internal |
| 323 | // storage; the framework will decide if it should be mounted. |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 324 | CHECK(mInternalEmulated == nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 325 | mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>( |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 326 | new android::vold::EmulatedVolume("/data/media")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 327 | mInternalEmulated->create(); |
| 328 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 329 | // Consider creating a virtual disk |
| 330 | updateVirtualDisk(); |
| 331 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | int VolumeManager::stop() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 336 | CHECK(mInternalEmulated != nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 337 | mInternalEmulated->destroy(); |
| 338 | mInternalEmulated = nullptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 342 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 343 | std::lock_guard<std::mutex> lock(mLock); |
| 344 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 345 | if (mDebug) { |
| 346 | LOG(VERBOSE) << "----------------"; |
| 347 | LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction(); |
| 348 | evt->dump(); |
| 349 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 350 | |
Mateusz Nowak | 6440379 | 2015-08-03 16:39:19 +0200 | [diff] [blame] | 351 | std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):""); |
| 352 | std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 353 | |
| 354 | if (devType != "disk") return; |
| 355 | |
| 356 | int major = atoi(evt->findParam("MAJOR")); |
| 357 | int minor = atoi(evt->findParam("MINOR")); |
| 358 | dev_t device = makedev(major, minor); |
| 359 | |
| 360 | switch (evt->getAction()) { |
| 361 | case NetlinkEvent::Action::kAdd: { |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 362 | for (const auto& source : mDiskSources) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 363 | if (source->matches(eventPath)) { |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 364 | // For now, assume that MMC and virtio-blk (the latter is |
| 365 | // emulator-specific; see Disk.cpp for details) devices are SD, |
| 366 | // and that everything else is USB |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 367 | int flags = source->getFlags(); |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 368 | if (major == kMajorBlockMmc |
| 369 | || (android::vold::IsRunningInEmulator() |
| 370 | && major >= (int) kMajorBlockExperimentalMin |
| 371 | && major <= (int) kMajorBlockExperimentalMax)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 372 | flags |= android::vold::Disk::Flags::kSd; |
| 373 | } else { |
| 374 | flags |= android::vold::Disk::Flags::kUsb; |
| 375 | } |
| 376 | |
| 377 | auto disk = new android::vold::Disk(eventPath, device, |
| 378 | source->getNickname(), flags); |
| 379 | disk->create(); |
| 380 | mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk)); |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | break; |
| 385 | } |
| 386 | case NetlinkEvent::Action::kChange: { |
Jeff Sharkey | 7d9d011 | 2015-04-14 23:14:23 -0700 | [diff] [blame] | 387 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 388 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 389 | if (disk->getDevice() == device) { |
| 390 | disk->readMetadata(); |
| 391 | disk->readPartitions(); |
| 392 | } |
| 393 | } |
| 394 | break; |
| 395 | } |
| 396 | case NetlinkEvent::Action::kRemove: { |
| 397 | auto i = mDisks.begin(); |
| 398 | while (i != mDisks.end()) { |
| 399 | if ((*i)->getDevice() == device) { |
| 400 | (*i)->destroy(); |
| 401 | i = mDisks.erase(i); |
| 402 | } else { |
| 403 | ++i; |
| 404 | } |
| 405 | } |
| 406 | break; |
| 407 | } |
| 408 | default: { |
| 409 | LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction(); |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 416 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 417 | mDiskSources.push_back(diskSource); |
| 418 | } |
| 419 | |
| 420 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 421 | for (auto disk : mDisks) { |
| 422 | if (disk->getId() == id) { |
| 423 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 426 | return nullptr; |
| 427 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 428 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 429 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 430 | // Vold could receive "mount" after "shutdown" command in the extreme case. |
| 431 | // If this happens, mInternalEmulated will equal nullptr and |
| 432 | // we need to deal with it in order to avoid null pointer crash. |
| 433 | if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 434 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 435 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 436 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 437 | auto vol = disk->findVolume(id); |
| 438 | if (vol != nullptr) { |
| 439 | return vol; |
| 440 | } |
| 441 | } |
| 442 | return nullptr; |
| 443 | } |
| 444 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 445 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 446 | std::list<std::string>& list) { |
| 447 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 448 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 449 | disk->listVolumes(type, list); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) { |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 454 | std::string path; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 455 | if (id == "private" || id == "null") { |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 456 | path = "/data"; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 457 | } else { |
| 458 | auto vol = findVolume(id); |
| 459 | if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) { |
| 460 | path = vol->getPath(); |
| 461 | } |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | if (path.empty()) { |
| 465 | LOG(WARNING) << "Failed to find volume for " << id; |
| 466 | return -1; |
| 467 | } |
| 468 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 469 | return android::vold::BenchmarkPrivate(path); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 472 | int VolumeManager::forgetPartition(const std::string& partGuid) { |
| 473 | std::string normalizedGuid; |
| 474 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 475 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 480 | if (unlink(keyPath.c_str()) != 0) { |
| 481 | LOG(ERROR) << "Failed to unlink " << keyPath; |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 488 | int VolumeManager::linkPrimary(userid_t userId) { |
| 489 | std::string source(mPrimary->getPath()); |
| 490 | if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) { |
| 491 | source = StringPrintf("%s/%d", source.c_str(), userId); |
Jeff Sharkey | 32679a8 | 2015-07-21 14:22:01 -0700 | [diff] [blame] | 492 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 496 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 497 | if (errno != ENOENT) { |
| 498 | SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno)); |
| 499 | } |
| 500 | } |
Jeff Sharkey | 1bfb375 | 2015-04-29 15:22:23 -0700 | [diff] [blame] | 501 | LOG(DEBUG) << "Linking " << source << " to " << target; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 502 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
| 503 | SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(), |
| 504 | strerror(errno)); |
| 505 | return -errno; |
| 506 | } |
| 507 | return 0; |
| 508 | } |
| 509 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 510 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
| 511 | mAddedUsers[userId] = userSerialNumber; |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | int VolumeManager::onUserRemoved(userid_t userId) { |
| 516 | mAddedUsers.erase(userId); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | int VolumeManager::onUserStarted(userid_t userId) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 521 | // Note that sometimes the system will spin up processes from Zygote |
| 522 | // before actually starting the user, so we're okay if Zygote |
| 523 | // already created this directory. |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 524 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 525 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 526 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 527 | mStartedUsers.insert(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 528 | if (mPrimary) { |
| 529 | linkPrimary(userId); |
| 530 | } |
| 531 | return 0; |
| 532 | } |
| 533 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 534 | int VolumeManager::onUserStopped(userid_t userId) { |
| 535 | mStartedUsers.erase(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 540 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 541 | for (userid_t userId : mStartedUsers) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 542 | linkPrimary(userId); |
| 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 547 | static int unmount_tree(const char* path) { |
| 548 | size_t path_len = strlen(path); |
| 549 | |
| 550 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 551 | if (fp == NULL) { |
| 552 | ALOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 553 | return -errno; |
| 554 | } |
| 555 | |
| 556 | // Some volumes can be stacked on each other, so force unmount in |
| 557 | // reverse order to give us the best chance of success. |
| 558 | std::list<std::string> toUnmount; |
| 559 | mntent* mentry; |
| 560 | while ((mentry = getmntent(fp)) != NULL) { |
| 561 | if (strncmp(mentry->mnt_dir, path, path_len) == 0) { |
| 562 | toUnmount.push_front(std::string(mentry->mnt_dir)); |
| 563 | } |
| 564 | } |
| 565 | endmntent(fp); |
| 566 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 567 | for (const auto& path : toUnmount) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 568 | if (umount2(path.c_str(), MNT_DETACH)) { |
| 569 | ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno)); |
| 570 | } |
| 571 | } |
| 572 | return 0; |
| 573 | } |
| 574 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 575 | int VolumeManager::remountUid(uid_t uid, const std::string& mode) { |
| 576 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 577 | |
| 578 | DIR* dir; |
| 579 | struct dirent* de; |
| 580 | char rootName[PATH_MAX]; |
| 581 | char pidName[PATH_MAX]; |
| 582 | int pidFd; |
| 583 | int nsFd; |
| 584 | struct stat sb; |
| 585 | pid_t child; |
| 586 | |
| 587 | if (!(dir = opendir("/proc"))) { |
| 588 | PLOG(ERROR) << "Failed to opendir"; |
| 589 | return -1; |
| 590 | } |
| 591 | |
| 592 | // Figure out root namespace to compare against below |
Daichi Hirono | 10d3488 | 2016-01-29 14:33:51 +0900 | [diff] [blame] | 593 | if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 594 | PLOG(ERROR) << "Failed to readlink"; |
| 595 | closedir(dir); |
| 596 | return -1; |
| 597 | } |
| 598 | |
| 599 | // Poke through all running PIDs look for apps running as UID |
| 600 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | a997db7 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 601 | pid_t pid; |
| 602 | if (de->d_type != DT_DIR) continue; |
| 603 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 604 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 605 | pidFd = -1; |
| 606 | nsFd = -1; |
| 607 | |
| 608 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 609 | if (pidFd < 0) { |
| 610 | goto next; |
| 611 | } |
| 612 | if (fstat(pidFd, &sb) != 0) { |
| 613 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 614 | goto next; |
| 615 | } |
| 616 | if (sb.st_uid != uid) { |
| 617 | goto next; |
| 618 | } |
| 619 | |
| 620 | // Matches so far, but refuse to touch if in root namespace |
| 621 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Daichi Hirono | 10d3488 | 2016-01-29 14:33:51 +0900 | [diff] [blame] | 622 | if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 623 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 624 | goto next; |
| 625 | } |
| 626 | if (!strcmp(rootName, pidName)) { |
| 627 | LOG(WARNING) << "Skipping due to root namespace"; |
| 628 | goto next; |
| 629 | } |
| 630 | |
| 631 | // We purposefully leave the namespace open across the fork |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 632 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 633 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 634 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 635 | goto next; |
| 636 | } |
| 637 | |
| 638 | if (!(child = fork())) { |
| 639 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 640 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 641 | _exit(1); |
| 642 | } |
| 643 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 644 | unmount_tree("/storage"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 645 | |
| 646 | std::string storageSource; |
| 647 | if (mode == "default") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 648 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 649 | } else if (mode == "read") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 650 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 651 | } else if (mode == "write") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 652 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 653 | } else { |
| 654 | // Sane default of no storage visible |
| 655 | _exit(0); |
| 656 | } |
| 657 | if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage", |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 658 | NULL, MS_BIND | MS_REC, NULL)) == -1) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 659 | PLOG(ERROR) << "Failed to mount " << storageSource << " for " |
| 660 | << de->d_name; |
| 661 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 662 | } |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 663 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, |
| 664 | MS_REC | MS_SLAVE, NULL)) == -1) { |
| 665 | PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " |
| 666 | << de->d_name; |
| 667 | _exit(1); |
| 668 | } |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 669 | |
| 670 | // Mount user-specific symlink helper into place |
| 671 | userid_t user_id = multiuser_get_user_id(uid); |
| 672 | std::string userSource(StringPrintf("/mnt/user/%d", user_id)); |
| 673 | if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self", |
| 674 | NULL, MS_BIND, NULL)) == -1) { |
| 675 | PLOG(ERROR) << "Failed to mount " << userSource << " for " |
| 676 | << de->d_name; |
| 677 | _exit(1); |
| 678 | } |
| 679 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 680 | _exit(0); |
| 681 | } |
| 682 | |
| 683 | if (child == -1) { |
| 684 | PLOG(ERROR) << "Failed to fork"; |
| 685 | goto next; |
| 686 | } else { |
| 687 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 688 | } |
| 689 | |
| 690 | next: |
| 691 | close(nsFd); |
| 692 | close(pidFd); |
| 693 | } |
| 694 | closedir(dir); |
| 695 | return 0; |
| 696 | } |
| 697 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 698 | int VolumeManager::reset() { |
| 699 | // Tear down all existing disks/volumes and start from a blank slate so |
| 700 | // newly connected framework hears all events. |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 701 | if (mInternalEmulated != nullptr) { |
| 702 | mInternalEmulated->destroy(); |
| 703 | mInternalEmulated->create(); |
| 704 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 705 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 706 | disk->destroy(); |
| 707 | disk->create(); |
| 708 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 709 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 710 | mAddedUsers.clear(); |
| 711 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 715 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 716 | int VolumeManager::shutdown() { |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 717 | if (mInternalEmulated == nullptr) { |
| 718 | return 0; // already shutdown |
| 719 | } |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 720 | shutting_down = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 721 | mInternalEmulated->destroy(); |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 722 | mInternalEmulated = nullptr; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 723 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 724 | disk->destroy(); |
| 725 | } |
| 726 | mDisks.clear(); |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 727 | shutting_down = false; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 728 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 731 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 732 | std::lock_guard<std::mutex> lock(mLock); |
| 733 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 734 | // First, try gracefully unmounting all known devices |
| 735 | if (mInternalEmulated != nullptr) { |
| 736 | mInternalEmulated->unmount(); |
| 737 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 738 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 739 | disk->unmountAll(); |
| 740 | } |
| 741 | |
| 742 | // Worst case we might have some stale mounts lurking around, so |
| 743 | // force unmount those just to be safe. |
| 744 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 745 | if (fp == NULL) { |
| 746 | SLOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 747 | return -errno; |
| 748 | } |
| 749 | |
| 750 | // Some volumes can be stacked on each other, so force unmount in |
| 751 | // reverse order to give us the best chance of success. |
| 752 | std::list<std::string> toUnmount; |
| 753 | mntent* mentry; |
| 754 | while ((mentry = getmntent(fp)) != NULL) { |
Tri Vo | e98adee | 2018-04-16 14:27:10 -0700 | [diff] [blame] | 755 | auto test = std::string(mentry->mnt_dir); |
| 756 | if ((android::base::StartsWith(test, "/mnt/") && |
Bowgo Tsai | c0cd37b | 2018-06-29 10:31:07 +0800 | [diff] [blame^] | 757 | !android::base::StartsWith(test, "/mnt/vendor") && |
| 758 | !android::base::StartsWith(test, "/mnt/product")) || |
Tri Vo | e98adee | 2018-04-16 14:27:10 -0700 | [diff] [blame] | 759 | android::base::StartsWith(test, "/storage/")) { |
| 760 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | endmntent(fp); |
| 764 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 765 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 766 | SLOGW("Tearing down stale mount %s", path.c_str()); |
| 767 | android::vold::ForceUnmount(path); |
| 768 | } |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 773 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 774 | char idHash[33]; |
| 775 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 776 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 777 | return -1; |
| 778 | } |
| 779 | |
| 780 | memset(mountPath, 0, mountPathLen); |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 781 | int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 782 | if ((written < 0) || (written >= mountPathLen)) { |
| 783 | errno = EINVAL; |
| 784 | return -1; |
| 785 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 786 | |
| 787 | if (access(mountPath, F_OK)) { |
| 788 | errno = ENOENT; |
| 789 | return -1; |
| 790 | } |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 795 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 796 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 797 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 798 | if (!isLegalAsecId(id)) { |
| 799 | SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id); |
| 800 | errno = EINVAL; |
| 801 | return -1; |
| 802 | } |
| 803 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 804 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 805 | SLOGE("Couldn't find ASEC %s", id); |
| 806 | return -1; |
| 807 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 808 | |
| 809 | memset(buffer, 0, maxlen); |
| 810 | if (access(asecFileName, F_OK)) { |
| 811 | errno = ENOENT; |
| 812 | return -1; |
| 813 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 814 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 815 | int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 816 | if ((written < 0) || (written >= maxlen)) { |
| 817 | SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id); |
| 818 | errno = EINVAL; |
| 819 | return -1; |
| 820 | } |
| 821 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 822 | return 0; |
| 823 | } |
| 824 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 825 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 826 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 827 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 828 | if (!isLegalAsecId(id)) { |
| 829 | SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id); |
| 830 | errno = EINVAL; |
| 831 | return -1; |
| 832 | } |
| 833 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 834 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 835 | SLOGE("Couldn't find ASEC %s", id); |
| 836 | return -1; |
| 837 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 838 | |
| 839 | memset(buffer, 0, maxlen); |
| 840 | if (access(asecFileName, F_OK)) { |
| 841 | errno = ENOENT; |
| 842 | return -1; |
| 843 | } |
| 844 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 845 | int written = snprintf(buffer, maxlen, "%s", asecFileName); |
| 846 | if ((written < 0) || (written >= maxlen)) { |
| 847 | errno = EINVAL; |
| 848 | return -1; |
| 849 | } |
| 850 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 854 | int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype, |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 855 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 856 | struct asec_superblock sb; |
| 857 | memset(&sb, 0, sizeof(sb)); |
| 858 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 859 | if (!isLegalAsecId(id)) { |
| 860 | SLOGE("createAsec: Invalid asec id \"%s\"", id); |
| 861 | errno = EINVAL; |
| 862 | return -1; |
| 863 | } |
| 864 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 865 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 866 | bool usingExt4 = false; |
| 867 | if (wantFilesystem) { |
| 868 | usingExt4 = !strcmp(fstype, "ext4"); |
| 869 | if (usingExt4) { |
| 870 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 871 | } else if (strcmp(fstype, "fat")) { |
| 872 | SLOGE("Invalid filesystem type %s", fstype); |
| 873 | errno = EINVAL; |
| 874 | return -1; |
| 875 | } |
| 876 | } |
| 877 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 878 | sb.magic = ASEC_SB_MAGIC; |
| 879 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 880 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 881 | if (numSectors < ((1024*1024)/512)) { |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 882 | SLOGE("Invalid container size specified (%lu sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 883 | errno = EINVAL; |
| 884 | return -1; |
| 885 | } |
| 886 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 887 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 888 | |
| 889 | if (!findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 890 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 891 | asecFileName, strerror(errno)); |
| 892 | errno = EADDRINUSE; |
| 893 | return -1; |
| 894 | } |
| 895 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 896 | const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 897 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 898 | int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id); |
| 899 | if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) { |
| 900 | errno = EINVAL; |
| 901 | return -1; |
| 902 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 903 | |
| 904 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 905 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 906 | asecFileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 907 | errno = EADDRINUSE; |
| 908 | return -1; |
| 909 | } |
| 910 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 911 | unsigned long numImgSectors; |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 912 | if (usingExt4) |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 913 | numImgSectors = adjustSectorNumExt4(numSectors); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 914 | else |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 915 | numImgSectors = adjustSectorNumFAT(numSectors); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 916 | |
| 917 | // Add +1 for our superblock which is at the end |
| 918 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 919 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 920 | return -1; |
| 921 | } |
| 922 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 923 | char idHash[33]; |
| 924 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 925 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 926 | unlink(asecFileName); |
| 927 | return -1; |
| 928 | } |
| 929 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 930 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 931 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 932 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 933 | unlink(asecFileName); |
| 934 | return -1; |
| 935 | } |
| 936 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 937 | char dmDevice[255]; |
| 938 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 939 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 940 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 941 | // XXX: This is all we support for now |
| 942 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 943 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 944 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 945 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 946 | Loop::destroyByDevice(loopDevice); |
| 947 | unlink(asecFileName); |
| 948 | return -1; |
| 949 | } |
| 950 | cleanupDm = true; |
| 951 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 952 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 953 | strlcpy(dmDevice, loopDevice, sizeof(dmDevice)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 954 | } |
| 955 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 956 | /* |
| 957 | * Drop down the superblock at the end of the file |
| 958 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 959 | if (writeSuperBlock(loopDevice, &sb, numImgSectors)) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 960 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 961 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 962 | } |
| 963 | Loop::destroyByDevice(loopDevice); |
| 964 | unlink(asecFileName); |
| 965 | return -1; |
| 966 | } |
| 967 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 968 | if (wantFilesystem) { |
| 969 | int formatStatus; |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 970 | char mountPoint[255]; |
| 971 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 972 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 973 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 974 | SLOGE("ASEC fs format failed: couldn't construct mountPoint"); |
| 975 | if (cleanupDm) { |
| 976 | Devmapper::destroy(idHash); |
| 977 | } |
| 978 | Loop::destroyByDevice(loopDevice); |
| 979 | unlink(asecFileName); |
| 980 | return -1; |
| 981 | } |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 982 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 983 | if (usingExt4) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 984 | formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 985 | } else { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 986 | formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 987 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 988 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 989 | if (formatStatus < 0) { |
| 990 | SLOGE("ASEC fs format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 991 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 992 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 993 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 994 | Loop::destroyByDevice(loopDevice); |
| 995 | unlink(asecFileName); |
| 996 | return -1; |
| 997 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 998 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 999 | if (mkdir(mountPoint, 0000)) { |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 1000 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1001 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 1002 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1003 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 1004 | } |
| 1005 | Loop::destroyByDevice(loopDevice); |
| 1006 | unlink(asecFileName); |
| 1007 | return -1; |
| 1008 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1009 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 1010 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1011 | int mountStatus; |
| 1012 | if (usingExt4) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1013 | mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint, |
| 1014 | false, false, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1015 | } else { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1016 | mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint, |
| 1017 | false, false, false, ownerUid, 0, 0000, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1021 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 1022 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1023 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 1024 | } |
| 1025 | Loop::destroyByDevice(loopDevice); |
| 1026 | unlink(asecFileName); |
| 1027 | return -1; |
| 1028 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1029 | |
| 1030 | if (usingExt4) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1031 | int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1032 | if (dirfd >= 0) { |
| 1033 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 1034 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 1035 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 1036 | } |
| 1037 | close(dirfd); |
| 1038 | } |
| 1039 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 1040 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1041 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1042 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1043 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1044 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1045 | return 0; |
| 1046 | } |
| 1047 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 1048 | int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1049 | char asecFileName[255]; |
| 1050 | char mountPoint[255]; |
| 1051 | bool cleanupDm = false; |
| 1052 | |
| 1053 | if (!isLegalAsecId(id)) { |
| 1054 | SLOGE("resizeAsec: Invalid asec id \"%s\"", id); |
| 1055 | errno = EINVAL; |
| 1056 | return -1; |
| 1057 | } |
| 1058 | |
| 1059 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1060 | SLOGE("Couldn't find ASEC %s", id); |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1064 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1065 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1066 | SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | if (isMountpointMounted(mountPoint)) { |
| 1071 | SLOGE("ASEC %s mounted. Unmount before resizing", id); |
| 1072 | errno = EBUSY; |
| 1073 | return -1; |
| 1074 | } |
| 1075 | |
| 1076 | struct asec_superblock sb; |
| 1077 | int fd; |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 1078 | unsigned long oldNumSec = 0; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1079 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1080 | if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1081 | SLOGE("Failed to open ASEC file (%s)", strerror(errno)); |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
| 1085 | struct stat info; |
| 1086 | if (fstat(fd, &info) < 0) { |
| 1087 | SLOGE("Failed to get file size (%s)", strerror(errno)); |
| 1088 | close(fd); |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
| 1092 | oldNumSec = info.st_size / 512; |
| 1093 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1094 | /* |
| 1095 | * Try to read superblock. |
| 1096 | */ |
| 1097 | memset(&sb, 0, sizeof(struct asec_superblock)); |
| 1098 | if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) { |
| 1099 | SLOGE("lseek failed (%s)", strerror(errno)); |
| 1100 | close(fd); |
| 1101 | return -1; |
| 1102 | } |
| 1103 | if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 1104 | SLOGE("superblock read failed (%s)", strerror(errno)); |
| 1105 | close(fd); |
| 1106 | return -1; |
| 1107 | } |
| 1108 | close(fd); |
| 1109 | |
| 1110 | if (mDebug) { |
| 1111 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
| 1112 | } |
| 1113 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
| 1114 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
| 1115 | errno = EMEDIUMTYPE; |
| 1116 | return -1; |
| 1117 | } |
| 1118 | |
Daniel Rosenberg | e4c291a | 2016-04-20 14:07:32 -0700 | [diff] [blame] | 1119 | unsigned long numImgSectors; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1120 | if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) { |
| 1121 | SLOGE("Only ext4 partitions are supported for resize"); |
| 1122 | errno = EINVAL; |
| 1123 | return -1; |
Daniel Rosenberg | e4c291a | 2016-04-20 14:07:32 -0700 | [diff] [blame] | 1124 | } else { |
| 1125 | numImgSectors = adjustSectorNumExt4(numSectors); |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * add one block for the superblock |
| 1130 | */ |
| 1131 | SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1); |
| 1132 | if (oldNumSec == numImgSectors + 1) { |
| 1133 | SLOGW("Size unchanged; ignoring resize request"); |
| 1134 | return 0; |
| 1135 | } else if (oldNumSec > numImgSectors + 1) { |
| 1136 | SLOGE("Only growing is currently supported."); |
| 1137 | close(fd); |
| 1138 | return -1; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) { |
| 1142 | SLOGE("Resize of ASEC image file failed. Could not resize %s", id); |
| 1143 | return -1; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Drop down a copy of the superblock at the end of the file |
| 1148 | */ |
| 1149 | if (writeSuperBlock(asecFileName, &sb, numImgSectors)) |
| 1150 | goto fail; |
| 1151 | |
| 1152 | char idHash[33]; |
| 1153 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 1154 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 1155 | goto fail; |
| 1156 | } |
| 1157 | |
| 1158 | char loopDevice[255]; |
| 1159 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 1160 | goto fail; |
| 1161 | |
| 1162 | char dmDevice[255]; |
| 1163 | |
| 1164 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) { |
| 1165 | Loop::destroyByDevice(loopDevice); |
| 1166 | goto fail; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * Wait for the device mapper node to be created. |
| 1171 | */ |
| 1172 | waitForDevMapper(dmDevice); |
| 1173 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1174 | if (android::vold::ext4::Resize(dmDevice, numImgSectors)) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1175 | SLOGE("Unable to resize %s (%s)", id, strerror(errno)); |
| 1176 | if (cleanupDm) { |
| 1177 | Devmapper::destroy(idHash); |
| 1178 | } |
| 1179 | Loop::destroyByDevice(loopDevice); |
| 1180 | goto fail; |
| 1181 | } |
| 1182 | |
| 1183 | return 0; |
| 1184 | fail: |
| 1185 | Loop::resizeImageFile(asecFileName, oldNumSec); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1189 | int VolumeManager::finalizeAsec(const char *id) { |
| 1190 | char asecFileName[255]; |
| 1191 | char loopDevice[255]; |
| 1192 | char mountPoint[255]; |
| 1193 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1194 | if (!isLegalAsecId(id)) { |
| 1195 | SLOGE("finalizeAsec: Invalid asec id \"%s\"", id); |
| 1196 | errno = EINVAL; |
| 1197 | return -1; |
| 1198 | } |
| 1199 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1200 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1201 | SLOGE("Couldn't find ASEC %s", id); |
| 1202 | return -1; |
| 1203 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1204 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1205 | char idHash[33]; |
| 1206 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1207 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1208 | return -1; |
| 1209 | } |
| 1210 | |
| 1211 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1212 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1213 | return -1; |
| 1214 | } |
| 1215 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1216 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1217 | struct asec_superblock sb; |
| 1218 | |
| 1219 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1220 | return -1; |
| 1221 | } |
| 1222 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1223 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1224 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1225 | SLOGE("ASEC finalize failed: couldn't construct mountPoint"); |
| 1226 | return -1; |
| 1227 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1228 | |
| 1229 | int result = 0; |
| 1230 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1231 | result = android::vold::ext4::Mount(loopDevice, mountPoint, |
| 1232 | true, true, true); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1233 | } else { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1234 | result = android::vold::vfat::Mount(loopDevice, mountPoint, |
| 1235 | true, true, true, 0, 0, 0227, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1239 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1240 | return -1; |
| 1241 | } |
| 1242 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1243 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1244 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1245 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1249 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 1250 | char asecFileName[255]; |
| 1251 | char loopDevice[255]; |
| 1252 | char mountPoint[255]; |
| 1253 | |
| 1254 | if (gid < AID_APP) { |
| 1255 | SLOGE("Group ID is not in application range"); |
| 1256 | return -1; |
| 1257 | } |
| 1258 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1259 | if (!isLegalAsecId(id)) { |
| 1260 | SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id); |
| 1261 | errno = EINVAL; |
| 1262 | return -1; |
| 1263 | } |
| 1264 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1265 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1266 | SLOGE("Couldn't find ASEC %s", id); |
| 1267 | return -1; |
| 1268 | } |
| 1269 | |
| 1270 | char idHash[33]; |
| 1271 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 1272 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
| 1276 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 1277 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 1278 | return -1; |
| 1279 | } |
| 1280 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1281 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1282 | struct asec_superblock sb; |
| 1283 | |
| 1284 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1285 | return -1; |
| 1286 | } |
| 1287 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1288 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1289 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1290 | SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id); |
| 1291 | return -1; |
| 1292 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1293 | |
| 1294 | int result = 0; |
| 1295 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1299 | int ret = android::vold::ext4::Mount(loopDevice, mountPoint, |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1300 | false /* read-only */, |
| 1301 | true /* remount */, |
| 1302 | false /* executable */); |
| 1303 | if (ret) { |
| 1304 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | char *paths[] = { mountPoint, NULL }; |
| 1309 | |
| 1310 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 1311 | if (fts) { |
| 1312 | // Traverse the entire hierarchy and chown to system UID. |
| 1313 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 1314 | // We don't care about the lost+found directory. |
| 1315 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 1316 | continue; |
| 1317 | } |
| 1318 | |
| 1319 | /* |
| 1320 | * There can only be one file marked as private right now. |
| 1321 | * This should be more robust, but it satisfies the requirements |
| 1322 | * we have for right now. |
| 1323 | */ |
| 1324 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 1325 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1326 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1327 | if (fd < 0) { |
| 1328 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 1329 | result = -1; |
| 1330 | continue; |
| 1331 | } |
| 1332 | |
| 1333 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 1334 | |
| 1335 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame] | 1336 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 1337 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1338 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 1339 | } |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1340 | |
Stephen Smalley | 5093e61 | 2014-02-12 09:43:08 -0500 | [diff] [blame] | 1341 | if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) { |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1342 | SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno)); |
| 1343 | result |= -1; |
| 1344 | } |
| 1345 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1346 | close(fd); |
| 1347 | } |
| 1348 | fts_close(fts); |
| 1349 | |
| 1350 | // Finally make the directory readable by everyone. |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1351 | int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1352 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 1353 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 1354 | result |= -1; |
| 1355 | } |
| 1356 | close(dirfd); |
| 1357 | } else { |
| 1358 | result |= -1; |
| 1359 | } |
| 1360 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1361 | result |= android::vold::ext4::Mount(loopDevice, mountPoint, |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1362 | true /* read-only */, |
| 1363 | true /* remount */, |
| 1364 | true /* execute */); |
| 1365 | |
| 1366 | if (result) { |
| 1367 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 1368 | return -1; |
| 1369 | } |
| 1370 | |
| 1371 | if (mDebug) { |
| 1372 | SLOGD("ASEC %s permissions fixed", id); |
| 1373 | } |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1377 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1378 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1379 | char *asecFilename2; |
| 1380 | char mountPoint[255]; |
| 1381 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1382 | const char *dir; |
| 1383 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1384 | if (!isLegalAsecId(id1)) { |
| 1385 | SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1); |
| 1386 | errno = EINVAL; |
| 1387 | return -1; |
| 1388 | } |
| 1389 | |
| 1390 | if (!isLegalAsecId(id2)) { |
| 1391 | SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2); |
| 1392 | errno = EINVAL; |
| 1393 | return -1; |
| 1394 | } |
| 1395 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1396 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 1397 | SLOGE("Couldn't find ASEC %s", id1); |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
| 1401 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1402 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1403 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1404 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1405 | SLOGE("Rename failed: couldn't construct mountpoint"); |
| 1406 | goto out_err; |
| 1407 | } |
| 1408 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1409 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1410 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1411 | errno = EBUSY; |
| 1412 | goto out_err; |
| 1413 | } |
| 1414 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1415 | written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1416 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1417 | SLOGE("Rename failed: couldn't construct mountpoint2"); |
| 1418 | goto out_err; |
| 1419 | } |
| 1420 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1421 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1422 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1423 | errno = EBUSY; |
| 1424 | goto out_err; |
| 1425 | } |
| 1426 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1427 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1428 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1429 | errno = EADDRINUSE; |
| 1430 | goto out_err; |
| 1431 | } |
| 1432 | |
| 1433 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1434 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1435 | goto out_err; |
| 1436 | } |
| 1437 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1438 | free(asecFilename2); |
| 1439 | return 0; |
| 1440 | |
| 1441 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1442 | free(asecFilename2); |
| 1443 | return -1; |
| 1444 | } |
| 1445 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1446 | #define UNMOUNT_RETRIES 5 |
| 1447 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1448 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1449 | char asecFileName[255]; |
| 1450 | char mountPoint[255]; |
| 1451 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1452 | if (!isLegalAsecId(id)) { |
| 1453 | SLOGE("unmountAsec: Invalid asec id \"%s\"", id); |
| 1454 | errno = EINVAL; |
| 1455 | return -1; |
| 1456 | } |
| 1457 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1458 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1459 | SLOGE("Couldn't find ASEC %s", id); |
| 1460 | return -1; |
| 1461 | } |
| 1462 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1463 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1464 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1465 | SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id); |
| 1466 | return -1; |
| 1467 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1468 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1469 | char idHash[33]; |
| 1470 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1471 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1472 | return -1; |
| 1473 | } |
| 1474 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1475 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 1476 | } |
| 1477 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1478 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1479 | char mountPoint[255]; |
| 1480 | |
| 1481 | char idHash[33]; |
| 1482 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 1483 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 1484 | return -1; |
| 1485 | } |
| 1486 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1487 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1488 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1489 | SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName); |
| 1490 | return -1; |
| 1491 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1492 | |
| 1493 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 1494 | } |
| 1495 | |
| 1496 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 1497 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1498 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1499 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 1500 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1501 | return -1; |
| 1502 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 1503 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1504 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1505 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1506 | rc = umount(mountPoint); |
| 1507 | if (!rc) { |
| 1508 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1509 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1510 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1511 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1512 | rc = 0; |
| 1513 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1514 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1515 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1516 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1517 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1518 | int signal = 0; // default is to just complain |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1519 | |
| 1520 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1521 | if (i > (UNMOUNT_RETRIES - 2)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1522 | signal = SIGKILL; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1523 | else if (i > (UNMOUNT_RETRIES - 3)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1524 | signal = SIGTERM; |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1525 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1526 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1527 | Process::killProcessesWithOpenFiles(mountPoint, signal); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1528 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1532 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1533 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1534 | return -1; |
| 1535 | } |
| 1536 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1537 | int retries = 10; |
| 1538 | |
| 1539 | while(retries--) { |
| 1540 | if (!rmdir(mountPoint)) { |
| 1541 | break; |
| 1542 | } |
| 1543 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1544 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1545 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1549 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 1550 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1551 | |
Paul Lawrence | 60dec16 | 2014-09-02 10:52:15 -0700 | [diff] [blame] | 1552 | for (i=1; i <= UNMOUNT_RETRIES; i++) { |
| 1553 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
| 1554 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
| 1555 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
| 1556 | continue; |
| 1557 | } else { |
| 1558 | break; |
| 1559 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1563 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1564 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1565 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1566 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1567 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1568 | |
| 1569 | AsecIdCollection::iterator it; |
| 1570 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1571 | ContainerData* cd = *it; |
| 1572 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1573 | free(*it); |
| 1574 | mActiveContainers->erase(it); |
| 1575 | break; |
| 1576 | } |
| 1577 | } |
| 1578 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1579 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1580 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1581 | return 0; |
| 1582 | } |
| 1583 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1584 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1585 | char asecFileName[255]; |
| 1586 | char mountPoint[255]; |
| 1587 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1588 | if (!isLegalAsecId(id)) { |
| 1589 | SLOGE("destroyAsec: Invalid asec id \"%s\"", id); |
| 1590 | errno = EINVAL; |
| 1591 | return -1; |
| 1592 | } |
| 1593 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1594 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1595 | SLOGE("Couldn't find ASEC %s", id); |
| 1596 | return -1; |
| 1597 | } |
| 1598 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1599 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1600 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1601 | SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id); |
| 1602 | return -1; |
| 1603 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1604 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1605 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1606 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1607 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1608 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1609 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1610 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1611 | return -1; |
| 1612 | } |
| 1613 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1614 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1615 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1616 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1617 | return -1; |
| 1618 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1619 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1620 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1621 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1622 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1623 | return 0; |
| 1624 | } |
| 1625 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1626 | /* |
| 1627 | * Legal ASEC ids consist of alphanumeric characters, '-', |
| 1628 | * '_', or '.'. ".." is not allowed. The first or last character |
| 1629 | * of the ASEC id cannot be '.' (dot). |
| 1630 | */ |
| 1631 | bool VolumeManager::isLegalAsecId(const char *id) const { |
| 1632 | size_t i; |
| 1633 | size_t len = strlen(id); |
| 1634 | |
| 1635 | if (len == 0) { |
| 1636 | return false; |
| 1637 | } |
| 1638 | if ((id[0] == '.') || (id[len - 1] == '.')) { |
| 1639 | return false; |
| 1640 | } |
| 1641 | |
| 1642 | for (i = 0; i < len; i++) { |
| 1643 | if (id[i] == '.') { |
| 1644 | // i=0 is guaranteed never to have a dot. See above. |
| 1645 | if (id[i-1] == '.') return false; |
| 1646 | continue; |
| 1647 | } |
| 1648 | if (id[i] == '_' || id[i] == '-') continue; |
| 1649 | if (id[i] >= 'a' && id[i] <= 'z') continue; |
| 1650 | if (id[i] >= 'A' && id[i] <= 'Z') continue; |
| 1651 | if (id[i] >= '0' && id[i] <= '9') continue; |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
| 1655 | return true; |
| 1656 | } |
| 1657 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1658 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1659 | int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1660 | if (dirfd < 0) { |
| 1661 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1662 | return false; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1665 | struct stat sb; |
| 1666 | bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0) |
| 1667 | && S_ISREG(sb.st_mode); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1668 | |
| 1669 | close(dirfd); |
| 1670 | |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
| 1674 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 1675 | const char **directory) const { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1676 | char *asecName; |
| 1677 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1678 | if (!isLegalAsecId(id)) { |
| 1679 | SLOGE("findAsec: Invalid asec id \"%s\"", id); |
| 1680 | errno = EINVAL; |
| 1681 | return -1; |
| 1682 | } |
| 1683 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1684 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 1685 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 1686 | return -1; |
| 1687 | } |
| 1688 | |
| 1689 | const char *dir; |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1690 | if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) { |
| 1691 | dir = VolumeManager::SEC_ASECDIR_INT; |
| 1692 | } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) { |
| 1693 | dir = VolumeManager::SEC_ASECDIR_EXT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1694 | } else { |
| 1695 | free(asecName); |
| 1696 | return -1; |
| 1697 | } |
| 1698 | |
| 1699 | if (directory != NULL) { |
| 1700 | *directory = dir; |
| 1701 | } |
| 1702 | |
| 1703 | if (asecPath != NULL) { |
| 1704 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1705 | if ((written < 0) || (size_t(written) >= asecPathLen)) { |
| 1706 | SLOGE("findAsec failed for %s: couldn't construct ASEC path", id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1707 | free(asecName); |
| 1708 | return -1; |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | free(asecName); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1716 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1717 | char asecFileName[255]; |
| 1718 | char mountPoint[255]; |
| 1719 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1720 | if (!isLegalAsecId(id)) { |
| 1721 | SLOGE("mountAsec: Invalid asec id \"%s\"", id); |
| 1722 | errno = EINVAL; |
| 1723 | return -1; |
| 1724 | } |
| 1725 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1726 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1727 | SLOGE("Couldn't find ASEC %s", id); |
| 1728 | return -1; |
| 1729 | } |
| 1730 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1731 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1732 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1733 | SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1734 | return -1; |
| 1735 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1736 | |
| 1737 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1738 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1739 | errno = EBUSY; |
| 1740 | return -1; |
| 1741 | } |
| 1742 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1743 | char idHash[33]; |
| 1744 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1745 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1746 | return -1; |
| 1747 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 1748 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1749 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1750 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 1751 | return -1; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1752 | |
| 1753 | char dmDevice[255]; |
| 1754 | bool cleanupDm = false; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1755 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1756 | unsigned long nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1757 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1758 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1759 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1760 | return -1; |
| 1761 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1762 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1763 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1764 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1765 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1766 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1767 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1768 | Loop::destroyByDevice(loopDevice); |
| 1769 | errno = EMEDIUMTYPE; |
| 1770 | return -1; |
| 1771 | } |
| 1772 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 1773 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1774 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) { |
| 1775 | Loop::destroyByDevice(loopDevice); |
| 1776 | return -1; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1777 | } |
| 1778 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1779 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1780 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1781 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1782 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1783 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1784 | } |
| 1785 | Loop::destroyByDevice(loopDevice); |
| 1786 | return -1; |
| 1787 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1788 | } |
| 1789 | |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1790 | /* |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1791 | * Wait for the device mapper node to be created. |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1792 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1793 | waitForDevMapper(dmDevice); |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1794 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1795 | int result; |
| 1796 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1797 | result = android::vold::ext4::Mount(dmDevice, mountPoint, |
| 1798 | readOnly, false, readOnly); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1799 | } else { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1800 | result = android::vold::vfat::Mount(dmDevice, mountPoint, |
| 1801 | readOnly, false, readOnly, ownerUid, 0, 0222, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1805 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1806 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1807 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1808 | } |
| 1809 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1810 | return -1; |
| 1811 | } |
| 1812 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1813 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1814 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1815 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1816 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1817 | return 0; |
| 1818 | } |
| 1819 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1820 | /** |
| 1821 | * Mounts an image file <code>img</code>. |
| 1822 | */ |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1823 | int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1824 | char mountPoint[255]; |
| 1825 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1826 | char idHash[33]; |
| 1827 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 1828 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 1829 | return -1; |
| 1830 | } |
| 1831 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1832 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1833 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1834 | SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1835 | return -1; |
| 1836 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1837 | |
| 1838 | if (isMountpointMounted(mountPoint)) { |
| 1839 | SLOGE("Image %s already mounted", img); |
| 1840 | errno = EBUSY; |
| 1841 | return -1; |
| 1842 | } |
| 1843 | |
| 1844 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1845 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug)) |
| 1846 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1847 | |
| 1848 | char dmDevice[255]; |
| 1849 | bool cleanupDm = false; |
| 1850 | int fd; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1851 | unsigned long nr_sec = 0; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1852 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1853 | if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1854 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1855 | Loop::destroyByDevice(loopDevice); |
| 1856 | return -1; |
| 1857 | } |
| 1858 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1859 | get_blkdev_size(fd, &nr_sec); |
| 1860 | if (nr_sec == 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1861 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1862 | Loop::destroyByDevice(loopDevice); |
| 1863 | close(fd); |
| 1864 | return -1; |
| 1865 | } |
| 1866 | |
| 1867 | close(fd); |
| 1868 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1869 | if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1870 | Loop::destroyByDevice(loopDevice); |
| 1871 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | if (mkdir(mountPoint, 0755)) { |
| 1875 | if (errno != EEXIST) { |
| 1876 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1877 | if (cleanupDm) { |
| 1878 | Devmapper::destroy(idHash); |
| 1879 | } |
| 1880 | Loop::destroyByDevice(loopDevice); |
| 1881 | return -1; |
| 1882 | } |
| 1883 | } |
| 1884 | |
yoshiyuki hama | 476a627 | 2015-01-28 16:37:23 +0900 | [diff] [blame] | 1885 | /* |
| 1886 | * Wait for the device mapper node to be created. |
| 1887 | */ |
| 1888 | waitForDevMapper(dmDevice); |
| 1889 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1890 | if (android::vold::vfat::Mount(dmDevice, mountPoint, |
| 1891 | true, false, true, 0, ownerGid, 0227, false)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1892 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1893 | if (cleanupDm) { |
| 1894 | Devmapper::destroy(idHash); |
| 1895 | } |
| 1896 | Loop::destroyByDevice(loopDevice); |
| 1897 | return -1; |
| 1898 | } |
| 1899 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1900 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1901 | if (mDebug) { |
| 1902 | SLOGD("Image %s mounted", img); |
| 1903 | } |
| 1904 | return 0; |
| 1905 | } |
| 1906 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1907 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1908 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1909 | if (fp == NULL) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1910 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1911 | return -1; |
| 1912 | } |
| 1913 | |
| 1914 | // Create a string to compare against that has a trailing slash |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1915 | int loopDirLen = strlen(VolumeManager::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1916 | char loopDir[loopDirLen + 2]; |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 1917 | strlcpy(loopDir, VolumeManager::LOOPDIR, sizeof(loopDir)); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1918 | loopDir[loopDirLen++] = '/'; |
| 1919 | loopDir[loopDirLen] = '\0'; |
| 1920 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1921 | mntent* mentry; |
| 1922 | while ((mentry = getmntent(fp)) != NULL) { |
| 1923 | if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1924 | int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1925 | if (fd >= 0) { |
| 1926 | struct loop_info64 li; |
| 1927 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1928 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1929 | (const char*) li.lo_file_name, false); |
| 1930 | } |
| 1931 | close(fd); |
| 1932 | } |
| 1933 | } |
| 1934 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1935 | endmntent(fp); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1939 | extern "C" int vold_unmountAll(void) { |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1940 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1941 | return vm->unmountAll(); |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1944 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1945 | { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1946 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1947 | if (fp == NULL) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1948 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1949 | return false; |
| 1950 | } |
| 1951 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1952 | bool found_mp = false; |
| 1953 | mntent* mentry; |
| 1954 | while ((mentry = getmntent(fp)) != NULL) { |
| 1955 | if (strcmp(mentry->mnt_dir, mp) == 0) { |
| 1956 | found_mp = true; |
| 1957 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1958 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1959 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1960 | endmntent(fp); |
| 1961 | return found_mp; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1962 | } |
| 1963 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1964 | int VolumeManager::mkdirs(char* path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1965 | // Only offer to create directories for paths managed by vold |
| 1966 | if (strncmp(path, "/storage/", 9) == 0) { |
| 1967 | // fs_mkdirs() does symlink checking and relative path enforcement |
| 1968 | return fs_mkdirs(path, 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1969 | } else { |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 1970 | SLOGE("Failed to find mounted volume for %s", path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1971 | return -EINVAL; |
| 1972 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1973 | } |