blob: 9867bbb027481e39d8099db3d0cba28139cdaef9 [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 Rootfb7c4d52010-06-30 18:48:41 -0700989/**
990 * Mounts an image file <code>img</code>.
991 */
Kenny Root508c0e12010-07-12 09:59:49 -0700992int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700993 char mountPoint[255];
994
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700995 char idHash[33];
996 if (!asecHash(img, idHash, sizeof(idHash))) {
997 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
998 return -1;
999 }
1000
1001 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1002
1003 if (isMountpointMounted(mountPoint)) {
1004 SLOGE("Image %s already mounted", img);
1005 errno = EBUSY;
1006 return -1;
1007 }
1008
1009 char loopDevice[255];
1010 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1011 if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) {
1012 SLOGE("Image loop device creation failed (%s)", strerror(errno));
1013 return -1;
1014 }
1015 if (mDebug) {
1016 SLOGD("New loop device created at %s", loopDevice);
1017 }
1018 } else {
1019 if (mDebug) {
1020 SLOGD("Found active loopback for %s at %s", img, loopDevice);
1021 }
1022 }
1023
1024 char dmDevice[255];
1025 bool cleanupDm = false;
1026 int fd;
1027 unsigned int nr_sec = 0;
1028
1029 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1030 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1031 Loop::destroyByDevice(loopDevice);
1032 return -1;
1033 }
1034
1035 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1036 SLOGE("Failed to get loop size (%s)", strerror(errno));
1037 Loop::destroyByDevice(loopDevice);
1038 close(fd);
1039 return -1;
1040 }
1041
1042 close(fd);
1043
1044 if (strcmp(key, "none")) {
1045 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
1046 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
1047 dmDevice, sizeof(dmDevice))) {
1048 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
1049 Loop::destroyByDevice(loopDevice);
1050 return -1;
1051 }
1052 if (mDebug) {
1053 SLOGD("New devmapper instance created at %s", dmDevice);
1054 }
1055 } else {
1056 if (mDebug) {
1057 SLOGD("Found active devmapper for %s at %s", img, dmDevice);
1058 }
1059 }
1060 cleanupDm = true;
1061 } else {
1062 strcpy(dmDevice, loopDevice);
1063 }
1064
1065 if (mkdir(mountPoint, 0755)) {
1066 if (errno != EEXIST) {
1067 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1068 if (cleanupDm) {
1069 Devmapper::destroy(idHash);
1070 }
1071 Loop::destroyByDevice(loopDevice);
1072 return -1;
1073 }
1074 }
1075
Kenny Roota3e06082010-08-27 08:31:35 -07001076 if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001077 0227, false)) {
1078 SLOGE("Image mount failed (%s)", strerror(errno));
1079 if (cleanupDm) {
1080 Devmapper::destroy(idHash);
1081 }
1082 Loop::destroyByDevice(loopDevice);
1083 return -1;
1084 }
1085
Kenny Rootcbacf782010-09-24 15:11:48 -07001086 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001087 if (mDebug) {
1088 SLOGD("Image %s mounted", img);
1089 }
1090 return 0;
1091}
1092
San Mehat49e2bce2009-10-12 16:29:01 -07001093int VolumeManager::mountVolume(const char *label) {
1094 Volume *v = lookupVolume(label);
1095
1096 if (!v) {
1097 errno = ENOENT;
1098 return -1;
1099 }
1100
San Mehata2677e42009-12-13 10:40:18 -08001101 return v->mountVol();
1102}
1103
Kenny Root508c0e12010-07-12 09:59:49 -07001104int VolumeManager::listMountedObbs(SocketClient* cli) {
1105 char device[256];
1106 char mount_path[256];
1107 char rest[256];
1108 FILE *fp;
1109 char line[1024];
1110
1111 if (!(fp = fopen("/proc/mounts", "r"))) {
1112 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1113 return -1;
1114 }
1115
1116 // Create a string to compare against that has a trailing slash
1117 int loopDirLen = sizeof(Volume::LOOPDIR);
1118 char loopDir[loopDirLen + 2];
1119 strcpy(loopDir, Volume::LOOPDIR);
1120 loopDir[loopDirLen++] = '/';
1121 loopDir[loopDirLen] = '\0';
1122
1123 while(fgets(line, sizeof(line), fp)) {
1124 line[strlen(line)-1] = '\0';
1125
1126 /*
1127 * Should look like:
1128 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1129 */
1130 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1131
1132 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1133 int fd = open(device, O_RDONLY);
1134 if (fd >= 0) {
1135 struct loop_info64 li;
1136 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1137 cli->sendMsg(ResponseCode::AsecListResult,
1138 (const char*) li.lo_file_name, false);
1139 }
1140 close(fd);
1141 }
1142 }
1143 }
1144
1145 fclose(fp);
1146 return 0;
1147}
1148
San Mehateba65e92010-01-29 05:15:16 -08001149int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1150 Volume *v = lookupVolume(label);
1151
1152 if (!v) {
1153 errno = ENOENT;
1154 return -1;
1155 }
1156
1157 if (strcmp(method, "ums")) {
1158 errno = ENOSYS;
1159 return -1;
1160 }
1161
1162 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001163 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001164 } else {
1165 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001166 }
1167 return 0;
1168}
1169
San Mehata2677e42009-12-13 10:40:18 -08001170int VolumeManager::shareVolume(const char *label, const char *method) {
1171 Volume *v = lookupVolume(label);
1172
1173 if (!v) {
1174 errno = ENOENT;
1175 return -1;
1176 }
1177
1178 /*
1179 * Eventually, we'll want to support additional share back-ends,
1180 * some of which may work while the media is mounted. For now,
1181 * we just support UMS
1182 */
1183 if (strcmp(method, "ums")) {
1184 errno = ENOSYS;
1185 return -1;
1186 }
1187
1188 if (v->getState() == Volume::State_NoMedia) {
1189 errno = ENODEV;
1190 return -1;
1191 }
1192
San Mehat49e2bce2009-10-12 16:29:01 -07001193 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001194 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001195 errno = EBUSY;
1196 return -1;
1197 }
1198
Ken Sumrall3b170052011-07-11 15:38:57 -07001199 if (mVolManagerDisabled) {
1200 errno = EBUSY;
1201 return -1;
1202 }
1203
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001204 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001205 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1206 // This volume does not support raw disk access
1207 errno = EINVAL;
1208 return -1;
1209 }
1210
1211 int fd;
1212 char nodepath[255];
1213 snprintf(nodepath,
1214 sizeof(nodepath), "/dev/block/vold/%d:%d",
1215 MAJOR(d), MINOR(d));
1216
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001217 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001218 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001219 return -1;
1220 }
1221
1222 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001223 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001224 close(fd);
1225 return -1;
1226 }
1227
1228 close(fd);
1229 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001230 if (mUmsSharingCount++ == 0) {
1231 FILE* fp;
1232 mSavedDirtyRatio = -1; // in case we fail
1233 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1234 char line[16];
1235 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1236 fprintf(fp, "%d\n", mUmsDirtyRatio);
1237 } else {
1238 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1239 }
1240 fclose(fp);
1241 } else {
1242 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1243 }
1244 }
San Mehata2677e42009-12-13 10:40:18 -08001245 return 0;
1246}
1247
1248int VolumeManager::unshareVolume(const char *label, const char *method) {
1249 Volume *v = lookupVolume(label);
1250
1251 if (!v) {
1252 errno = ENOENT;
1253 return -1;
1254 }
1255
1256 if (strcmp(method, "ums")) {
1257 errno = ENOSYS;
1258 return -1;
1259 }
1260
1261 if (v->getState() != Volume::State_Shared) {
1262 errno = EINVAL;
1263 return -1;
1264 }
1265
San Mehata2677e42009-12-13 10:40:18 -08001266 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001267 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001268 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001269 return -1;
1270 }
1271
1272 char ch = 0;
1273 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001274 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001275 close(fd);
1276 return -1;
1277 }
1278
1279 close(fd);
1280 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001281 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1282 FILE* fp;
1283 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1284 fprintf(fp, "%d\n", mSavedDirtyRatio);
1285 fclose(fp);
1286 } else {
1287 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1288 }
1289 mSavedDirtyRatio = -1;
1290 }
San Mehata2677e42009-12-13 10:40:18 -08001291 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001292}
1293
Ken Sumrall3b170052011-07-11 15:38:57 -07001294extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001295 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001296 vm->disableVolumeManager();
1297 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001298 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001299}
1300
1301extern "C" int vold_getNumDirectVolumes(void) {
1302 VolumeManager *vm = VolumeManager::Instance();
1303 return vm->getNumDirectVolumes();
1304}
1305
1306int VolumeManager::getNumDirectVolumes(void) {
1307 VolumeCollection::iterator i;
1308 int n=0;
1309
1310 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1311 if ((*i)->getShareDevice() != (dev_t)0) {
1312 n++;
1313 }
1314 }
1315 return n;
1316}
1317
1318extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1319 VolumeManager *vm = VolumeManager::Instance();
1320 return vm->getDirectVolumeList(vol_list);
1321}
1322
1323int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1324 VolumeCollection::iterator i;
1325 int n=0;
1326 dev_t d;
1327
1328 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1329 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1330 (*i)->getVolInfo(&vol_list[n]);
1331 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
1332 "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d));
1333 n++;
1334 }
1335 }
1336
1337 return 0;
1338}
1339
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001340int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001341 Volume *v = lookupVolume(label);
1342
1343 if (!v) {
1344 errno = ENOENT;
1345 return -1;
1346 }
1347
San Mehata2677e42009-12-13 10:40:18 -08001348 if (v->getState() == Volume::State_NoMedia) {
1349 errno = ENODEV;
1350 return -1;
1351 }
1352
San Mehat49e2bce2009-10-12 16:29:01 -07001353 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001354 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001355 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001356 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001357 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001358 }
1359
San Mehat1a06eda2010-04-15 12:58:50 -07001360 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001361
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001362 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001363}
1364
Ken Sumrall425524d2012-06-14 20:55:28 -07001365extern "C" int vold_unmountAllAsecs(void) {
1366 int rc;
1367
1368 VolumeManager *vm = VolumeManager::Instance();
1369 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1370 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1371 rc = -1;
1372 }
1373 return rc;
1374}
1375
1376#define ID_BUF_LEN 256
1377#define ASEC_SUFFIX ".asec"
1378#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1379int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1380 DIR *d = opendir(directory);
1381 int rc = 0;
1382
1383 if (!d) {
1384 SLOGE("Could not open asec dir %s", directory);
1385 return -1;
1386 }
1387
1388 size_t dirent_len = offsetof(struct dirent, d_name) +
1389 pathconf(directory, _PC_NAME_MAX) + 1;
1390
1391 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1392 if (dent == NULL) {
1393 SLOGE("Failed to allocate memory for asec dir");
1394 return -1;
1395 }
1396
1397 struct dirent *result;
1398 while (!readdir_r(d, dent, &result) && result != NULL) {
1399 if (dent->d_name[0] == '.')
1400 continue;
1401 if (dent->d_type != DT_REG)
1402 continue;
1403 size_t name_len = strlen(dent->d_name);
1404 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1405 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1406 char id[ID_BUF_LEN];
1407 strlcpy(id, dent->d_name, name_len - 4);
1408 if (unmountAsec(id, true)) {
1409 /* Register the error, but try to unmount more asecs */
1410 rc = -1;
1411 }
1412 }
1413 }
1414 closedir(d);
1415
1416 free(dent);
1417
1418 return rc;
1419}
1420
San Mehata2677e42009-12-13 10:40:18 -08001421/*
1422 * Looks up a volume by it's label or mount-point
1423 */
San Mehat49e2bce2009-10-12 16:29:01 -07001424Volume *VolumeManager::lookupVolume(const char *label) {
1425 VolumeCollection::iterator i;
1426
1427 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001428 if (label[0] == '/') {
1429 if (!strcmp(label, (*i)->getMountpoint()))
1430 return (*i);
1431 } else {
1432 if (!strcmp(label, (*i)->getLabel()))
1433 return (*i);
1434 }
San Mehat49e2bce2009-10-12 16:29:01 -07001435 }
1436 return NULL;
1437}
San Mehata19b2502010-01-06 10:33:53 -08001438
1439bool VolumeManager::isMountpointMounted(const char *mp)
1440{
1441 char device[256];
1442 char mount_path[256];
1443 char rest[256];
1444 FILE *fp;
1445 char line[1024];
1446
1447 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001448 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001449 return false;
1450 }
1451
1452 while(fgets(line, sizeof(line), fp)) {
1453 line[strlen(line)-1] = '\0';
1454 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1455 if (!strcmp(mount_path, mp)) {
1456 fclose(fp);
1457 return true;
1458 }
San Mehata19b2502010-01-06 10:33:53 -08001459 }
1460
1461 fclose(fp);
1462 return false;
1463}
1464
San Mehat1a06eda2010-04-15 12:58:50 -07001465int VolumeManager::cleanupAsec(Volume *v, bool force) {
1466 while(mActiveContainers->size()) {
1467 AsecIdCollection::iterator it = mActiveContainers->begin();
Kenny Rootcbacf782010-09-24 15:11:48 -07001468 ContainerData* cd = *it;
1469 SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
1470 if (cd->type == ASEC) {
1471 if (unmountAsec(cd->id, force)) {
1472 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1473 return -1;
1474 }
1475 } else if (cd->type == OBB) {
1476 if (unmountObb(cd->id, force)) {
1477 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1478 return -1;
1479 }
1480 } else {
1481 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001482 return -1;
1483 }
1484 }
1485 return 0;
1486}
1487