blob: ec681643b130e2abd7dcc6319123017235bcb50f [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Cuid1104f72015-01-02 13:28:28 -080017#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070018#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080019#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070020#include <fts.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080021#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 Mehata19b2502010-01-06 10:33:53 -080027#include <sys/stat.h>
28#include <sys/types.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070029#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080030#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080031
San Mehata2677e42009-12-13 10:40:18 -080032#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070033
34#define LOG_TAG "Vold"
35
Kenny Root7b18a7b2010-03-15 13:13:41 -070036#include <openssl/md5.h>
37
Elliott Hughes7e128fb2015-12-04 15:50:53 -080038#include <android-base/logging.h>
39#include <android-base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070040#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070041#include <cutils/log.h>
42
Robert Craigb9e3ba52014-02-04 10:53:00 -050043#include <selinux/android.h>
44
San Mehatfd7f5872009-10-12 11:32:47 -070045#include <sysutils/NetlinkEvent.h>
46
Kenny Root344ca102012-04-03 17:23:01 -070047#include <private/android_filesystem_config.h>
48
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070049#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070050#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070051#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070052#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080053#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080054#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070055#include "fs/Ext4.h"
56#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070057#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080058#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080059#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080060#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090061#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070062#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080063
Mike Lockwood97f2fc12011-06-07 10:51:38 -070064#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
65
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070066#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
67 + (number & (~((1U << po2) - 1))))
68
Jeff Sharkey36801cc2015-03-13 16:09:20 -070069using android::base::StringPrintf;
70
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070071/*
72 * Path to external storage where *only* root can access ASEC image files
73 */
74const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
75
76/*
77 * Path to internal storage where *only* root can access ASEC image files
78 */
79const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
80
81/*
82 * Path to where secure containers are mounted
83 */
84const char *VolumeManager::ASECDIR = "/mnt/asec";
85
86/*
87 * Path to where OBBs are mounted
88 */
89const char *VolumeManager::LOOPDIR = "/mnt/obb";
90
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091static const char* kUserMountPath = "/mnt/user";
92
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080094static const unsigned int kMajorBlockExperimentalMin = 240;
95static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070097/* writes superblock at end of file or device given by name */
98static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070099 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700100 if (sbfd < 0) {
101 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
102 return -1;
103 }
104
105 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
106 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
107 close(sbfd);
108 return -1;
109 }
110
111 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
112 SLOGE("Failed to write superblock (%s)", strerror(errno));
113 close(sbfd);
114 return -1;
115 }
116 close(sbfd);
117 return 0;
118}
119
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200120static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700121 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
122 // preventing costly operations or unexpected ENOSPC error.
123 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200124 unsigned long clusterSectors = 4096 / 512;
125 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700126 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700127 return ROUND_UP_POWER_OF_2(numSectors, 3);
128}
129
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200130static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700131 /*
132 * Add some headroom
133 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200134 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700135 numSectors += fatSize + 2;
136 /*
137 * FAT is aligned to 32 kb with 512b sectors.
138 */
139 return ROUND_UP_POWER_OF_2(numSectors, 6);
140}
141
142static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
143 if (Loop::lookupActive(idHash, buffer, len)) {
144 if (Loop::create(idHash, asecFileName, buffer, len)) {
145 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
146 return -1;
147 }
148 if (debug) {
149 SLOGD("New loop device created at %s", buffer);
150 }
151 } else {
152 if (debug) {
153 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
154 }
155 }
156 return 0;
157}
158
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200159static 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 Rosenbergfcd34a02014-05-22 11:23:56 -0700160 if (strcmp(key, "none")) {
161 if (Devmapper::lookupActive(idHash, buffer, len)) {
162 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
163 buffer, len)) {
164 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
165 return -1;
166 }
167 if (debug) {
168 SLOGD("New devmapper instance created at %s", buffer);
169 }
170 } else {
171 if (debug) {
172 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
173 }
174 }
175 *createdDMDevice = true;
176 } else {
177 strcpy(buffer, loopDevice);
178 *createdDMDevice = false;
179 }
180 return 0;
181}
182
183static void waitForDevMapper(const char *dmDevice) {
184 /*
185 * Wait for the device mapper node to be created. Sometimes it takes a
186 * while. Wait for up to 1 second. We could also inspect incoming uevents,
187 * but that would take more effort.
188 */
189 int tries = 25;
190 while (tries--) {
191 if (!access(dmDevice, F_OK) || errno != ENOENT) {
192 break;
193 }
194 usleep(40 * 1000);
195 }
196}
197
San Mehatf1b736b2009-10-10 17:22:08 -0700198VolumeManager *VolumeManager::sInstance = NULL;
199
200VolumeManager *VolumeManager::Instance() {
201 if (!sInstance)
202 sInstance = new VolumeManager();
203 return sInstance;
204}
205
206VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800207 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800208 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700209 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400210 mUmsSharingCount = 0;
211 mSavedDirtyRatio = -1;
212 // set dirty ratio to 0 when UMS is active
213 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700214}
215
216VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800217 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700218}
219
Kenny Root7b18a7b2010-03-15 13:13:41 -0700220char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700221 static const char* digits = "0123456789abcdef";
222
Kenny Root7b18a7b2010-03-15 13:13:41 -0700223 unsigned char sig[MD5_DIGEST_LENGTH];
224
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700225 if (buffer == NULL) {
226 SLOGE("Destination buffer is NULL");
227 errno = ESPIPE;
228 return NULL;
229 } else if (id == NULL) {
230 SLOGE("Source buffer is NULL");
231 errno = ESPIPE;
232 return NULL;
233 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800234 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700235 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800236 errno = ESPIPE;
237 return NULL;
238 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700239
240 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800241
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700242 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700243 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700244 *p++ = digits[sig[i] >> 4];
245 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800246 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700247 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800248
249 return buffer;
250}
251
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700252int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800253 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700254 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800255}
256
San Mehatf1b736b2009-10-10 17:22:08 -0700257int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700258 // Always start from a clean slate by unmounting everything in
259 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700260 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700261
262 // Assume that we always have an emulated volume on internal
263 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700264 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700265 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700266 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700267 mInternalEmulated->create();
268
San Mehatf1b736b2009-10-10 17:22:08 -0700269 return 0;
270}
271
272int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700273 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700274 mInternalEmulated->destroy();
275 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700276 return 0;
277}
278
San Mehatfd7f5872009-10-12 11:32:47 -0700279void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700280 std::lock_guard<std::mutex> lock(mLock);
281
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700282 if (mDebug) {
283 LOG(VERBOSE) << "----------------";
284 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
285 evt->dump();
286 }
San Mehatf1b736b2009-10-10 17:22:08 -0700287
Mateusz Nowak64403792015-08-03 16:39:19 +0200288 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
289 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700290
291 if (devType != "disk") return;
292
293 int major = atoi(evt->findParam("MAJOR"));
294 int minor = atoi(evt->findParam("MINOR"));
295 dev_t device = makedev(major, minor);
296
297 switch (evt->getAction()) {
298 case NetlinkEvent::Action::kAdd: {
299 for (auto source : mDiskSources) {
300 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800301 // For now, assume that MMC and virtio-blk (the latter is
302 // emulator-specific; see Disk.cpp for details) devices are SD,
303 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700304 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800305 if (major == kMajorBlockMmc
306 || (android::vold::IsRunningInEmulator()
307 && major >= (int) kMajorBlockExperimentalMin
308 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700309 flags |= android::vold::Disk::Flags::kSd;
310 } else {
311 flags |= android::vold::Disk::Flags::kUsb;
312 }
313
314 auto disk = new android::vold::Disk(eventPath, device,
315 source->getNickname(), flags);
316 disk->create();
317 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
318 break;
319 }
320 }
321 break;
322 }
323 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700324 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 for (auto disk : mDisks) {
326 if (disk->getDevice() == device) {
327 disk->readMetadata();
328 disk->readPartitions();
329 }
330 }
331 break;
332 }
333 case NetlinkEvent::Action::kRemove: {
334 auto i = mDisks.begin();
335 while (i != mDisks.end()) {
336 if ((*i)->getDevice() == device) {
337 (*i)->destroy();
338 i = mDisks.erase(i);
339 } else {
340 ++i;
341 }
342 }
343 break;
344 }
345 default: {
346 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
347 break;
348 }
349 }
350}
351
352void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
353 mDiskSources.push_back(diskSource);
354}
355
356std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
357 for (auto disk : mDisks) {
358 if (disk->getId() == id) {
359 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700360 }
361 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700362 return nullptr;
363}
San Mehatf1b736b2009-10-10 17:22:08 -0700364
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700365std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
366 if (mInternalEmulated->getId() == id) {
367 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700368 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700369 for (auto disk : mDisks) {
370 auto vol = disk->findVolume(id);
371 if (vol != nullptr) {
372 return vol;
373 }
374 }
375 return nullptr;
376}
377
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700378void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
379 std::list<std::string>& list) {
380 list.clear();
381 for (auto disk : mDisks) {
382 disk->listVolumes(type, list);
383 }
384}
385
386nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700387 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700388 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700389 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700390 } else {
391 auto vol = findVolume(id);
392 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
393 path = vol->getPath();
394 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700395 }
396
397 if (path.empty()) {
398 LOG(WARNING) << "Failed to find volume for " << id;
399 return -1;
400 }
401
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700402 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700403}
404
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700405int VolumeManager::forgetPartition(const std::string& partGuid) {
406 std::string normalizedGuid;
407 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
408 LOG(WARNING) << "Invalid GUID " << partGuid;
409 return -1;
410 }
411
412 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
413 if (unlink(keyPath.c_str()) != 0) {
414 LOG(ERROR) << "Failed to unlink " << keyPath;
415 return -1;
416 }
417
418 return 0;
419}
420
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700421int VolumeManager::linkPrimary(userid_t userId) {
422 std::string source(mPrimary->getPath());
423 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
424 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700425 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700426 }
427
428 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
429 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
430 if (errno != ENOENT) {
431 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
432 }
433 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700434 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700435 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
436 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
437 strerror(errno));
438 return -errno;
439 }
440 return 0;
441}
442
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700443int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
444 mAddedUsers[userId] = userSerialNumber;
445 return 0;
446}
447
448int VolumeManager::onUserRemoved(userid_t userId) {
449 mAddedUsers.erase(userId);
450 return 0;
451}
452
453int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700454 // Note that sometimes the system will spin up processes from Zygote
455 // before actually starting the user, so we're okay if Zygote
456 // already created this directory.
457 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
458 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
459
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700460 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700461 if (mPrimary) {
462 linkPrimary(userId);
463 }
464 return 0;
465}
466
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700467int VolumeManager::onUserStopped(userid_t userId) {
468 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700469 return 0;
470}
471
472int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
473 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700474 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700475 linkPrimary(userId);
476 }
477 return 0;
478}
479
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700480static int unmount_tree(const char* path) {
481 size_t path_len = strlen(path);
482
483 FILE* fp = setmntent("/proc/mounts", "r");
484 if (fp == NULL) {
485 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
486 return -errno;
487 }
488
489 // Some volumes can be stacked on each other, so force unmount in
490 // reverse order to give us the best chance of success.
491 std::list<std::string> toUnmount;
492 mntent* mentry;
493 while ((mentry = getmntent(fp)) != NULL) {
494 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
495 toUnmount.push_front(std::string(mentry->mnt_dir));
496 }
497 }
498 endmntent(fp);
499
500 for (auto path : toUnmount) {
501 if (umount2(path.c_str(), MNT_DETACH)) {
502 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
503 }
504 }
505 return 0;
506}
507
Jeff Sharkey66270a22015-06-24 11:49:24 -0700508int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
509 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
510
511 DIR* dir;
512 struct dirent* de;
513 char rootName[PATH_MAX];
514 char pidName[PATH_MAX];
515 int pidFd;
516 int nsFd;
517 struct stat sb;
518 pid_t child;
519
520 if (!(dir = opendir("/proc"))) {
521 PLOG(ERROR) << "Failed to opendir";
522 return -1;
523 }
524
525 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900526 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700527 PLOG(ERROR) << "Failed to readlink";
528 closedir(dir);
529 return -1;
530 }
531
532 // Poke through all running PIDs look for apps running as UID
533 while ((de = readdir(dir))) {
534 pidFd = -1;
535 nsFd = -1;
536
537 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
538 if (pidFd < 0) {
539 goto next;
540 }
541 if (fstat(pidFd, &sb) != 0) {
542 PLOG(WARNING) << "Failed to stat " << de->d_name;
543 goto next;
544 }
545 if (sb.st_uid != uid) {
546 goto next;
547 }
548
549 // Matches so far, but refuse to touch if in root namespace
550 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900551 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700552 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
553 goto next;
554 }
555 if (!strcmp(rootName, pidName)) {
556 LOG(WARNING) << "Skipping due to root namespace";
557 goto next;
558 }
559
560 // We purposefully leave the namespace open across the fork
561 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
562 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700563 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700564 goto next;
565 }
566
567 if (!(child = fork())) {
568 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700569 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700570 _exit(1);
571 }
572
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700573 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700574
575 std::string storageSource;
576 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700577 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700578 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700579 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700580 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700581 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700582 } else {
583 // Sane default of no storage visible
584 _exit(0);
585 }
586 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
Hidehiko Abe674bed12016-03-09 16:42:10 +0900587 NULL, MS_BIND | MS_REC, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700588 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
589 << de->d_name;
590 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700591 }
Hidehiko Abe674bed12016-03-09 16:42:10 +0900592 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
593 MS_REC | MS_SLAVE, NULL)) == -1) {
594 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
595 << de->d_name;
596 _exit(1);
597 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700598
599 // Mount user-specific symlink helper into place
600 userid_t user_id = multiuser_get_user_id(uid);
601 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
602 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
603 NULL, MS_BIND, NULL)) == -1) {
604 PLOG(ERROR) << "Failed to mount " << userSource << " for "
605 << de->d_name;
606 _exit(1);
607 }
608
Jeff Sharkey66270a22015-06-24 11:49:24 -0700609 _exit(0);
610 }
611
612 if (child == -1) {
613 PLOG(ERROR) << "Failed to fork";
614 goto next;
615 } else {
616 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
617 }
618
619next:
620 close(nsFd);
621 close(pidFd);
622 }
623 closedir(dir);
624 return 0;
625}
626
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700627int VolumeManager::reset() {
628 // Tear down all existing disks/volumes and start from a blank slate so
629 // newly connected framework hears all events.
630 mInternalEmulated->destroy();
631 mInternalEmulated->create();
632 for (auto disk : mDisks) {
633 disk->destroy();
634 disk->create();
635 }
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700636 mAddedUsers.clear();
637 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700638 return 0;
639}
640
641int VolumeManager::shutdown() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700642 mInternalEmulated->destroy();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700643 for (auto disk : mDisks) {
644 disk->destroy();
645 }
646 mDisks.clear();
647 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700648}
649
Jeff Sharkey9c484982015-03-31 10:35:33 -0700650int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700651 std::lock_guard<std::mutex> lock(mLock);
652
Jeff Sharkey9c484982015-03-31 10:35:33 -0700653 // First, try gracefully unmounting all known devices
654 if (mInternalEmulated != nullptr) {
655 mInternalEmulated->unmount();
656 }
657 for (auto disk : mDisks) {
658 disk->unmountAll();
659 }
660
661 // Worst case we might have some stale mounts lurking around, so
662 // force unmount those just to be safe.
663 FILE* fp = setmntent("/proc/mounts", "r");
664 if (fp == NULL) {
665 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
666 return -errno;
667 }
668
669 // Some volumes can be stacked on each other, so force unmount in
670 // reverse order to give us the best chance of success.
671 std::list<std::string> toUnmount;
672 mntent* mentry;
673 while ((mentry = getmntent(fp)) != NULL) {
674 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
675 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
676 toUnmount.push_front(std::string(mentry->mnt_dir));
677 }
678 }
679 endmntent(fp);
680
681 for (auto path : toUnmount) {
682 SLOGW("Tearing down stale mount %s", path.c_str());
683 android::vold::ForceUnmount(path);
684 }
685
686 return 0;
687}
688
Kenny Root508c0e12010-07-12 09:59:49 -0700689int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
690 char idHash[33];
691 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
692 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
693 return -1;
694 }
695
696 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700697 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400698 if ((written < 0) || (written >= mountPathLen)) {
699 errno = EINVAL;
700 return -1;
701 }
Kenny Root508c0e12010-07-12 09:59:49 -0700702
703 if (access(mountPath, F_OK)) {
704 errno = ENOENT;
705 return -1;
706 }
707
708 return 0;
709}
710
San Mehata19b2502010-01-06 10:33:53 -0800711int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700712 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700713
Nick Kralevich0de7c612014-01-27 14:58:06 -0800714 if (!isLegalAsecId(id)) {
715 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
716 errno = EINVAL;
717 return -1;
718 }
719
Kenny Root344ca102012-04-03 17:23:01 -0700720 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
721 SLOGE("Couldn't find ASEC %s", id);
722 return -1;
723 }
San Mehat88ac2c02010-03-23 11:15:58 -0700724
725 memset(buffer, 0, maxlen);
726 if (access(asecFileName, F_OK)) {
727 errno = ENOENT;
728 return -1;
729 }
San Mehata19b2502010-01-06 10:33:53 -0800730
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700731 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400732 if ((written < 0) || (written >= maxlen)) {
733 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
734 errno = EINVAL;
735 return -1;
736 }
737
San Mehata19b2502010-01-06 10:33:53 -0800738 return 0;
739}
740
Dianne Hackborn736910c2011-06-27 13:37:07 -0700741int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
742 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700743
Nick Kralevich0de7c612014-01-27 14:58:06 -0800744 if (!isLegalAsecId(id)) {
745 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
746 errno = EINVAL;
747 return -1;
748 }
749
Kenny Root344ca102012-04-03 17:23:01 -0700750 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
751 SLOGE("Couldn't find ASEC %s", id);
752 return -1;
753 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700754
755 memset(buffer, 0, maxlen);
756 if (access(asecFileName, F_OK)) {
757 errno = ENOENT;
758 return -1;
759 }
760
rpcraigd1c226f2012-10-09 06:58:16 -0400761 int written = snprintf(buffer, maxlen, "%s", asecFileName);
762 if ((written < 0) || (written >= maxlen)) {
763 errno = EINVAL;
764 return -1;
765 }
766
Dianne Hackborn736910c2011-06-27 13:37:07 -0700767 return 0;
768}
769
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200770int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700771 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800772 struct asec_superblock sb;
773 memset(&sb, 0, sizeof(sb));
774
Nick Kralevich0de7c612014-01-27 14:58:06 -0800775 if (!isLegalAsecId(id)) {
776 SLOGE("createAsec: Invalid asec id \"%s\"", id);
777 errno = EINVAL;
778 return -1;
779 }
780
Kenny Root344ca102012-04-03 17:23:01 -0700781 const bool wantFilesystem = strcmp(fstype, "none");
782 bool usingExt4 = false;
783 if (wantFilesystem) {
784 usingExt4 = !strcmp(fstype, "ext4");
785 if (usingExt4) {
786 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
787 } else if (strcmp(fstype, "fat")) {
788 SLOGE("Invalid filesystem type %s", fstype);
789 errno = EINVAL;
790 return -1;
791 }
792 }
793
San Mehatfcf24fe2010-03-03 12:37:32 -0800794 sb.magic = ASEC_SB_MAGIC;
795 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800796
San Mehatd31e3802010-02-18 08:37:45 -0800797 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200798 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800799 errno = EINVAL;
800 return -1;
801 }
802
San Mehata19b2502010-01-06 10:33:53 -0800803 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700804
805 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
806 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
807 asecFileName, strerror(errno));
808 errno = EADDRINUSE;
809 return -1;
810 }
811
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700812 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700813
rpcraigd1c226f2012-10-09 06:58:16 -0400814 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
815 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
816 errno = EINVAL;
817 return -1;
818 }
San Mehata19b2502010-01-06 10:33:53 -0800819
820 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700821 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700822 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800823 errno = EADDRINUSE;
824 return -1;
825 }
826
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200827 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700828 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700829 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700830 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700831 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800832
833 // Add +1 for our superblock which is at the end
834 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700835 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800836 return -1;
837 }
838
San Mehatd9a4e352010-03-12 13:32:47 -0800839 char idHash[33];
840 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700841 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800842 unlink(asecFileName);
843 return -1;
844 }
845
San Mehata19b2502010-01-06 10:33:53 -0800846 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800847 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700848 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800849 unlink(asecFileName);
850 return -1;
851 }
852
San Mehatb78a32c2010-01-10 13:02:12 -0800853 char dmDevice[255];
854 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800855
San Mehatb78a32c2010-01-10 13:02:12 -0800856 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800857 // XXX: This is all we support for now
858 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800859 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800860 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700861 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800862 Loop::destroyByDevice(loopDevice);
863 unlink(asecFileName);
864 return -1;
865 }
866 cleanupDm = true;
867 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800868 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800869 strcpy(dmDevice, loopDevice);
870 }
871
San Mehatfcf24fe2010-03-03 12:37:32 -0800872 /*
873 * Drop down the superblock at the end of the file
874 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700875 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800876 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800877 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800878 }
879 Loop::destroyByDevice(loopDevice);
880 unlink(asecFileName);
881 return -1;
882 }
883
Kenny Root344ca102012-04-03 17:23:01 -0700884 if (wantFilesystem) {
885 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400886 char mountPoint[255];
887
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700888 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400889 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
890 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
891 if (cleanupDm) {
892 Devmapper::destroy(idHash);
893 }
894 Loop::destroyByDevice(loopDevice);
895 unlink(asecFileName);
896 return -1;
897 }
rpcraiga54e13a2012-09-21 14:17:08 -0400898
Kenny Root344ca102012-04-03 17:23:01 -0700899 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700900 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700901 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700902 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800903 }
San Mehata19b2502010-01-06 10:33:53 -0800904
Kenny Root344ca102012-04-03 17:23:01 -0700905 if (formatStatus < 0) {
906 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800907 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800908 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800909 }
San Mehateb13a902010-01-07 12:12:50 -0800910 Loop::destroyByDevice(loopDevice);
911 unlink(asecFileName);
912 return -1;
913 }
Kenny Root344ca102012-04-03 17:23:01 -0700914
Kenny Root344ca102012-04-03 17:23:01 -0700915 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800916 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700917 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800918 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800919 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800920 }
921 Loop::destroyByDevice(loopDevice);
922 unlink(asecFileName);
923 return -1;
924 }
San Mehatb78a32c2010-01-10 13:02:12 -0800925 }
San Mehata1091cb2010-02-28 20:17:20 -0800926
Kenny Root344ca102012-04-03 17:23:01 -0700927 int mountStatus;
928 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700929 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
930 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -0700931 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700932 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
933 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -0700934 }
935
936 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700937 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800938 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800939 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800940 }
941 Loop::destroyByDevice(loopDevice);
942 unlink(asecFileName);
943 return -1;
944 }
Kenny Root344ca102012-04-03 17:23:01 -0700945
946 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700947 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700948 if (dirfd >= 0) {
949 if (fchown(dirfd, ownerUid, AID_SYSTEM)
950 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
951 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
952 }
953 close(dirfd);
954 }
955 }
San Mehata1091cb2010-02-28 20:17:20 -0800956 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700957 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800958 }
San Mehat88705162010-01-15 09:26:28 -0800959
Kenny Rootcbacf782010-09-24 15:11:48 -0700960 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800961 return 0;
962}
963
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200964int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700965 char asecFileName[255];
966 char mountPoint[255];
967 bool cleanupDm = false;
968
969 if (!isLegalAsecId(id)) {
970 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
971 errno = EINVAL;
972 return -1;
973 }
974
975 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
976 SLOGE("Couldn't find ASEC %s", id);
977 return -1;
978 }
979
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700980 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700981 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
982 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
983 return -1;
984 }
985
986 if (isMountpointMounted(mountPoint)) {
987 SLOGE("ASEC %s mounted. Unmount before resizing", id);
988 errno = EBUSY;
989 return -1;
990 }
991
992 struct asec_superblock sb;
993 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200994 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700995
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700996 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700997 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
998 return -1;
999 }
1000
1001 struct stat info;
1002 if (fstat(fd, &info) < 0) {
1003 SLOGE("Failed to get file size (%s)", strerror(errno));
1004 close(fd);
1005 return -1;
1006 }
1007
1008 oldNumSec = info.st_size / 512;
1009
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001010 /*
1011 * Try to read superblock.
1012 */
1013 memset(&sb, 0, sizeof(struct asec_superblock));
1014 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1015 SLOGE("lseek failed (%s)", strerror(errno));
1016 close(fd);
1017 return -1;
1018 }
1019 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1020 SLOGE("superblock read failed (%s)", strerror(errno));
1021 close(fd);
1022 return -1;
1023 }
1024 close(fd);
1025
1026 if (mDebug) {
1027 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1028 }
1029 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1030 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1031 errno = EMEDIUMTYPE;
1032 return -1;
1033 }
1034
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001035 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001036 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1037 SLOGE("Only ext4 partitions are supported for resize");
1038 errno = EINVAL;
1039 return -1;
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001040 } else {
1041 numImgSectors = adjustSectorNumExt4(numSectors);
1042 }
1043
1044 /*
1045 * add one block for the superblock
1046 */
1047 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1048 if (oldNumSec == numImgSectors + 1) {
1049 SLOGW("Size unchanged; ignoring resize request");
1050 return 0;
1051 } else if (oldNumSec > numImgSectors + 1) {
1052 SLOGE("Only growing is currently supported.");
1053 close(fd);
1054 return -1;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001055 }
1056
1057 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1058 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1059 return -1;
1060 }
1061
1062 /*
1063 * Drop down a copy of the superblock at the end of the file
1064 */
1065 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1066 goto fail;
1067
1068 char idHash[33];
1069 if (!asecHash(id, idHash, sizeof(idHash))) {
1070 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1071 goto fail;
1072 }
1073
1074 char loopDevice[255];
1075 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1076 goto fail;
1077
1078 char dmDevice[255];
1079
1080 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1081 Loop::destroyByDevice(loopDevice);
1082 goto fail;
1083 }
1084
1085 /*
1086 * Wait for the device mapper node to be created.
1087 */
1088 waitForDevMapper(dmDevice);
1089
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001090 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001091 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1092 if (cleanupDm) {
1093 Devmapper::destroy(idHash);
1094 }
1095 Loop::destroyByDevice(loopDevice);
1096 goto fail;
1097 }
1098
1099 return 0;
1100fail:
1101 Loop::resizeImageFile(asecFileName, oldNumSec);
1102 return -1;
1103}
1104
San Mehata19b2502010-01-06 10:33:53 -08001105int VolumeManager::finalizeAsec(const char *id) {
1106 char asecFileName[255];
1107 char loopDevice[255];
1108 char mountPoint[255];
1109
Nick Kralevich0de7c612014-01-27 14:58:06 -08001110 if (!isLegalAsecId(id)) {
1111 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1112 errno = EINVAL;
1113 return -1;
1114 }
1115
Kenny Root344ca102012-04-03 17:23:01 -07001116 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1117 SLOGE("Couldn't find ASEC %s", id);
1118 return -1;
1119 }
San Mehata19b2502010-01-06 10:33:53 -08001120
San Mehatd9a4e352010-03-12 13:32:47 -08001121 char idHash[33];
1122 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001123 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001124 return -1;
1125 }
1126
1127 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001128 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001129 return -1;
1130 }
1131
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001132 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001133 struct asec_superblock sb;
1134
1135 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1136 return -1;
1137 }
1138
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001139 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001140 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1141 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1142 return -1;
1143 }
Kenny Root344ca102012-04-03 17:23:01 -07001144
1145 int result = 0;
1146 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001147 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1148 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001149 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001150 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1151 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001152 }
1153
1154 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001155 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001156 return -1;
1157 }
1158
San Mehatd9a4e352010-03-12 13:32:47 -08001159 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001160 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001161 }
San Mehata19b2502010-01-06 10:33:53 -08001162 return 0;
1163}
1164
Kenny Root344ca102012-04-03 17:23:01 -07001165int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1166 char asecFileName[255];
1167 char loopDevice[255];
1168 char mountPoint[255];
1169
1170 if (gid < AID_APP) {
1171 SLOGE("Group ID is not in application range");
1172 return -1;
1173 }
1174
Nick Kralevich0de7c612014-01-27 14:58:06 -08001175 if (!isLegalAsecId(id)) {
1176 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1177 errno = EINVAL;
1178 return -1;
1179 }
1180
Kenny Root344ca102012-04-03 17:23:01 -07001181 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1182 SLOGE("Couldn't find ASEC %s", id);
1183 return -1;
1184 }
1185
1186 char idHash[33];
1187 if (!asecHash(id, idHash, sizeof(idHash))) {
1188 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1189 return -1;
1190 }
1191
1192 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1193 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1194 return -1;
1195 }
1196
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001197 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001198 struct asec_superblock sb;
1199
1200 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1201 return -1;
1202 }
1203
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001204 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001205 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1206 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1207 return -1;
1208 }
Kenny Root344ca102012-04-03 17:23:01 -07001209
1210 int result = 0;
1211 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1212 return 0;
1213 }
1214
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001215 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001216 false /* read-only */,
1217 true /* remount */,
1218 false /* executable */);
1219 if (ret) {
1220 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1221 return -1;
1222 }
1223
1224 char *paths[] = { mountPoint, NULL };
1225
1226 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1227 if (fts) {
1228 // Traverse the entire hierarchy and chown to system UID.
1229 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1230 // We don't care about the lost+found directory.
1231 if (!strcmp(ftsent->fts_name, "lost+found")) {
1232 continue;
1233 }
1234
1235 /*
1236 * There can only be one file marked as private right now.
1237 * This should be more robust, but it satisfies the requirements
1238 * we have for right now.
1239 */
1240 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1241
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001242 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001243 if (fd < 0) {
1244 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1245 result = -1;
1246 continue;
1247 }
1248
1249 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1250
1251 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001252 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001253 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001254 result |= fchmod(fd, privateFile ? 0640 : 0644);
1255 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001256
Stephen Smalley5093e612014-02-12 09:43:08 -05001257 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001258 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1259 result |= -1;
1260 }
1261
Kenny Root344ca102012-04-03 17:23:01 -07001262 close(fd);
1263 }
1264 fts_close(fts);
1265
1266 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001267 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001268 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1269 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1270 result |= -1;
1271 }
1272 close(dirfd);
1273 } else {
1274 result |= -1;
1275 }
1276
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001277 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001278 true /* read-only */,
1279 true /* remount */,
1280 true /* execute */);
1281
1282 if (result) {
1283 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1284 return -1;
1285 }
1286
1287 if (mDebug) {
1288 SLOGD("ASEC %s permissions fixed", id);
1289 }
1290 return 0;
1291}
1292
San Mehat048b0802010-01-23 08:17:06 -08001293int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001294 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001295 char *asecFilename2;
1296 char mountPoint[255];
1297
Kenny Root344ca102012-04-03 17:23:01 -07001298 const char *dir;
1299
Nick Kralevich0de7c612014-01-27 14:58:06 -08001300 if (!isLegalAsecId(id1)) {
1301 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1302 errno = EINVAL;
1303 return -1;
1304 }
1305
1306 if (!isLegalAsecId(id2)) {
1307 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1308 errno = EINVAL;
1309 return -1;
1310 }
1311
Kenny Root344ca102012-04-03 17:23:01 -07001312 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1313 SLOGE("Couldn't find ASEC %s", id1);
1314 return -1;
1315 }
1316
1317 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001318
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001319 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001320 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1321 SLOGE("Rename failed: couldn't construct mountpoint");
1322 goto out_err;
1323 }
1324
San Mehat048b0802010-01-23 08:17:06 -08001325 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001326 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001327 errno = EBUSY;
1328 goto out_err;
1329 }
1330
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001331 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001332 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1333 SLOGE("Rename failed: couldn't construct mountpoint2");
1334 goto out_err;
1335 }
1336
San Mehat96956ed2010-02-24 08:42:51 -08001337 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001338 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001339 errno = EBUSY;
1340 goto out_err;
1341 }
1342
San Mehat048b0802010-01-23 08:17:06 -08001343 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001344 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001345 errno = EADDRINUSE;
1346 goto out_err;
1347 }
1348
1349 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001350 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001351 goto out_err;
1352 }
1353
San Mehat048b0802010-01-23 08:17:06 -08001354 free(asecFilename2);
1355 return 0;
1356
1357out_err:
San Mehat048b0802010-01-23 08:17:06 -08001358 free(asecFilename2);
1359 return -1;
1360}
1361
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001362#define UNMOUNT_RETRIES 5
1363#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001364int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001365 char asecFileName[255];
1366 char mountPoint[255];
1367
Nick Kralevich0de7c612014-01-27 14:58:06 -08001368 if (!isLegalAsecId(id)) {
1369 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1370 errno = EINVAL;
1371 return -1;
1372 }
1373
Kenny Root344ca102012-04-03 17:23:01 -07001374 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1375 SLOGE("Couldn't find ASEC %s", id);
1376 return -1;
1377 }
1378
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001379 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001380 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1381 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1382 return -1;
1383 }
San Mehata19b2502010-01-06 10:33:53 -08001384
San Mehatd9a4e352010-03-12 13:32:47 -08001385 char idHash[33];
1386 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001387 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001388 return -1;
1389 }
1390
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001391 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1392}
1393
Kenny Root508c0e12010-07-12 09:59:49 -07001394int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001395 char mountPoint[255];
1396
1397 char idHash[33];
1398 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1399 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1400 return -1;
1401 }
1402
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001403 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001404 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1405 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1406 return -1;
1407 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001408
1409 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1410}
1411
1412int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1413 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001414 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001415 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001416 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001417 return -1;
1418 }
San Mehat23969932010-01-09 07:08:06 -08001419
San Mehatb78a32c2010-01-10 13:02:12 -08001420 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001421 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001422 rc = umount(mountPoint);
1423 if (!rc) {
1424 break;
San Mehata19b2502010-01-06 10:33:53 -08001425 }
San Mehatb78a32c2010-01-10 13:02:12 -08001426 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001427 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001428 rc = 0;
1429 break;
San Mehata19b2502010-01-06 10:33:53 -08001430 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001431 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001432 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001433
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001434 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001435
1436 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001437 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001438 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001439 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001440 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001441 }
San Mehat8c940ef2010-02-13 14:19:53 -08001442
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001443 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001444 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001445 }
1446
1447 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001448 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001449 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001450 return -1;
1451 }
1452
San Mehat12f4b892010-02-24 11:43:22 -08001453 int retries = 10;
1454
1455 while(retries--) {
1456 if (!rmdir(mountPoint)) {
1457 break;
1458 }
1459
San Mehat97ac40e2010-03-24 10:24:19 -07001460 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001461 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001462 }
1463
1464 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001465 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001466 }
San Mehat88705162010-01-15 09:26:28 -08001467
Paul Lawrence60dec162014-09-02 10:52:15 -07001468 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1469 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1470 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1471 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1472 continue;
1473 } else {
1474 break;
1475 }
San Mehata19b2502010-01-06 10:33:53 -08001476 }
1477
1478 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001479 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001480 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001481 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001482 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001483 }
San Mehat88705162010-01-15 09:26:28 -08001484
1485 AsecIdCollection::iterator it;
1486 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001487 ContainerData* cd = *it;
1488 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001489 free(*it);
1490 mActiveContainers->erase(it);
1491 break;
1492 }
1493 }
1494 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001495 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001496 }
San Mehatb78a32c2010-01-10 13:02:12 -08001497 return 0;
1498}
1499
San Mehat4ba89482010-02-18 09:00:18 -08001500int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001501 char asecFileName[255];
1502 char mountPoint[255];
1503
Nick Kralevich0de7c612014-01-27 14:58:06 -08001504 if (!isLegalAsecId(id)) {
1505 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1506 errno = EINVAL;
1507 return -1;
1508 }
1509
Kenny Root344ca102012-04-03 17:23:01 -07001510 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1511 SLOGE("Couldn't find ASEC %s", id);
1512 return -1;
1513 }
1514
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001515 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001516 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1517 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1518 return -1;
1519 }
San Mehatb78a32c2010-01-10 13:02:12 -08001520
San Mehat0586d542010-01-12 15:38:59 -08001521 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001522 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001523 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001524 }
San Mehat4ba89482010-02-18 09:00:18 -08001525 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001526 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001527 return -1;
1528 }
1529 }
San Mehata19b2502010-01-06 10:33:53 -08001530
San Mehat0586d542010-01-12 15:38:59 -08001531 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001532 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001533 return -1;
1534 }
San Mehata19b2502010-01-06 10:33:53 -08001535
San Mehatd9a4e352010-03-12 13:32:47 -08001536 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001537 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001538 }
San Mehata19b2502010-01-06 10:33:53 -08001539 return 0;
1540}
1541
Nick Kralevich0de7c612014-01-27 14:58:06 -08001542/*
1543 * Legal ASEC ids consist of alphanumeric characters, '-',
1544 * '_', or '.'. ".." is not allowed. The first or last character
1545 * of the ASEC id cannot be '.' (dot).
1546 */
1547bool VolumeManager::isLegalAsecId(const char *id) const {
1548 size_t i;
1549 size_t len = strlen(id);
1550
1551 if (len == 0) {
1552 return false;
1553 }
1554 if ((id[0] == '.') || (id[len - 1] == '.')) {
1555 return false;
1556 }
1557
1558 for (i = 0; i < len; i++) {
1559 if (id[i] == '.') {
1560 // i=0 is guaranteed never to have a dot. See above.
1561 if (id[i-1] == '.') return false;
1562 continue;
1563 }
1564 if (id[i] == '_' || id[i] == '-') continue;
1565 if (id[i] >= 'a' && id[i] <= 'z') continue;
1566 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1567 if (id[i] >= '0' && id[i] <= '9') continue;
1568 return false;
1569 }
1570
1571 return true;
1572}
1573
Kenny Root344ca102012-04-03 17:23:01 -07001574bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001575 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001576 if (dirfd < 0) {
1577 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001578 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001579 }
1580
Nick Kralevich25e581a2015-02-06 08:55:08 -08001581 struct stat sb;
1582 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1583 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001584
1585 close(dirfd);
1586
1587 return ret;
1588}
1589
1590int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1591 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001592 char *asecName;
1593
Nick Kralevich0de7c612014-01-27 14:58:06 -08001594 if (!isLegalAsecId(id)) {
1595 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1596 errno = EINVAL;
1597 return -1;
1598 }
1599
Kenny Root344ca102012-04-03 17:23:01 -07001600 if (asprintf(&asecName, "%s.asec", id) < 0) {
1601 SLOGE("Couldn't allocate string to write ASEC name");
1602 return -1;
1603 }
1604
1605 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001606 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1607 dir = VolumeManager::SEC_ASECDIR_INT;
1608 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1609 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001610 } else {
1611 free(asecName);
1612 return -1;
1613 }
1614
1615 if (directory != NULL) {
1616 *directory = dir;
1617 }
1618
1619 if (asecPath != NULL) {
1620 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001621 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1622 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001623 free(asecName);
1624 return -1;
1625 }
1626 }
1627
1628 free(asecName);
1629 return 0;
1630}
1631
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001632int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001633 char asecFileName[255];
1634 char mountPoint[255];
1635
Nick Kralevich0de7c612014-01-27 14:58:06 -08001636 if (!isLegalAsecId(id)) {
1637 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1638 errno = EINVAL;
1639 return -1;
1640 }
1641
Kenny Root344ca102012-04-03 17:23:01 -07001642 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1643 SLOGE("Couldn't find ASEC %s", id);
1644 return -1;
1645 }
1646
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001647 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001648 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001649 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001650 return -1;
1651 }
San Mehata19b2502010-01-06 10:33:53 -08001652
1653 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001654 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001655 errno = EBUSY;
1656 return -1;
1657 }
1658
San Mehatd9a4e352010-03-12 13:32:47 -08001659 char idHash[33];
1660 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001661 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001662 return -1;
1663 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001664
San Mehata19b2502010-01-06 10:33:53 -08001665 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001666 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1667 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001668
1669 char dmDevice[255];
1670 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001671
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001672 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001673 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001674
Kenny Root344ca102012-04-03 17:23:01 -07001675 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1676 return -1;
1677 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001678
San Mehatd9a4e352010-03-12 13:32:47 -08001679 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001680 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001681 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001682 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001683 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001684 Loop::destroyByDevice(loopDevice);
1685 errno = EMEDIUMTYPE;
1686 return -1;
1687 }
1688 nr_sec--; // We don't want the devmapping to extend onto our superblock
1689
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001690 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1691 Loop::destroyByDevice(loopDevice);
1692 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001693 }
1694
Kenny Root344ca102012-04-03 17:23:01 -07001695 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001696 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001697 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001698 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001699 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001700 }
1701 Loop::destroyByDevice(loopDevice);
1702 return -1;
1703 }
San Mehata19b2502010-01-06 10:33:53 -08001704 }
1705
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001706 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001707 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001708 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001709 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001710
Kenny Root344ca102012-04-03 17:23:01 -07001711 int result;
1712 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001713 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1714 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001715 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001716 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1717 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001718 }
1719
1720 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001721 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001722 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001723 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001724 }
1725 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001726 return -1;
1727 }
1728
Kenny Rootcbacf782010-09-24 15:11:48 -07001729 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001730 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001731 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001732 }
San Mehata19b2502010-01-06 10:33:53 -08001733 return 0;
1734}
1735
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001736/**
1737 * Mounts an image file <code>img</code>.
1738 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001739int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001740 char mountPoint[255];
1741
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001742 char idHash[33];
1743 if (!asecHash(img, idHash, sizeof(idHash))) {
1744 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1745 return -1;
1746 }
1747
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001748 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001749 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001750 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001751 return -1;
1752 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001753
1754 if (isMountpointMounted(mountPoint)) {
1755 SLOGE("Image %s already mounted", img);
1756 errno = EBUSY;
1757 return -1;
1758 }
1759
1760 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001761 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1762 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001763
1764 char dmDevice[255];
1765 bool cleanupDm = false;
1766 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001767 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001768
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001769 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001770 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1771 Loop::destroyByDevice(loopDevice);
1772 return -1;
1773 }
1774
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001775 get_blkdev_size(fd, &nr_sec);
1776 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001777 SLOGE("Failed to get loop size (%s)", strerror(errno));
1778 Loop::destroyByDevice(loopDevice);
1779 close(fd);
1780 return -1;
1781 }
1782
1783 close(fd);
1784
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001785 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001786 Loop::destroyByDevice(loopDevice);
1787 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001788 }
1789
1790 if (mkdir(mountPoint, 0755)) {
1791 if (errno != EEXIST) {
1792 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1793 if (cleanupDm) {
1794 Devmapper::destroy(idHash);
1795 }
1796 Loop::destroyByDevice(loopDevice);
1797 return -1;
1798 }
1799 }
1800
yoshiyuki hama476a6272015-01-28 16:37:23 +09001801 /*
1802 * Wait for the device mapper node to be created.
1803 */
1804 waitForDevMapper(dmDevice);
1805
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001806 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1807 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001808 SLOGE("Image mount failed (%s)", strerror(errno));
1809 if (cleanupDm) {
1810 Devmapper::destroy(idHash);
1811 }
1812 Loop::destroyByDevice(loopDevice);
1813 return -1;
1814 }
1815
Kenny Rootcbacf782010-09-24 15:11:48 -07001816 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001817 if (mDebug) {
1818 SLOGD("Image %s mounted", img);
1819 }
1820 return 0;
1821}
1822
Kenny Root508c0e12010-07-12 09:59:49 -07001823int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001824 FILE *fp = setmntent("/proc/mounts", "r");
1825 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001826 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1827 return -1;
1828 }
1829
1830 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001831 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001832 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001833 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001834 loopDir[loopDirLen++] = '/';
1835 loopDir[loopDirLen] = '\0';
1836
Yabin Cuid1104f72015-01-02 13:28:28 -08001837 mntent* mentry;
1838 while ((mentry = getmntent(fp)) != NULL) {
1839 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001840 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001841 if (fd >= 0) {
1842 struct loop_info64 li;
1843 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1844 cli->sendMsg(ResponseCode::AsecListResult,
1845 (const char*) li.lo_file_name, false);
1846 }
1847 close(fd);
1848 }
1849 }
1850 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001851 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001852 return 0;
1853}
1854
Jeff Sharkey9c484982015-03-31 10:35:33 -07001855extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001856 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001857 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001858}
1859
San Mehata19b2502010-01-06 10:33:53 -08001860bool VolumeManager::isMountpointMounted(const char *mp)
1861{
Yabin Cuid1104f72015-01-02 13:28:28 -08001862 FILE *fp = setmntent("/proc/mounts", "r");
1863 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001864 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001865 return false;
1866 }
1867
Yabin Cuid1104f72015-01-02 13:28:28 -08001868 bool found_mp = false;
1869 mntent* mentry;
1870 while ((mentry = getmntent(fp)) != NULL) {
1871 if (strcmp(mentry->mnt_dir, mp) == 0) {
1872 found_mp = true;
1873 break;
San Mehata19b2502010-01-06 10:33:53 -08001874 }
San Mehata19b2502010-01-06 10:33:53 -08001875 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001876 endmntent(fp);
1877 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001878}
1879
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001880int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001881 // Only offer to create directories for paths managed by vold
1882 if (strncmp(path, "/storage/", 9) == 0) {
1883 // fs_mkdirs() does symlink checking and relative path enforcement
1884 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001885 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001886 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001887 return -EINVAL;
1888 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001889}