main.cpp revision d18304287dbabc7835be771400b85d4ae8b63de6
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 "Netd"
28
29#include "cutils/log.h"
30
31#include "CommandListener.h"
32#include "NetlinkManager.h"
33
34static void coldboot(const char *path);
35
36int main() {
37
38    CommandListener *cl;
39    NetlinkManager *nm;
40
41    LOGI("Netd 1.0 starting");
42
43    if (!(nm = NetlinkManager::Instance())) {
44        LOGE("Unable to create NetlinkManager");
45        exit(1);
46    };
47
48
49    cl = new CommandListener();
50    nm->setBroadcaster((SocketListener *) cl);
51
52    if (nm->start()) {
53        LOGE("Unable to start NetlinkManager (%s)", strerror(errno));
54        exit(1);
55    }
56
57    /*
58     * Now that we're up, we can respond to commands
59     */
60    if (cl->startListener()) {
61        LOGE("Unable to start CommandListener (%s)", strerror(errno));
62        exit(1);
63    }
64
65    // Eventually we'll become the monitoring thread
66    while(1) {
67        sleep(1000);
68    }
69
70    LOGI("Netd exiting");
71    exit(0);
72}
73
74static void do_coldboot(DIR *d, int lvl)
75{
76    struct dirent *de;
77    int dfd, fd;
78
79    dfd = dirfd(d);
80
81    fd = openat(dfd, "uevent", O_WRONLY);
82    if(fd >= 0) {
83        write(fd, "add\n", 4);
84        close(fd);
85    }
86
87    while((de = readdir(d))) {
88        DIR *d2;
89
90        if (de->d_name[0] == '.')
91            continue;
92
93        if (de->d_type != DT_DIR && lvl > 0)
94            continue;
95
96        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
97        if(fd < 0)
98            continue;
99
100        d2 = fdopendir(fd);
101        if(d2 == 0)
102            close(fd);
103        else {
104            do_coldboot(d2, lvl + 1);
105            closedir(d2);
106        }
107    }
108}
109
110static void coldboot(const char *path)
111{
112    DIR *d = opendir(path);
113    if(d) {
114        do_coldboot(d, 0);
115        closedir(d);
116    }
117}
118