1/*
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 "Disk.h"
18#include "VolumeManager.h"
19#include "CommandListener.h"
20#include "CryptCommandListener.h"
21#include "NetlinkManager.h"
22#include "cryptfs.h"
23#include "sehandle.h"
24
25#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
27#include <cutils/klog.h>
28#include <cutils/properties.h>
29#include <cutils/sockets.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <string.h>
35#include <sys/stat.h>
36#include <sys/types.h>
37#include <getopt.h>
38#include <fcntl.h>
39#include <dirent.h>
40#include <fs_mgr.h>
41
42static int process_config(VolumeManager *vm, bool* has_adoptable);
43static void coldboot(const char *path);
44static void parse_args(int argc, char** argv);
45
46struct fstab *fstab;
47
48struct selabel_handle *sehandle;
49
50using android::base::StringPrintf;
51
52int main(int argc, char** argv) {
53    setenv("ANDROID_LOG_TAGS", "*:v", 1);
54    android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
55
56    LOG(INFO) << "Vold 3.0 (the awakening) firing up";
57
58    LOG(VERBOSE) << "Detected support for:"
59            << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
60            << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
61            << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
62
63    VolumeManager *vm;
64    CommandListener *cl;
65    CryptCommandListener *ccl;
66    NetlinkManager *nm;
67
68    parse_args(argc, argv);
69
70    sehandle = selinux_android_file_context_handle();
71    if (sehandle) {
72        selinux_android_set_sehandle(sehandle);
73    }
74
75    // Quickly throw a CLOEXEC on the socket we just inherited from init
76    fcntl(android_get_control_socket("vold"), F_SETFD, FD_CLOEXEC);
77    fcntl(android_get_control_socket("cryptd"), F_SETFD, FD_CLOEXEC);
78
79    mkdir("/dev/block/vold", 0755);
80
81    /* For when cryptfs checks and mounts an encrypted filesystem */
82    klog_set_level(6);
83
84    /* Create our singleton managers */
85    if (!(vm = VolumeManager::Instance())) {
86        LOG(ERROR) << "Unable to create VolumeManager";
87        exit(1);
88    }
89
90    if (!(nm = NetlinkManager::Instance())) {
91        LOG(ERROR) << "Unable to create NetlinkManager";
92        exit(1);
93    }
94
95    if (property_get_bool("vold.debug", false)) {
96        vm->setDebug(true);
97    }
98
99    cl = new CommandListener();
100    ccl = new CryptCommandListener();
101    vm->setBroadcaster((SocketListener *) cl);
102    nm->setBroadcaster((SocketListener *) cl);
103
104    if (vm->start()) {
105        PLOG(ERROR) << "Unable to start VolumeManager";
106        exit(1);
107    }
108
109    bool has_adoptable;
110
111    if (process_config(vm, &has_adoptable)) {
112        PLOG(ERROR) << "Error reading configuration... continuing anyways";
113    }
114
115    if (nm->start()) {
116        PLOG(ERROR) << "Unable to start NetlinkManager";
117        exit(1);
118    }
119
120    /*
121     * Now that we're up, we can respond to commands
122     */
123    if (cl->startListener()) {
124        PLOG(ERROR) << "Unable to start CommandListener";
125        exit(1);
126    }
127
128    if (ccl->startListener()) {
129        PLOG(ERROR) << "Unable to start CryptCommandListener";
130        exit(1);
131    }
132
133    // This call should go after listeners are started to avoid
134    // a deadlock between vold and init (see b/34278978 for details)
135    property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
136
137    // Do coldboot here so it won't block booting,
138    // also the cold boot is needed in case we have flash drive
139    // connected before Vold launched
140    coldboot("/sys/block");
141    // Eventually we'll become the monitoring thread
142    while(1) {
143        pause();
144    }
145
146    LOG(ERROR) << "Vold exiting";
147    exit(0);
148}
149
150static void parse_args(int argc, char** argv) {
151    static struct option opts[] = {
152        {"blkid_context", required_argument, 0, 'b' },
153        {"blkid_untrusted_context", required_argument, 0, 'B' },
154        {"fsck_context", required_argument, 0, 'f' },
155        {"fsck_untrusted_context", required_argument, 0, 'F' },
156    };
157
158    int c;
159    while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
160        switch (c) {
161        case 'b': android::vold::sBlkidContext = optarg; break;
162        case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
163        case 'f': android::vold::sFsckContext = optarg; break;
164        case 'F': android::vold::sFsckUntrustedContext = optarg; break;
165        }
166    }
167
168    CHECK(android::vold::sBlkidContext != nullptr);
169    CHECK(android::vold::sBlkidUntrustedContext != nullptr);
170    CHECK(android::vold::sFsckContext != nullptr);
171    CHECK(android::vold::sFsckUntrustedContext != nullptr);
172}
173
174static void do_coldboot(DIR *d, int lvl) {
175    struct dirent *de;
176    int dfd, fd;
177
178    dfd = dirfd(d);
179
180    fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
181    if(fd >= 0) {
182        write(fd, "add\n", 4);
183        close(fd);
184    }
185
186    while((de = readdir(d))) {
187        DIR *d2;
188
189        if (de->d_name[0] == '.')
190            continue;
191
192        if (de->d_type != DT_DIR && lvl > 0)
193            continue;
194
195        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
196        if(fd < 0)
197            continue;
198
199        d2 = fdopendir(fd);
200        if(d2 == 0)
201            close(fd);
202        else {
203            do_coldboot(d2, lvl + 1);
204            closedir(d2);
205        }
206    }
207}
208
209static void coldboot(const char *path) {
210    DIR *d = opendir(path);
211    if(d) {
212        do_coldboot(d, 0);
213        closedir(d);
214    }
215}
216
217static int process_config(VolumeManager *vm, bool* has_adoptable) {
218    fstab = fs_mgr_read_fstab_default();
219    if (!fstab) {
220        PLOG(ERROR) << "Failed to open default fstab";
221        return -1;
222    }
223
224    /* Loop through entries looking for ones that vold manages */
225    *has_adoptable = false;
226    for (int i = 0; i < fstab->num_entries; i++) {
227        if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
228            if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
229                LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
230                continue;
231            }
232
233            std::string sysPattern(fstab->recs[i].blk_device);
234            std::string nickname(fstab->recs[i].label);
235            int flags = 0;
236
237            if (fs_mgr_is_encryptable(&fstab->recs[i])) {
238                flags |= android::vold::Disk::Flags::kAdoptable;
239                *has_adoptable = true;
240            }
241            if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
242                    || property_get_bool("vold.debug.default_primary", false)) {
243                flags |= android::vold::Disk::Flags::kDefaultPrimary;
244            }
245
246            vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
247                    new VolumeManager::DiskSource(sysPattern, nickname, flags)));
248        }
249    }
250    return 0;
251}
252