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