blob: 7ce25b044494177c235fc4a3c98c7775d425cac6 [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>
San Mehata19b2502010-01-06 10:33:53 -080022#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/mount.h>
25
San Mehata2677e42009-12-13 10:40:18 -080026#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070027
28#define LOG_TAG "Vold"
29
Kenny Root7b18a7b2010-03-15 13:13:41 -070030#include <openssl/md5.h>
31
San Mehatf1b736b2009-10-10 17:22:08 -070032#include <cutils/log.h>
33
San Mehatfd7f5872009-10-12 11:32:47 -070034#include <sysutils/NetlinkEvent.h>
35
San Mehatf1b736b2009-10-10 17:22:08 -070036#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070037#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080038#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080039#include "Loop.h"
40#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080041#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080042#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080043#include "Asec.h"
San Mehat23969932010-01-09 07:08:06 -080044
San Mehatf1b736b2009-10-10 17:22:08 -070045VolumeManager *VolumeManager::sInstance = NULL;
46
47VolumeManager *VolumeManager::Instance() {
48 if (!sInstance)
49 sInstance = new VolumeManager();
50 return sInstance;
51}
52
53VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080054 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -070055 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -080056 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -070057 mBroadcaster = NULL;
Mike Lockwood99635f62010-06-25 23:04:04 -040058 mUsbMassStorageEnabled = false;
59 mUsbConnected = false;
60
61 readInitialState();
62}
63
64void VolumeManager::readInitialState() {
65 FILE *fp;
66 char state[255];
67
68 /*
69 * Read the initial mass storage enabled state
70 */
71 if ((fp = fopen("/sys/devices/virtual/usb_composite/usb_mass_storage/enable", "r"))) {
72 if (fgets(state, sizeof(state), fp)) {
73 mUsbMassStorageEnabled = !strncmp(state, "1", 1);
74 } else {
75 SLOGE("Failed to read usb_mass_storage enabled state (%s)", strerror(errno));
76 }
77 fclose(fp);
78 } else {
79 SLOGD("USB mass storage support is not enabled in the kernel");
80 }
81
82 /*
83 * Read the initial USB connected state
84 */
85 if ((fp = fopen("/sys/devices/virtual/switch/usb_configuration/state", "r"))) {
86 if (fgets(state, sizeof(state), fp)) {
87 mUsbConnected = !strncmp(state, "1", 1);
88 } else {
89 SLOGE("Failed to read usb_configuration switch (%s)", strerror(errno));
90 }
91 fclose(fp);
92 } else {
93 SLOGD("usb_configuration switch is not enabled in the kernel");
94 }
San Mehatf1b736b2009-10-10 17:22:08 -070095}
96
97VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -080098 delete mVolumes;
99 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700100}
101
Kenny Root7b18a7b2010-03-15 13:13:41 -0700102char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700103 static const char* digits = "0123456789abcdef";
104
Kenny Root7b18a7b2010-03-15 13:13:41 -0700105 unsigned char sig[MD5_DIGEST_LENGTH];
106
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700107 if (buffer == NULL) {
108 SLOGE("Destination buffer is NULL");
109 errno = ESPIPE;
110 return NULL;
111 } else if (id == NULL) {
112 SLOGE("Source buffer is NULL");
113 errno = ESPIPE;
114 return NULL;
115 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
116 SLOGE("Target hash buffer size < %d bytes (%d)",
117 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800118 errno = ESPIPE;
119 return NULL;
120 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700121
122 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800123
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700124 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700125 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700126 *p++ = digits[sig[i] >> 4];
127 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800128 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700129 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800130
131 return buffer;
132}
133
134void VolumeManager::setDebug(bool enable) {
135 mDebug = enable;
136 VolumeCollection::iterator it;
137 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
138 (*it)->setDebug(enable);
139 }
140}
141
San Mehatf1b736b2009-10-10 17:22:08 -0700142int VolumeManager::start() {
143 return 0;
144}
145
146int VolumeManager::stop() {
147 return 0;
148}
149
150int VolumeManager::addVolume(Volume *v) {
151 mVolumes->push_back(v);
152 return 0;
153}
154
Mike Lockwood99635f62010-06-25 23:04:04 -0400155void VolumeManager::notifyUmsAvailable(bool available) {
San Mehata2677e42009-12-13 10:40:18 -0800156 char msg[255];
157
San Mehata2677e42009-12-13 10:40:18 -0800158 snprintf(msg, sizeof(msg), "Share method ums now %s",
Mike Lockwood99635f62010-06-25 23:04:04 -0400159 (available ? "available" : "unavailable"));
160 SLOGD(msg);
San Mehata2677e42009-12-13 10:40:18 -0800161 getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange,
162 msg, false);
163}
164
165void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) {
San Mehat0cde53c2009-12-22 08:32:33 -0800166 const char *devpath = evt->findParam("DEVPATH");
San Mehata2677e42009-12-13 10:40:18 -0800167 const char *name = evt->findParam("SWITCH_NAME");
168 const char *state = evt->findParam("SWITCH_STATE");
169
San Mehat0cde53c2009-12-22 08:32:33 -0800170 if (!name || !state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700171 SLOGW("Switch %s event missing name/state info", devpath);
San Mehat0cde53c2009-12-22 08:32:33 -0800172 return;
173 }
174
Mike Lockwood99635f62010-06-25 23:04:04 -0400175 bool oldAvailable = massStorageAvailable();
176 if (!strcmp(name, "usb_configuration")) {
177 mUsbConnected = !strcmp(state, "1");
178 SLOGD("USB %s", mUsbConnected ? "connected" : "disconnected");
179 bool newAvailable = massStorageAvailable();
180 if (newAvailable != oldAvailable) {
181 notifyUmsAvailable(newAvailable);
San Mehata2677e42009-12-13 10:40:18 -0800182 }
183 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700184 SLOGW("Ignoring unknown switch '%s'", name);
San Mehata2677e42009-12-13 10:40:18 -0800185 }
186}
Mike Lockwood99635f62010-06-25 23:04:04 -0400187void VolumeManager::handleUsbCompositeEvent(NetlinkEvent *evt) {
188 const char *function = evt->findParam("FUNCTION");
189 const char *enabled = evt->findParam("ENABLED");
190
191 if (!function || !enabled) {
192 SLOGW("usb_composite event missing function/enabled info");
193 return;
194 }
195
196 if (!strcmp(function, "usb_mass_storage")) {
197 bool oldAvailable = massStorageAvailable();
198 mUsbMassStorageEnabled = !strcmp(enabled, "1");
199 SLOGD("usb_mass_storage function %s", mUsbMassStorageEnabled ? "enabled" : "disabled");
200 bool newAvailable = massStorageAvailable();
201 if (newAvailable != oldAvailable) {
202 notifyUmsAvailable(newAvailable);
203 }
204 }
205}
San Mehata2677e42009-12-13 10:40:18 -0800206
San Mehatfd7f5872009-10-12 11:32:47 -0700207void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
208 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700209
San Mehatfd7f5872009-10-12 11:32:47 -0700210 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700211 VolumeCollection::iterator it;
212 bool hit = false;
213 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700214 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800215#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700216 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800217#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700218 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700219 break;
220 }
221 }
222
223 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800224#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700225 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800226#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700227 }
228}
229
San Mehatf1b736b2009-10-10 17:22:08 -0700230int VolumeManager::listVolumes(SocketClient *cli) {
231 VolumeCollection::iterator i;
232
233 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
234 char *buffer;
235 asprintf(&buffer, "%s %s %d",
236 (*i)->getLabel(), (*i)->getMountpoint(),
237 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800238 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700239 free(buffer);
240 }
San Mehata2677e42009-12-13 10:40:18 -0800241 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700242 return 0;
243}
San Mehat49e2bce2009-10-12 16:29:01 -0700244
San Mehata2677e42009-12-13 10:40:18 -0800245int VolumeManager::formatVolume(const char *label) {
246 Volume *v = lookupVolume(label);
247
248 if (!v) {
249 errno = ENOENT;
250 return -1;
251 }
252
253 return v->formatVol();
254}
255
San Mehata19b2502010-01-06 10:33:53 -0800256int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700257 char asecFileName[255];
258 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
259
260 memset(buffer, 0, maxlen);
261 if (access(asecFileName, F_OK)) {
262 errno = ENOENT;
263 return -1;
264 }
San Mehata19b2502010-01-06 10:33:53 -0800265
San Mehat3bb60202010-02-19 18:14:36 -0800266 snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800267 return 0;
268}
269
San Mehat8b8f71b2010-01-11 09:17:25 -0800270int VolumeManager::createAsec(const char *id, unsigned int numSectors,
San Mehata19b2502010-01-06 10:33:53 -0800271 const char *fstype, const char *key, int ownerUid) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800272 struct asec_superblock sb;
273 memset(&sb, 0, sizeof(sb));
274
275 sb.magic = ASEC_SB_MAGIC;
276 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800277
San Mehatd31e3802010-02-18 08:37:45 -0800278 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700279 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800280 errno = EINVAL;
281 return -1;
282 }
283
San Mehata19b2502010-01-06 10:33:53 -0800284 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700285 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800286 errno = EADDRINUSE;
287 return -1;
288 }
289
290 char asecFileName[255];
San Mehat3bb60202010-02-19 18:14:36 -0800291 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800292
293 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700294 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
San Mehata19b2502010-01-06 10:33:53 -0800295 asecFileName, strerror(errno));
296 errno = EADDRINUSE;
297 return -1;
298 }
299
San Mehatfcf24fe2010-03-03 12:37:32 -0800300 /*
301 * Add some headroom
302 */
303 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
304 unsigned numImgSectors = numSectors + fatSize + 2;
305
306 if (numImgSectors % 63) {
307 numImgSectors += (63 - (numImgSectors % 63));
308 }
309
310 // Add +1 for our superblock which is at the end
311 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700312 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800313 return -1;
314 }
315
San Mehatd9a4e352010-03-12 13:32:47 -0800316 char idHash[33];
317 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700318 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800319 unlink(asecFileName);
320 return -1;
321 }
322
San Mehata19b2502010-01-06 10:33:53 -0800323 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800324 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700325 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800326 unlink(asecFileName);
327 return -1;
328 }
329
San Mehatb78a32c2010-01-10 13:02:12 -0800330 char dmDevice[255];
331 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800332
San Mehatb78a32c2010-01-10 13:02:12 -0800333 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800334 // XXX: This is all we support for now
335 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800336 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800337 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700338 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800339 Loop::destroyByDevice(loopDevice);
340 unlink(asecFileName);
341 return -1;
342 }
343 cleanupDm = true;
344 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800345 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800346 strcpy(dmDevice, loopDevice);
347 }
348
San Mehatfcf24fe2010-03-03 12:37:32 -0800349 /*
350 * Drop down the superblock at the end of the file
351 */
352
353 int sbfd = open(loopDevice, O_RDWR);
354 if (sbfd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700355 SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800356 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800357 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800358 }
359 Loop::destroyByDevice(loopDevice);
360 unlink(asecFileName);
361 return -1;
362 }
363
364 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
365 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700366 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800367 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800368 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800369 }
370 Loop::destroyByDevice(loopDevice);
371 unlink(asecFileName);
372 return -1;
373 }
374
375 if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
376 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700377 SLOGE("Failed to write superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800378 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800379 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800380 }
381 Loop::destroyByDevice(loopDevice);
382 unlink(asecFileName);
383 return -1;
384 }
385 close(sbfd);
386
San Mehata1091cb2010-02-28 20:17:20 -0800387 if (strcmp(fstype, "none")) {
388 if (strcmp(fstype, "fat")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700389 SLOGW("Unknown fstype '%s' specified for container", fstype);
San Mehatb78a32c2010-01-10 13:02:12 -0800390 }
San Mehata19b2502010-01-06 10:33:53 -0800391
San Mehatfcf24fe2010-03-03 12:37:32 -0800392 if (Fat::format(dmDevice, numImgSectors)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700393 SLOGE("ASEC FAT 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 }
San Mehata1091cb2010-02-28 20:17:20 -0800401 char mountPoint[255];
San Mehata19b2502010-01-06 10:33:53 -0800402
San Mehata1091cb2010-02-28 20:17:20 -0800403 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
404 if (mkdir(mountPoint, 0777)) {
405 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700406 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800407 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800408 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800409 }
410 Loop::destroyByDevice(loopDevice);
411 unlink(asecFileName);
412 return -1;
413 }
San Mehatb78a32c2010-01-10 13:02:12 -0800414 }
San Mehata1091cb2010-02-28 20:17:20 -0800415
416 if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid,
417 0, 0000, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700418 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800419 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800420 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800421 }
422 Loop::destroyByDevice(loopDevice);
423 unlink(asecFileName);
424 return -1;
425 }
426 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700427 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800428 }
San Mehat88705162010-01-15 09:26:28 -0800429
430 mActiveContainers->push_back(strdup(id));
San Mehata19b2502010-01-06 10:33:53 -0800431 return 0;
432}
433
434int VolumeManager::finalizeAsec(const char *id) {
435 char asecFileName[255];
436 char loopDevice[255];
437 char mountPoint[255];
438
San Mehat3bb60202010-02-19 18:14:36 -0800439 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800440
San Mehatd9a4e352010-03-12 13:32:47 -0800441 char idHash[33];
442 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700443 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800444 return -1;
445 }
446
447 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700448 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800449 return -1;
450 }
451
San Mehat3bb60202010-02-19 18:14:36 -0800452 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatfff0b472010-01-06 19:19:46 -0800453 // XXX:
454 if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700455 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800456 return -1;
457 }
458
San Mehatd9a4e352010-03-12 13:32:47 -0800459 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700460 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800461 }
San Mehata19b2502010-01-06 10:33:53 -0800462 return 0;
463}
464
San Mehat048b0802010-01-23 08:17:06 -0800465int VolumeManager::renameAsec(const char *id1, const char *id2) {
466 char *asecFilename1;
467 char *asecFilename2;
468 char mountPoint[255];
469
San Mehat3bb60202010-02-19 18:14:36 -0800470 asprintf(&asecFilename1, "%s/%s.asec", Volume::SEC_ASECDIR, id1);
471 asprintf(&asecFilename2, "%s/%s.asec", Volume::SEC_ASECDIR, id2);
San Mehat048b0802010-01-23 08:17:06 -0800472
San Mehat3bb60202010-02-19 18:14:36 -0800473 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
San Mehat048b0802010-01-23 08:17:06 -0800474 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700475 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800476 errno = EBUSY;
477 goto out_err;
478 }
479
San Mehat96956ed2010-02-24 08:42:51 -0800480 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
481 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700482 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800483 errno = EBUSY;
484 goto out_err;
485 }
486
San Mehat048b0802010-01-23 08:17:06 -0800487 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700488 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800489 errno = EADDRINUSE;
490 goto out_err;
491 }
492
493 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700494 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800495 goto out_err;
496 }
497
498 free(asecFilename1);
499 free(asecFilename2);
500 return 0;
501
502out_err:
503 free(asecFilename1);
504 free(asecFilename2);
505 return -1;
506}
507
San Mehat4ba89482010-02-18 09:00:18 -0800508#define ASEC_UNMOUNT_RETRIES 5
509int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800510 char asecFileName[255];
511 char mountPoint[255];
512
San Mehat3bb60202010-02-19 18:14:36 -0800513 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
514 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800515
San Mehatd9a4e352010-03-12 13:32:47 -0800516 char idHash[33];
517 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700518 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800519 return -1;
520 }
521
San Mehat0586d542010-01-12 15:38:59 -0800522 if (!isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700523 SLOGE("Unmount request for ASEC %s when not mounted", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800524 errno = EINVAL;
525 return -1;
526 }
San Mehat23969932010-01-09 07:08:06 -0800527
San Mehatb78a32c2010-01-10 13:02:12 -0800528 int i, rc;
San Mehat8c940ef2010-02-13 14:19:53 -0800529 for (i = 1; i <= ASEC_UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -0800530 rc = umount(mountPoint);
531 if (!rc) {
532 break;
San Mehata19b2502010-01-06 10:33:53 -0800533 }
San Mehatb78a32c2010-01-10 13:02:12 -0800534 if (rc && (errno == EINVAL || errno == ENOENT)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700535 SLOGI("Secure container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800536 rc = 0;
537 break;
San Mehata19b2502010-01-06 10:33:53 -0800538 }
San Mehat97ac40e2010-03-24 10:24:19 -0700539 SLOGW("ASEC %s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -0800540 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800541
San Mehat4ba89482010-02-18 09:00:18 -0800542 int action = 0; // default is to just complain
543
544 if (force) {
545 if (i > (ASEC_UNMOUNT_RETRIES - 2))
546 action = 2; // SIGKILL
547 else if (i > (ASEC_UNMOUNT_RETRIES - 3))
548 action = 1; // SIGHUP
549 }
San Mehat8c940ef2010-02-13 14:19:53 -0800550
San Mehat586536c2010-02-16 17:12:00 -0800551 Process::killProcessesWithOpenFiles(mountPoint, action);
San Mehat8c940ef2010-02-13 14:19:53 -0800552 usleep(1000 * 1000);
San Mehatb78a32c2010-01-10 13:02:12 -0800553 }
554
555 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -0800556 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700557 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800558 return -1;
559 }
560
San Mehat12f4b892010-02-24 11:43:22 -0800561 int retries = 10;
562
563 while(retries--) {
564 if (!rmdir(mountPoint)) {
565 break;
566 }
567
San Mehat97ac40e2010-03-24 10:24:19 -0700568 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehat12f4b892010-02-24 11:43:22 -0800569 usleep(1000 * 1000);
570 }
571
572 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -0700573 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -0800574 }
San Mehat88705162010-01-15 09:26:28 -0800575
San Mehatd9a4e352010-03-12 13:32:47 -0800576 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700577 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800578 }
579
580 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800581 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800582 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800583 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700584 SLOGW("Failed to find loop device for {%s} (%s)", asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800585 }
San Mehat88705162010-01-15 09:26:28 -0800586
587 AsecIdCollection::iterator it;
588 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
589 if (!strcmp(*it, id)) {
590 free(*it);
591 mActiveContainers->erase(it);
592 break;
593 }
594 }
595 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700596 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -0800597 }
San Mehatb78a32c2010-01-10 13:02:12 -0800598 return 0;
599}
600
San Mehat4ba89482010-02-18 09:00:18 -0800601int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -0800602 char asecFileName[255];
603 char mountPoint[255];
604
San Mehat3bb60202010-02-19 18:14:36 -0800605 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehat55013f72010-02-24 12:12:34 -0800606 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatb78a32c2010-01-10 13:02:12 -0800607
San Mehat0586d542010-01-12 15:38:59 -0800608 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800609 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700610 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -0800611 }
San Mehat4ba89482010-02-18 09:00:18 -0800612 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700613 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800614 return -1;
615 }
616 }
San Mehata19b2502010-01-06 10:33:53 -0800617
San Mehat0586d542010-01-12 15:38:59 -0800618 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700619 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800620 return -1;
621 }
San Mehata19b2502010-01-06 10:33:53 -0800622
San Mehatd9a4e352010-03-12 13:32:47 -0800623 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700624 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800625 }
San Mehata19b2502010-01-06 10:33:53 -0800626 return 0;
627}
628
629int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
630 char asecFileName[255];
631 char mountPoint[255];
632
San Mehat3bb60202010-02-19 18:14:36 -0800633 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
634 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800635
636 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700637 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -0800638 errno = EBUSY;
639 return -1;
640 }
641
San Mehatd9a4e352010-03-12 13:32:47 -0800642 char idHash[33];
643 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700644 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800645 return -1;
646 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700647
San Mehata19b2502010-01-06 10:33:53 -0800648 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800649 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
650 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700651 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800652 return -1;
653 }
San Mehatd9a4e352010-03-12 13:32:47 -0800654 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700655 SLOGD("New loop device created at %s", loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800656 }
San Mehatb78a32c2010-01-10 13:02:12 -0800657 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800658 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700659 SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800660 }
San Mehatb78a32c2010-01-10 13:02:12 -0800661 }
662
663 char dmDevice[255];
664 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -0800665 int fd;
666 unsigned int nr_sec = 0;
667
668 if ((fd = open(loopDevice, O_RDWR)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700669 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800670 Loop::destroyByDevice(loopDevice);
671 return -1;
672 }
673
674 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700675 SLOGE("Failed to get loop size (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800676 Loop::destroyByDevice(loopDevice);
677 close(fd);
678 return -1;
679 }
680
681 /*
682 * Validate superblock
683 */
684 struct asec_superblock sb;
685 memset(&sb, 0, sizeof(sb));
686 if (lseek(fd, ((nr_sec-1) * 512), SEEK_SET) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700687 SLOGE("lseek failed (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800688 close(fd);
689 Loop::destroyByDevice(loopDevice);
690 return -1;
691 }
692 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700693 SLOGE("superblock read failed (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800694 close(fd);
695 Loop::destroyByDevice(loopDevice);
696 return -1;
697 }
698
699 close(fd);
700
San Mehatd9a4e352010-03-12 13:32:47 -0800701 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700702 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -0800703 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800704 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -0700705 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -0800706 Loop::destroyByDevice(loopDevice);
707 errno = EMEDIUMTYPE;
708 return -1;
709 }
710 nr_sec--; // We don't want the devmapping to extend onto our superblock
711
San Mehatb78a32c2010-01-10 13:02:12 -0800712 if (strcmp(key, "none")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800713 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
714 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -0800715 dmDevice, sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700716 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800717 Loop::destroyByDevice(loopDevice);
718 return -1;
719 }
San Mehatd9a4e352010-03-12 13:32:47 -0800720 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700721 SLOGD("New devmapper instance created at %s", dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800722 }
San Mehatb78a32c2010-01-10 13:02:12 -0800723 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800724 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700725 SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800726 }
San Mehatb78a32c2010-01-10 13:02:12 -0800727 }
728 cleanupDm = true;
729 } else {
730 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800731 }
732
733 if (mkdir(mountPoint, 0777)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800734 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700735 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800736 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800737 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800738 }
739 Loop::destroyByDevice(loopDevice);
740 return -1;
741 }
San Mehata19b2502010-01-06 10:33:53 -0800742 }
743
San Mehatb78a32c2010-01-10 13:02:12 -0800744 if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0,
San Mehatcff5ec32010-01-08 12:31:44 -0800745 0222, false)) {
746// 0227, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700747 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800748 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800749 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800750 }
751 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800752 return -1;
753 }
754
San Mehat88705162010-01-15 09:26:28 -0800755 mActiveContainers->push_back(strdup(id));
San Mehatd9a4e352010-03-12 13:32:47 -0800756 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700757 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800758 }
San Mehata19b2502010-01-06 10:33:53 -0800759 return 0;
760}
761
San Mehat49e2bce2009-10-12 16:29:01 -0700762int VolumeManager::mountVolume(const char *label) {
763 Volume *v = lookupVolume(label);
764
765 if (!v) {
766 errno = ENOENT;
767 return -1;
768 }
769
San Mehata2677e42009-12-13 10:40:18 -0800770 return v->mountVol();
771}
772
773int VolumeManager::shareAvailable(const char *method, bool *avail) {
774
775 if (strcmp(method, "ums")) {
776 errno = ENOSYS;
777 return -1;
778 }
779
Mike Lockwood99635f62010-06-25 23:04:04 -0400780 *avail = massStorageAvailable();
San Mehata2677e42009-12-13 10:40:18 -0800781 return 0;
782}
783
San Mehateba65e92010-01-29 05:15:16 -0800784int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
785 Volume *v = lookupVolume(label);
786
787 if (!v) {
788 errno = ENOENT;
789 return -1;
790 }
791
792 if (strcmp(method, "ums")) {
793 errno = ENOSYS;
794 return -1;
795 }
796
797 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -0800798 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -0800799 } else {
800 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -0800801 }
802 return 0;
803}
804
San Mehata2677e42009-12-13 10:40:18 -0800805int VolumeManager::simulate(const char *cmd, const char *arg) {
806
807 if (!strcmp(cmd, "ums")) {
808 if (!strcmp(arg, "connect")) {
Mike Lockwood99635f62010-06-25 23:04:04 -0400809 notifyUmsAvailable(true);
San Mehata2677e42009-12-13 10:40:18 -0800810 } else if (!strcmp(arg, "disconnect")) {
Mike Lockwood99635f62010-06-25 23:04:04 -0400811 notifyUmsAvailable(false);
San Mehata2677e42009-12-13 10:40:18 -0800812 } else {
813 errno = EINVAL;
814 return -1;
815 }
816 } else {
817 errno = EINVAL;
818 return -1;
819 }
820 return 0;
821}
822
823int VolumeManager::shareVolume(const char *label, const char *method) {
824 Volume *v = lookupVolume(label);
825
826 if (!v) {
827 errno = ENOENT;
828 return -1;
829 }
830
831 /*
832 * Eventually, we'll want to support additional share back-ends,
833 * some of which may work while the media is mounted. For now,
834 * we just support UMS
835 */
836 if (strcmp(method, "ums")) {
837 errno = ENOSYS;
838 return -1;
839 }
840
841 if (v->getState() == Volume::State_NoMedia) {
842 errno = ENODEV;
843 return -1;
844 }
845
San Mehat49e2bce2009-10-12 16:29:01 -0700846 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -0800847 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -0700848 errno = EBUSY;
849 return -1;
850 }
851
San Mehata2677e42009-12-13 10:40:18 -0800852 dev_t d = v->getDiskDevice();
853 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
854 // This volume does not support raw disk access
855 errno = EINVAL;
856 return -1;
857 }
858
859 int fd;
860 char nodepath[255];
861 snprintf(nodepath,
862 sizeof(nodepath), "/dev/block/vold/%d:%d",
863 MAJOR(d), MINOR(d));
864
San Mehat0cde53c2009-12-22 08:32:33 -0800865 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file",
866 O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700867 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800868 return -1;
869 }
870
871 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700872 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800873 close(fd);
874 return -1;
875 }
876
877 close(fd);
878 v->handleVolumeShared();
879 return 0;
880}
881
882int VolumeManager::unshareVolume(const char *label, const char *method) {
883 Volume *v = lookupVolume(label);
884
885 if (!v) {
886 errno = ENOENT;
887 return -1;
888 }
889
890 if (strcmp(method, "ums")) {
891 errno = ENOSYS;
892 return -1;
893 }
894
895 if (v->getState() != Volume::State_Shared) {
896 errno = EINVAL;
897 return -1;
898 }
899
900 dev_t d = v->getDiskDevice();
901
902 int fd;
903 char nodepath[255];
904 snprintf(nodepath,
905 sizeof(nodepath), "/dev/block/vold/%d:%d",
906 MAJOR(d), MINOR(d));
907
San Mehat0cde53c2009-12-22 08:32:33 -0800908 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700909 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800910 return -1;
911 }
912
913 char ch = 0;
914 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700915 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800916 close(fd);
917 return -1;
918 }
919
920 close(fd);
921 v->handleVolumeUnshared();
922 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700923}
924
San Mehat4ba89482010-02-18 09:00:18 -0800925int VolumeManager::unmountVolume(const char *label, bool force) {
San Mehat49e2bce2009-10-12 16:29:01 -0700926 Volume *v = lookupVolume(label);
927
928 if (!v) {
929 errno = ENOENT;
930 return -1;
931 }
932
San Mehata2677e42009-12-13 10:40:18 -0800933 if (v->getState() == Volume::State_NoMedia) {
934 errno = ENODEV;
935 return -1;
936 }
937
San Mehat49e2bce2009-10-12 16:29:01 -0700938 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700939 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -0800940 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -0700941 errno = EBUSY;
942 return -1;
943 }
944
San Mehat1a06eda2010-04-15 12:58:50 -0700945 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -0800946
San Mehat4ba89482010-02-18 09:00:18 -0800947 return v->unmountVol(force);
San Mehat49e2bce2009-10-12 16:29:01 -0700948}
949
San Mehata2677e42009-12-13 10:40:18 -0800950/*
951 * Looks up a volume by it's label or mount-point
952 */
San Mehat49e2bce2009-10-12 16:29:01 -0700953Volume *VolumeManager::lookupVolume(const char *label) {
954 VolumeCollection::iterator i;
955
956 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -0800957 if (label[0] == '/') {
958 if (!strcmp(label, (*i)->getMountpoint()))
959 return (*i);
960 } else {
961 if (!strcmp(label, (*i)->getLabel()))
962 return (*i);
963 }
San Mehat49e2bce2009-10-12 16:29:01 -0700964 }
965 return NULL;
966}
San Mehata19b2502010-01-06 10:33:53 -0800967
968bool VolumeManager::isMountpointMounted(const char *mp)
969{
970 char device[256];
971 char mount_path[256];
972 char rest[256];
973 FILE *fp;
974 char line[1024];
975
976 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700977 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800978 return false;
979 }
980
981 while(fgets(line, sizeof(line), fp)) {
982 line[strlen(line)-1] = '\0';
983 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
984 if (!strcmp(mount_path, mp)) {
985 fclose(fp);
986 return true;
987 }
San Mehata19b2502010-01-06 10:33:53 -0800988 }
989
990 fclose(fp);
991 return false;
992}
993
San Mehat1a06eda2010-04-15 12:58:50 -0700994int VolumeManager::cleanupAsec(Volume *v, bool force) {
995 while(mActiveContainers->size()) {
996 AsecIdCollection::iterator it = mActiveContainers->begin();
997 SLOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint());
998 if (unmountAsec(*it, force)) {
999 SLOGE("Failed to unmount ASEC %s (%s)", *it, strerror(errno));
1000 return -1;
1001 }
1002 }
1003 return 0;
1004}
1005