main.cpp revision 99635f6c289fe2528c226403ea215c917ce86037
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 <stdio.h>
18#include <stdlib.h>
19#include <errno.h>
20#include <string.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <fcntl.h>
25#include <dirent.h>
26
27#define LOG_TAG "Vold"
28
29#include "cutils/log.h"
30
31#include "VolumeManager.h"
32#include "CommandListener.h"
33#include "NetlinkManager.h"
34#include "DirectVolume.h"
35
36static int process_config(VolumeManager *vm);
37static void coldboot(const char *path);
38
39int main() {
40
41    VolumeManager *vm;
42    CommandListener *cl;
43    NetlinkManager *nm;
44
45    SLOGI("Vold 2.1 (the revenge) firing up");
46
47    mkdir("/dev/block/vold", 0755);
48
49    /* Create our singleton managers */
50    if (!(vm = VolumeManager::Instance())) {
51        SLOGE("Unable to create VolumeManager");
52        exit(1);
53    };
54
55    if (!(nm = NetlinkManager::Instance())) {
56        SLOGE("Unable to create NetlinkManager");
57        exit(1);
58    };
59
60
61    cl = new CommandListener();
62    vm->setBroadcaster((SocketListener *) cl);
63    nm->setBroadcaster((SocketListener *) cl);
64
65    if (vm->start()) {
66        SLOGE("Unable to start VolumeManager (%s)", strerror(errno));
67        exit(1);
68    }
69
70    if (process_config(vm)) {
71        SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno));
72    }
73
74    if (nm->start()) {
75        SLOGE("Unable to start NetlinkManager (%s)", strerror(errno));
76        exit(1);
77    }
78
79    coldboot("/sys/block");
80//    coldboot("/sys/class/switch");
81
82    /*
83     * Now that we're up, we can respond to commands
84     */
85    if (cl->startListener()) {
86        SLOGE("Unable to start CommandListener (%s)", strerror(errno));
87        exit(1);
88    }
89
90    // Eventually we'll become the monitoring thread
91    while(1) {
92        sleep(1000);
93    }
94
95    SLOGI("Vold exiting");
96    exit(0);
97}
98
99static void do_coldboot(DIR *d, int lvl)
100{
101    struct dirent *de;
102    int dfd, fd;
103
104    dfd = dirfd(d);
105
106    fd = openat(dfd, "uevent", O_WRONLY);
107    if(fd >= 0) {
108        write(fd, "add\n", 4);
109        close(fd);
110    }
111
112    while((de = readdir(d))) {
113        DIR *d2;
114
115        if (de->d_name[0] == '.')
116            continue;
117
118        if (de->d_type != DT_DIR && lvl > 0)
119            continue;
120
121        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
122        if(fd < 0)
123            continue;
124
125        d2 = fdopendir(fd);
126        if(d2 == 0)
127            close(fd);
128        else {
129            do_coldboot(d2, lvl + 1);
130            closedir(d2);
131        }
132    }
133}
134
135static void coldboot(const char *path)
136{
137    DIR *d = opendir(path);
138    if(d) {
139        do_coldboot(d, 0);
140        closedir(d);
141    }
142}
143
144static int process_config(VolumeManager *vm) {
145    FILE *fp;
146    int n = 0;
147    char line[255];
148
149    if (!(fp = fopen("/etc/vold.fstab", "r"))) {
150        return -1;
151    }
152
153    while(fgets(line, sizeof(line), fp)) {
154        char *next = line;
155        char *type, *label, *mount_point;
156
157        n++;
158        line[strlen(line)-1] = '\0';
159
160        if (line[0] == '#' || line[0] == '\0')
161            continue;
162
163        if (!(type = strsep(&next, " \t"))) {
164            SLOGE("Error parsing type");
165            goto out_syntax;
166        }
167        if (!(label = strsep(&next, " \t"))) {
168            SLOGE("Error parsing label");
169            goto out_syntax;
170        }
171        if (!(mount_point = strsep(&next, " \t"))) {
172            SLOGE("Error parsing mount point");
173            goto out_syntax;
174        }
175
176        if (!strcmp(type, "dev_mount")) {
177            DirectVolume *dv = NULL;
178            char *part, *sysfs_path;
179
180            if (!(part = strsep(&next, " \t"))) {
181                SLOGE("Error parsing partition");
182                goto out_syntax;
183            }
184            if (strcmp(part, "auto") && atoi(part) == 0) {
185                SLOGE("Partition must either be 'auto' or 1 based index instead of '%s'", part);
186                goto out_syntax;
187            }
188
189            if (!strcmp(part, "auto")) {
190                dv = new DirectVolume(vm, label, mount_point, -1);
191            } else {
192                dv = new DirectVolume(vm, label, mount_point, atoi(part));
193            }
194
195            while((sysfs_path = strsep(&next, " \t"))) {
196                if (dv->addPath(sysfs_path)) {
197                    SLOGE("Failed to add devpath %s to volume %s", sysfs_path,
198                         label);
199                    goto out_fail;
200                }
201            }
202            vm->addVolume(dv);
203        } else if (!strcmp(type, "map_mount")) {
204        } else {
205            SLOGE("Unknown type '%s'", type);
206            goto out_syntax;
207        }
208    }
209
210    fclose(fp);
211    return 0;
212
213out_syntax:
214    SLOGE("Syntax error on config line %d", n);
215    errno = -EINVAL;
216out_fail:
217    fclose(fp);
218    return -1;
219}
220