blob: ce861788e5eb6175f71b9b2c47233381f5d8b578 [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
Ken Sumrallb9738ea2013-03-19 19:42:30 -070030#include "cutils/klog.h"
San Mehatf1b736b2009-10-10 17:22:08 -070031#include "cutils/log.h"
Ken Sumrall56ad03c2013-02-13 13:00:19 -080032#include "cutils/properties.h"
San Mehatf1b736b2009-10-10 17:22:08 -070033
34#include "VolumeManager.h"
35#include "CommandListener.h"
36#include "NetlinkManager.h"
San Mehatae10b912009-10-12 14:57:05 -070037#include "DirectVolume.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070038#include "cryptfs.h"
San Mehatf1b736b2009-10-10 17:22:08 -070039
40static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070041static void coldboot(const char *path);
San Mehatf1b736b2009-10-10 17:22:08 -070042
Ken Sumrall56ad03c2013-02-13 13:00:19 -080043#define FSTAB_PREFIX "/fstab."
44struct fstab *fstab;
45
San Mehatf1b736b2009-10-10 17:22:08 -070046int main() {
47
48 VolumeManager *vm;
49 CommandListener *cl;
50 NetlinkManager *nm;
51
San Mehat97ac40e2010-03-24 10:24:19 -070052 SLOGI("Vold 2.1 (the revenge) firing up");
San Mehata2677e42009-12-13 10:40:18 -080053
54 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070055
Ken Sumrallb9738ea2013-03-19 19:42:30 -070056 /* For when cryptfs checks and mounts an encrypted filesystem */
57 klog_set_level(6);
58
San Mehatf1b736b2009-10-10 17:22:08 -070059 /* Create our singleton managers */
60 if (!(vm = VolumeManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070061 SLOGE("Unable to create VolumeManager");
San Mehatf1b736b2009-10-10 17:22:08 -070062 exit(1);
63 };
64
65 if (!(nm = NetlinkManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070066 SLOGE("Unable to create NetlinkManager");
San Mehatf1b736b2009-10-10 17:22:08 -070067 exit(1);
68 };
69
San Mehata2677e42009-12-13 10:40:18 -080070
San Mehatf1b736b2009-10-10 17:22:08 -070071 cl = new CommandListener();
72 vm->setBroadcaster((SocketListener *) cl);
73 nm->setBroadcaster((SocketListener *) cl);
74
75 if (vm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070076 SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070077 exit(1);
78 }
79
80 if (process_config(vm)) {
San Mehat97ac40e2010-03-24 10:24:19 -070081 SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070082 }
83
Divya Sharma6f00a4a2014-09-07 09:26:38 +030084 /*
85 * Mount /data to dm-req-crypt if PFE activated.
86 * Must call after process_config() because of fstab initialization.
87 */
88 cryptfs_pfe_boot();
89
San Mehatf1b736b2009-10-10 17:22:08 -070090 if (nm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070091 SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070092 exit(1);
93 }
94
San Mehat3578c412009-10-12 14:51:52 -070095 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -080096// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -070097
San Mehatf1b736b2009-10-10 17:22:08 -070098 /*
99 * Now that we're up, we can respond to commands
100 */
101 if (cl->startListener()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700102 SLOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700103 exit(1);
104 }
105
106 // Eventually we'll become the monitoring thread
107 while(1) {
108 sleep(1000);
109 }
110
San Mehat97ac40e2010-03-24 10:24:19 -0700111 SLOGI("Vold exiting");
San Mehatf1b736b2009-10-10 17:22:08 -0700112 exit(0);
113}
114
San Mehat3578c412009-10-12 14:51:52 -0700115static void do_coldboot(DIR *d, int lvl)
116{
117 struct dirent *de;
118 int dfd, fd;
119
120 dfd = dirfd(d);
121
122 fd = openat(dfd, "uevent", O_WRONLY);
123 if(fd >= 0) {
124 write(fd, "add\n", 4);
125 close(fd);
126 }
127
128 while((de = readdir(d))) {
129 DIR *d2;
130
131 if (de->d_name[0] == '.')
132 continue;
133
134 if (de->d_type != DT_DIR && lvl > 0)
135 continue;
136
137 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
138 if(fd < 0)
139 continue;
140
141 d2 = fdopendir(fd);
142 if(d2 == 0)
143 close(fd);
144 else {
145 do_coldboot(d2, lvl + 1);
146 closedir(d2);
147 }
148 }
149}
150
151static void coldboot(const char *path)
152{
153 DIR *d = opendir(path);
154 if(d) {
155 do_coldboot(d, 0);
156 closedir(d);
157 }
158}
159
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800160static int process_config(VolumeManager *vm)
Ken Sumrall29d8da82011-05-18 17:20:07 -0700161{
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800162 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
163 char propbuf[PROPERTY_VALUE_MAX];
164 int i;
165 int ret = -1;
166 int flags;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700167
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800168 property_get("ro.hardware", propbuf, "");
169 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700170
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800171 fstab = fs_mgr_read_fstab(fstab_filename);
172 if (!fstab) {
173 SLOGE("failed to open %s\n", fstab_filename);
San Mehatf1b736b2009-10-10 17:22:08 -0700174 return -1;
175 }
176
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800177 /* Loop through entries looking for ones that vold manages */
178 for (i = 0; i < fstab->num_entries; i++) {
179 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
San Mehatae10b912009-10-12 14:57:05 -0700180 DirectVolume *dv = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800181 flags = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700182
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 }
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700190 /* Only set this flag if there is not an emulated sd card */
191 if (fs_mgr_is_noemulatedsd(&fstab->recs[i]) &&
192 !strcmp(fstab->recs[i].fs_type, "vfat")) {
193 flags |= VOL_PROVIDES_ASEC;
194 }
195 dv = new DirectVolume(vm, &(fstab->recs[i]), flags);
196
197 if (dv->addPath(fstab->recs[i].blk_device)) {
198 SLOGE("Failed to add devpath %s to volume %s",
199 fstab->recs[i].blk_device, fstab->recs[i].label);
200 goto out_fail;
201 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700202
San Mehatf1b736b2009-10-10 17:22:08 -0700203 vm->addVolume(dv);
San Mehatf1b736b2009-10-10 17:22:08 -0700204 }
205 }
206
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800207 ret = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700208
San Mehatf1b736b2009-10-10 17:22:08 -0700209out_fail:
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800210 return ret;
San Mehatf1b736b2009-10-10 17:22:08 -0700211}