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