Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

system-vold: Commit

system/vold


Commit MetaInfo

Revisiónd6925769d810834a88f3180da23019ecdb04fe4e (tree)
Tiempo2013-06-09 13:36:12
AutorChih-Wei Huang <cwhuang@linu...>
CommiterChih-Wei Huang

Log Message

vold: add class AutoVolume to support auto-mount and fake sdcard

We support up to 4 USB/MMC/SD to be auto mounted.
Besides, always allow auto mount even SDCARD is not empty.

Cambiar Resumen

Diferencia

--- a/Android.mk
+++ b/Android.mk
@@ -8,6 +8,7 @@ common_src_files := \
88 NetlinkHandler.cpp \
99 Volume.cpp \
1010 DirectVolume.cpp \
11+ AutoVolume.cpp \
1112 Process.cpp \
1213 Ext4.cpp \
1314 Fat.cpp \
--- /dev/null
+++ b/AutoVolume.cpp
@@ -0,0 +1,132 @@
1+/*
2+ * Copyright (C) 2010 The Android-x86 Open Source Project
3+ *
4+ * Author: Chih-Wei Huang <cwhuang@linux.org.tw>
5+ *
6+ * Licensed under the Apache License, Version 2.0 (the "License");
7+ * you may not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+#define LOG_TAG "AutoVolume"
20+
21+#include <dirent.h>
22+#include <stdlib.h>
23+
24+#include <cutils/log.h>
25+#include <sysutils/NetlinkEvent.h>
26+
27+#include "Fat.h"
28+#include "Loop.h"
29+#include "AutoVolume.h"
30+
31+AutoVolume::AutoVolume(VolumeManager *vm, const char *label, const char *mount_point, const char *sdcard)
32+ : DirectVolume(vm, label, mount_point, -1), mSdcard(0)
33+{
34+ if (sdcard && *sdcard) {
35+ if (strncmp(sdcard, "/dev/", 5) && !access(sdcard, F_OK)) {
36+ char loopdev[256];
37+ if (!Loop::create(label, sdcard, loopdev, sizeof(loopdev)))
38+ sdcard = loopdev;
39+ } else if (!strcmp(sdcard, "ramdisk")) {
40+ // FIXME: do not hardcode ramdisk device
41+ const char *ramdisk = "/dev/block/ram1";
42+ Fat::format(ramdisk, 0);
43+ sdcard = ramdisk;
44+ }
45+ if (const char *d = strrchr(sdcard, '/'))
46+ sdcard = ++d;
47+ else if (!strcmp(sdcard, "premount"))
48+ setState(State_Idle);
49+ mSdcard = strdup(sdcard);
50+ }
51+}
52+
53+AutoVolume::~AutoVolume()
54+{
55+ free(mSdcard);
56+}
57+
58+static bool isExternalStorage(const char *dir, const char *syspath)
59+{
60+ bool ret = false;
61+ if (DIR *d = opendir(dir)) {
62+ while (struct dirent *e = readdir(d)) {
63+ char buf[256];
64+ if (e->d_name[0] != '.' &&
65+ strcmp(e->d_name, "module") && strcmp(e->d_name, "uevent") &&
66+ strcmp(e->d_name, "unbind") && strcmp(e->d_name, "bind") &&
67+ strcmp(e->d_name, "new_id") && strcmp(e->d_name, "remove_id")) {
68+ char p[256];
69+ snprintf(p, sizeof(p), "%s/%s", dir, e->d_name);
70+ ssize_t sz = readlink(p, buf, sizeof(buf));
71+ // skip the beginning "../../../.."
72+ if (sz > 11 && !strncmp(buf + 11, syspath, sz - 11)) {
73+ ret = true;
74+ break;
75+ }
76+ }
77+ }
78+ closedir(d);
79+ }
80+ return ret;
81+}
82+
83+int AutoVolume::handleBlockEvent(NetlinkEvent *evt)
84+{
85+ if (evt->getAction() == NetlinkEvent::NlActionAdd) {
86+ const char *dt = evt->findParam("DEVTYPE");
87+ const char *dp = evt->findParam("DEVPATH");
88+ bool isdisk = !strcmp(dt, "disk");
89+ if (mSdcard) {
90+ if (const char *d = strrchr(dp, '/')) {
91+ int ret = strcmp(++d, mSdcard);
92+ if (isdisk) {
93+ if (ret) {
94+ char p[256];
95+ snprintf(p, sizeof(p), "/sys%s/%s", dp, mSdcard);
96+ ret = access(p, F_OK);
97+ }
98+ if (!ret)
99+ addPath(dp);
100+ } else {
101+ if (!ret) {
102+ const char *t = evt->findParam("PARTN");
103+ mPartIdx = t ? atoi(t) : 1;
104+ }
105+ }
106+ }
107+ } else {
108+ if (isdisk && mPaths->empty()) {
109+ const char *storages[] = {
110+ "/sys/bus/mmc/drivers/mmcblk", // MMC block device
111+ "/sys/bus/usb/drivers/usb-storage", // USB Mass Storage
112+ "/sys/bus/usb/drivers/rts5139", // Realtek RTS5139 USB card reader
113+ };
114+
115+ size_t i = 0;
116+ while (i < sizeof(storages) / sizeof(const char *)) {
117+ if (isExternalStorage(storages[i++], dp)) {
118+ addPath(dp);
119+ break;
120+ }
121+ }
122+ }
123+ }
124+ }
125+
126+ int ret = DirectVolume::handleBlockEvent(evt);
127+
128+ if (evt->getAction() == NetlinkEvent::NlActionRemove && getState() == State_NoMedia)
129+ mPaths->clear();
130+
131+ return ret;
132+}
--- /dev/null
+++ b/AutoVolume.h
@@ -0,0 +1,35 @@
1+/*
2+ * Copyright (C) 2010 The Android-x86 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+#ifndef _AUTOVOLUME_H
18+#define _AUTOVOLUME_H
19+
20+#include "DirectVolume.h"
21+
22+
23+class AutoVolume : public DirectVolume {
24+public:
25+ AutoVolume(VolumeManager *vm, const char *label, const char *mount_point, const char *part_name = 0);
26+ virtual ~AutoVolume();
27+
28+protected:
29+ int handleBlockEvent(NetlinkEvent *evt);
30+
31+private:
32+ char *mSdcard;
33+};
34+
35+#endif
--- a/main.cpp
+++ b/main.cpp
@@ -34,7 +34,7 @@
3434 #include "VolumeManager.h"
3535 #include "CommandListener.h"
3636 #include "NetlinkManager.h"
37-#include "DirectVolume.h"
37+#include "AutoVolume.h"
3838 #include "cryptfs.h"
3939
4040 static int process_config(VolumeManager *vm);
@@ -159,13 +159,38 @@ static int process_config(VolumeManager *vm)
159159 int ret = -1;
160160 int flags;
161161
162+ if (FILE *fp = fopen("/proc/cmdline", "r")) {
163+ while (fscanf(fp, "%s", propbuf) > 0) {
164+ if (!strncmp(propbuf, "SDCARD=", 7)) {
165+ const char *sdcard = propbuf + 7;
166+ if (*sdcard) {
167+ if (Volume *vol = new AutoVolume(vm, "sdcard", "/mnt/sdcard1", sdcard)) {
168+ vm->addVolume(vol);
169+ break;
170+ }
171+ }
172+ }
173+ }
174+ fclose(fp);
175+ }
176+
162177 property_get("ro.hardware", propbuf, "");
163178 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
164179
165180 fstab = fs_mgr_read_fstab(fstab_filename);
166181 if (!fstab) {
167- SLOGE("failed to open %s\n", fstab_filename);
168- return -1;
182+ SLOGD("failed to open %s, use AutoVolume", fstab_filename);
183+ // no volume added yet, create 4 AutoVolume objects
184+ // to mount USB/MMC/SD automatically
185+ Volume *vol = 0;
186+ char label[8] = "usb0", mp[16] = "/mnt/USB0";
187+ while (label[3] < '4') {
188+ vol = new AutoVolume(vm, label, mp);
189+ if (vol)
190+ vm->addVolume(vol);
191+ label[3]++, mp[8]++;
192+ }
193+ return vol ? 0 : -ENOMEM;
169194 }
170195
171196 /* Loop through entries looking for ones that vold manages */
Show on old repository browser