blob: 038801011d450da2fee78874682056219b4df994 [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;
382 if (usingExt4) {
383 formatStatus = Ext4::format(dmDevice);
384 } else {
385 formatStatus = Fat::format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800386 }
San Mehata19b2502010-01-06 10:33:53 -0800387
Kenny Root344ca102012-04-03 17:23:01 -0700388 if (formatStatus < 0) {
389 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800390 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800391 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800392 }
San Mehateb13a902010-01-07 12:12:50 -0800393 Loop::destroyByDevice(loopDevice);
394 unlink(asecFileName);
395 return -1;
396 }
Kenny Root344ca102012-04-03 17:23:01 -0700397
San Mehata1091cb2010-02-28 20:17:20 -0800398 char mountPoint[255];
San Mehata19b2502010-01-06 10:33:53 -0800399
San Mehata1091cb2010-02-28 20:17:20 -0800400 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
Kenny Root344ca102012-04-03 17:23:01 -0700401 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800402 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700403 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800404 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800405 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800406 }
407 Loop::destroyByDevice(loopDevice);
408 unlink(asecFileName);
409 return -1;
410 }
San Mehatb78a32c2010-01-10 13:02:12 -0800411 }
San Mehata1091cb2010-02-28 20:17:20 -0800412
Kenny Root344ca102012-04-03 17:23:01 -0700413 int mountStatus;
414 if (usingExt4) {
415 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
416 } else {
417 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
418 false);
419 }
420
421 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700422 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800423 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800424 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800425 }
426 Loop::destroyByDevice(loopDevice);
427 unlink(asecFileName);
428 return -1;
429 }
Kenny Root344ca102012-04-03 17:23:01 -0700430
431 if (usingExt4) {
432 int dirfd = open(mountPoint, O_DIRECTORY);
433 if (dirfd >= 0) {
434 if (fchown(dirfd, ownerUid, AID_SYSTEM)
435 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
436 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
437 }
438 close(dirfd);
439 }
440 }
San Mehata1091cb2010-02-28 20:17:20 -0800441 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700442 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800443 }
San Mehat88705162010-01-15 09:26:28 -0800444
Kenny Rootcbacf782010-09-24 15:11:48 -0700445 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800446 return 0;
447}
448
449int VolumeManager::finalizeAsec(const char *id) {
450 char asecFileName[255];
451 char loopDevice[255];
452 char mountPoint[255];
453
Kenny Root344ca102012-04-03 17:23:01 -0700454 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
455 SLOGE("Couldn't find ASEC %s", id);
456 return -1;
457 }
San Mehata19b2502010-01-06 10:33:53 -0800458
San Mehatd9a4e352010-03-12 13:32:47 -0800459 char idHash[33];
460 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700461 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800462 return -1;
463 }
464
465 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700466 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800467 return -1;
468 }
469
Kenny Root344ca102012-04-03 17:23:01 -0700470 unsigned int nr_sec = 0;
471 struct asec_superblock sb;
472
473 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
474 return -1;
475 }
476
San Mehat3bb60202010-02-19 18:14:36 -0800477 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
Kenny Root344ca102012-04-03 17:23:01 -0700478
479 int result = 0;
480 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
481 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
482 } else {
483 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
484 }
485
486 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700487 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800488 return -1;
489 }
490
San Mehatd9a4e352010-03-12 13:32:47 -0800491 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700492 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800493 }
San Mehata19b2502010-01-06 10:33:53 -0800494 return 0;
495}
496
Kenny Root344ca102012-04-03 17:23:01 -0700497int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
498 char asecFileName[255];
499 char loopDevice[255];
500 char mountPoint[255];
501
502 if (gid < AID_APP) {
503 SLOGE("Group ID is not in application range");
504 return -1;
505 }
506
507 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
508 SLOGE("Couldn't find ASEC %s", id);
509 return -1;
510 }
511
512 char idHash[33];
513 if (!asecHash(id, idHash, sizeof(idHash))) {
514 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
515 return -1;
516 }
517
518 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
519 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
520 return -1;
521 }
522
523 unsigned int nr_sec = 0;
524 struct asec_superblock sb;
525
526 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
527 return -1;
528 }
529
530 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
531
532 int result = 0;
533 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
534 return 0;
535 }
536
537 int ret = Ext4::doMount(loopDevice, mountPoint,
538 false /* read-only */,
539 true /* remount */,
540 false /* executable */);
541 if (ret) {
542 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
543 return -1;
544 }
545
546 char *paths[] = { mountPoint, NULL };
547
548 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
549 if (fts) {
550 // Traverse the entire hierarchy and chown to system UID.
551 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
552 // We don't care about the lost+found directory.
553 if (!strcmp(ftsent->fts_name, "lost+found")) {
554 continue;
555 }
556
557 /*
558 * There can only be one file marked as private right now.
559 * This should be more robust, but it satisfies the requirements
560 * we have for right now.
561 */
562 const bool privateFile = !strcmp(ftsent->fts_name, filename);
563
564 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
565 if (fd < 0) {
566 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
567 result = -1;
568 continue;
569 }
570
571 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
572
573 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700574 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700575 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700576 result |= fchmod(fd, privateFile ? 0640 : 0644);
577 }
578 close(fd);
579 }
580 fts_close(fts);
581
582 // Finally make the directory readable by everyone.
583 int dirfd = open(mountPoint, O_DIRECTORY);
584 if (dirfd < 0 || fchmod(dirfd, 0755)) {
585 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
586 result |= -1;
587 }
588 close(dirfd);
589 } else {
590 result |= -1;
591 }
592
593 result |= Ext4::doMount(loopDevice, mountPoint,
594 true /* read-only */,
595 true /* remount */,
596 true /* execute */);
597
598 if (result) {
599 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
600 return -1;
601 }
602
603 if (mDebug) {
604 SLOGD("ASEC %s permissions fixed", id);
605 }
606 return 0;
607}
608
San Mehat048b0802010-01-23 08:17:06 -0800609int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700610 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800611 char *asecFilename2;
612 char mountPoint[255];
613
Kenny Root344ca102012-04-03 17:23:01 -0700614 const char *dir;
615
616 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
617 SLOGE("Couldn't find ASEC %s", id1);
618 return -1;
619 }
620
621 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800622
San Mehat3bb60202010-02-19 18:14:36 -0800623 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
San Mehat048b0802010-01-23 08:17:06 -0800624 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700625 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800626 errno = EBUSY;
627 goto out_err;
628 }
629
San Mehat96956ed2010-02-24 08:42:51 -0800630 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
631 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700632 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800633 errno = EBUSY;
634 goto out_err;
635 }
636
San Mehat048b0802010-01-23 08:17:06 -0800637 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700638 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800639 errno = EADDRINUSE;
640 goto out_err;
641 }
642
643 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700644 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800645 goto out_err;
646 }
647
San Mehat048b0802010-01-23 08:17:06 -0800648 free(asecFilename2);
649 return 0;
650
651out_err:
San Mehat048b0802010-01-23 08:17:06 -0800652 free(asecFilename2);
653 return -1;
654}
655
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700656#define UNMOUNT_RETRIES 5
657#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800658int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800659 char asecFileName[255];
660 char mountPoint[255];
661
Kenny Root344ca102012-04-03 17:23:01 -0700662 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
663 SLOGE("Couldn't find ASEC %s", id);
664 return -1;
665 }
666
San Mehat3bb60202010-02-19 18:14:36 -0800667 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800668
San Mehatd9a4e352010-03-12 13:32:47 -0800669 char idHash[33];
670 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700671 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800672 return -1;
673 }
674
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700675 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
676}
677
Kenny Root508c0e12010-07-12 09:59:49 -0700678int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700679 char mountPoint[255];
680
681 char idHash[33];
682 if (!asecHash(fileName, idHash, sizeof(idHash))) {
683 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
684 return -1;
685 }
686
687 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
688
689 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
690}
691
692int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
693 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -0800694 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700695 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -0700696 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -0800697 return -1;
698 }
San Mehat23969932010-01-09 07:08:06 -0800699
San Mehatb78a32c2010-01-10 13:02:12 -0800700 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700701 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -0800702 rc = umount(mountPoint);
703 if (!rc) {
704 break;
San Mehata19b2502010-01-06 10:33:53 -0800705 }
San Mehatb78a32c2010-01-10 13:02:12 -0800706 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700707 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800708 rc = 0;
709 break;
San Mehata19b2502010-01-06 10:33:53 -0800710 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700711 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -0800712 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800713
San Mehat4ba89482010-02-18 09:00:18 -0800714 int action = 0; // default is to just complain
715
716 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700717 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -0800718 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700719 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -0800720 action = 1; // SIGHUP
721 }
San Mehat8c940ef2010-02-13 14:19:53 -0800722
San Mehat586536c2010-02-16 17:12:00 -0800723 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700724 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -0800725 }
726
727 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -0800728 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700729 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800730 return -1;
731 }
732
San Mehat12f4b892010-02-24 11:43:22 -0800733 int retries = 10;
734
735 while(retries--) {
736 if (!rmdir(mountPoint)) {
737 break;
738 }
739
San Mehat97ac40e2010-03-24 10:24:19 -0700740 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700741 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -0800742 }
743
744 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -0700745 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -0800746 }
San Mehat88705162010-01-15 09:26:28 -0800747
San Mehatd9a4e352010-03-12 13:32:47 -0800748 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700749 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800750 }
751
752 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800753 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800754 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800755 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700756 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800757 }
San Mehat88705162010-01-15 09:26:28 -0800758
759 AsecIdCollection::iterator it;
760 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -0700761 ContainerData* cd = *it;
762 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -0800763 free(*it);
764 mActiveContainers->erase(it);
765 break;
766 }
767 }
768 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700769 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -0800770 }
San Mehatb78a32c2010-01-10 13:02:12 -0800771 return 0;
772}
773
San Mehat4ba89482010-02-18 09:00:18 -0800774int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -0800775 char asecFileName[255];
776 char mountPoint[255];
777
Kenny Root344ca102012-04-03 17:23:01 -0700778 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
779 SLOGE("Couldn't find ASEC %s", id);
780 return -1;
781 }
782
San Mehat55013f72010-02-24 12:12:34 -0800783 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatb78a32c2010-01-10 13:02:12 -0800784
San Mehat0586d542010-01-12 15:38:59 -0800785 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800786 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700787 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -0800788 }
San Mehat4ba89482010-02-18 09:00:18 -0800789 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700790 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800791 return -1;
792 }
793 }
San Mehata19b2502010-01-06 10:33:53 -0800794
San Mehat0586d542010-01-12 15:38:59 -0800795 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700796 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800797 return -1;
798 }
San Mehata19b2502010-01-06 10:33:53 -0800799
San Mehatd9a4e352010-03-12 13:32:47 -0800800 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700801 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800802 }
San Mehata19b2502010-01-06 10:33:53 -0800803 return 0;
804}
805
Kenny Root344ca102012-04-03 17:23:01 -0700806bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
807 int dirfd = open(dir, O_DIRECTORY);
808 if (dirfd < 0) {
809 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
810 return -1;
811 }
812
813 bool ret = false;
814
815 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
816 ret = true;
817 }
818
819 close(dirfd);
820
821 return ret;
822}
823
824int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
825 const char **directory) const {
826 int dirfd, fd;
827 const int idLen = strlen(id);
828 char *asecName;
829
830 if (asprintf(&asecName, "%s.asec", id) < 0) {
831 SLOGE("Couldn't allocate string to write ASEC name");
832 return -1;
833 }
834
835 const char *dir;
836 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
837 dir = Volume::SEC_ASECDIR_INT;
838 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
839 dir = Volume::SEC_ASECDIR_EXT;
840 } else {
841 free(asecName);
842 return -1;
843 }
844
845 if (directory != NULL) {
846 *directory = dir;
847 }
848
849 if (asecPath != NULL) {
850 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
851 if (written < 0 || static_cast<size_t>(written) >= asecPathLen) {
852 free(asecName);
853 return -1;
854 }
855 }
856
857 free(asecName);
858 return 0;
859}
860
San Mehata19b2502010-01-06 10:33:53 -0800861int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
862 char asecFileName[255];
863 char mountPoint[255];
864
Kenny Root344ca102012-04-03 17:23:01 -0700865 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
866 SLOGE("Couldn't find ASEC %s", id);
867 return -1;
868 }
869
San Mehat3bb60202010-02-19 18:14:36 -0800870 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800871
872 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700873 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -0800874 errno = EBUSY;
875 return -1;
876 }
877
San Mehatd9a4e352010-03-12 13:32:47 -0800878 char idHash[33];
879 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700880 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800881 return -1;
882 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700883
San Mehata19b2502010-01-06 10:33:53 -0800884 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800885 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
886 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700887 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800888 return -1;
889 }
San Mehatd9a4e352010-03-12 13:32:47 -0800890 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700891 SLOGD("New loop device created at %s", loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800892 }
San Mehatb78a32c2010-01-10 13:02:12 -0800893 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800894 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700895 SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800896 }
San Mehatb78a32c2010-01-10 13:02:12 -0800897 }
898
899 char dmDevice[255];
900 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -0800901 int fd;
902 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800903 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -0800904
Kenny Root344ca102012-04-03 17:23:01 -0700905 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
906 return -1;
907 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800908
San Mehatd9a4e352010-03-12 13:32:47 -0800909 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700910 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -0800911 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800912 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -0700913 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -0800914 Loop::destroyByDevice(loopDevice);
915 errno = EMEDIUMTYPE;
916 return -1;
917 }
918 nr_sec--; // We don't want the devmapping to extend onto our superblock
919
San Mehatb78a32c2010-01-10 13:02:12 -0800920 if (strcmp(key, "none")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800921 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
922 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -0800923 dmDevice, sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700924 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800925 Loop::destroyByDevice(loopDevice);
926 return -1;
927 }
San Mehatd9a4e352010-03-12 13:32:47 -0800928 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700929 SLOGD("New devmapper instance created at %s", dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800930 }
San Mehatb78a32c2010-01-10 13:02:12 -0800931 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800932 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700933 SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800934 }
San Mehatb78a32c2010-01-10 13:02:12 -0800935 }
936 cleanupDm = true;
937 } else {
938 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800939 }
940
Kenny Root344ca102012-04-03 17:23:01 -0700941 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800942 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700943 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800944 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800945 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800946 }
947 Loop::destroyByDevice(loopDevice);
948 return -1;
949 }
San Mehata19b2502010-01-06 10:33:53 -0800950 }
951
Kenny Rootcdc2a1c2012-05-03 13:49:46 -0700952 /*
953 * The device mapper node needs to be created. Sometimes it takes a
954 * while. Wait for up to 1 second. We could also inspect incoming uevents,
955 * but that would take more effort.
956 */
957 int tries = 25;
958 while (tries--) {
959 if (!access(dmDevice, F_OK) || errno != ENOENT) {
960 break;
961 }
962 usleep(40 * 1000);
963 }
964
Kenny Root344ca102012-04-03 17:23:01 -0700965 int result;
966 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
967 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
968 } else {
969 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
970 }
971
972 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700973 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800974 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800975 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800976 }
977 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800978 return -1;
979 }
980
Kenny Rootcbacf782010-09-24 15:11:48 -0700981 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -0800982 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700983 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800984 }
San Mehata19b2502010-01-06 10:33:53 -0800985 return 0;
986}
987
Kenny Rooteacf7e02012-08-09 11:28:37 -0700988Volume* VolumeManager::getVolumeForFile(const char *fileName) {
989 VolumeCollection::iterator i;
990
991 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
992 const char* mountPoint = (*i)->getMountpoint();
993 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
994 return *i;
995 }
996 }
997
998 return NULL;
999}
1000
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001001/**
1002 * Mounts an image file <code>img</code>.
1003 */
Kenny Root508c0e12010-07-12 09:59:49 -07001004int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001005 char mountPoint[255];
1006
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001007 char idHash[33];
1008 if (!asecHash(img, idHash, sizeof(idHash))) {
1009 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1010 return -1;
1011 }
1012
1013 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1014
1015 if (isMountpointMounted(mountPoint)) {
1016 SLOGE("Image %s already mounted", img);
1017 errno = EBUSY;
1018 return -1;
1019 }
1020
1021 char loopDevice[255];
1022 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1023 if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) {
1024 SLOGE("Image loop device creation failed (%s)", strerror(errno));
1025 return -1;
1026 }
1027 if (mDebug) {
1028 SLOGD("New loop device created at %s", loopDevice);
1029 }
1030 } else {
1031 if (mDebug) {
1032 SLOGD("Found active loopback for %s at %s", img, loopDevice);
1033 }
1034 }
1035
1036 char dmDevice[255];
1037 bool cleanupDm = false;
1038 int fd;
1039 unsigned int nr_sec = 0;
1040
1041 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1042 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1043 Loop::destroyByDevice(loopDevice);
1044 return -1;
1045 }
1046
1047 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1048 SLOGE("Failed to get loop size (%s)", strerror(errno));
1049 Loop::destroyByDevice(loopDevice);
1050 close(fd);
1051 return -1;
1052 }
1053
1054 close(fd);
1055
1056 if (strcmp(key, "none")) {
1057 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
1058 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
1059 dmDevice, sizeof(dmDevice))) {
1060 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
1061 Loop::destroyByDevice(loopDevice);
1062 return -1;
1063 }
1064 if (mDebug) {
1065 SLOGD("New devmapper instance created at %s", dmDevice);
1066 }
1067 } else {
1068 if (mDebug) {
1069 SLOGD("Found active devmapper for %s at %s", img, dmDevice);
1070 }
1071 }
1072 cleanupDm = true;
1073 } else {
1074 strcpy(dmDevice, loopDevice);
1075 }
1076
1077 if (mkdir(mountPoint, 0755)) {
1078 if (errno != EEXIST) {
1079 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1080 if (cleanupDm) {
1081 Devmapper::destroy(idHash);
1082 }
1083 Loop::destroyByDevice(loopDevice);
1084 return -1;
1085 }
1086 }
1087
Kenny Roota3e06082010-08-27 08:31:35 -07001088 if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001089 0227, false)) {
1090 SLOGE("Image mount failed (%s)", strerror(errno));
1091 if (cleanupDm) {
1092 Devmapper::destroy(idHash);
1093 }
1094 Loop::destroyByDevice(loopDevice);
1095 return -1;
1096 }
1097
Kenny Rootcbacf782010-09-24 15:11:48 -07001098 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001099 if (mDebug) {
1100 SLOGD("Image %s mounted", img);
1101 }
1102 return 0;
1103}
1104
San Mehat49e2bce2009-10-12 16:29:01 -07001105int VolumeManager::mountVolume(const char *label) {
1106 Volume *v = lookupVolume(label);
1107
1108 if (!v) {
1109 errno = ENOENT;
1110 return -1;
1111 }
1112
San Mehata2677e42009-12-13 10:40:18 -08001113 return v->mountVol();
1114}
1115
Kenny Root508c0e12010-07-12 09:59:49 -07001116int VolumeManager::listMountedObbs(SocketClient* cli) {
1117 char device[256];
1118 char mount_path[256];
1119 char rest[256];
1120 FILE *fp;
1121 char line[1024];
1122
1123 if (!(fp = fopen("/proc/mounts", "r"))) {
1124 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1125 return -1;
1126 }
1127
1128 // Create a string to compare against that has a trailing slash
Kenny Rooteacf7e02012-08-09 11:28:37 -07001129 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001130 char loopDir[loopDirLen + 2];
1131 strcpy(loopDir, Volume::LOOPDIR);
1132 loopDir[loopDirLen++] = '/';
1133 loopDir[loopDirLen] = '\0';
1134
1135 while(fgets(line, sizeof(line), fp)) {
1136 line[strlen(line)-1] = '\0';
1137
1138 /*
1139 * Should look like:
1140 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1141 */
1142 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1143
1144 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1145 int fd = open(device, O_RDONLY);
1146 if (fd >= 0) {
1147 struct loop_info64 li;
1148 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1149 cli->sendMsg(ResponseCode::AsecListResult,
1150 (const char*) li.lo_file_name, false);
1151 }
1152 close(fd);
1153 }
1154 }
1155 }
1156
1157 fclose(fp);
1158 return 0;
1159}
1160
San Mehateba65e92010-01-29 05:15:16 -08001161int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1162 Volume *v = lookupVolume(label);
1163
1164 if (!v) {
1165 errno = ENOENT;
1166 return -1;
1167 }
1168
1169 if (strcmp(method, "ums")) {
1170 errno = ENOSYS;
1171 return -1;
1172 }
1173
1174 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001175 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001176 } else {
1177 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001178 }
1179 return 0;
1180}
1181
San Mehata2677e42009-12-13 10:40:18 -08001182int VolumeManager::shareVolume(const char *label, const char *method) {
1183 Volume *v = lookupVolume(label);
1184
1185 if (!v) {
1186 errno = ENOENT;
1187 return -1;
1188 }
1189
1190 /*
1191 * Eventually, we'll want to support additional share back-ends,
1192 * some of which may work while the media is mounted. For now,
1193 * we just support UMS
1194 */
1195 if (strcmp(method, "ums")) {
1196 errno = ENOSYS;
1197 return -1;
1198 }
1199
1200 if (v->getState() == Volume::State_NoMedia) {
1201 errno = ENODEV;
1202 return -1;
1203 }
1204
San Mehat49e2bce2009-10-12 16:29:01 -07001205 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001206 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001207 errno = EBUSY;
1208 return -1;
1209 }
1210
Ken Sumrall3b170052011-07-11 15:38:57 -07001211 if (mVolManagerDisabled) {
1212 errno = EBUSY;
1213 return -1;
1214 }
1215
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001216 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001217 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1218 // This volume does not support raw disk access
1219 errno = EINVAL;
1220 return -1;
1221 }
1222
1223 int fd;
1224 char nodepath[255];
1225 snprintf(nodepath,
1226 sizeof(nodepath), "/dev/block/vold/%d:%d",
1227 MAJOR(d), MINOR(d));
1228
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001229 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001230 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001231 return -1;
1232 }
1233
1234 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001235 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001236 close(fd);
1237 return -1;
1238 }
1239
1240 close(fd);
1241 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001242 if (mUmsSharingCount++ == 0) {
1243 FILE* fp;
1244 mSavedDirtyRatio = -1; // in case we fail
1245 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1246 char line[16];
1247 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1248 fprintf(fp, "%d\n", mUmsDirtyRatio);
1249 } else {
1250 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1251 }
1252 fclose(fp);
1253 } else {
1254 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1255 }
1256 }
San Mehata2677e42009-12-13 10:40:18 -08001257 return 0;
1258}
1259
1260int VolumeManager::unshareVolume(const char *label, const char *method) {
1261 Volume *v = lookupVolume(label);
1262
1263 if (!v) {
1264 errno = ENOENT;
1265 return -1;
1266 }
1267
1268 if (strcmp(method, "ums")) {
1269 errno = ENOSYS;
1270 return -1;
1271 }
1272
1273 if (v->getState() != Volume::State_Shared) {
1274 errno = EINVAL;
1275 return -1;
1276 }
1277
San Mehata2677e42009-12-13 10:40:18 -08001278 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001279 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001280 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001281 return -1;
1282 }
1283
1284 char ch = 0;
1285 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001286 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001287 close(fd);
1288 return -1;
1289 }
1290
1291 close(fd);
1292 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001293 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1294 FILE* fp;
1295 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1296 fprintf(fp, "%d\n", mSavedDirtyRatio);
1297 fclose(fp);
1298 } else {
1299 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1300 }
1301 mSavedDirtyRatio = -1;
1302 }
San Mehata2677e42009-12-13 10:40:18 -08001303 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001304}
1305
Ken Sumrall3b170052011-07-11 15:38:57 -07001306extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001307 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001308 vm->disableVolumeManager();
1309 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001310 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001311}
1312
1313extern "C" int vold_getNumDirectVolumes(void) {
1314 VolumeManager *vm = VolumeManager::Instance();
1315 return vm->getNumDirectVolumes();
1316}
1317
1318int VolumeManager::getNumDirectVolumes(void) {
1319 VolumeCollection::iterator i;
1320 int n=0;
1321
1322 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1323 if ((*i)->getShareDevice() != (dev_t)0) {
1324 n++;
1325 }
1326 }
1327 return n;
1328}
1329
1330extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1331 VolumeManager *vm = VolumeManager::Instance();
1332 return vm->getDirectVolumeList(vol_list);
1333}
1334
1335int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1336 VolumeCollection::iterator i;
1337 int n=0;
1338 dev_t d;
1339
1340 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1341 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1342 (*i)->getVolInfo(&vol_list[n]);
1343 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
1344 "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d));
1345 n++;
1346 }
1347 }
1348
1349 return 0;
1350}
1351
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001352int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001353 Volume *v = lookupVolume(label);
1354
1355 if (!v) {
1356 errno = ENOENT;
1357 return -1;
1358 }
1359
San Mehata2677e42009-12-13 10:40:18 -08001360 if (v->getState() == Volume::State_NoMedia) {
1361 errno = ENODEV;
1362 return -1;
1363 }
1364
San Mehat49e2bce2009-10-12 16:29:01 -07001365 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001366 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001367 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001368 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001369 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001370 }
1371
San Mehat1a06eda2010-04-15 12:58:50 -07001372 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001373
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001374 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001375}
1376
Ken Sumrall425524d2012-06-14 20:55:28 -07001377extern "C" int vold_unmountAllAsecs(void) {
1378 int rc;
1379
1380 VolumeManager *vm = VolumeManager::Instance();
1381 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1382 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1383 rc = -1;
1384 }
1385 return rc;
1386}
1387
1388#define ID_BUF_LEN 256
1389#define ASEC_SUFFIX ".asec"
1390#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1391int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1392 DIR *d = opendir(directory);
1393 int rc = 0;
1394
1395 if (!d) {
1396 SLOGE("Could not open asec dir %s", directory);
1397 return -1;
1398 }
1399
1400 size_t dirent_len = offsetof(struct dirent, d_name) +
1401 pathconf(directory, _PC_NAME_MAX) + 1;
1402
1403 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1404 if (dent == NULL) {
1405 SLOGE("Failed to allocate memory for asec dir");
1406 return -1;
1407 }
1408
1409 struct dirent *result;
1410 while (!readdir_r(d, dent, &result) && result != NULL) {
1411 if (dent->d_name[0] == '.')
1412 continue;
1413 if (dent->d_type != DT_REG)
1414 continue;
1415 size_t name_len = strlen(dent->d_name);
1416 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1417 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1418 char id[ID_BUF_LEN];
1419 strlcpy(id, dent->d_name, name_len - 4);
1420 if (unmountAsec(id, true)) {
1421 /* Register the error, but try to unmount more asecs */
1422 rc = -1;
1423 }
1424 }
1425 }
1426 closedir(d);
1427
1428 free(dent);
1429
1430 return rc;
1431}
1432
San Mehata2677e42009-12-13 10:40:18 -08001433/*
1434 * Looks up a volume by it's label or mount-point
1435 */
San Mehat49e2bce2009-10-12 16:29:01 -07001436Volume *VolumeManager::lookupVolume(const char *label) {
1437 VolumeCollection::iterator i;
1438
1439 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001440 if (label[0] == '/') {
1441 if (!strcmp(label, (*i)->getMountpoint()))
1442 return (*i);
1443 } else {
1444 if (!strcmp(label, (*i)->getLabel()))
1445 return (*i);
1446 }
San Mehat49e2bce2009-10-12 16:29:01 -07001447 }
1448 return NULL;
1449}
San Mehata19b2502010-01-06 10:33:53 -08001450
1451bool VolumeManager::isMountpointMounted(const char *mp)
1452{
1453 char device[256];
1454 char mount_path[256];
1455 char rest[256];
1456 FILE *fp;
1457 char line[1024];
1458
1459 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001460 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001461 return false;
1462 }
1463
1464 while(fgets(line, sizeof(line), fp)) {
1465 line[strlen(line)-1] = '\0';
1466 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1467 if (!strcmp(mount_path, mp)) {
1468 fclose(fp);
1469 return true;
1470 }
San Mehata19b2502010-01-06 10:33:53 -08001471 }
1472
1473 fclose(fp);
1474 return false;
1475}
1476
San Mehat1a06eda2010-04-15 12:58:50 -07001477int VolumeManager::cleanupAsec(Volume *v, bool force) {
Kenny Rooteacf7e02012-08-09 11:28:37 -07001478 int rc = unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1479
1480 AsecIdCollection toUnmount;
1481 // Find the remaining OBB files that are on external storage.
1482 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1483 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001484 ContainerData* cd = *it;
Kenny Rooteacf7e02012-08-09 11:28:37 -07001485
Kenny Rootcbacf782010-09-24 15:11:48 -07001486 if (cd->type == ASEC) {
Kenny Rooteacf7e02012-08-09 11:28:37 -07001487 // nothing
Kenny Rootcbacf782010-09-24 15:11:48 -07001488 } else if (cd->type == OBB) {
Kenny Rooteacf7e02012-08-09 11:28:37 -07001489 if (v == getVolumeForFile(cd->id)) {
1490 toUnmount.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001491 }
1492 } else {
1493 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001494 }
1495 }
Kenny Rooteacf7e02012-08-09 11:28:37 -07001496
1497 for (AsecIdCollection::iterator it = toUnmount.begin(); it != toUnmount.end(); ++it) {
1498 ContainerData *cd = *it;
1499 SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
1500 if (unmountObb(cd->id, force)) {
1501 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1502 rc = -1;
1503 }
1504 }
1505
1506 return rc;
1507
San Mehat1a06eda2010-04-15 12:58:50 -07001508}
1509