blob: 9c75980d5ba4d2d4ce8cfbe1f65c5f457d8013d1 [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>
Ken Sumrall56ad03c2013-02-13 13:00:19 -080026#include <fs_mgr.h>
San Mehatf1b736b2009-10-10 17:22:08 -070027
28#define LOG_TAG "Vold"
29
30#include "cutils/log.h"
Ken Sumrall56ad03c2013-02-13 13:00:19 -080031#include "cutils/properties.h"
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#include "VolumeManager.h"
34#include "CommandListener.h"
35#include "NetlinkManager.h"
San Mehatae10b912009-10-12 14:57:05 -070036#include "DirectVolume.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070037#include "cryptfs.h"
San Mehatf1b736b2009-10-10 17:22:08 -070038
39static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070040static void coldboot(const char *path);
San Mehatf1b736b2009-10-10 17:22:08 -070041
Ken Sumrall56ad03c2013-02-13 13:00:19 -080042#define FSTAB_PREFIX "/fstab."
43struct fstab *fstab;
44
San Mehatf1b736b2009-10-10 17:22:08 -070045int main() {
46
47 VolumeManager *vm;
48 CommandListener *cl;
49 NetlinkManager *nm;
50
San Mehat97ac40e2010-03-24 10:24:19 -070051 SLOGI("Vold 2.1 (the revenge) firing up");
San Mehata2677e42009-12-13 10:40:18 -080052
53 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070054
55 /* Create our singleton managers */
56 if (!(vm = VolumeManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070057 SLOGE("Unable to create VolumeManager");
San Mehatf1b736b2009-10-10 17:22:08 -070058 exit(1);
59 };
60
61 if (!(nm = NetlinkManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070062 SLOGE("Unable to create NetlinkManager");
San Mehatf1b736b2009-10-10 17:22:08 -070063 exit(1);
64 };
65
San Mehata2677e42009-12-13 10:40:18 -080066
San Mehatf1b736b2009-10-10 17:22:08 -070067 cl = new CommandListener();
68 vm->setBroadcaster((SocketListener *) cl);
69 nm->setBroadcaster((SocketListener *) cl);
70
71 if (vm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070072 SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070073 exit(1);
74 }
75
76 if (process_config(vm)) {
San Mehat97ac40e2010-03-24 10:24:19 -070077 SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070078 }
79
80 if (nm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070081 SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070082 exit(1);
83 }
84
San Mehat3578c412009-10-12 14:51:52 -070085 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -080086// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -070087
San Mehatf1b736b2009-10-10 17:22:08 -070088 /*
89 * Now that we're up, we can respond to commands
90 */
91 if (cl->startListener()) {
San Mehat97ac40e2010-03-24 10:24:19 -070092 SLOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070093 exit(1);
94 }
95
96 // Eventually we'll become the monitoring thread
97 while(1) {
98 sleep(1000);
99 }
100
San Mehat97ac40e2010-03-24 10:24:19 -0700101 SLOGI("Vold exiting");
San Mehatf1b736b2009-10-10 17:22:08 -0700102 exit(0);
103}
104
San Mehat3578c412009-10-12 14:51:52 -0700105static void do_coldboot(DIR *d, int lvl)
106{
107 struct dirent *de;
108 int dfd, fd;
109
110 dfd = dirfd(d);
111
112 fd = openat(dfd, "uevent", O_WRONLY);
113 if(fd >= 0) {
114 write(fd, "add\n", 4);
115 close(fd);
116 }
117
118 while((de = readdir(d))) {
119 DIR *d2;
120
121 if (de->d_name[0] == '.')
122 continue;
123
124 if (de->d_type != DT_DIR && lvl > 0)
125 continue;
126
127 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
128 if(fd < 0)
129 continue;
130
131 d2 = fdopendir(fd);
132 if(d2 == 0)
133 close(fd);
134 else {
135 do_coldboot(d2, lvl + 1);
136 closedir(d2);
137 }
138 }
139}
140
141static void coldboot(const char *path)
142{
143 DIR *d = opendir(path);
144 if(d) {
145 do_coldboot(d, 0);
146 closedir(d);
147 }
148}
149
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800150static int process_config(VolumeManager *vm)
Ken Sumrall29d8da82011-05-18 17:20:07 -0700151{
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800152 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
153 char propbuf[PROPERTY_VALUE_MAX];
154 int i;
155 int ret = -1;
156 int flags;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700157
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800158 property_get("ro.hardware", propbuf, "");
159 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700160
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800161 fstab = fs_mgr_read_fstab(fstab_filename);
162 if (!fstab) {
163 SLOGE("failed to open %s\n", fstab_filename);
San Mehatf1b736b2009-10-10 17:22:08 -0700164 return -1;
165 }
166
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800167 /* Loop through entries looking for ones that vold manages */
168 for (i = 0; i < fstab->num_entries; i++) {
169 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
San Mehatae10b912009-10-12 14:57:05 -0700170 DirectVolume *dv = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800171 flags = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700172
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800173 dv = new DirectVolume(vm, fstab->recs[i].label,
174 fstab->recs[i].mount_point,
175 fstab->recs[i].partnum);
176
177 if (dv->addPath(fstab->recs[i].blk_device)) {
178 SLOGE("Failed to add devpath %s to volume %s",
179 fstab->recs[i].blk_device, fstab->recs[i].label);
180 goto out_fail;
San Mehatf1b736b2009-10-10 17:22:08 -0700181 }
182
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800183 /* Set any flags that might be set for this volume */
184 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
185 flags |= VOL_NONREMOVABLE;
San Mehata2677e42009-12-13 10:40:18 -0800186 }
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800187 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
188 flags |= VOL_ENCRYPTABLE;
San Mehatf1b736b2009-10-10 17:22:08 -0700189 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700190 dv->setFlags(flags);
191
San Mehatf1b736b2009-10-10 17:22:08 -0700192 vm->addVolume(dv);
San Mehatf1b736b2009-10-10 17:22:08 -0700193 }
194 }
195
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800196 ret = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700197
San Mehatf1b736b2009-10-10 17:22:08 -0700198out_fail:
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800199 return ret;
San Mehatf1b736b2009-10-10 17:22:08 -0700200}