blob: 0404ce70cdf9b312b75ab483e54ed249dac99a2c [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>
Yabin Cuid1104f72015-01-02 13:28:28 -080029#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080030
San Mehata2677e42009-12-13 10:40:18 -080031#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#define LOG_TAG "Vold"
34
Kenny Root7b18a7b2010-03-15 13:13:41 -070035#include <openssl/md5.h>
36
Jeff Sharkey36801cc2015-03-13 16:09:20 -070037#include <base/logging.h>
38#include <base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070039#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070040#include <cutils/log.h>
41
Robert Craigb9e3ba52014-02-04 10:53:00 -050042#include <selinux/android.h>
43
San Mehatfd7f5872009-10-12 11:32:47 -070044#include <sysutils/NetlinkEvent.h>
45
Kenny Root344ca102012-04-03 17:23:01 -070046#include <private/android_filesystem_config.h>
47
Jeff Sharkey36801cc2015-03-13 16:09:20 -070048#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070049#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070050#include "NetlinkManager.h"
San Mehatae10b912009-10-12 14:57:05 -070051#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080052#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080053#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070054#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080055#include "Fat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070056#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080057#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080058#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080059#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090060#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070061#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080062
Jeff Sharkey36801cc2015-03-13 16:09:20 -070063#define DEBUG_NETLINK 0
64
Mike Lockwood97f2fc12011-06-07 10:51:38 -070065#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
66
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070067#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
68 + (number & (~((1U << po2) - 1))))
69
Jeff Sharkey36801cc2015-03-13 16:09:20 -070070using android::base::StringPrintf;
71
72static const char* kUserMountPath = "/mnt/user";
73
74static const unsigned int kMajorBlockScsi = 8;
75static const unsigned int kMajorBlockMmc = 179;
76
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070077/* writes superblock at end of file or device given by name */
78static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
79 int sbfd = open(name, O_RDWR);
80 if (sbfd < 0) {
81 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
82 return -1;
83 }
84
85 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
86 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
87 close(sbfd);
88 return -1;
89 }
90
91 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
92 SLOGE("Failed to write superblock (%s)", strerror(errno));
93 close(sbfd);
94 return -1;
95 }
96 close(sbfd);
97 return 0;
98}
99
100static int adjustSectorNumExt4(unsigned numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700101 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
102 // preventing costly operations or unexpected ENOSPC error.
103 // Ext4::format() uses default block size without clustering.
104 unsigned clusterSectors = 4096 / 512;
105 unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
106 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700107 return ROUND_UP_POWER_OF_2(numSectors, 3);
108}
109
110static int adjustSectorNumFAT(unsigned numSectors) {
111 /*
112 * Add some headroom
113 */
114 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
115 numSectors += fatSize + 2;
116 /*
117 * FAT is aligned to 32 kb with 512b sectors.
118 */
119 return ROUND_UP_POWER_OF_2(numSectors, 6);
120}
121
122static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
123 if (Loop::lookupActive(idHash, buffer, len)) {
124 if (Loop::create(idHash, asecFileName, buffer, len)) {
125 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
126 return -1;
127 }
128 if (debug) {
129 SLOGD("New loop device created at %s", buffer);
130 }
131 } else {
132 if (debug) {
133 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
134 }
135 }
136 return 0;
137}
138
139static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) {
140 if (strcmp(key, "none")) {
141 if (Devmapper::lookupActive(idHash, buffer, len)) {
142 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
143 buffer, len)) {
144 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
145 return -1;
146 }
147 if (debug) {
148 SLOGD("New devmapper instance created at %s", buffer);
149 }
150 } else {
151 if (debug) {
152 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
153 }
154 }
155 *createdDMDevice = true;
156 } else {
157 strcpy(buffer, loopDevice);
158 *createdDMDevice = false;
159 }
160 return 0;
161}
162
163static void waitForDevMapper(const char *dmDevice) {
164 /*
165 * Wait for the device mapper node to be created. Sometimes it takes a
166 * while. Wait for up to 1 second. We could also inspect incoming uevents,
167 * but that would take more effort.
168 */
169 int tries = 25;
170 while (tries--) {
171 if (!access(dmDevice, F_OK) || errno != ENOENT) {
172 break;
173 }
174 usleep(40 * 1000);
175 }
176}
177
San Mehatf1b736b2009-10-10 17:22:08 -0700178VolumeManager *VolumeManager::sInstance = NULL;
179
180VolumeManager *VolumeManager::Instance() {
181 if (!sInstance)
182 sInstance = new VolumeManager();
183 return sInstance;
184}
185
186VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800187 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700188 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -0800189 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700190 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400191 mUmsSharingCount = 0;
192 mSavedDirtyRatio = -1;
193 // set dirty ratio to 0 when UMS is active
194 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -0700195 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700196}
197
198VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800199 delete mVolumes;
200 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700201}
202
Kenny Root7b18a7b2010-03-15 13:13:41 -0700203char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700204 static const char* digits = "0123456789abcdef";
205
Kenny Root7b18a7b2010-03-15 13:13:41 -0700206 unsigned char sig[MD5_DIGEST_LENGTH];
207
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700208 if (buffer == NULL) {
209 SLOGE("Destination buffer is NULL");
210 errno = ESPIPE;
211 return NULL;
212 } else if (id == NULL) {
213 SLOGE("Source buffer is NULL");
214 errno = ESPIPE;
215 return NULL;
216 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800217 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700218 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800219 errno = ESPIPE;
220 return NULL;
221 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700222
223 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800224
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700225 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700226 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700227 *p++ = digits[sig[i] >> 4];
228 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800229 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700230 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800231
232 return buffer;
233}
234
235void VolumeManager::setDebug(bool enable) {
236 mDebug = enable;
237 VolumeCollection::iterator it;
238 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
239 (*it)->setDebug(enable);
240 }
241}
242
San Mehatf1b736b2009-10-10 17:22:08 -0700243int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700244 // Always start from a clean slate by unmounting everything in
245 // directories that we own, in case we crashed.
246 FILE* fp = setmntent("/proc/mounts", "r");
247 if (fp == NULL) {
248 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
249 return -errno;
250 }
251
252 // Some volumes can be stacked on each other, so force unmount in
253 // reverse order to give us the best chance of success.
254 std::list<std::string> toUnmount;
255 mntent* mentry;
256 while ((mentry = getmntent(fp)) != NULL) {
257 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
258 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
259 toUnmount.push_front(std::string(mentry->mnt_dir));
260 }
261 }
262 endmntent(fp);
263
264 for (auto path : toUnmount) {
265 SLOGW("Tearing down stale mount %s", path.c_str());
266 android::vold::ForceUnmount(path);
267 }
268
269 // TODO: nuke all files under mnt and storage tmpfs too?
270
271 // Assume that we always have an emulated volume on internal
272 // storage; the framework will decide if it should be mounted.
273 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
274 new android::vold::EmulatedVolume("/data/media", ""));
275 mInternalEmulated->create();
276
San Mehatf1b736b2009-10-10 17:22:08 -0700277 return 0;
278}
279
280int VolumeManager::stop() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700281 mInternalEmulated->destroy();
282 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700283 return 0;
284}
285
286int VolumeManager::addVolume(Volume *v) {
287 mVolumes->push_back(v);
288 return 0;
289}
290
San Mehatfd7f5872009-10-12 11:32:47 -0700291void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700292#if DEBUG_NETLINK
293 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
294 evt->dump();
Tim Murray8439dc92014-12-15 11:56:11 -0800295#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700296
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700297 std::string eventPath(evt->findParam("DEVPATH"));
298 std::string devType(evt->findParam("DEVTYPE"));
299
300 if (devType != "disk") return;
301
302 int major = atoi(evt->findParam("MAJOR"));
303 int minor = atoi(evt->findParam("MINOR"));
304 dev_t device = makedev(major, minor);
305
306 switch (evt->getAction()) {
307 case NetlinkEvent::Action::kAdd: {
308 for (auto source : mDiskSources) {
309 if (source->matches(eventPath)) {
310 // For now, assume that MMC devices are SD, and that
311 // everything else is USB
312 int flags = source->getFlags();
313 if (major == kMajorBlockMmc) {
314 flags |= android::vold::Disk::Flags::kSd;
315 } else {
316 flags |= android::vold::Disk::Flags::kUsb;
317 }
318
319 auto disk = new android::vold::Disk(eventPath, device,
320 source->getNickname(), flags);
321 disk->create();
322 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
323 break;
324 }
325 }
326 break;
327 }
328 case NetlinkEvent::Action::kChange: {
329 for (auto disk : mDisks) {
330 if (disk->getDevice() == device) {
331 disk->readMetadata();
332 disk->readPartitions();
333 }
334 }
335 break;
336 }
337 case NetlinkEvent::Action::kRemove: {
338 auto i = mDisks.begin();
339 while (i != mDisks.end()) {
340 if ((*i)->getDevice() == device) {
341 (*i)->destroy();
342 i = mDisks.erase(i);
343 } else {
344 ++i;
345 }
346 }
347 break;
348 }
349 default: {
350 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
351 break;
352 }
353 }
354}
355
356void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
357 mDiskSources.push_back(diskSource);
358}
359
360std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
361 for (auto disk : mDisks) {
362 if (disk->getId() == id) {
363 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700364 }
365 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700366 return nullptr;
367}
San Mehatf1b736b2009-10-10 17:22:08 -0700368
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700369std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
370 if (mInternalEmulated->getId() == id) {
371 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700372 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700373 for (auto disk : mDisks) {
374 auto vol = disk->findVolume(id);
375 if (vol != nullptr) {
376 return vol;
377 }
378 }
379 return nullptr;
380}
381
382int VolumeManager::linkPrimary(userid_t userId) {
383 std::string source(mPrimary->getPath());
384 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
385 source = StringPrintf("%s/%d", source.c_str(), userId);
386 }
387
388 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
389 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
390 if (errno != ENOENT) {
391 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
392 }
393 }
394 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
395 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
396 strerror(errno));
397 return -errno;
398 }
399 return 0;
400}
401
402int VolumeManager::startUser(userid_t userId) {
403 // Note that sometimes the system will spin up processes from Zygote
404 // before actually starting the user, so we're okay if Zygote
405 // already created this directory.
406 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
407 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
408
409 mUsers.push_back(userId);
410 if (mPrimary) {
411 linkPrimary(userId);
412 }
413 return 0;
414}
415
416int VolumeManager::cleanupUser(userid_t userId) {
417 mUsers.remove(userId);
418 return 0;
419}
420
421int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
422 mPrimary = vol;
423 for (userid_t userId : mUsers) {
424 linkPrimary(userId);
425 }
426 return 0;
427}
428
429int VolumeManager::reset() {
430 // Tear down all existing disks/volumes and start from a blank slate so
431 // newly connected framework hears all events.
432 mInternalEmulated->destroy();
433 mInternalEmulated->create();
434 for (auto disk : mDisks) {
435 disk->destroy();
436 disk->create();
437 }
438 mUsers.clear();
439 return 0;
440}
441
442int VolumeManager::shutdown() {
443 for (auto disk : mDisks) {
444 disk->destroy();
445 }
446 mDisks.clear();
447 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700448}
449
JP Abgrall40b64a62014-07-24 18:02:16 -0700450int VolumeManager::listVolumes(SocketClient *cli, bool broadcast) {
San Mehatf1b736b2009-10-10 17:22:08 -0700451 VolumeCollection::iterator i;
JP Abgrall40b64a62014-07-24 18:02:16 -0700452 char msg[256];
San Mehatf1b736b2009-10-10 17:22:08 -0700453
454 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
455 char *buffer;
456 asprintf(&buffer, "%s %s %d",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700457 (*i)->getLabel(), (*i)->getFuseMountpoint(),
San Mehatf1b736b2009-10-10 17:22:08 -0700458 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800459 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700460 free(buffer);
JP Abgrall40b64a62014-07-24 18:02:16 -0700461 if (broadcast) {
462 if((*i)->getUuid()) {
463 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
464 (*i)->getFuseMountpoint(), (*i)->getUuid());
465 mBroadcaster->sendBroadcast(ResponseCode::VolumeUuidChange,
466 msg, false);
467 }
468 if((*i)->getUserLabel()) {
469 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
470 (*i)->getFuseMountpoint(), (*i)->getUserLabel());
471 mBroadcaster->sendBroadcast(ResponseCode::VolumeUserLabelChange,
472 msg, false);
473 }
474 }
San Mehatf1b736b2009-10-10 17:22:08 -0700475 }
San Mehata2677e42009-12-13 10:40:18 -0800476 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700477 return 0;
478}
San Mehat49e2bce2009-10-12 16:29:01 -0700479
Ken Sumrall9caab762013-06-11 19:10:20 -0700480int VolumeManager::formatVolume(const char *label, bool wipe) {
San Mehata2677e42009-12-13 10:40:18 -0800481 Volume *v = lookupVolume(label);
482
483 if (!v) {
484 errno = ENOENT;
485 return -1;
486 }
487
Ken Sumrall3b170052011-07-11 15:38:57 -0700488 if (mVolManagerDisabled) {
489 errno = EBUSY;
490 return -1;
491 }
492
Ken Sumrall9caab762013-06-11 19:10:20 -0700493 return v->formatVol(wipe);
San Mehata2677e42009-12-13 10:40:18 -0800494}
495
Kenny Root508c0e12010-07-12 09:59:49 -0700496int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
497 char idHash[33];
498 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
499 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
500 return -1;
501 }
502
503 memset(mountPath, 0, mountPathLen);
rpcraigd1c226f2012-10-09 06:58:16 -0400504 int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
505 if ((written < 0) || (written >= mountPathLen)) {
506 errno = EINVAL;
507 return -1;
508 }
Kenny Root508c0e12010-07-12 09:59:49 -0700509
510 if (access(mountPath, F_OK)) {
511 errno = ENOENT;
512 return -1;
513 }
514
515 return 0;
516}
517
San Mehata19b2502010-01-06 10:33:53 -0800518int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700519 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700520
Nick Kralevich0de7c612014-01-27 14:58:06 -0800521 if (!isLegalAsecId(id)) {
522 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
523 errno = EINVAL;
524 return -1;
525 }
526
Kenny Root344ca102012-04-03 17:23:01 -0700527 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
528 SLOGE("Couldn't find ASEC %s", id);
529 return -1;
530 }
San Mehat88ac2c02010-03-23 11:15:58 -0700531
532 memset(buffer, 0, maxlen);
533 if (access(asecFileName, F_OK)) {
534 errno = ENOENT;
535 return -1;
536 }
San Mehata19b2502010-01-06 10:33:53 -0800537
rpcraigd1c226f2012-10-09 06:58:16 -0400538 int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
539 if ((written < 0) || (written >= maxlen)) {
540 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
541 errno = EINVAL;
542 return -1;
543 }
544
San Mehata19b2502010-01-06 10:33:53 -0800545 return 0;
546}
547
Dianne Hackborn736910c2011-06-27 13:37:07 -0700548int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
549 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700550
Nick Kralevich0de7c612014-01-27 14:58:06 -0800551 if (!isLegalAsecId(id)) {
552 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
553 errno = EINVAL;
554 return -1;
555 }
556
Kenny Root344ca102012-04-03 17:23:01 -0700557 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
558 SLOGE("Couldn't find ASEC %s", id);
559 return -1;
560 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700561
562 memset(buffer, 0, maxlen);
563 if (access(asecFileName, F_OK)) {
564 errno = ENOENT;
565 return -1;
566 }
567
rpcraigd1c226f2012-10-09 06:58:16 -0400568 int written = snprintf(buffer, maxlen, "%s", asecFileName);
569 if ((written < 0) || (written >= maxlen)) {
570 errno = EINVAL;
571 return -1;
572 }
573
Dianne Hackborn736910c2011-06-27 13:37:07 -0700574 return 0;
575}
576
Kenny Root344ca102012-04-03 17:23:01 -0700577int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
578 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800579 struct asec_superblock sb;
580 memset(&sb, 0, sizeof(sb));
581
Nick Kralevich0de7c612014-01-27 14:58:06 -0800582 if (!isLegalAsecId(id)) {
583 SLOGE("createAsec: Invalid asec id \"%s\"", id);
584 errno = EINVAL;
585 return -1;
586 }
587
Kenny Root344ca102012-04-03 17:23:01 -0700588 const bool wantFilesystem = strcmp(fstype, "none");
589 bool usingExt4 = false;
590 if (wantFilesystem) {
591 usingExt4 = !strcmp(fstype, "ext4");
592 if (usingExt4) {
593 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
594 } else if (strcmp(fstype, "fat")) {
595 SLOGE("Invalid filesystem type %s", fstype);
596 errno = EINVAL;
597 return -1;
598 }
599 }
600
San Mehatfcf24fe2010-03-03 12:37:32 -0800601 sb.magic = ASEC_SB_MAGIC;
602 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800603
San Mehatd31e3802010-02-18 08:37:45 -0800604 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700605 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800606 errno = EINVAL;
607 return -1;
608 }
609
San Mehata19b2502010-01-06 10:33:53 -0800610 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700611 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800612 errno = EADDRINUSE;
613 return -1;
614 }
615
616 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700617
618 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
619 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
620 asecFileName, strerror(errno));
621 errno = EADDRINUSE;
622 return -1;
623 }
624
625 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
626
rpcraigd1c226f2012-10-09 06:58:16 -0400627 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
628 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
629 errno = EINVAL;
630 return -1;
631 }
San Mehata19b2502010-01-06 10:33:53 -0800632
633 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700634 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700635 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800636 errno = EADDRINUSE;
637 return -1;
638 }
639
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700640 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700641 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700642 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700643 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700644 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800645
646 // Add +1 for our superblock which is at the end
647 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700648 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800649 return -1;
650 }
651
San Mehatd9a4e352010-03-12 13:32:47 -0800652 char idHash[33];
653 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700654 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800655 unlink(asecFileName);
656 return -1;
657 }
658
San Mehata19b2502010-01-06 10:33:53 -0800659 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800660 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700661 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800662 unlink(asecFileName);
663 return -1;
664 }
665
San Mehatb78a32c2010-01-10 13:02:12 -0800666 char dmDevice[255];
667 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800668
San Mehatb78a32c2010-01-10 13:02:12 -0800669 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800670 // XXX: This is all we support for now
671 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800672 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800673 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700674 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800675 Loop::destroyByDevice(loopDevice);
676 unlink(asecFileName);
677 return -1;
678 }
679 cleanupDm = true;
680 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800681 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800682 strcpy(dmDevice, loopDevice);
683 }
684
San Mehatfcf24fe2010-03-03 12:37:32 -0800685 /*
686 * Drop down the superblock at the end of the file
687 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700688 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800689 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800690 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800691 }
692 Loop::destroyByDevice(loopDevice);
693 unlink(asecFileName);
694 return -1;
695 }
696
Kenny Root344ca102012-04-03 17:23:01 -0700697 if (wantFilesystem) {
698 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400699 char mountPoint[255];
700
rpcraigd1c226f2012-10-09 06:58:16 -0400701 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
702 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
703 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
704 if (cleanupDm) {
705 Devmapper::destroy(idHash);
706 }
707 Loop::destroyByDevice(loopDevice);
708 unlink(asecFileName);
709 return -1;
710 }
rpcraiga54e13a2012-09-21 14:17:08 -0400711
Kenny Root344ca102012-04-03 17:23:01 -0700712 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700713 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700714 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700715 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800716 }
San Mehata19b2502010-01-06 10:33:53 -0800717
Kenny Root344ca102012-04-03 17:23:01 -0700718 if (formatStatus < 0) {
719 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800720 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800721 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800722 }
San Mehateb13a902010-01-07 12:12:50 -0800723 Loop::destroyByDevice(loopDevice);
724 unlink(asecFileName);
725 return -1;
726 }
Kenny Root344ca102012-04-03 17:23:01 -0700727
Kenny Root344ca102012-04-03 17:23:01 -0700728 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800729 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700730 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800731 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800732 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800733 }
734 Loop::destroyByDevice(loopDevice);
735 unlink(asecFileName);
736 return -1;
737 }
San Mehatb78a32c2010-01-10 13:02:12 -0800738 }
San Mehata1091cb2010-02-28 20:17:20 -0800739
Kenny Root344ca102012-04-03 17:23:01 -0700740 int mountStatus;
741 if (usingExt4) {
742 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
743 } else {
744 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
745 false);
746 }
747
748 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700749 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800750 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800751 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800752 }
753 Loop::destroyByDevice(loopDevice);
754 unlink(asecFileName);
755 return -1;
756 }
Kenny Root344ca102012-04-03 17:23:01 -0700757
758 if (usingExt4) {
759 int dirfd = open(mountPoint, O_DIRECTORY);
760 if (dirfd >= 0) {
761 if (fchown(dirfd, ownerUid, AID_SYSTEM)
762 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
763 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
764 }
765 close(dirfd);
766 }
767 }
San Mehata1091cb2010-02-28 20:17:20 -0800768 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700769 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800770 }
San Mehat88705162010-01-15 09:26:28 -0800771
Kenny Rootcbacf782010-09-24 15:11:48 -0700772 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800773 return 0;
774}
775
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700776int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
777 char asecFileName[255];
778 char mountPoint[255];
779 bool cleanupDm = false;
780
781 if (!isLegalAsecId(id)) {
782 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
783 errno = EINVAL;
784 return -1;
785 }
786
787 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
788 SLOGE("Couldn't find ASEC %s", id);
789 return -1;
790 }
791
792 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
793 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
794 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
795 return -1;
796 }
797
798 if (isMountpointMounted(mountPoint)) {
799 SLOGE("ASEC %s mounted. Unmount before resizing", id);
800 errno = EBUSY;
801 return -1;
802 }
803
804 struct asec_superblock sb;
805 int fd;
806 unsigned int oldNumSec = 0;
807
808 if ((fd = open(asecFileName, O_RDONLY)) < 0) {
809 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
810 return -1;
811 }
812
813 struct stat info;
814 if (fstat(fd, &info) < 0) {
815 SLOGE("Failed to get file size (%s)", strerror(errno));
816 close(fd);
817 return -1;
818 }
819
820 oldNumSec = info.st_size / 512;
821
822 unsigned numImgSectors;
823 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
824 numImgSectors = adjustSectorNumExt4(numSectors);
825 else
826 numImgSectors = adjustSectorNumFAT(numSectors);
827 /*
828 * add one block for the superblock
829 */
830 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700831 if (oldNumSec == numImgSectors + 1) {
832 SLOGW("Size unchanged; ignoring resize request");
833 return 0;
834 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700835 SLOGE("Only growing is currently supported.");
836 close(fd);
837 return -1;
838 }
839
840 /*
841 * Try to read superblock.
842 */
843 memset(&sb, 0, sizeof(struct asec_superblock));
844 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
845 SLOGE("lseek failed (%s)", strerror(errno));
846 close(fd);
847 return -1;
848 }
849 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
850 SLOGE("superblock read failed (%s)", strerror(errno));
851 close(fd);
852 return -1;
853 }
854 close(fd);
855
856 if (mDebug) {
857 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
858 }
859 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
860 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
861 errno = EMEDIUMTYPE;
862 return -1;
863 }
864
865 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
866 SLOGE("Only ext4 partitions are supported for resize");
867 errno = EINVAL;
868 return -1;
869 }
870
871 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
872 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
873 return -1;
874 }
875
876 /*
877 * Drop down a copy of the superblock at the end of the file
878 */
879 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
880 goto fail;
881
882 char idHash[33];
883 if (!asecHash(id, idHash, sizeof(idHash))) {
884 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
885 goto fail;
886 }
887
888 char loopDevice[255];
889 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
890 goto fail;
891
892 char dmDevice[255];
893
894 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
895 Loop::destroyByDevice(loopDevice);
896 goto fail;
897 }
898
899 /*
900 * Wait for the device mapper node to be created.
901 */
902 waitForDevMapper(dmDevice);
903
904 if (Ext4::resize(dmDevice, numImgSectors)) {
905 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
906 if (cleanupDm) {
907 Devmapper::destroy(idHash);
908 }
909 Loop::destroyByDevice(loopDevice);
910 goto fail;
911 }
912
913 return 0;
914fail:
915 Loop::resizeImageFile(asecFileName, oldNumSec);
916 return -1;
917}
918
San Mehata19b2502010-01-06 10:33:53 -0800919int VolumeManager::finalizeAsec(const char *id) {
920 char asecFileName[255];
921 char loopDevice[255];
922 char mountPoint[255];
923
Nick Kralevich0de7c612014-01-27 14:58:06 -0800924 if (!isLegalAsecId(id)) {
925 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
926 errno = EINVAL;
927 return -1;
928 }
929
Kenny Root344ca102012-04-03 17:23:01 -0700930 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
931 SLOGE("Couldn't find ASEC %s", id);
932 return -1;
933 }
San Mehata19b2502010-01-06 10:33:53 -0800934
San Mehatd9a4e352010-03-12 13:32:47 -0800935 char idHash[33];
936 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700937 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800938 return -1;
939 }
940
941 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700942 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800943 return -1;
944 }
945
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900946 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700947 struct asec_superblock sb;
948
949 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
950 return -1;
951 }
952
rpcraigd1c226f2012-10-09 06:58:16 -0400953 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
954 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
955 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
956 return -1;
957 }
Kenny Root344ca102012-04-03 17:23:01 -0700958
959 int result = 0;
960 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
961 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
962 } else {
963 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
964 }
965
966 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700967 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800968 return -1;
969 }
970
San Mehatd9a4e352010-03-12 13:32:47 -0800971 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700972 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800973 }
San Mehata19b2502010-01-06 10:33:53 -0800974 return 0;
975}
976
Kenny Root344ca102012-04-03 17:23:01 -0700977int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
978 char asecFileName[255];
979 char loopDevice[255];
980 char mountPoint[255];
981
982 if (gid < AID_APP) {
983 SLOGE("Group ID is not in application range");
984 return -1;
985 }
986
Nick Kralevich0de7c612014-01-27 14:58:06 -0800987 if (!isLegalAsecId(id)) {
988 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
989 errno = EINVAL;
990 return -1;
991 }
992
Kenny Root344ca102012-04-03 17:23:01 -0700993 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
994 SLOGE("Couldn't find ASEC %s", id);
995 return -1;
996 }
997
998 char idHash[33];
999 if (!asecHash(id, idHash, sizeof(idHash))) {
1000 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1001 return -1;
1002 }
1003
1004 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1005 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1006 return -1;
1007 }
1008
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001009 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001010 struct asec_superblock sb;
1011
1012 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1013 return -1;
1014 }
1015
rpcraigd1c226f2012-10-09 06:58:16 -04001016 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1017 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1018 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1019 return -1;
1020 }
Kenny Root344ca102012-04-03 17:23:01 -07001021
1022 int result = 0;
1023 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1024 return 0;
1025 }
1026
1027 int ret = Ext4::doMount(loopDevice, mountPoint,
1028 false /* read-only */,
1029 true /* remount */,
1030 false /* executable */);
1031 if (ret) {
1032 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1033 return -1;
1034 }
1035
1036 char *paths[] = { mountPoint, NULL };
1037
1038 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1039 if (fts) {
1040 // Traverse the entire hierarchy and chown to system UID.
1041 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1042 // We don't care about the lost+found directory.
1043 if (!strcmp(ftsent->fts_name, "lost+found")) {
1044 continue;
1045 }
1046
1047 /*
1048 * There can only be one file marked as private right now.
1049 * This should be more robust, but it satisfies the requirements
1050 * we have for right now.
1051 */
1052 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1053
1054 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
1055 if (fd < 0) {
1056 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1057 result = -1;
1058 continue;
1059 }
1060
1061 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1062
1063 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001064 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001065 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001066 result |= fchmod(fd, privateFile ? 0640 : 0644);
1067 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001068
Stephen Smalley5093e612014-02-12 09:43:08 -05001069 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001070 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1071 result |= -1;
1072 }
1073
Kenny Root344ca102012-04-03 17:23:01 -07001074 close(fd);
1075 }
1076 fts_close(fts);
1077
1078 // Finally make the directory readable by everyone.
1079 int dirfd = open(mountPoint, O_DIRECTORY);
1080 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1081 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1082 result |= -1;
1083 }
1084 close(dirfd);
1085 } else {
1086 result |= -1;
1087 }
1088
1089 result |= Ext4::doMount(loopDevice, mountPoint,
1090 true /* read-only */,
1091 true /* remount */,
1092 true /* execute */);
1093
1094 if (result) {
1095 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1096 return -1;
1097 }
1098
1099 if (mDebug) {
1100 SLOGD("ASEC %s permissions fixed", id);
1101 }
1102 return 0;
1103}
1104
San Mehat048b0802010-01-23 08:17:06 -08001105int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001106 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001107 char *asecFilename2;
1108 char mountPoint[255];
1109
Kenny Root344ca102012-04-03 17:23:01 -07001110 const char *dir;
1111
Nick Kralevich0de7c612014-01-27 14:58:06 -08001112 if (!isLegalAsecId(id1)) {
1113 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1114 errno = EINVAL;
1115 return -1;
1116 }
1117
1118 if (!isLegalAsecId(id2)) {
1119 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1120 errno = EINVAL;
1121 return -1;
1122 }
1123
Kenny Root344ca102012-04-03 17:23:01 -07001124 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1125 SLOGE("Couldn't find ASEC %s", id1);
1126 return -1;
1127 }
1128
1129 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001130
rpcraigd1c226f2012-10-09 06:58:16 -04001131 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
1132 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1133 SLOGE("Rename failed: couldn't construct mountpoint");
1134 goto out_err;
1135 }
1136
San Mehat048b0802010-01-23 08:17:06 -08001137 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001138 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001139 errno = EBUSY;
1140 goto out_err;
1141 }
1142
rpcraigd1c226f2012-10-09 06:58:16 -04001143 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
1144 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1145 SLOGE("Rename failed: couldn't construct mountpoint2");
1146 goto out_err;
1147 }
1148
San Mehat96956ed2010-02-24 08:42:51 -08001149 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001150 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001151 errno = EBUSY;
1152 goto out_err;
1153 }
1154
San Mehat048b0802010-01-23 08:17:06 -08001155 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001156 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001157 errno = EADDRINUSE;
1158 goto out_err;
1159 }
1160
1161 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001162 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001163 goto out_err;
1164 }
1165
San Mehat048b0802010-01-23 08:17:06 -08001166 free(asecFilename2);
1167 return 0;
1168
1169out_err:
San Mehat048b0802010-01-23 08:17:06 -08001170 free(asecFilename2);
1171 return -1;
1172}
1173
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001174#define UNMOUNT_RETRIES 5
1175#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001176int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001177 char asecFileName[255];
1178 char mountPoint[255];
1179
Nick Kralevich0de7c612014-01-27 14:58:06 -08001180 if (!isLegalAsecId(id)) {
1181 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1182 errno = EINVAL;
1183 return -1;
1184 }
1185
Kenny Root344ca102012-04-03 17:23:01 -07001186 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1187 SLOGE("Couldn't find ASEC %s", id);
1188 return -1;
1189 }
1190
rpcraigd1c226f2012-10-09 06:58:16 -04001191 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1192 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1193 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1194 return -1;
1195 }
San Mehata19b2502010-01-06 10:33:53 -08001196
San Mehatd9a4e352010-03-12 13:32:47 -08001197 char idHash[33];
1198 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001199 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001200 return -1;
1201 }
1202
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001203 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1204}
1205
Kenny Root508c0e12010-07-12 09:59:49 -07001206int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001207 char mountPoint[255];
1208
1209 char idHash[33];
1210 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1211 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1212 return -1;
1213 }
1214
rpcraigd1c226f2012-10-09 06:58:16 -04001215 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1216 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1217 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1218 return -1;
1219 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001220
1221 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1222}
1223
1224int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1225 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001226 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001227 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001228 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001229 return -1;
1230 }
San Mehat23969932010-01-09 07:08:06 -08001231
San Mehatb78a32c2010-01-10 13:02:12 -08001232 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001233 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001234 rc = umount(mountPoint);
1235 if (!rc) {
1236 break;
San Mehata19b2502010-01-06 10:33:53 -08001237 }
San Mehatb78a32c2010-01-10 13:02:12 -08001238 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001239 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001240 rc = 0;
1241 break;
San Mehata19b2502010-01-06 10:33:53 -08001242 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001243 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001244 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001245
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001246 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001247
1248 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001249 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001250 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001251 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001252 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001253 }
San Mehat8c940ef2010-02-13 14:19:53 -08001254
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001255 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001256 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001257 }
1258
1259 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001260 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001261 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001262 return -1;
1263 }
1264
San Mehat12f4b892010-02-24 11:43:22 -08001265 int retries = 10;
1266
1267 while(retries--) {
1268 if (!rmdir(mountPoint)) {
1269 break;
1270 }
1271
San Mehat97ac40e2010-03-24 10:24:19 -07001272 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001273 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001274 }
1275
1276 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001277 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001278 }
San Mehat88705162010-01-15 09:26:28 -08001279
Paul Lawrence60dec162014-09-02 10:52:15 -07001280 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1281 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1282 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1283 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1284 continue;
1285 } else {
1286 break;
1287 }
San Mehata19b2502010-01-06 10:33:53 -08001288 }
1289
1290 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001291 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001292 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001293 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001294 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001295 }
San Mehat88705162010-01-15 09:26:28 -08001296
1297 AsecIdCollection::iterator it;
1298 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001299 ContainerData* cd = *it;
1300 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001301 free(*it);
1302 mActiveContainers->erase(it);
1303 break;
1304 }
1305 }
1306 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001307 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001308 }
San Mehatb78a32c2010-01-10 13:02:12 -08001309 return 0;
1310}
1311
San Mehat4ba89482010-02-18 09:00:18 -08001312int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001313 char asecFileName[255];
1314 char mountPoint[255];
1315
Nick Kralevich0de7c612014-01-27 14:58:06 -08001316 if (!isLegalAsecId(id)) {
1317 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1318 errno = EINVAL;
1319 return -1;
1320 }
1321
Kenny Root344ca102012-04-03 17:23:01 -07001322 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1323 SLOGE("Couldn't find ASEC %s", id);
1324 return -1;
1325 }
1326
rpcraigd1c226f2012-10-09 06:58:16 -04001327 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1328 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1329 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1330 return -1;
1331 }
San Mehatb78a32c2010-01-10 13:02:12 -08001332
San Mehat0586d542010-01-12 15:38:59 -08001333 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001334 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001335 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001336 }
San Mehat4ba89482010-02-18 09:00:18 -08001337 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001338 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001339 return -1;
1340 }
1341 }
San Mehata19b2502010-01-06 10:33:53 -08001342
San Mehat0586d542010-01-12 15:38:59 -08001343 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001344 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001345 return -1;
1346 }
San Mehata19b2502010-01-06 10:33:53 -08001347
San Mehatd9a4e352010-03-12 13:32:47 -08001348 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001349 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001350 }
San Mehata19b2502010-01-06 10:33:53 -08001351 return 0;
1352}
1353
Nick Kralevich0de7c612014-01-27 14:58:06 -08001354/*
1355 * Legal ASEC ids consist of alphanumeric characters, '-',
1356 * '_', or '.'. ".." is not allowed. The first or last character
1357 * of the ASEC id cannot be '.' (dot).
1358 */
1359bool VolumeManager::isLegalAsecId(const char *id) const {
1360 size_t i;
1361 size_t len = strlen(id);
1362
1363 if (len == 0) {
1364 return false;
1365 }
1366 if ((id[0] == '.') || (id[len - 1] == '.')) {
1367 return false;
1368 }
1369
1370 for (i = 0; i < len; i++) {
1371 if (id[i] == '.') {
1372 // i=0 is guaranteed never to have a dot. See above.
1373 if (id[i-1] == '.') return false;
1374 continue;
1375 }
1376 if (id[i] == '_' || id[i] == '-') continue;
1377 if (id[i] >= 'a' && id[i] <= 'z') continue;
1378 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1379 if (id[i] >= '0' && id[i] <= '9') continue;
1380 return false;
1381 }
1382
1383 return true;
1384}
1385
Kenny Root344ca102012-04-03 17:23:01 -07001386bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1387 int dirfd = open(dir, O_DIRECTORY);
1388 if (dirfd < 0) {
1389 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001390 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001391 }
1392
Nick Kralevich25e581a2015-02-06 08:55:08 -08001393 struct stat sb;
1394 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1395 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001396
1397 close(dirfd);
1398
1399 return ret;
1400}
1401
1402int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1403 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001404 char *asecName;
1405
Nick Kralevich0de7c612014-01-27 14:58:06 -08001406 if (!isLegalAsecId(id)) {
1407 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1408 errno = EINVAL;
1409 return -1;
1410 }
1411
Kenny Root344ca102012-04-03 17:23:01 -07001412 if (asprintf(&asecName, "%s.asec", id) < 0) {
1413 SLOGE("Couldn't allocate string to write ASEC name");
1414 return -1;
1415 }
1416
1417 const char *dir;
1418 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
1419 dir = Volume::SEC_ASECDIR_INT;
1420 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
1421 dir = Volume::SEC_ASECDIR_EXT;
1422 } else {
1423 free(asecName);
1424 return -1;
1425 }
1426
1427 if (directory != NULL) {
1428 *directory = dir;
1429 }
1430
1431 if (asecPath != NULL) {
1432 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001433 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1434 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001435 free(asecName);
1436 return -1;
1437 }
1438 }
1439
1440 free(asecName);
1441 return 0;
1442}
1443
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001444int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001445 char asecFileName[255];
1446 char mountPoint[255];
1447
Nick Kralevich0de7c612014-01-27 14:58:06 -08001448 if (!isLegalAsecId(id)) {
1449 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1450 errno = EINVAL;
1451 return -1;
1452 }
1453
Kenny Root344ca102012-04-03 17:23:01 -07001454 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1455 SLOGE("Couldn't find ASEC %s", id);
1456 return -1;
1457 }
1458
rpcraigd1c226f2012-10-09 06:58:16 -04001459 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1460 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001461 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001462 return -1;
1463 }
San Mehata19b2502010-01-06 10:33:53 -08001464
1465 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001466 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001467 errno = EBUSY;
1468 return -1;
1469 }
1470
San Mehatd9a4e352010-03-12 13:32:47 -08001471 char idHash[33];
1472 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001473 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001474 return -1;
1475 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001476
San Mehata19b2502010-01-06 10:33:53 -08001477 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001478 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1479 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001480
1481 char dmDevice[255];
1482 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001483
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001484 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001485 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001486
Kenny Root344ca102012-04-03 17:23:01 -07001487 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1488 return -1;
1489 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001490
San Mehatd9a4e352010-03-12 13:32:47 -08001491 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001492 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001493 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001494 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001495 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001496 Loop::destroyByDevice(loopDevice);
1497 errno = EMEDIUMTYPE;
1498 return -1;
1499 }
1500 nr_sec--; // We don't want the devmapping to extend onto our superblock
1501
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001502 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1503 Loop::destroyByDevice(loopDevice);
1504 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001505 }
1506
Kenny Root344ca102012-04-03 17:23:01 -07001507 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001508 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001509 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001510 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001511 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001512 }
1513 Loop::destroyByDevice(loopDevice);
1514 return -1;
1515 }
San Mehata19b2502010-01-06 10:33:53 -08001516 }
1517
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001518 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001519 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001520 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001521 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001522
Kenny Root344ca102012-04-03 17:23:01 -07001523 int result;
1524 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001525 result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001526 } else {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001527 result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001528 }
1529
1530 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001531 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001532 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001533 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001534 }
1535 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001536 return -1;
1537 }
1538
Kenny Rootcbacf782010-09-24 15:11:48 -07001539 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001540 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001541 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001542 }
San Mehata19b2502010-01-06 10:33:53 -08001543 return 0;
1544}
1545
Kenny Root93ecb382012-08-09 11:28:37 -07001546Volume* VolumeManager::getVolumeForFile(const char *fileName) {
1547 VolumeCollection::iterator i;
1548
1549 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001550 const char* mountPoint = (*i)->getFuseMountpoint();
Kenny Root93ecb382012-08-09 11:28:37 -07001551 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
1552 return *i;
1553 }
1554 }
1555
1556 return NULL;
1557}
1558
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001559/**
1560 * Mounts an image file <code>img</code>.
1561 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001562int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001563 char mountPoint[255];
1564
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001565 char idHash[33];
1566 if (!asecHash(img, idHash, sizeof(idHash))) {
1567 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1568 return -1;
1569 }
1570
rpcraigd1c226f2012-10-09 06:58:16 -04001571 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1572 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001573 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001574 return -1;
1575 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001576
1577 if (isMountpointMounted(mountPoint)) {
1578 SLOGE("Image %s already mounted", img);
1579 errno = EBUSY;
1580 return -1;
1581 }
1582
1583 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001584 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1585 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001586
1587 char dmDevice[255];
1588 bool cleanupDm = false;
1589 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001590 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001591
1592 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1593 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1594 Loop::destroyByDevice(loopDevice);
1595 return -1;
1596 }
1597
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001598 get_blkdev_size(fd, &nr_sec);
1599 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001600 SLOGE("Failed to get loop size (%s)", strerror(errno));
1601 Loop::destroyByDevice(loopDevice);
1602 close(fd);
1603 return -1;
1604 }
1605
1606 close(fd);
1607
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001608 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001609 Loop::destroyByDevice(loopDevice);
1610 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001611 }
1612
1613 if (mkdir(mountPoint, 0755)) {
1614 if (errno != EEXIST) {
1615 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1616 if (cleanupDm) {
1617 Devmapper::destroy(idHash);
1618 }
1619 Loop::destroyByDevice(loopDevice);
1620 return -1;
1621 }
1622 }
1623
Jeff Sharkey69479042012-09-25 16:14:57 -07001624 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001625 0227, false)) {
1626 SLOGE("Image mount failed (%s)", strerror(errno));
1627 if (cleanupDm) {
1628 Devmapper::destroy(idHash);
1629 }
1630 Loop::destroyByDevice(loopDevice);
1631 return -1;
1632 }
1633
Kenny Rootcbacf782010-09-24 15:11:48 -07001634 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001635 if (mDebug) {
1636 SLOGD("Image %s mounted", img);
1637 }
1638 return 0;
1639}
1640
San Mehat49e2bce2009-10-12 16:29:01 -07001641int VolumeManager::mountVolume(const char *label) {
1642 Volume *v = lookupVolume(label);
1643
1644 if (!v) {
1645 errno = ENOENT;
1646 return -1;
1647 }
1648
San Mehata2677e42009-12-13 10:40:18 -08001649 return v->mountVol();
1650}
1651
Kenny Root508c0e12010-07-12 09:59:49 -07001652int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001653 FILE *fp = setmntent("/proc/mounts", "r");
1654 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001655 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1656 return -1;
1657 }
1658
1659 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001660 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001661 char loopDir[loopDirLen + 2];
1662 strcpy(loopDir, Volume::LOOPDIR);
1663 loopDir[loopDirLen++] = '/';
1664 loopDir[loopDirLen] = '\0';
1665
Yabin Cuid1104f72015-01-02 13:28:28 -08001666 mntent* mentry;
1667 while ((mentry = getmntent(fp)) != NULL) {
1668 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
1669 int fd = open(mentry->mnt_fsname, O_RDONLY);
Kenny Root508c0e12010-07-12 09:59:49 -07001670 if (fd >= 0) {
1671 struct loop_info64 li;
1672 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1673 cli->sendMsg(ResponseCode::AsecListResult,
1674 (const char*) li.lo_file_name, false);
1675 }
1676 close(fd);
1677 }
1678 }
1679 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001680 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001681 return 0;
1682}
1683
San Mehateba65e92010-01-29 05:15:16 -08001684int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1685 Volume *v = lookupVolume(label);
1686
1687 if (!v) {
1688 errno = ENOENT;
1689 return -1;
1690 }
1691
1692 if (strcmp(method, "ums")) {
1693 errno = ENOSYS;
1694 return -1;
1695 }
1696
1697 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001698 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001699 } else {
1700 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001701 }
1702 return 0;
1703}
1704
San Mehata2677e42009-12-13 10:40:18 -08001705int VolumeManager::shareVolume(const char *label, const char *method) {
1706 Volume *v = lookupVolume(label);
1707
1708 if (!v) {
1709 errno = ENOENT;
1710 return -1;
1711 }
1712
1713 /*
1714 * Eventually, we'll want to support additional share back-ends,
1715 * some of which may work while the media is mounted. For now,
1716 * we just support UMS
1717 */
1718 if (strcmp(method, "ums")) {
1719 errno = ENOSYS;
1720 return -1;
1721 }
1722
1723 if (v->getState() == Volume::State_NoMedia) {
1724 errno = ENODEV;
1725 return -1;
1726 }
1727
San Mehat49e2bce2009-10-12 16:29:01 -07001728 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001729 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001730 errno = EBUSY;
1731 return -1;
1732 }
1733
Ken Sumrall3b170052011-07-11 15:38:57 -07001734 if (mVolManagerDisabled) {
1735 errno = EBUSY;
1736 return -1;
1737 }
1738
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001739 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001740 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1741 // This volume does not support raw disk access
1742 errno = EINVAL;
1743 return -1;
1744 }
1745
1746 int fd;
1747 char nodepath[255];
rpcraigd1c226f2012-10-09 06:58:16 -04001748 int written = snprintf(nodepath,
San Mehata2677e42009-12-13 10:40:18 -08001749 sizeof(nodepath), "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -08001750 major(d), minor(d));
San Mehata2677e42009-12-13 10:40:18 -08001751
rpcraigd1c226f2012-10-09 06:58:16 -04001752 if ((written < 0) || (size_t(written) >= sizeof(nodepath))) {
1753 SLOGE("shareVolume failed: couldn't construct nodepath");
1754 return -1;
1755 }
1756
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001757 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001758 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001759 return -1;
1760 }
1761
1762 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001763 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001764 close(fd);
1765 return -1;
1766 }
1767
1768 close(fd);
1769 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001770 if (mUmsSharingCount++ == 0) {
1771 FILE* fp;
1772 mSavedDirtyRatio = -1; // in case we fail
1773 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1774 char line[16];
1775 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1776 fprintf(fp, "%d\n", mUmsDirtyRatio);
1777 } else {
1778 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1779 }
1780 fclose(fp);
1781 } else {
1782 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1783 }
1784 }
San Mehata2677e42009-12-13 10:40:18 -08001785 return 0;
1786}
1787
1788int VolumeManager::unshareVolume(const char *label, const char *method) {
1789 Volume *v = lookupVolume(label);
1790
1791 if (!v) {
1792 errno = ENOENT;
1793 return -1;
1794 }
1795
1796 if (strcmp(method, "ums")) {
1797 errno = ENOSYS;
1798 return -1;
1799 }
1800
1801 if (v->getState() != Volume::State_Shared) {
1802 errno = EINVAL;
1803 return -1;
1804 }
1805
San Mehata2677e42009-12-13 10:40:18 -08001806 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001807 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001808 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001809 return -1;
1810 }
1811
1812 char ch = 0;
1813 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001814 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001815 close(fd);
1816 return -1;
1817 }
1818
1819 close(fd);
1820 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001821 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1822 FILE* fp;
1823 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1824 fprintf(fp, "%d\n", mSavedDirtyRatio);
1825 fclose(fp);
1826 } else {
1827 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1828 }
1829 mSavedDirtyRatio = -1;
1830 }
San Mehata2677e42009-12-13 10:40:18 -08001831 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001832}
1833
Ken Sumrall3b170052011-07-11 15:38:57 -07001834extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001835 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001836 vm->disableVolumeManager();
1837 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001838 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001839}
1840
1841extern "C" int vold_getNumDirectVolumes(void) {
1842 VolumeManager *vm = VolumeManager::Instance();
1843 return vm->getNumDirectVolumes();
1844}
1845
1846int VolumeManager::getNumDirectVolumes(void) {
1847 VolumeCollection::iterator i;
1848 int n=0;
1849
1850 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1851 if ((*i)->getShareDevice() != (dev_t)0) {
1852 n++;
1853 }
1854 }
1855 return n;
1856}
1857
1858extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1859 VolumeManager *vm = VolumeManager::Instance();
1860 return vm->getDirectVolumeList(vol_list);
1861}
1862
1863int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1864 VolumeCollection::iterator i;
1865 int n=0;
1866 dev_t d;
1867
1868 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1869 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1870 (*i)->getVolInfo(&vol_list[n]);
1871 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
Colin Cross346c5b22014-01-22 23:59:41 -08001872 "/dev/block/vold/%d:%d", major(d), minor(d));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001873 n++;
1874 }
1875 }
1876
1877 return 0;
1878}
1879
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001880int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001881 Volume *v = lookupVolume(label);
1882
1883 if (!v) {
1884 errno = ENOENT;
1885 return -1;
1886 }
1887
San Mehata2677e42009-12-13 10:40:18 -08001888 if (v->getState() == Volume::State_NoMedia) {
1889 errno = ENODEV;
1890 return -1;
1891 }
1892
San Mehat49e2bce2009-10-12 16:29:01 -07001893 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001894 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001895 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001896 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001897 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001898 }
1899
San Mehat1a06eda2010-04-15 12:58:50 -07001900 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001901
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001902 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001903}
1904
Ken Sumrall425524d2012-06-14 20:55:28 -07001905extern "C" int vold_unmountAllAsecs(void) {
1906 int rc;
1907
1908 VolumeManager *vm = VolumeManager::Instance();
1909 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1910 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1911 rc = -1;
1912 }
1913 return rc;
1914}
1915
1916#define ID_BUF_LEN 256
1917#define ASEC_SUFFIX ".asec"
1918#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1919int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1920 DIR *d = opendir(directory);
1921 int rc = 0;
1922
1923 if (!d) {
1924 SLOGE("Could not open asec dir %s", directory);
1925 return -1;
1926 }
1927
1928 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -07001929 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Ken Sumrall425524d2012-06-14 20:55:28 -07001930
1931 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1932 if (dent == NULL) {
1933 SLOGE("Failed to allocate memory for asec dir");
1934 return -1;
1935 }
1936
1937 struct dirent *result;
1938 while (!readdir_r(d, dent, &result) && result != NULL) {
1939 if (dent->d_name[0] == '.')
1940 continue;
1941 if (dent->d_type != DT_REG)
1942 continue;
1943 size_t name_len = strlen(dent->d_name);
1944 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1945 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1946 char id[ID_BUF_LEN];
1947 strlcpy(id, dent->d_name, name_len - 4);
1948 if (unmountAsec(id, true)) {
1949 /* Register the error, but try to unmount more asecs */
1950 rc = -1;
1951 }
1952 }
1953 }
1954 closedir(d);
1955
1956 free(dent);
1957
1958 return rc;
1959}
1960
San Mehata2677e42009-12-13 10:40:18 -08001961/*
1962 * Looks up a volume by it's label or mount-point
1963 */
San Mehat49e2bce2009-10-12 16:29:01 -07001964Volume *VolumeManager::lookupVolume(const char *label) {
1965 VolumeCollection::iterator i;
1966
1967 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001968 if (label[0] == '/') {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001969 if (!strcmp(label, (*i)->getFuseMountpoint()))
San Mehata2677e42009-12-13 10:40:18 -08001970 return (*i);
1971 } else {
1972 if (!strcmp(label, (*i)->getLabel()))
1973 return (*i);
1974 }
San Mehat49e2bce2009-10-12 16:29:01 -07001975 }
1976 return NULL;
1977}
San Mehata19b2502010-01-06 10:33:53 -08001978
1979bool VolumeManager::isMountpointMounted(const char *mp)
1980{
Yabin Cuid1104f72015-01-02 13:28:28 -08001981 FILE *fp = setmntent("/proc/mounts", "r");
1982 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001983 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001984 return false;
1985 }
1986
Yabin Cuid1104f72015-01-02 13:28:28 -08001987 bool found_mp = false;
1988 mntent* mentry;
1989 while ((mentry = getmntent(fp)) != NULL) {
1990 if (strcmp(mentry->mnt_dir, mp) == 0) {
1991 found_mp = true;
1992 break;
San Mehata19b2502010-01-06 10:33:53 -08001993 }
San Mehata19b2502010-01-06 10:33:53 -08001994 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001995 endmntent(fp);
1996 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001997}
1998
San Mehat1a06eda2010-04-15 12:58:50 -07001999int VolumeManager::cleanupAsec(Volume *v, bool force) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07002000 int rc = 0;
Kenny Root93ecb382012-08-09 11:28:37 -07002001
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07002002 char asecFileName[255];
2003
2004 AsecIdCollection removeAsec;
2005 AsecIdCollection removeObb;
2006
Kenny Root93ecb382012-08-09 11:28:37 -07002007 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
2008 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07002009 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07002010
Kenny Rootcbacf782010-09-24 15:11:48 -07002011 if (cd->type == ASEC) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07002012 if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) {
2013 SLOGE("Couldn't find ASEC %s; cleaning up", cd->id);
2014 removeAsec.push_back(cd);
2015 } else {
2016 SLOGD("Found ASEC at path %s", asecFileName);
2017 if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT,
2018 strlen(Volume::SEC_ASECDIR_EXT))) {
2019 removeAsec.push_back(cd);
2020 }
2021 }
Kenny Rootcbacf782010-09-24 15:11:48 -07002022 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07002023 if (v == getVolumeForFile(cd->id)) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07002024 removeObb.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07002025 }
2026 } else {
2027 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07002028 }
2029 }
Kenny Root93ecb382012-08-09 11:28:37 -07002030
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07002031 for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) {
Kenny Root93ecb382012-08-09 11:28:37 -07002032 ContainerData *cd = *it;
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07002033 SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel());
2034 if (unmountAsec(cd->id, force)) {
2035 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
2036 rc = -1;
2037 }
2038 }
2039
2040 for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) {
2041 ContainerData *cd = *it;
2042 SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel());
Kenny Root93ecb382012-08-09 11:28:37 -07002043 if (unmountObb(cd->id, force)) {
2044 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
2045 rc = -1;
2046 }
2047 }
2048
2049 return rc;
San Mehat1a06eda2010-04-15 12:58:50 -07002050}
2051
Jeff Sharkey71ebe152013-09-17 17:24:38 -07002052int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07002053 // Only offer to create directories for paths managed by vold
2054 if (strncmp(path, "/storage/", 9) == 0) {
2055 // fs_mkdirs() does symlink checking and relative path enforcement
2056 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07002057 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08002058 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07002059 return -EINVAL;
2060 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07002061}