blob: 2bc1fe90b45a45c288c2437a895cc38e74ca95e2 [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
17#include <stdio.h>
San Mehatfd7f5872009-10-12 11:32:47 -070018#include <stdlib.h>
19#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080021#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070022#include <fts.h>
23#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080024#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/mount.h>
Ken Sumrall425524d2012-06-14 20:55:28 -070027#include <dirent.h>
San Mehata19b2502010-01-06 10:33:53 -080028
San Mehata2677e42009-12-13 10:40:18 -080029#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070030
31#define LOG_TAG "Vold"
32
Kenny Root7b18a7b2010-03-15 13:13:41 -070033#include <openssl/md5.h>
34
San Mehatf1b736b2009-10-10 17:22:08 -070035#include <cutils/log.h>
36
San Mehatfd7f5872009-10-12 11:32:47 -070037#include <sysutils/NetlinkEvent.h>
38
Kenny Root344ca102012-04-03 17:23:01 -070039#include <private/android_filesystem_config.h>
40
San Mehatf1b736b2009-10-10 17:22:08 -070041#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070042#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080043#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080044#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070045#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080046#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080047#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080048#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080049#include "Asec.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070050#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080051
Mike Lockwood97f2fc12011-06-07 10:51:38 -070052#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
53
San Mehatf1b736b2009-10-10 17:22:08 -070054VolumeManager *VolumeManager::sInstance = NULL;
55
56VolumeManager *VolumeManager::Instance() {
57 if (!sInstance)
58 sInstance = new VolumeManager();
59 return sInstance;
60}
61
62VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080063 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -070064 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -080065 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -070066 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -040067 mUmsSharingCount = 0;
68 mSavedDirtyRatio = -1;
69 // set dirty ratio to 0 when UMS is active
70 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -070071 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -070072}
73
74VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -080075 delete mVolumes;
76 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -070077}
78
Kenny Root7b18a7b2010-03-15 13:13:41 -070079char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -070080 static const char* digits = "0123456789abcdef";
81
Kenny Root7b18a7b2010-03-15 13:13:41 -070082 unsigned char sig[MD5_DIGEST_LENGTH];
83
Kenny Rootacc9e7d2010-06-18 19:06:50 -070084 if (buffer == NULL) {
85 SLOGE("Destination buffer is NULL");
86 errno = ESPIPE;
87 return NULL;
88 } else if (id == NULL) {
89 SLOGE("Source buffer is NULL");
90 errno = ESPIPE;
91 return NULL;
92 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
93 SLOGE("Target hash buffer size < %d bytes (%d)",
94 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -080095 errno = ESPIPE;
96 return NULL;
97 }
Kenny Root7b18a7b2010-03-15 13:13:41 -070098
99 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800100
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700101 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700102 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700103 *p++ = digits[sig[i] >> 4];
104 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800105 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700106 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800107
108 return buffer;
109}
110
111void VolumeManager::setDebug(bool enable) {
112 mDebug = enable;
113 VolumeCollection::iterator it;
114 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
115 (*it)->setDebug(enable);
116 }
117}
118
San Mehatf1b736b2009-10-10 17:22:08 -0700119int VolumeManager::start() {
120 return 0;
121}
122
123int VolumeManager::stop() {
124 return 0;
125}
126
127int VolumeManager::addVolume(Volume *v) {
128 mVolumes->push_back(v);
129 return 0;
130}
131
San Mehatfd7f5872009-10-12 11:32:47 -0700132void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
133 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700134
San Mehatfd7f5872009-10-12 11:32:47 -0700135 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700136 VolumeCollection::iterator it;
137 bool hit = false;
138 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700139 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800140#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700141 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800142#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700143 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700144 break;
145 }
146 }
147
148 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800149#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700150 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800151#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700152 }
153}
154
San Mehatf1b736b2009-10-10 17:22:08 -0700155int VolumeManager::listVolumes(SocketClient *cli) {
156 VolumeCollection::iterator i;
157
158 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
159 char *buffer;
160 asprintf(&buffer, "%s %s %d",
161 (*i)->getLabel(), (*i)->getMountpoint(),
162 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800163 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700164 free(buffer);
165 }
San Mehata2677e42009-12-13 10:40:18 -0800166 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700167 return 0;
168}
San Mehat49e2bce2009-10-12 16:29:01 -0700169
San Mehata2677e42009-12-13 10:40:18 -0800170int VolumeManager::formatVolume(const char *label) {
171 Volume *v = lookupVolume(label);
172
173 if (!v) {
174 errno = ENOENT;
175 return -1;
176 }
177
Ken Sumrall3b170052011-07-11 15:38:57 -0700178 if (mVolManagerDisabled) {
179 errno = EBUSY;
180 return -1;
181 }
182
San Mehata2677e42009-12-13 10:40:18 -0800183 return v->formatVol();
184}
185
Kenny Root508c0e12010-07-12 09:59:49 -0700186int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
187 char idHash[33];
188 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
189 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
190 return -1;
191 }
192
193 memset(mountPath, 0, mountPathLen);
194 snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
195
196 if (access(mountPath, F_OK)) {
197 errno = ENOENT;
198 return -1;
199 }
200
201 return 0;
202}
203
San Mehata19b2502010-01-06 10:33:53 -0800204int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700205 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700206
207 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
208 SLOGE("Couldn't find ASEC %s", id);
209 return -1;
210 }
San Mehat88ac2c02010-03-23 11:15:58 -0700211
212 memset(buffer, 0, maxlen);
213 if (access(asecFileName, F_OK)) {
214 errno = ENOENT;
215 return -1;
216 }
San Mehata19b2502010-01-06 10:33:53 -0800217
San Mehat3bb60202010-02-19 18:14:36 -0800218 snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800219 return 0;
220}
221
Dianne Hackborn736910c2011-06-27 13:37:07 -0700222int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
223 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700224
225 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
226 SLOGE("Couldn't find ASEC %s", id);
227 return -1;
228 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700229
230 memset(buffer, 0, maxlen);
231 if (access(asecFileName, F_OK)) {
232 errno = ENOENT;
233 return -1;
234 }
235
236 snprintf(buffer, maxlen, "%s", asecFileName);
237 return 0;
238}
239
Kenny Root344ca102012-04-03 17:23:01 -0700240int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
241 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800242 struct asec_superblock sb;
243 memset(&sb, 0, sizeof(sb));
244
Kenny Root344ca102012-04-03 17:23:01 -0700245 const bool wantFilesystem = strcmp(fstype, "none");
246 bool usingExt4 = false;
247 if (wantFilesystem) {
248 usingExt4 = !strcmp(fstype, "ext4");
249 if (usingExt4) {
250 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
251 } else if (strcmp(fstype, "fat")) {
252 SLOGE("Invalid filesystem type %s", fstype);
253 errno = EINVAL;
254 return -1;
255 }
256 }
257
San Mehatfcf24fe2010-03-03 12:37:32 -0800258 sb.magic = ASEC_SB_MAGIC;
259 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800260
San Mehatd31e3802010-02-18 08:37:45 -0800261 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700262 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800263 errno = EINVAL;
264 return -1;
265 }
266
San Mehata19b2502010-01-06 10:33:53 -0800267 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700268 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800269 errno = EADDRINUSE;
270 return -1;
271 }
272
273 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700274
275 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
276 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
277 asecFileName, strerror(errno));
278 errno = EADDRINUSE;
279 return -1;
280 }
281
282 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
283
284 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
San Mehata19b2502010-01-06 10:33:53 -0800285
286 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700287 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700288 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800289 errno = EADDRINUSE;
290 return -1;
291 }
292
San Mehatfcf24fe2010-03-03 12:37:32 -0800293 /*
294 * Add some headroom
295 */
296 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
297 unsigned numImgSectors = numSectors + fatSize + 2;
298
299 if (numImgSectors % 63) {
300 numImgSectors += (63 - (numImgSectors % 63));
301 }
302
303 // Add +1 for our superblock which is at the end
304 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700305 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800306 return -1;
307 }
308
San Mehatd9a4e352010-03-12 13:32:47 -0800309 char idHash[33];
310 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700311 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800312 unlink(asecFileName);
313 return -1;
314 }
315
San Mehata19b2502010-01-06 10:33:53 -0800316 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800317 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700318 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800319 unlink(asecFileName);
320 return -1;
321 }
322
San Mehatb78a32c2010-01-10 13:02:12 -0800323 char dmDevice[255];
324 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800325
San Mehatb78a32c2010-01-10 13:02:12 -0800326 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800327 // XXX: This is all we support for now
328 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800329 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800330 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700331 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800332 Loop::destroyByDevice(loopDevice);
333 unlink(asecFileName);
334 return -1;
335 }
336 cleanupDm = true;
337 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800338 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800339 strcpy(dmDevice, loopDevice);
340 }
341
San Mehatfcf24fe2010-03-03 12:37:32 -0800342 /*
343 * Drop down the superblock at the end of the file
344 */
345
346 int sbfd = open(loopDevice, O_RDWR);
347 if (sbfd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700348 SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800349 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800350 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800351 }
352 Loop::destroyByDevice(loopDevice);
353 unlink(asecFileName);
354 return -1;
355 }
356
357 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
358 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700359 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800360 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800361 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800362 }
363 Loop::destroyByDevice(loopDevice);
364 unlink(asecFileName);
365 return -1;
366 }
367
368 if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
369 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700370 SLOGE("Failed to write superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800371 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800372 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800373 }
374 Loop::destroyByDevice(loopDevice);
375 unlink(asecFileName);
376 return -1;
377 }
378 close(sbfd);
379
Kenny Root344ca102012-04-03 17:23:01 -0700380 if (wantFilesystem) {
381 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400382 char mountPoint[255];
383
384 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
385
Kenny Root344ca102012-04-03 17:23:01 -0700386 if (usingExt4) {
rpcraiga54e13a2012-09-21 14:17:08 -0400387 formatStatus = Ext4::format(dmDevice, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700388 } else {
389 formatStatus = Fat::format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800390 }
San Mehata19b2502010-01-06 10:33:53 -0800391
Kenny Root344ca102012-04-03 17:23:01 -0700392 if (formatStatus < 0) {
393 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800394 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800395 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800396 }
San Mehateb13a902010-01-07 12:12:50 -0800397 Loop::destroyByDevice(loopDevice);
398 unlink(asecFileName);
399 return -1;
400 }
Kenny Root344ca102012-04-03 17:23:01 -0700401
Kenny Root344ca102012-04-03 17:23:01 -0700402 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800403 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700404 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800405 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800406 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800407 }
408 Loop::destroyByDevice(loopDevice);
409 unlink(asecFileName);
410 return -1;
411 }
San Mehatb78a32c2010-01-10 13:02:12 -0800412 }
San Mehata1091cb2010-02-28 20:17:20 -0800413
Kenny Root344ca102012-04-03 17:23:01 -0700414 int mountStatus;
415 if (usingExt4) {
416 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
417 } else {
418 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
419 false);
420 }
421
422 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700423 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800424 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800425 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800426 }
427 Loop::destroyByDevice(loopDevice);
428 unlink(asecFileName);
429 return -1;
430 }
Kenny Root344ca102012-04-03 17:23:01 -0700431
432 if (usingExt4) {
433 int dirfd = open(mountPoint, O_DIRECTORY);
434 if (dirfd >= 0) {
435 if (fchown(dirfd, ownerUid, AID_SYSTEM)
436 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
437 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
438 }
439 close(dirfd);
440 }
441 }
San Mehata1091cb2010-02-28 20:17:20 -0800442 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700443 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800444 }
San Mehat88705162010-01-15 09:26:28 -0800445
Kenny Rootcbacf782010-09-24 15:11:48 -0700446 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800447 return 0;
448}
449
450int VolumeManager::finalizeAsec(const char *id) {
451 char asecFileName[255];
452 char loopDevice[255];
453 char mountPoint[255];
454
Kenny Root344ca102012-04-03 17:23:01 -0700455 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
456 SLOGE("Couldn't find ASEC %s", id);
457 return -1;
458 }
San Mehata19b2502010-01-06 10:33:53 -0800459
San Mehatd9a4e352010-03-12 13:32:47 -0800460 char idHash[33];
461 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700462 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800463 return -1;
464 }
465
466 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700467 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800468 return -1;
469 }
470
Kenny Root344ca102012-04-03 17:23:01 -0700471 unsigned int nr_sec = 0;
472 struct asec_superblock sb;
473
474 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
475 return -1;
476 }
477
San Mehat3bb60202010-02-19 18:14:36 -0800478 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
Kenny Root344ca102012-04-03 17:23:01 -0700479
480 int result = 0;
481 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
482 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
483 } else {
484 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
485 }
486
487 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700488 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800489 return -1;
490 }
491
San Mehatd9a4e352010-03-12 13:32:47 -0800492 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700493 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800494 }
San Mehata19b2502010-01-06 10:33:53 -0800495 return 0;
496}
497
Kenny Root344ca102012-04-03 17:23:01 -0700498int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
499 char asecFileName[255];
500 char loopDevice[255];
501 char mountPoint[255];
502
503 if (gid < AID_APP) {
504 SLOGE("Group ID is not in application range");
505 return -1;
506 }
507
508 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
509 SLOGE("Couldn't find ASEC %s", id);
510 return -1;
511 }
512
513 char idHash[33];
514 if (!asecHash(id, idHash, sizeof(idHash))) {
515 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
516 return -1;
517 }
518
519 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
520 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
521 return -1;
522 }
523
524 unsigned int nr_sec = 0;
525 struct asec_superblock sb;
526
527 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
528 return -1;
529 }
530
531 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
532
533 int result = 0;
534 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
535 return 0;
536 }
537
538 int ret = Ext4::doMount(loopDevice, mountPoint,
539 false /* read-only */,
540 true /* remount */,
541 false /* executable */);
542 if (ret) {
543 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
544 return -1;
545 }
546
547 char *paths[] = { mountPoint, NULL };
548
549 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
550 if (fts) {
551 // Traverse the entire hierarchy and chown to system UID.
552 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
553 // We don't care about the lost+found directory.
554 if (!strcmp(ftsent->fts_name, "lost+found")) {
555 continue;
556 }
557
558 /*
559 * There can only be one file marked as private right now.
560 * This should be more robust, but it satisfies the requirements
561 * we have for right now.
562 */
563 const bool privateFile = !strcmp(ftsent->fts_name, filename);
564
565 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
566 if (fd < 0) {
567 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
568 result = -1;
569 continue;
570 }
571
572 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
573
574 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700575 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700576 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700577 result |= fchmod(fd, privateFile ? 0640 : 0644);
578 }
579 close(fd);
580 }
581 fts_close(fts);
582
583 // Finally make the directory readable by everyone.
584 int dirfd = open(mountPoint, O_DIRECTORY);
585 if (dirfd < 0 || fchmod(dirfd, 0755)) {
586 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
587 result |= -1;
588 }
589 close(dirfd);
590 } else {
591 result |= -1;
592 }
593
594 result |= Ext4::doMount(loopDevice, mountPoint,
595 true /* read-only */,
596 true /* remount */,
597 true /* execute */);
598
599 if (result) {
600 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
601 return -1;
602 }
603
604 if (mDebug) {
605 SLOGD("ASEC %s permissions fixed", id);
606 }
607 return 0;
608}
609
San Mehat048b0802010-01-23 08:17:06 -0800610int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700611 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800612 char *asecFilename2;
613 char mountPoint[255];
614
Kenny Root344ca102012-04-03 17:23:01 -0700615 const char *dir;
616
617 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
618 SLOGE("Couldn't find ASEC %s", id1);
619 return -1;
620 }
621
622 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800623
San Mehat3bb60202010-02-19 18:14:36 -0800624 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
San Mehat048b0802010-01-23 08:17:06 -0800625 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700626 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800627 errno = EBUSY;
628 goto out_err;
629 }
630
San Mehat96956ed2010-02-24 08:42:51 -0800631 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
632 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700633 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800634 errno = EBUSY;
635 goto out_err;
636 }
637
San Mehat048b0802010-01-23 08:17:06 -0800638 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700639 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800640 errno = EADDRINUSE;
641 goto out_err;
642 }
643
644 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700645 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800646 goto out_err;
647 }
648
San Mehat048b0802010-01-23 08:17:06 -0800649 free(asecFilename2);
650 return 0;
651
652out_err:
San Mehat048b0802010-01-23 08:17:06 -0800653 free(asecFilename2);
654 return -1;
655}
656
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700657#define UNMOUNT_RETRIES 5
658#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800659int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800660 char asecFileName[255];
661 char mountPoint[255];
662
Kenny Root344ca102012-04-03 17:23:01 -0700663 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
664 SLOGE("Couldn't find ASEC %s", id);
665 return -1;
666 }
667
San Mehat3bb60202010-02-19 18:14:36 -0800668 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800669
San Mehatd9a4e352010-03-12 13:32:47 -0800670 char idHash[33];
671 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700672 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800673 return -1;
674 }
675
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700676 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
677}
678
Kenny Root508c0e12010-07-12 09:59:49 -0700679int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700680 char mountPoint[255];
681
682 char idHash[33];
683 if (!asecHash(fileName, idHash, sizeof(idHash))) {
684 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
685 return -1;
686 }
687
688 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
689
690 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
691}
692
693int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
694 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -0800695 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700696 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -0700697 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -0800698 return -1;
699 }
San Mehat23969932010-01-09 07:08:06 -0800700
San Mehatb78a32c2010-01-10 13:02:12 -0800701 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700702 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -0800703 rc = umount(mountPoint);
704 if (!rc) {
705 break;
San Mehata19b2502010-01-06 10:33:53 -0800706 }
San Mehatb78a32c2010-01-10 13:02:12 -0800707 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700708 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800709 rc = 0;
710 break;
San Mehata19b2502010-01-06 10:33:53 -0800711 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700712 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -0800713 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800714
San Mehat4ba89482010-02-18 09:00:18 -0800715 int action = 0; // default is to just complain
716
717 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700718 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -0800719 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700720 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -0800721 action = 1; // SIGHUP
722 }
San Mehat8c940ef2010-02-13 14:19:53 -0800723
San Mehat586536c2010-02-16 17:12:00 -0800724 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700725 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -0800726 }
727
728 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -0800729 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700730 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800731 return -1;
732 }
733
San Mehat12f4b892010-02-24 11:43:22 -0800734 int retries = 10;
735
736 while(retries--) {
737 if (!rmdir(mountPoint)) {
738 break;
739 }
740
San Mehat97ac40e2010-03-24 10:24:19 -0700741 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700742 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -0800743 }
744
745 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -0700746 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -0800747 }
San Mehat88705162010-01-15 09:26:28 -0800748
San Mehatd9a4e352010-03-12 13:32:47 -0800749 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700750 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800751 }
752
753 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800754 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800755 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800756 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700757 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800758 }
San Mehat88705162010-01-15 09:26:28 -0800759
760 AsecIdCollection::iterator it;
761 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -0700762 ContainerData* cd = *it;
763 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -0800764 free(*it);
765 mActiveContainers->erase(it);
766 break;
767 }
768 }
769 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700770 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -0800771 }
San Mehatb78a32c2010-01-10 13:02:12 -0800772 return 0;
773}
774
San Mehat4ba89482010-02-18 09:00:18 -0800775int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -0800776 char asecFileName[255];
777 char mountPoint[255];
778
Kenny Root344ca102012-04-03 17:23:01 -0700779 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
780 SLOGE("Couldn't find ASEC %s", id);
781 return -1;
782 }
783
San Mehat55013f72010-02-24 12:12:34 -0800784 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatb78a32c2010-01-10 13:02:12 -0800785
San Mehat0586d542010-01-12 15:38:59 -0800786 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800787 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700788 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -0800789 }
San Mehat4ba89482010-02-18 09:00:18 -0800790 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700791 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800792 return -1;
793 }
794 }
San Mehata19b2502010-01-06 10:33:53 -0800795
San Mehat0586d542010-01-12 15:38:59 -0800796 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700797 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800798 return -1;
799 }
San Mehata19b2502010-01-06 10:33:53 -0800800
San Mehatd9a4e352010-03-12 13:32:47 -0800801 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700802 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800803 }
San Mehata19b2502010-01-06 10:33:53 -0800804 return 0;
805}
806
Kenny Root344ca102012-04-03 17:23:01 -0700807bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
808 int dirfd = open(dir, O_DIRECTORY);
809 if (dirfd < 0) {
810 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
811 return -1;
812 }
813
814 bool ret = false;
815
816 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
817 ret = true;
818 }
819
820 close(dirfd);
821
822 return ret;
823}
824
825int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
826 const char **directory) const {
827 int dirfd, fd;
828 const int idLen = strlen(id);
829 char *asecName;
830
831 if (asprintf(&asecName, "%s.asec", id) < 0) {
832 SLOGE("Couldn't allocate string to write ASEC name");
833 return -1;
834 }
835
836 const char *dir;
837 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
838 dir = Volume::SEC_ASECDIR_INT;
839 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
840 dir = Volume::SEC_ASECDIR_EXT;
841 } else {
842 free(asecName);
843 return -1;
844 }
845
846 if (directory != NULL) {
847 *directory = dir;
848 }
849
850 if (asecPath != NULL) {
851 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
852 if (written < 0 || static_cast<size_t>(written) >= asecPathLen) {
853 free(asecName);
854 return -1;
855 }
856 }
857
858 free(asecName);
859 return 0;
860}
861
San Mehata19b2502010-01-06 10:33:53 -0800862int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
863 char asecFileName[255];
864 char mountPoint[255];
865
Kenny Root344ca102012-04-03 17:23:01 -0700866 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
867 SLOGE("Couldn't find ASEC %s", id);
868 return -1;
869 }
870
San Mehat3bb60202010-02-19 18:14:36 -0800871 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800872
873 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700874 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -0800875 errno = EBUSY;
876 return -1;
877 }
878
San Mehatd9a4e352010-03-12 13:32:47 -0800879 char idHash[33];
880 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700881 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800882 return -1;
883 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700884
San Mehata19b2502010-01-06 10:33:53 -0800885 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800886 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
887 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700888 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800889 return -1;
890 }
San Mehatd9a4e352010-03-12 13:32:47 -0800891 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700892 SLOGD("New loop device created at %s", loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800893 }
San Mehatb78a32c2010-01-10 13:02:12 -0800894 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800895 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700896 SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800897 }
San Mehatb78a32c2010-01-10 13:02:12 -0800898 }
899
900 char dmDevice[255];
901 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -0800902 int fd;
903 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800904 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -0800905
Kenny Root344ca102012-04-03 17:23:01 -0700906 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
907 return -1;
908 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800909
San Mehatd9a4e352010-03-12 13:32:47 -0800910 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700911 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -0800912 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800913 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -0700914 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -0800915 Loop::destroyByDevice(loopDevice);
916 errno = EMEDIUMTYPE;
917 return -1;
918 }
919 nr_sec--; // We don't want the devmapping to extend onto our superblock
920
San Mehatb78a32c2010-01-10 13:02:12 -0800921 if (strcmp(key, "none")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800922 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
923 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -0800924 dmDevice, sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700925 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800926 Loop::destroyByDevice(loopDevice);
927 return -1;
928 }
San Mehatd9a4e352010-03-12 13:32:47 -0800929 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700930 SLOGD("New devmapper instance created at %s", dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800931 }
San Mehatb78a32c2010-01-10 13:02:12 -0800932 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800933 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700934 SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800935 }
San Mehatb78a32c2010-01-10 13:02:12 -0800936 }
937 cleanupDm = true;
938 } else {
939 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800940 }
941
Kenny Root344ca102012-04-03 17:23:01 -0700942 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800943 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700944 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800945 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800946 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800947 }
948 Loop::destroyByDevice(loopDevice);
949 return -1;
950 }
San Mehata19b2502010-01-06 10:33:53 -0800951 }
952
Kenny Rootcdc2a1c2012-05-03 13:49:46 -0700953 /*
954 * The device mapper node needs to be created. Sometimes it takes a
955 * while. Wait for up to 1 second. We could also inspect incoming uevents,
956 * but that would take more effort.
957 */
958 int tries = 25;
959 while (tries--) {
960 if (!access(dmDevice, F_OK) || errno != ENOENT) {
961 break;
962 }
963 usleep(40 * 1000);
964 }
965
Kenny Root344ca102012-04-03 17:23:01 -0700966 int result;
967 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
968 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
969 } else {
970 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
971 }
972
973 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700974 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800975 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800976 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800977 }
978 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800979 return -1;
980 }
981
Kenny Rootcbacf782010-09-24 15:11:48 -0700982 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -0800983 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700984 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800985 }
San Mehata19b2502010-01-06 10:33:53 -0800986 return 0;
987}
988
Kenny Root93ecb382012-08-09 11:28:37 -0700989Volume* VolumeManager::getVolumeForFile(const char *fileName) {
990 VolumeCollection::iterator i;
991
992 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
993 const char* mountPoint = (*i)->getMountpoint();
994 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
995 return *i;
996 }
997 }
998
999 return NULL;
1000}
1001
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001002/**
1003 * Mounts an image file <code>img</code>.
1004 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001005int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001006 char mountPoint[255];
1007
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001008 char idHash[33];
1009 if (!asecHash(img, idHash, sizeof(idHash))) {
1010 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1011 return -1;
1012 }
1013
1014 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1015
1016 if (isMountpointMounted(mountPoint)) {
1017 SLOGE("Image %s already mounted", img);
1018 errno = EBUSY;
1019 return -1;
1020 }
1021
1022 char loopDevice[255];
1023 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1024 if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) {
1025 SLOGE("Image loop device creation failed (%s)", strerror(errno));
1026 return -1;
1027 }
1028 if (mDebug) {
1029 SLOGD("New loop device created at %s", loopDevice);
1030 }
1031 } else {
1032 if (mDebug) {
1033 SLOGD("Found active loopback for %s at %s", img, loopDevice);
1034 }
1035 }
1036
1037 char dmDevice[255];
1038 bool cleanupDm = false;
1039 int fd;
1040 unsigned int nr_sec = 0;
1041
1042 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1043 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1044 Loop::destroyByDevice(loopDevice);
1045 return -1;
1046 }
1047
1048 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1049 SLOGE("Failed to get loop size (%s)", strerror(errno));
1050 Loop::destroyByDevice(loopDevice);
1051 close(fd);
1052 return -1;
1053 }
1054
1055 close(fd);
1056
1057 if (strcmp(key, "none")) {
1058 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
1059 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
1060 dmDevice, sizeof(dmDevice))) {
1061 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
1062 Loop::destroyByDevice(loopDevice);
1063 return -1;
1064 }
1065 if (mDebug) {
1066 SLOGD("New devmapper instance created at %s", dmDevice);
1067 }
1068 } else {
1069 if (mDebug) {
1070 SLOGD("Found active devmapper for %s at %s", img, dmDevice);
1071 }
1072 }
1073 cleanupDm = true;
1074 } else {
1075 strcpy(dmDevice, loopDevice);
1076 }
1077
1078 if (mkdir(mountPoint, 0755)) {
1079 if (errno != EEXIST) {
1080 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1081 if (cleanupDm) {
1082 Devmapper::destroy(idHash);
1083 }
1084 Loop::destroyByDevice(loopDevice);
1085 return -1;
1086 }
1087 }
1088
Jeff Sharkey69479042012-09-25 16:14:57 -07001089 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001090 0227, false)) {
1091 SLOGE("Image mount failed (%s)", strerror(errno));
1092 if (cleanupDm) {
1093 Devmapper::destroy(idHash);
1094 }
1095 Loop::destroyByDevice(loopDevice);
1096 return -1;
1097 }
1098
Kenny Rootcbacf782010-09-24 15:11:48 -07001099 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001100 if (mDebug) {
1101 SLOGD("Image %s mounted", img);
1102 }
1103 return 0;
1104}
1105
San Mehat49e2bce2009-10-12 16:29:01 -07001106int VolumeManager::mountVolume(const char *label) {
1107 Volume *v = lookupVolume(label);
1108
1109 if (!v) {
1110 errno = ENOENT;
1111 return -1;
1112 }
1113
San Mehata2677e42009-12-13 10:40:18 -08001114 return v->mountVol();
1115}
1116
Kenny Root508c0e12010-07-12 09:59:49 -07001117int VolumeManager::listMountedObbs(SocketClient* cli) {
1118 char device[256];
1119 char mount_path[256];
1120 char rest[256];
1121 FILE *fp;
1122 char line[1024];
1123
1124 if (!(fp = fopen("/proc/mounts", "r"))) {
1125 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1126 return -1;
1127 }
1128
1129 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001130 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001131 char loopDir[loopDirLen + 2];
1132 strcpy(loopDir, Volume::LOOPDIR);
1133 loopDir[loopDirLen++] = '/';
1134 loopDir[loopDirLen] = '\0';
1135
1136 while(fgets(line, sizeof(line), fp)) {
1137 line[strlen(line)-1] = '\0';
1138
1139 /*
1140 * Should look like:
1141 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1142 */
1143 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1144
1145 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1146 int fd = open(device, O_RDONLY);
1147 if (fd >= 0) {
1148 struct loop_info64 li;
1149 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1150 cli->sendMsg(ResponseCode::AsecListResult,
1151 (const char*) li.lo_file_name, false);
1152 }
1153 close(fd);
1154 }
1155 }
1156 }
1157
1158 fclose(fp);
1159 return 0;
1160}
1161
San Mehateba65e92010-01-29 05:15:16 -08001162int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1163 Volume *v = lookupVolume(label);
1164
1165 if (!v) {
1166 errno = ENOENT;
1167 return -1;
1168 }
1169
1170 if (strcmp(method, "ums")) {
1171 errno = ENOSYS;
1172 return -1;
1173 }
1174
1175 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001176 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001177 } else {
1178 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001179 }
1180 return 0;
1181}
1182
San Mehata2677e42009-12-13 10:40:18 -08001183int VolumeManager::shareVolume(const char *label, const char *method) {
1184 Volume *v = lookupVolume(label);
1185
1186 if (!v) {
1187 errno = ENOENT;
1188 return -1;
1189 }
1190
1191 /*
1192 * Eventually, we'll want to support additional share back-ends,
1193 * some of which may work while the media is mounted. For now,
1194 * we just support UMS
1195 */
1196 if (strcmp(method, "ums")) {
1197 errno = ENOSYS;
1198 return -1;
1199 }
1200
1201 if (v->getState() == Volume::State_NoMedia) {
1202 errno = ENODEV;
1203 return -1;
1204 }
1205
San Mehat49e2bce2009-10-12 16:29:01 -07001206 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001207 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001208 errno = EBUSY;
1209 return -1;
1210 }
1211
Ken Sumrall3b170052011-07-11 15:38:57 -07001212 if (mVolManagerDisabled) {
1213 errno = EBUSY;
1214 return -1;
1215 }
1216
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001217 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001218 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1219 // This volume does not support raw disk access
1220 errno = EINVAL;
1221 return -1;
1222 }
1223
1224 int fd;
1225 char nodepath[255];
1226 snprintf(nodepath,
1227 sizeof(nodepath), "/dev/block/vold/%d:%d",
1228 MAJOR(d), MINOR(d));
1229
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001230 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001231 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001232 return -1;
1233 }
1234
1235 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001236 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001237 close(fd);
1238 return -1;
1239 }
1240
1241 close(fd);
1242 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001243 if (mUmsSharingCount++ == 0) {
1244 FILE* fp;
1245 mSavedDirtyRatio = -1; // in case we fail
1246 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1247 char line[16];
1248 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1249 fprintf(fp, "%d\n", mUmsDirtyRatio);
1250 } else {
1251 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1252 }
1253 fclose(fp);
1254 } else {
1255 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1256 }
1257 }
San Mehata2677e42009-12-13 10:40:18 -08001258 return 0;
1259}
1260
1261int VolumeManager::unshareVolume(const char *label, const char *method) {
1262 Volume *v = lookupVolume(label);
1263
1264 if (!v) {
1265 errno = ENOENT;
1266 return -1;
1267 }
1268
1269 if (strcmp(method, "ums")) {
1270 errno = ENOSYS;
1271 return -1;
1272 }
1273
1274 if (v->getState() != Volume::State_Shared) {
1275 errno = EINVAL;
1276 return -1;
1277 }
1278
San Mehata2677e42009-12-13 10:40:18 -08001279 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001280 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001281 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001282 return -1;
1283 }
1284
1285 char ch = 0;
1286 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001287 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001288 close(fd);
1289 return -1;
1290 }
1291
1292 close(fd);
1293 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001294 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1295 FILE* fp;
1296 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1297 fprintf(fp, "%d\n", mSavedDirtyRatio);
1298 fclose(fp);
1299 } else {
1300 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1301 }
1302 mSavedDirtyRatio = -1;
1303 }
San Mehata2677e42009-12-13 10:40:18 -08001304 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001305}
1306
Ken Sumrall3b170052011-07-11 15:38:57 -07001307extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001308 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001309 vm->disableVolumeManager();
1310 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001311 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001312}
1313
1314extern "C" int vold_getNumDirectVolumes(void) {
1315 VolumeManager *vm = VolumeManager::Instance();
1316 return vm->getNumDirectVolumes();
1317}
1318
1319int VolumeManager::getNumDirectVolumes(void) {
1320 VolumeCollection::iterator i;
1321 int n=0;
1322
1323 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1324 if ((*i)->getShareDevice() != (dev_t)0) {
1325 n++;
1326 }
1327 }
1328 return n;
1329}
1330
1331extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1332 VolumeManager *vm = VolumeManager::Instance();
1333 return vm->getDirectVolumeList(vol_list);
1334}
1335
1336int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1337 VolumeCollection::iterator i;
1338 int n=0;
1339 dev_t d;
1340
1341 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1342 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1343 (*i)->getVolInfo(&vol_list[n]);
1344 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
1345 "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d));
1346 n++;
1347 }
1348 }
1349
1350 return 0;
1351}
1352
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001353int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001354 Volume *v = lookupVolume(label);
1355
1356 if (!v) {
1357 errno = ENOENT;
1358 return -1;
1359 }
1360
San Mehata2677e42009-12-13 10:40:18 -08001361 if (v->getState() == Volume::State_NoMedia) {
1362 errno = ENODEV;
1363 return -1;
1364 }
1365
San Mehat49e2bce2009-10-12 16:29:01 -07001366 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001367 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001368 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001369 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001370 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001371 }
1372
San Mehat1a06eda2010-04-15 12:58:50 -07001373 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001374
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001375 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001376}
1377
Ken Sumrall425524d2012-06-14 20:55:28 -07001378extern "C" int vold_unmountAllAsecs(void) {
1379 int rc;
1380
1381 VolumeManager *vm = VolumeManager::Instance();
1382 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1383 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1384 rc = -1;
1385 }
1386 return rc;
1387}
1388
1389#define ID_BUF_LEN 256
1390#define ASEC_SUFFIX ".asec"
1391#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1392int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1393 DIR *d = opendir(directory);
1394 int rc = 0;
1395
1396 if (!d) {
1397 SLOGE("Could not open asec dir %s", directory);
1398 return -1;
1399 }
1400
1401 size_t dirent_len = offsetof(struct dirent, d_name) +
1402 pathconf(directory, _PC_NAME_MAX) + 1;
1403
1404 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1405 if (dent == NULL) {
1406 SLOGE("Failed to allocate memory for asec dir");
1407 return -1;
1408 }
1409
1410 struct dirent *result;
1411 while (!readdir_r(d, dent, &result) && result != NULL) {
1412 if (dent->d_name[0] == '.')
1413 continue;
1414 if (dent->d_type != DT_REG)
1415 continue;
1416 size_t name_len = strlen(dent->d_name);
1417 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1418 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1419 char id[ID_BUF_LEN];
1420 strlcpy(id, dent->d_name, name_len - 4);
1421 if (unmountAsec(id, true)) {
1422 /* Register the error, but try to unmount more asecs */
1423 rc = -1;
1424 }
1425 }
1426 }
1427 closedir(d);
1428
1429 free(dent);
1430
1431 return rc;
1432}
1433
San Mehata2677e42009-12-13 10:40:18 -08001434/*
1435 * Looks up a volume by it's label or mount-point
1436 */
San Mehat49e2bce2009-10-12 16:29:01 -07001437Volume *VolumeManager::lookupVolume(const char *label) {
1438 VolumeCollection::iterator i;
1439
1440 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001441 if (label[0] == '/') {
1442 if (!strcmp(label, (*i)->getMountpoint()))
1443 return (*i);
1444 } else {
1445 if (!strcmp(label, (*i)->getLabel()))
1446 return (*i);
1447 }
San Mehat49e2bce2009-10-12 16:29:01 -07001448 }
1449 return NULL;
1450}
San Mehata19b2502010-01-06 10:33:53 -08001451
1452bool VolumeManager::isMountpointMounted(const char *mp)
1453{
1454 char device[256];
1455 char mount_path[256];
1456 char rest[256];
1457 FILE *fp;
1458 char line[1024];
1459
1460 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001461 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001462 return false;
1463 }
1464
1465 while(fgets(line, sizeof(line), fp)) {
1466 line[strlen(line)-1] = '\0';
1467 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1468 if (!strcmp(mount_path, mp)) {
1469 fclose(fp);
1470 return true;
1471 }
San Mehata19b2502010-01-06 10:33:53 -08001472 }
1473
1474 fclose(fp);
1475 return false;
1476}
1477
San Mehat1a06eda2010-04-15 12:58:50 -07001478int VolumeManager::cleanupAsec(Volume *v, bool force) {
Kenny Root93ecb382012-08-09 11:28:37 -07001479 int rc = unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1480
1481 AsecIdCollection toUnmount;
1482 // Find the remaining OBB files that are on external storage.
1483 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1484 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001485 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07001486
Kenny Rootcbacf782010-09-24 15:11:48 -07001487 if (cd->type == ASEC) {
Kenny Root93ecb382012-08-09 11:28:37 -07001488 // nothing
Kenny Rootcbacf782010-09-24 15:11:48 -07001489 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07001490 if (v == getVolumeForFile(cd->id)) {
1491 toUnmount.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001492 }
1493 } else {
1494 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001495 }
1496 }
Kenny Root93ecb382012-08-09 11:28:37 -07001497
1498 for (AsecIdCollection::iterator it = toUnmount.begin(); it != toUnmount.end(); ++it) {
1499 ContainerData *cd = *it;
1500 SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
1501 if (unmountObb(cd->id, force)) {
1502 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1503 rc = -1;
1504 }
1505 }
1506
1507 return rc;
1508
San Mehat1a06eda2010-04-15 12:58:50 -07001509}
1510