blob: 9c4577491393bf0f3bd38a76f6998ddae4f65a0c [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 Mehat97ac40e2010-03-24 10:24:19 -070045 SLOGI("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())) {
San Mehat97ac40e2010-03-24 10:24:19 -070051 SLOGE("Unable to create VolumeManager");
San Mehatf1b736b2009-10-10 17:22:08 -070052 exit(1);
53 };
54
55 if (!(nm = NetlinkManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070056 SLOGE("Unable to create NetlinkManager");
San Mehatf1b736b2009-10-10 17:22:08 -070057 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()) {
San Mehat97ac40e2010-03-24 10:24:19 -070066 SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070067 exit(1);
68 }
69
70 if (process_config(vm)) {
San Mehat97ac40e2010-03-24 10:24:19 -070071 SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070072 }
73
74 if (nm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070075 SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070076 exit(1);
77 }
78
San Mehat3578c412009-10-12 14:51:52 -070079 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -080080// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -070081
San Mehatf1b736b2009-10-10 17:22:08 -070082 /*
83 * Now that we're up, we can respond to commands
84 */
85 if (cl->startListener()) {
San Mehat97ac40e2010-03-24 10:24:19 -070086 SLOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070087 exit(1);
88 }
89
90 // Eventually we'll become the monitoring thread
91 while(1) {
92 sleep(1000);
93 }
94
San Mehat97ac40e2010-03-24 10:24:19 -070095 SLOGI("Vold exiting");
San Mehatf1b736b2009-10-10 17:22:08 -070096 exit(0);
97}
98
San Mehat3578c412009-10-12 14:51:52 -070099static void do_coldboot(DIR *d, int lvl)
100{
101 struct dirent *de;
102 int dfd, fd;
103
104 dfd = dirfd(d);
105
106 fd = openat(dfd, "uevent", O_WRONLY);
107 if(fd >= 0) {
108 write(fd, "add\n", 4);
109 close(fd);
110 }
111
112 while((de = readdir(d))) {
113 DIR *d2;
114
115 if (de->d_name[0] == '.')
116 continue;
117
118 if (de->d_type != DT_DIR && lvl > 0)
119 continue;
120
121 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
122 if(fd < 0)
123 continue;
124
125 d2 = fdopendir(fd);
126 if(d2 == 0)
127 close(fd);
128 else {
129 do_coldboot(d2, lvl + 1);
130 closedir(d2);
131 }
132 }
133}
134
135static void coldboot(const char *path)
136{
137 DIR *d = opendir(path);
138 if(d) {
139 do_coldboot(d, 0);
140 closedir(d);
141 }
142}
143
San Mehatf1b736b2009-10-10 17:22:08 -0700144static int process_config(VolumeManager *vm) {
145 FILE *fp;
146 int n = 0;
147 char line[255];
148
149 if (!(fp = fopen("/etc/vold.fstab", "r"))) {
150 return -1;
151 }
152
153 while(fgets(line, sizeof(line), fp)) {
Jinho You74ca25a2010-11-15 14:02:32 +0800154 const char *delim = " \t";
155 char *save_ptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700156 char *type, *label, *mount_point;
157
158 n++;
159 line[strlen(line)-1] = '\0';
160
161 if (line[0] == '#' || line[0] == '\0')
162 continue;
163
Jinho You74ca25a2010-11-15 14:02:32 +0800164 if (!(type = strtok_r(line, delim, &save_ptr))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700165 SLOGE("Error parsing type");
San Mehatf1b736b2009-10-10 17:22:08 -0700166 goto out_syntax;
167 }
Jinho You74ca25a2010-11-15 14:02:32 +0800168 if (!(label = strtok_r(NULL, delim, &save_ptr))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700169 SLOGE("Error parsing label");
San Mehatf1b736b2009-10-10 17:22:08 -0700170 goto out_syntax;
171 }
Jinho You74ca25a2010-11-15 14:02:32 +0800172 if (!(mount_point = strtok_r(NULL, delim, &save_ptr))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700173 SLOGE("Error parsing mount point");
San Mehatf1b736b2009-10-10 17:22:08 -0700174 goto out_syntax;
175 }
176
177 if (!strcmp(type, "dev_mount")) {
San Mehatae10b912009-10-12 14:57:05 -0700178 DirectVolume *dv = NULL;
Jinho You74ca25a2010-11-15 14:02:32 +0800179 char *part;
San Mehatf1b736b2009-10-10 17:22:08 -0700180
Jinho You74ca25a2010-11-15 14:02:32 +0800181 if (!(part = strtok_r(NULL, delim, &save_ptr))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700182 SLOGE("Error parsing partition");
San Mehatf1b736b2009-10-10 17:22:08 -0700183 goto out_syntax;
184 }
185 if (strcmp(part, "auto") && atoi(part) == 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700186 SLOGE("Partition must either be 'auto' or 1 based index instead of '%s'", part);
San Mehatf1b736b2009-10-10 17:22:08 -0700187 goto out_syntax;
188 }
189
San Mehata2677e42009-12-13 10:40:18 -0800190 if (!strcmp(part, "auto")) {
191 dv = new DirectVolume(vm, label, mount_point, -1);
192 } else {
193 dv = new DirectVolume(vm, label, mount_point, atoi(part));
194 }
San Mehatf1b736b2009-10-10 17:22:08 -0700195
Jinho You74ca25a2010-11-15 14:02:32 +0800196 while (char *sysfs_path = strtok_r(NULL, delim, &save_ptr)) {
San Mehatf1b736b2009-10-10 17:22:08 -0700197 if (dv->addPath(sysfs_path)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700198 SLOGE("Failed to add devpath %s to volume %s", sysfs_path,
San Mehatf1b736b2009-10-10 17:22:08 -0700199 label);
200 goto out_fail;
201 }
202 }
203 vm->addVolume(dv);
204 } else if (!strcmp(type, "map_mount")) {
205 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700206 SLOGE("Unknown type '%s'", type);
San Mehatf1b736b2009-10-10 17:22:08 -0700207 goto out_syntax;
208 }
209 }
210
211 fclose(fp);
212 return 0;
213
214out_syntax:
San Mehat97ac40e2010-03-24 10:24:19 -0700215 SLOGE("Syntax error on config line %d", n);
San Mehatf1b736b2009-10-10 17:22:08 -0700216 errno = -EINVAL;
217out_fail:
218 fclose(fp);
219 return -1;
220}