main.cpp revision f1b736bc5605e92e917ab27f5abf3ba839be2270
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
22#define LOG_TAG "Vold"
23
24#include "cutils/log.h"
25
26#include "VolumeManager.h"
27#include "CommandListener.h"
28#include "NetlinkManager.h"
29#include "DeviceVolume.h"
30
31static int process_config(VolumeManager *vm);
32
33int main() {
34
35    VolumeManager *vm;
36    CommandListener *cl;
37    NetlinkManager *nm;
38
39    LOGI("Vold 2.0 firing up");
40
41    /* Create our singleton managers */
42    if (!(vm = VolumeManager::Instance())) {
43        LOGE("Unable to create VolumeManager");
44        exit(1);
45    };
46
47    if (!(nm = NetlinkManager::Instance())) {
48        LOGE("Unable to create NetlinkManager");
49        exit(1);
50    };
51
52    cl = new CommandListener();
53    vm->setBroadcaster((SocketListener *) cl);
54    nm->setBroadcaster((SocketListener *) cl);
55
56    if (vm->start()) {
57        LOGE("Unable to start VolumeManager (%s)", strerror(errno));
58        exit(1);
59    }
60
61    if (process_config(vm)) {
62        LOGE("Error reading configuration (%s)", strerror(errno));
63        exit(1);
64    }
65
66    if (nm->start()) {
67        LOGE("Unable to start NetlinkManager (%s)", strerror(errno));
68        exit(1);
69    }
70
71    /*
72     * Now that we're up, we can respond to commands
73     */
74    if (cl->startListener()) {
75        LOGE("Unable to start CommandListener (%s)", strerror(errno));
76        exit(1);
77    }
78
79    // Eventually we'll become the monitoring thread
80    while(1) {
81        sleep(1000);
82    }
83
84    LOGI("Vold exiting");
85    exit(0);
86}
87
88static int process_config(VolumeManager *vm) {
89    FILE *fp;
90    int n = 0;
91    char line[255];
92
93    if (!(fp = fopen("/etc/vold.fstab", "r"))) {
94        return -1;
95    }
96
97    while(fgets(line, sizeof(line), fp)) {
98        char *next = line;
99        char *type, *label, *mount_point;
100
101        n++;
102        line[strlen(line)-1] = '\0';
103
104        if (line[0] == '#' || line[0] == '\0')
105            continue;
106
107        if (!(type = strsep(&next, " \t"))) {
108            LOGE("Error parsing type");
109            goto out_syntax;
110        }
111        if (!(label = strsep(&next, " \t"))) {
112            LOGE("Error parsing label");
113            goto out_syntax;
114        }
115        if (!(mount_point = strsep(&next, " \t"))) {
116            LOGE("Error parsing mount point");
117            goto out_syntax;
118        }
119
120        if (!strcmp(type, "dev_mount")) {
121            DeviceVolume *dv = NULL;
122            char *part, *sysfs_path;
123
124            if (!(part = strsep(&next, " \t"))) {
125                LOGE("Error parsing partition");
126                goto out_syntax;
127            }
128            if (strcmp(part, "auto") && atoi(part) == 0) {
129                LOGE("Partition must either be 'auto' or 1 based index instead of '%s'", part);
130                goto out_syntax;
131            }
132
133            dv = new DeviceVolume(label, mount_point, atoi(part));
134
135            while((sysfs_path = strsep(&next, " \t"))) {
136                if (dv->addPath(sysfs_path)) {
137                    LOGE("Failed to add devpath %s to volume %s", sysfs_path,
138                         label);
139                    goto out_fail;
140                }
141            }
142            vm->addVolume(dv);
143        } else if (!strcmp(type, "map_mount")) {
144        } else {
145            LOGE("Unknown type '%s'", type);
146            goto out_syntax;
147        }
148    }
149
150    fclose(fp);
151    return 0;
152
153out_syntax:
154    LOGE("Syntax error on config line %d", n);
155    errno = -EINVAL;
156out_fail:
157    fclose(fp);
158    return -1;
159}
160