blob: 51e2de0ed31a6203e21c37e3e428ab8e927d2fd4 [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
Jeff Sharkey36801cc2015-03-13 16:09:20 -070030#include <base/logging.h>
31#include <base/stringprintf.h>
Ken Sumrallb9738ea2013-03-19 19:42:30 -070032#include "cutils/klog.h"
San Mehatf1b736b2009-10-10 17:22:08 -070033#include "cutils/log.h"
Ken Sumrall56ad03c2013-02-13 13:00:19 -080034#include "cutils/properties.h"
San Mehatf1b736b2009-10-10 17:22:08 -070035
Jeff Sharkey36801cc2015-03-13 16:09:20 -070036#include "Disk.h"
San Mehatf1b736b2009-10-10 17:22:08 -070037#include "VolumeManager.h"
38#include "CommandListener.h"
39#include "NetlinkManager.h"
San Mehatae10b912009-10-12 14:57:05 -070040#include "DirectVolume.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070041#include "cryptfs.h"
Stephen Smalley684e6622014-09-30 10:29:24 -040042#include "sehandle.h"
San Mehatf1b736b2009-10-10 17:22:08 -070043
44static int process_config(VolumeManager *vm);
San Mehat3578c412009-10-12 14:51:52 -070045static void coldboot(const char *path);
San Mehatf1b736b2009-10-10 17:22:08 -070046
Jeff Sharkey36801cc2015-03-13 16:09:20 -070047//#define DEBUG_FSTAB "/data/local/tmp/fstab.debug"
48
Ken Sumrall56ad03c2013-02-13 13:00:19 -080049struct fstab *fstab;
50
Stephen Smalley684e6622014-09-30 10:29:24 -040051struct selabel_handle *sehandle;
52
Jeff Sharkey36801cc2015-03-13 16:09:20 -070053using android::base::StringPrintf;
54
55int main(int argc, char* argv[]) {
56 setenv("ANDROID_LOG_TAGS", "*:v", 1);
57 android::base::InitLogging(argv);
San Mehatf1b736b2009-10-10 17:22:08 -070058
59 VolumeManager *vm;
60 CommandListener *cl;
61 NetlinkManager *nm;
62
San Mehat97ac40e2010-03-24 10:24:19 -070063 SLOGI("Vold 2.1 (the revenge) firing up");
San Mehata2677e42009-12-13 10:40:18 -080064
Stephen Smalley684e6622014-09-30 10:29:24 -040065 sehandle = selinux_android_file_context_handle();
66 if (sehandle)
67 selinux_android_set_sehandle(sehandle);
68
San Mehata2677e42009-12-13 10:40:18 -080069 mkdir("/dev/block/vold", 0755);
San Mehatf1b736b2009-10-10 17:22:08 -070070
Ken Sumrallb9738ea2013-03-19 19:42:30 -070071 /* For when cryptfs checks and mounts an encrypted filesystem */
72 klog_set_level(6);
73
San Mehatf1b736b2009-10-10 17:22:08 -070074 /* Create our singleton managers */
75 if (!(vm = VolumeManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070076 SLOGE("Unable to create VolumeManager");
San Mehatf1b736b2009-10-10 17:22:08 -070077 exit(1);
78 };
79
80 if (!(nm = NetlinkManager::Instance())) {
San Mehat97ac40e2010-03-24 10:24:19 -070081 SLOGE("Unable to create NetlinkManager");
San Mehatf1b736b2009-10-10 17:22:08 -070082 exit(1);
83 };
84
San Mehata2677e42009-12-13 10:40:18 -080085
San Mehatf1b736b2009-10-10 17:22:08 -070086 cl = new CommandListener();
87 vm->setBroadcaster((SocketListener *) cl);
88 nm->setBroadcaster((SocketListener *) cl);
89
90 if (vm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070091 SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070092 exit(1);
93 }
94
95 if (process_config(vm)) {
San Mehat97ac40e2010-03-24 10:24:19 -070096 SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070097 }
98
99 if (nm->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700100 SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700101 exit(1);
102 }
103
San Mehat3578c412009-10-12 14:51:52 -0700104 coldboot("/sys/block");
San Mehat0cde53c2009-12-22 08:32:33 -0800105// coldboot("/sys/class/switch");
San Mehat3578c412009-10-12 14:51:52 -0700106
San Mehatf1b736b2009-10-10 17:22:08 -0700107 /*
108 * Now that we're up, we can respond to commands
109 */
110 if (cl->startListener()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700111 SLOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -0700112 exit(1);
113 }
114
115 // Eventually we'll become the monitoring thread
116 while(1) {
117 sleep(1000);
118 }
119
San Mehat97ac40e2010-03-24 10:24:19 -0700120 SLOGI("Vold exiting");
San Mehatf1b736b2009-10-10 17:22:08 -0700121 exit(0);
122}
123
San Mehat3578c412009-10-12 14:51:52 -0700124static void do_coldboot(DIR *d, int lvl)
125{
126 struct dirent *de;
127 int dfd, fd;
128
129 dfd = dirfd(d);
130
131 fd = openat(dfd, "uevent", O_WRONLY);
132 if(fd >= 0) {
133 write(fd, "add\n", 4);
134 close(fd);
135 }
136
137 while((de = readdir(d))) {
138 DIR *d2;
139
140 if (de->d_name[0] == '.')
141 continue;
142
143 if (de->d_type != DT_DIR && lvl > 0)
144 continue;
145
146 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
147 if(fd < 0)
148 continue;
149
150 d2 = fdopendir(fd);
151 if(d2 == 0)
152 close(fd);
153 else {
154 do_coldboot(d2, lvl + 1);
155 closedir(d2);
156 }
157 }
158}
159
160static void coldboot(const char *path)
161{
162 DIR *d = opendir(path);
163 if(d) {
164 do_coldboot(d, 0);
165 closedir(d);
166 }
167}
168
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800169static int process_config(VolumeManager *vm)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 {
171 char hardware[PROPERTY_VALUE_MAX];
172 property_get("ro.hardware", hardware, "");
173 std::string fstab_filename(StringPrintf("/fstab.%s", hardware));
Ken Sumrall29d8da82011-05-18 17:20:07 -0700174
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175#ifdef DEBUG_FSTAB
176 if (access(DEBUG_FSTAB, R_OK) == 0) {
177 LOG(DEBUG) << "Found debug fstab; switching!";
178 fstab_filename = DEBUG_FSTAB;
179 }
180#endif
Ken Sumrall29d8da82011-05-18 17:20:07 -0700181
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700182 fstab = fs_mgr_read_fstab(fstab_filename.c_str());
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800183 if (!fstab) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184 PLOG(ERROR) << "Failed to open " << fstab_filename;
San Mehatf1b736b2009-10-10 17:22:08 -0700185 return -1;
186 }
187
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800188 /* Loop through entries looking for ones that vold manages */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700189 for (int i = 0; i < fstab->num_entries; i++) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800190 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800191 if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700192 LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
193 continue;
San Mehata2677e42009-12-13 10:40:18 -0800194 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195
196 std::string sysPattern(fstab->recs[i].blk_device);
197 std::string nickname(fstab->recs[i].label);
198 int flags = 0;
199
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800200 if (fs_mgr_is_encryptable(&fstab->recs[i])) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700201 flags |= android::vold::Disk::Flags::kAdoptable;
San Mehatf1b736b2009-10-10 17:22:08 -0700202 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700203 if (fs_mgr_is_noemulatedsd(&fstab->recs[i])) {
204 flags |= android::vold::Disk::Flags::kDefaultPrimary;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700205 }
Ken Sumrall29d8da82011-05-18 17:20:07 -0700206
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700207 vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
208 new VolumeManager::DiskSource(sysPattern, nickname, flags)));
San Mehatf1b736b2009-10-10 17:22:08 -0700209 }
210 }
211
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700212 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700213}