blob: c8b4505c3efb708c74d19523567247c92bedf582 [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
30#include <cutils/log.h>
31
San Mehatfd7f5872009-10-12 11:32:47 -070032#include <sysutils/NetlinkEvent.h>
33
San Mehatf1b736b2009-10-10 17:22:08 -070034#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070035#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080036#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080037#include "Loop.h"
38#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080039#include "Devmapper.h"
San Mehatf1b736b2009-10-10 17:22:08 -070040
San Mehat23969932010-01-09 07:08:06 -080041extern "C" void KillProcessesWithOpenFiles(const char *, int, int, int);
42
San Mehatf1b736b2009-10-10 17:22:08 -070043VolumeManager *VolumeManager::sInstance = NULL;
44
45VolumeManager *VolumeManager::Instance() {
46 if (!sInstance)
47 sInstance = new VolumeManager();
48 return sInstance;
49}
50
51VolumeManager::VolumeManager() {
52 mBlockDevices = new BlockDeviceCollection();
53 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -080054 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -070055 mBroadcaster = NULL;
San Mehata2677e42009-12-13 10:40:18 -080056 mUsbMassStorageConnected = false;
San Mehatf1b736b2009-10-10 17:22:08 -070057}
58
59VolumeManager::~VolumeManager() {
60 delete mBlockDevices;
San Mehat88705162010-01-15 09:26:28 -080061 delete mVolumes;
62 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -070063}
64
65int VolumeManager::start() {
66 return 0;
67}
68
69int VolumeManager::stop() {
70 return 0;
71}
72
73int VolumeManager::addVolume(Volume *v) {
74 mVolumes->push_back(v);
75 return 0;
76}
77
San Mehata2677e42009-12-13 10:40:18 -080078void VolumeManager::notifyUmsConnected(bool connected) {
79 char msg[255];
80
81 if (connected) {
82 mUsbMassStorageConnected = true;
83 } else {
84 mUsbMassStorageConnected = false;
85 }
86 snprintf(msg, sizeof(msg), "Share method ums now %s",
87 (connected ? "available" : "unavailable"));
88
89 getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange,
90 msg, false);
91}
92
93void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) {
San Mehat0cde53c2009-12-22 08:32:33 -080094 const char *devpath = evt->findParam("DEVPATH");
San Mehata2677e42009-12-13 10:40:18 -080095 const char *name = evt->findParam("SWITCH_NAME");
96 const char *state = evt->findParam("SWITCH_STATE");
97
San Mehat0cde53c2009-12-22 08:32:33 -080098 if (!name || !state) {
99 LOGW("Switch %s event missing name/state info", devpath);
100 return;
101 }
102
San Mehata2677e42009-12-13 10:40:18 -0800103 if (!strcmp(name, "usb_mass_storage")) {
104
105 if (!strcmp(state, "online")) {
106 notifyUmsConnected(true);
107 } else {
108 notifyUmsConnected(false);
109 }
110 } else {
111 LOGW("Ignoring unknown switch '%s'", name);
112 }
113}
114
San Mehatfd7f5872009-10-12 11:32:47 -0700115void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
116 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700117
San Mehatfd7f5872009-10-12 11:32:47 -0700118 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700119 VolumeCollection::iterator it;
120 bool hit = false;
121 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700122 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800123#ifdef NETLINK_DEBUG
124 LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
125#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700126 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700127 break;
128 }
129 }
130
131 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800132#ifdef NETLINK_DEBUG
San Mehatfd7f5872009-10-12 11:32:47 -0700133 LOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800134#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700135 }
136}
137
San Mehatf1b736b2009-10-10 17:22:08 -0700138int VolumeManager::listVolumes(SocketClient *cli) {
139 VolumeCollection::iterator i;
140
141 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
142 char *buffer;
143 asprintf(&buffer, "%s %s %d",
144 (*i)->getLabel(), (*i)->getMountpoint(),
145 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800146 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700147 free(buffer);
148 }
San Mehata2677e42009-12-13 10:40:18 -0800149 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700150 return 0;
151}
San Mehat49e2bce2009-10-12 16:29:01 -0700152
San Mehata2677e42009-12-13 10:40:18 -0800153int VolumeManager::formatVolume(const char *label) {
154 Volume *v = lookupVolume(label);
155
156 if (!v) {
157 errno = ENOENT;
158 return -1;
159 }
160
161 return v->formatVol();
162}
163
San Mehata19b2502010-01-06 10:33:53 -0800164int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
165 char mountPoint[255];
166
167 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
San Mehata19b2502010-01-06 10:33:53 -0800168 snprintf(buffer, maxlen, "/asec/%s", id);
169 return 0;
170}
171
San Mehat8b8f71b2010-01-11 09:17:25 -0800172int VolumeManager::createAsec(const char *id, unsigned int numSectors,
San Mehata19b2502010-01-06 10:33:53 -0800173 const char *fstype, const char *key, int ownerUid) {
174
175 mkdir("/sdcard/android_secure", 0777);
176
177 if (lookupVolume(id)) {
178 LOGE("ASEC volume '%s' currently exists", id);
179 errno = EADDRINUSE;
180 return -1;
181 }
182
183 char asecFileName[255];
184 snprintf(asecFileName, sizeof(asecFileName),
185 "/sdcard/android_secure/%s.asec", id);
186
187 if (!access(asecFileName, F_OK)) {
188 LOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
189 asecFileName, strerror(errno));
190 errno = EADDRINUSE;
191 return -1;
192 }
193
San Mehat8b8f71b2010-01-11 09:17:25 -0800194 if (Loop::createImageFile(asecFileName, numSectors)) {
San Mehata19b2502010-01-06 10:33:53 -0800195 LOGE("ASEC image file creation failed (%s)", strerror(errno));
196 return -1;
197 }
198
199 char loopDevice[255];
San Mehat8da6bcb2010-01-09 12:24:05 -0800200 if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800201 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
202 unlink(asecFileName);
203 return -1;
204 }
205
San Mehatb78a32c2010-01-10 13:02:12 -0800206 char dmDevice[255];
207 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800208
San Mehatb78a32c2010-01-10 13:02:12 -0800209 if (strcmp(key, "none")) {
San Mehat8b8f71b2010-01-11 09:17:25 -0800210 if (Devmapper::create(id, loopDevice, key, numSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800211 sizeof(dmDevice))) {
212 LOGE("ASEC device mapping failed (%s)", strerror(errno));
213 Loop::destroyByDevice(loopDevice);
214 unlink(asecFileName);
215 return -1;
216 }
217 cleanupDm = true;
218 } else {
219 strcpy(dmDevice, loopDevice);
220 }
221
222 if (Fat::format(dmDevice)) {
San Mehata19b2502010-01-06 10:33:53 -0800223 LOGE("ASEC FAT format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800224 if (cleanupDm) {
225 Devmapper::destroy(id);
226 }
San Mehata19b2502010-01-06 10:33:53 -0800227 Loop::destroyByDevice(loopDevice);
228 unlink(asecFileName);
229 return -1;
230 }
231
232 char mountPoint[255];
233
234 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
235 if (mkdir(mountPoint, 0777)) {
San Mehateb13a902010-01-07 12:12:50 -0800236 if (errno != EEXIST) {
237 LOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800238 if (cleanupDm) {
239 Devmapper::destroy(id);
240 }
San Mehateb13a902010-01-07 12:12:50 -0800241 Loop::destroyByDevice(loopDevice);
242 unlink(asecFileName);
243 return -1;
244 }
San Mehata19b2502010-01-06 10:33:53 -0800245 }
246
San Mehatb78a32c2010-01-10 13:02:12 -0800247 if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid,
San Mehatcff5ec32010-01-08 12:31:44 -0800248 0, 0000, false)) {
249// 0, 0007, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800250 LOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800251 if (cleanupDm) {
252 Devmapper::destroy(id);
253 }
San Mehata19b2502010-01-06 10:33:53 -0800254 Loop::destroyByDevice(loopDevice);
255 unlink(asecFileName);
256 return -1;
257 }
San Mehat88705162010-01-15 09:26:28 -0800258
259 mActiveContainers->push_back(strdup(id));
San Mehata19b2502010-01-06 10:33:53 -0800260 return 0;
261}
262
263int VolumeManager::finalizeAsec(const char *id) {
264 char asecFileName[255];
265 char loopDevice[255];
266 char mountPoint[255];
267
268 snprintf(asecFileName, sizeof(asecFileName),
269 "/sdcard/android_secure/%s.asec", id);
270
271 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
272 LOGE("Unable to finalize %s (%s)", id, strerror(errno));
273 return -1;
274 }
275
276 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
San Mehatfff0b472010-01-06 19:19:46 -0800277 // XXX:
278 if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800279 LOGE("ASEC finalize mount failed (%s)", strerror(errno));
280 return -1;
281 }
282
283 LOGD("ASEC %s finalized", id);
284 return 0;
285}
286
San Mehatb78a32c2010-01-10 13:02:12 -0800287int VolumeManager::unmountAsec(const char *id) {
San Mehata19b2502010-01-06 10:33:53 -0800288 char asecFileName[255];
289 char mountPoint[255];
290
291 snprintf(asecFileName, sizeof(asecFileName),
292 "/sdcard/android_secure/%s.asec", id);
293 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
294
San Mehat0586d542010-01-12 15:38:59 -0800295 if (!isMountpointMounted(mountPoint)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800296 LOGE("Unmount request for ASEC %s when not mounted", id);
297 errno = EINVAL;
298 return -1;
299 }
San Mehat23969932010-01-09 07:08:06 -0800300
San Mehatb78a32c2010-01-10 13:02:12 -0800301 int i, rc;
302 for (i = 0; i < 10; i++) {
303 rc = umount(mountPoint);
304 if (!rc) {
305 break;
San Mehata19b2502010-01-06 10:33:53 -0800306 }
San Mehatb78a32c2010-01-10 13:02:12 -0800307 if (rc && (errno == EINVAL || errno == ENOENT)) {
308 rc = 0;
309 break;
San Mehata19b2502010-01-06 10:33:53 -0800310 }
San Mehatb78a32c2010-01-10 13:02:12 -0800311 LOGW("ASEC %s unmount attempt %d failed (%s)",
312 id, i +1, strerror(errno));
313
314 if (i >= 5) {
315 KillProcessesWithOpenFiles(mountPoint, (i < 7 ? 0 : 1),
316 NULL, 0);
317 }
318 usleep(1000 * 250);
319 }
320
321 if (rc) {
322 LOGE("Failed to unmount ASEC %s", id);
323 return -1;
324 }
325
San Mehat88705162010-01-15 09:26:28 -0800326 unlink(mountPoint);
327
San Mehatb78a32c2010-01-10 13:02:12 -0800328 if (Devmapper::destroy(id) && errno != ENXIO) {
329 LOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800330 }
331
332 char loopDevice[255];
333 if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
334 Loop::destroyByDevice(loopDevice);
335 }
San Mehat88705162010-01-15 09:26:28 -0800336
337 AsecIdCollection::iterator it;
338 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
339 if (!strcmp(*it, id)) {
340 free(*it);
341 mActiveContainers->erase(it);
342 break;
343 }
344 }
345 if (it == mActiveContainers->end()) {
346 LOGW("mActiveContainers is inconsistent!");
347 }
San Mehatb78a32c2010-01-10 13:02:12 -0800348 return 0;
349}
350
351int VolumeManager::destroyAsec(const char *id) {
352 char asecFileName[255];
353 char mountPoint[255];
354
355 snprintf(asecFileName, sizeof(asecFileName),
356 "/sdcard/android_secure/%s.asec", id);
357 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
358
San Mehat0586d542010-01-12 15:38:59 -0800359 if (isMountpointMounted(mountPoint)) {
San Mehat68f8ebd2010-01-23 07:21:21 -0800360 LOGD("Unmounting container before destroy");
San Mehat0586d542010-01-12 15:38:59 -0800361 if (unmountAsec(id)) {
362 LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
363 return -1;
364 }
365 }
San Mehata19b2502010-01-06 10:33:53 -0800366
San Mehat0586d542010-01-12 15:38:59 -0800367 if (unlink(asecFileName)) {
San Mehat68f8ebd2010-01-23 07:21:21 -0800368 LOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800369 return -1;
370 }
San Mehata19b2502010-01-06 10:33:53 -0800371
372 LOGD("ASEC %s destroyed", id);
373 return 0;
374}
375
376int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
377 char asecFileName[255];
378 char mountPoint[255];
379
380 snprintf(asecFileName, sizeof(asecFileName),
381 "/sdcard/android_secure/%s.asec", id);
382 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
383
384 if (isMountpointMounted(mountPoint)) {
385 LOGE("ASEC %s already mounted", id);
386 errno = EBUSY;
387 return -1;
388 }
389
390 char loopDevice[255];
391 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat8da6bcb2010-01-09 12:24:05 -0800392 if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800393 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
394 return -1;
395 }
San Mehatb78a32c2010-01-10 13:02:12 -0800396 LOGD("New loop device created at %s", loopDevice);
397 } else {
398 LOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
399 }
400
401 char dmDevice[255];
402 bool cleanupDm = false;
403 if (strcmp(key, "none")) {
404 if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) {
405 unsigned int nr_sec = 0;
406 int fd;
407
408 if ((fd = open(loopDevice, O_RDWR)) < 0) {
409 LOGE("Failed to open loopdevice (%s)", strerror(errno));
410 Loop::destroyByDevice(loopDevice);
411 return -1;
412 }
413
414 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
415 LOGE("Failed to get loop size (%s)", strerror(errno));
416 Loop::destroyByDevice(loopDevice);
417 close(fd);
418 return -1;
419 }
420 close(fd);
San Mehat8b8f71b2010-01-11 09:17:25 -0800421 if (Devmapper::create(id, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -0800422 dmDevice, sizeof(dmDevice))) {
423 LOGE("ASEC device mapping failed (%s)", strerror(errno));
424 Loop::destroyByDevice(loopDevice);
425 return -1;
426 }
427 LOGD("New devmapper instance created at %s", dmDevice);
428 } else {
429 LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
430 }
431 cleanupDm = true;
432 } else {
433 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800434 }
435
436 if (mkdir(mountPoint, 0777)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800437 if (errno != EEXIST) {
438 LOGE("Mountpoint creation failed (%s)", strerror(errno));
439 if (cleanupDm) {
440 Devmapper::destroy(id);
441 }
442 Loop::destroyByDevice(loopDevice);
443 return -1;
444 }
San Mehata19b2502010-01-06 10:33:53 -0800445 }
446
San Mehatb78a32c2010-01-10 13:02:12 -0800447 if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0,
San Mehatcff5ec32010-01-08 12:31:44 -0800448 0222, false)) {
449// 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800450 LOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800451 if (cleanupDm) {
452 Devmapper::destroy(id);
453 }
454 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800455 return -1;
456 }
457
San Mehat88705162010-01-15 09:26:28 -0800458 mActiveContainers->push_back(strdup(id));
San Mehata19b2502010-01-06 10:33:53 -0800459 LOGD("ASEC %s mounted", id);
460 return 0;
461}
462
San Mehat49e2bce2009-10-12 16:29:01 -0700463int VolumeManager::mountVolume(const char *label) {
464 Volume *v = lookupVolume(label);
465
466 if (!v) {
467 errno = ENOENT;
468 return -1;
469 }
470
San Mehata2677e42009-12-13 10:40:18 -0800471 return v->mountVol();
472}
473
474int VolumeManager::shareAvailable(const char *method, bool *avail) {
475
476 if (strcmp(method, "ums")) {
477 errno = ENOSYS;
478 return -1;
479 }
480
481 if (mUsbMassStorageConnected)
482 *avail = true;
483 else
484 *avail = false;
485 return 0;
486}
487
488int VolumeManager::simulate(const char *cmd, const char *arg) {
489
490 if (!strcmp(cmd, "ums")) {
491 if (!strcmp(arg, "connect")) {
492 notifyUmsConnected(true);
493 } else if (!strcmp(arg, "disconnect")) {
494 notifyUmsConnected(false);
495 } else {
496 errno = EINVAL;
497 return -1;
498 }
499 } else {
500 errno = EINVAL;
501 return -1;
502 }
503 return 0;
504}
505
506int VolumeManager::shareVolume(const char *label, const char *method) {
507 Volume *v = lookupVolume(label);
508
509 if (!v) {
510 errno = ENOENT;
511 return -1;
512 }
513
514 /*
515 * Eventually, we'll want to support additional share back-ends,
516 * some of which may work while the media is mounted. For now,
517 * we just support UMS
518 */
519 if (strcmp(method, "ums")) {
520 errno = ENOSYS;
521 return -1;
522 }
523
524 if (v->getState() == Volume::State_NoMedia) {
525 errno = ENODEV;
526 return -1;
527 }
528
San Mehat49e2bce2009-10-12 16:29:01 -0700529 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -0800530 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -0700531 errno = EBUSY;
532 return -1;
533 }
534
San Mehata2677e42009-12-13 10:40:18 -0800535 dev_t d = v->getDiskDevice();
536 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
537 // This volume does not support raw disk access
538 errno = EINVAL;
539 return -1;
540 }
541
542 int fd;
543 char nodepath[255];
544 snprintf(nodepath,
545 sizeof(nodepath), "/dev/block/vold/%d:%d",
546 MAJOR(d), MINOR(d));
547
San Mehat0cde53c2009-12-22 08:32:33 -0800548 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file",
549 O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800550 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
551 return -1;
552 }
553
554 if (write(fd, nodepath, strlen(nodepath)) < 0) {
555 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
556 close(fd);
557 return -1;
558 }
559
560 close(fd);
561 v->handleVolumeShared();
562 return 0;
563}
564
565int VolumeManager::unshareVolume(const char *label, const char *method) {
566 Volume *v = lookupVolume(label);
567
568 if (!v) {
569 errno = ENOENT;
570 return -1;
571 }
572
573 if (strcmp(method, "ums")) {
574 errno = ENOSYS;
575 return -1;
576 }
577
578 if (v->getState() != Volume::State_Shared) {
579 errno = EINVAL;
580 return -1;
581 }
582
583 dev_t d = v->getDiskDevice();
584
585 int fd;
586 char nodepath[255];
587 snprintf(nodepath,
588 sizeof(nodepath), "/dev/block/vold/%d:%d",
589 MAJOR(d), MINOR(d));
590
San Mehat0cde53c2009-12-22 08:32:33 -0800591 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800592 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
593 return -1;
594 }
595
596 char ch = 0;
597 if (write(fd, &ch, 1) < 0) {
598 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
599 close(fd);
600 return -1;
601 }
602
603 close(fd);
604 v->handleVolumeUnshared();
605 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700606}
607
608int VolumeManager::unmountVolume(const char *label) {
609 Volume *v = lookupVolume(label);
610
611 if (!v) {
612 errno = ENOENT;
613 return -1;
614 }
615
San Mehata2677e42009-12-13 10:40:18 -0800616 if (v->getState() == Volume::State_NoMedia) {
617 errno = ENODEV;
618 return -1;
619 }
620
San Mehat49e2bce2009-10-12 16:29:01 -0700621 if (v->getState() != Volume::State_Mounted) {
San Mehata2677e42009-12-13 10:40:18 -0800622 LOGW("Attempt to unmount volume which isn't mounted (%d)\n",
623 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -0700624 errno = EBUSY;
625 return -1;
626 }
627
San Mehat88705162010-01-15 09:26:28 -0800628 while(mActiveContainers->size()) {
629 AsecIdCollection::iterator it = mActiveContainers->begin();
630 LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint());
631 if (unmountAsec(*it)) {
632 LOGE("Failed to unmount ASEC %s (%s) - unmount of %s may fail!", *it,
633 strerror(errno), v->getMountpoint());
634 }
635 }
636
San Mehata2677e42009-12-13 10:40:18 -0800637 return v->unmountVol();
San Mehat49e2bce2009-10-12 16:29:01 -0700638}
639
San Mehata2677e42009-12-13 10:40:18 -0800640/*
641 * Looks up a volume by it's label or mount-point
642 */
San Mehat49e2bce2009-10-12 16:29:01 -0700643Volume *VolumeManager::lookupVolume(const char *label) {
644 VolumeCollection::iterator i;
645
646 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -0800647 if (label[0] == '/') {
648 if (!strcmp(label, (*i)->getMountpoint()))
649 return (*i);
650 } else {
651 if (!strcmp(label, (*i)->getLabel()))
652 return (*i);
653 }
San Mehat49e2bce2009-10-12 16:29:01 -0700654 }
655 return NULL;
656}
San Mehata19b2502010-01-06 10:33:53 -0800657
658bool VolumeManager::isMountpointMounted(const char *mp)
659{
660 char device[256];
661 char mount_path[256];
662 char rest[256];
663 FILE *fp;
664 char line[1024];
665
666 if (!(fp = fopen("/proc/mounts", "r"))) {
667 LOGE("Error opening /proc/mounts (%s)", strerror(errno));
668 return false;
669 }
670
671 while(fgets(line, sizeof(line), fp)) {
672 line[strlen(line)-1] = '\0';
673 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
674 if (!strcmp(mount_path, mp)) {
675 fclose(fp);
676 return true;
677 }
678
679 }
680
681 fclose(fp);
682 return false;
683}
684