blob: c07f48d175f43fdc2839a110a8832b7a0aefd7d3 [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"
Stephen Smalley684e6622014-09-30 10:29:24 -040039#include "sehandle.h"
San Mehatf1b736b2009-10-10 17:22:08 -070040
41static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070042static void coldboot(const char *path);
San Mehatf1b736b2009-10-10 17:22:08 -070043
Ken Sumrall56ad03c2013-02-13 13:00:19 -080044#define FSTAB_PREFIX "/fstab."
45struct fstab *fstab;
46
Stephen Smalley684e6622014-09-30 10:29:24 -040047struct selabel_handle *sehandle;
48
San Mehatf1b736b2009-10-10 17:22:08 -070049int main() {
50
51 VolumeManager *vm;
52 CommandListener *cl;
53 NetlinkManager *nm;
54
San Mehat97ac40e2010-03-24 10:24:19 -070055 SLOGI("Vold 2.1 (the revenge) firing up");
San Mehata2677e42009-12-13 10:40:18 -080056
Stephen Smalley684e6622014-09-30 10:29:24 -040057 sehandle = selinux_android_file_context_handle();
58 if (sehandle)
59 selinux_android_set_sehandle(sehandle);
60
San Mehata2677e42009-12-13 10:40:18 -080061 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070062
Ken Sumrallb9738ea2013-03-19 19:42:30 -070063 /* For when cryptfs checks and mounts an encrypted filesystem */
64 klog_set_level(6);
65
San Mehatf1b736b2009-10-10 17:22:08 -070066 /* Create our singleton managers */
67 if (!(vm = VolumeManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070068 SLOGE("Unable to create VolumeManager");
San Mehatf1b736b2009-10-10 17:22:08 -070069 exit(1);
70 };
71
72 if (!(nm = NetlinkManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070073 SLOGE("Unable to create NetlinkManager");
San Mehatf1b736b2009-10-10 17:22:08 -070074 exit(1);
75 };
76
San Mehata2677e42009-12-13 10:40:18 -080077
San Mehatf1b736b2009-10-10 17:22:08 -070078 cl = new CommandListener();
79 vm->setBroadcaster((SocketListener *) cl);
80 nm->setBroadcaster((SocketListener *) cl);
81
82 if (vm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070083 SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070084 exit(1);
85 }
86
87 if (process_config(vm)) {
San Mehat97ac40e2010-03-24 10:24:19 -070088 SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070089 }
90
91 if (nm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070092 SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070093 exit(1);
94 }
95
San Mehat3578c412009-10-12 14:51:52 -070096 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -080097// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -070098
San Mehatf1b736b2009-10-10 17:22:08 -070099 /*
100 * Now that we're up, we can respond to commands
101 */
102 if (cl->startListener()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700103 SLOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700104 exit(1);
105 }
106
107 // Eventually we'll become the monitoring thread
108 while(1) {
109 sleep(1000);
110 }
111
San Mehat97ac40e2010-03-24 10:24:19 -0700112 SLOGI("Vold exiting");
San Mehatf1b736b2009-10-10 17:22:08 -0700113 exit(0);
114}
115
San Mehat3578c412009-10-12 14:51:52 -0700116static void do_coldboot(DIR *d, int lvl)
117{
118 struct dirent *de;
119 int dfd, fd;
120
121 dfd = dirfd(d);
122
123 fd = openat(dfd, "uevent", O_WRONLY);
124 if(fd >= 0) {
125 write(fd, "add\n", 4);
126 close(fd);
127 }
128
129 while((de = readdir(d))) {
130 DIR *d2;
131
132 if (de->d_name[0] == '.')
133 continue;
134
135 if (de->d_type != DT_DIR && lvl > 0)
136 continue;
137
138 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
139 if(fd < 0)
140 continue;
141
142 d2 = fdopendir(fd);
143 if(d2 == 0)
144 close(fd);
145 else {
146 do_coldboot(d2, lvl + 1);
147 closedir(d2);
148 }
149 }
150}
151
152static void coldboot(const char *path)
153{
154 DIR *d = opendir(path);
155 if(d) {
156 do_coldboot(d, 0);
157 closedir(d);
158 }
159}
160
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800161static int process_config(VolumeManager *vm)
Ken Sumrall29d8da82011-05-18 17:20:07 -0700162{
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800163 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
164 char propbuf[PROPERTY_VALUE_MAX];
165 int i;
166 int ret = -1;
167 int flags;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700168
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800169 property_get("ro.hardware", propbuf, "");
170 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700171
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800172 fstab = fs_mgr_read_fstab(fstab_filename);
173 if (!fstab) {
174 SLOGE("failed to open %s\n", fstab_filename);
San Mehatf1b736b2009-10-10 17:22:08 -0700175 return -1;
176 }
177
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800178 /* Loop through entries looking for ones that vold manages */
179 for (i = 0; i < fstab->num_entries; i++) {
180 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
San Mehatae10b912009-10-12 14:57:05 -0700181 DirectVolume *dv = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800182 flags = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700183
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800184 /* Set any flags that might be set for this volume */
185 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
186 flags |= VOL_NONREMOVABLE;
San Mehata2677e42009-12-13 10:40:18 -0800187 }
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800188 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
189 flags |= VOL_ENCRYPTABLE;
San Mehatf1b736b2009-10-10 17:22:08 -0700190 }
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700191 /* Only set this flag if there is not an emulated sd card */
192 if (fs_mgr_is_noemulatedsd(&fstab->recs[i]) &&
193 !strcmp(fstab->recs[i].fs_type, "vfat")) {
194 flags |= VOL_PROVIDES_ASEC;
195 }
196 dv = new DirectVolume(vm, &(fstab->recs[i]), flags);
197
198 if (dv->addPath(fstab->recs[i].blk_device)) {
199 SLOGE("Failed to add devpath %s to volume %s",
200 fstab->recs[i].blk_device, fstab->recs[i].label);
201 goto out_fail;
202 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700203
San Mehatf1b736b2009-10-10 17:22:08 -0700204 vm->addVolume(dv);
San Mehatf1b736b2009-10-10 17:22:08 -0700205 }
206 }
207
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800208 ret = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700209
San Mehatf1b736b2009-10-10 17:22:08 -0700210out_fail:
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800211 return ret;
San Mehatf1b736b2009-10-10 17:22:08 -0700212}