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