blob: 551003b27026dc2ea6fc2ca34fce4a11cc536851 [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>
18#include <stdlib.h>
19#include <errno.h>
20#include <string.h>
San Mehat3578c412009-10-12 14:51:52 -070021#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <fcntl.h>
25#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070026
27#define LOG_TAG "Vold"
28
29#include "cutils/log.h"
30
31#include "VolumeManager.h"
32#include "CommandListener.h"
33#include "NetlinkManager.h"
San Mehatae10b912009-10-12 14:57:05 -070034#include "DirectVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070035
36static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070037static void coldboot(const char *path);
San Mehatf1b736b2009-10-10 17:22:08 -070038
39int main() {
40
41 VolumeManager *vm;
42 CommandListener *cl;
43 NetlinkManager *nm;
44
San Mehatf1b07fb2010-03-02 13:16:33 -080045 LOGI("Vold 2.1 (the revenge) firing up");
San Mehata2677e42009-12-13 10:40:18 -080046
47 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070048
49 /* Create our singleton managers */
50 if (!(vm = VolumeManager::Instance())) {
51 LOGE("Unable to create VolumeManager");
52 exit(1);
53 };
54
55 if (!(nm = NetlinkManager::Instance())) {
56 LOGE("Unable to create NetlinkManager");
57 exit(1);
58 };
59
San Mehata2677e42009-12-13 10:40:18 -080060
San Mehatf1b736b2009-10-10 17:22:08 -070061 cl = new CommandListener();
62 vm->setBroadcaster((SocketListener *) cl);
63 nm->setBroadcaster((SocketListener *) cl);
64
65 if (vm->start()) {
66 LOGE("Unable to start VolumeManager (%s)", strerror(errno));
67 exit(1);
68 }
69
70 if (process_config(vm)) {
San Mehatf1b07fb2010-03-02 13:16:33 -080071 LOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070072 }
73
74 if (nm->start()) {
75 LOGE("Unable to start NetlinkManager (%s)", strerror(errno));
76 exit(1);
77 }
78
San Mehat3578c412009-10-12 14:51:52 -070079 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -080080 /*
81 * Switch uevents are broken.
82 * For now we manually bootstrap
83 * the ums switch
84 */
85 {
86 FILE *fp;
87 char state[255];
88
San Mehat79e31be2010-01-04 08:30:00 -080089 if ((fp = fopen("/sys/devices/virtual/switch/usb_mass_storage/state",
San Mehat0cde53c2009-12-22 08:32:33 -080090 "r"))) {
San Mehatf1b07fb2010-03-02 13:16:33 -080091 if (fgets(state, sizeof(state), fp)) {
92 if (!strncmp(state, "online", 6)) {
93 vm->notifyUmsConnected(true);
94 } else {
95 vm->notifyUmsConnected(false);
96 }
San Mehat79e31be2010-01-04 08:30:00 -080097 } else {
San Mehatf1b07fb2010-03-02 13:16:33 -080098 LOGE("Failed to read switch state (%s)", strerror(errno));
San Mehat79e31be2010-01-04 08:30:00 -080099 }
San Mehatf1b07fb2010-03-02 13:16:33 -0800100
San Mehat0cde53c2009-12-22 08:32:33 -0800101 fclose(fp);
San Mehat0cde53c2009-12-22 08:32:33 -0800102 } else {
San Mehat79e31be2010-01-04 08:30:00 -0800103 LOGW("No UMS switch available");
San Mehat0cde53c2009-12-22 08:32:33 -0800104 }
San Mehat0cde53c2009-12-22 08:32:33 -0800105 }
106// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700107
San Mehatf1b736b2009-10-10 17:22:08 -0700108 /*
109 * Now that we're up, we can respond to commands
110 */
111 if (cl->startListener()) {
112 LOGE("Unable to start CommandListener (%s)", strerror(errno));
113 exit(1);
114 }
115
116 // Eventually we'll become the monitoring thread
117 while(1) {
118 sleep(1000);
119 }
120
121 LOGI("Vold exiting");
122 exit(0);
123}
124
San Mehat3578c412009-10-12 14:51:52 -0700125static void do_coldboot(DIR *d, int lvl)
126{
127 struct dirent *de;
128 int dfd, fd;
129
130 dfd = dirfd(d);
131
132 fd = openat(dfd, "uevent", O_WRONLY);
133 if(fd >= 0) {
134 write(fd, "add\n", 4);
135 close(fd);
136 }
137
138 while((de = readdir(d))) {
139 DIR *d2;
140
141 if (de->d_name[0] == '.')
142 continue;
143
144 if (de->d_type != DT_DIR && lvl > 0)
145 continue;
146
147 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
148 if(fd < 0)
149 continue;
150
151 d2 = fdopendir(fd);
152 if(d2 == 0)
153 close(fd);
154 else {
155 do_coldboot(d2, lvl + 1);
156 closedir(d2);
157 }
158 }
159}
160
161static void coldboot(const char *path)
162{
163 DIR *d = opendir(path);
164 if(d) {
165 do_coldboot(d, 0);
166 closedir(d);
167 }
168}
169
San Mehatf1b736b2009-10-10 17:22:08 -0700170static int process_config(VolumeManager *vm) {
171 FILE *fp;
172 int n = 0;
173 char line[255];
174
175 if (!(fp = fopen("/etc/vold.fstab", "r"))) {
176 return -1;
177 }
178
179 while(fgets(line, sizeof(line), fp)) {
180 char *next = line;
181 char *type, *label, *mount_point;
182
183 n++;
184 line[strlen(line)-1] = '\0';
185
186 if (line[0] == '#' || line[0] == '\0')
187 continue;
188
189 if (!(type = strsep(&next, " \t"))) {
190 LOGE("Error parsing type");
191 goto out_syntax;
192 }
193 if (!(label = strsep(&next, " \t"))) {
194 LOGE("Error parsing label");
195 goto out_syntax;
196 }
197 if (!(mount_point = strsep(&next, " \t"))) {
198 LOGE("Error parsing mount point");
199 goto out_syntax;
200 }
201
202 if (!strcmp(type, "dev_mount")) {
San Mehatae10b912009-10-12 14:57:05 -0700203 DirectVolume *dv = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700204 char *part, *sysfs_path;
205
206 if (!(part = strsep(&next, " \t"))) {
207 LOGE("Error parsing partition");
208 goto out_syntax;
209 }
210 if (strcmp(part, "auto") && atoi(part) == 0) {
211 LOGE("Partition must either be 'auto' or 1 based index instead of '%s'", part);
212 goto out_syntax;
213 }
214
San Mehata2677e42009-12-13 10:40:18 -0800215 if (!strcmp(part, "auto")) {
216 dv = new DirectVolume(vm, label, mount_point, -1);
217 } else {
218 dv = new DirectVolume(vm, label, mount_point, atoi(part));
219 }
San Mehatf1b736b2009-10-10 17:22:08 -0700220
221 while((sysfs_path = strsep(&next, " \t"))) {
222 if (dv->addPath(sysfs_path)) {
223 LOGE("Failed to add devpath %s to volume %s", sysfs_path,
224 label);
225 goto out_fail;
226 }
227 }
228 vm->addVolume(dv);
229 } else if (!strcmp(type, "map_mount")) {
230 } else {
231 LOGE("Unknown type '%s'", type);
232 goto out_syntax;
233 }
234 }
235
236 fclose(fp);
237 return 0;
238
239out_syntax:
240 LOGE("Syntax error on config line %d", n);
241 errno = -EINVAL;
242out_fail:
243 fclose(fp);
244 return -1;
245}