blob: 104ebe1327e6cc07200b9bc563d7485bc7ae6148 [file] [log] [blame]
San Mehatd1830422010-01-15 08:02:39 -08001/*
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>
San Mehat5c1b8af2010-01-21 15:37:10 -080019#include <signal.h>
San Mehatd1830422010-01-15 08:02:39 -080020#include <errno.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/types.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080024#include <sys/wait.h>
San Mehatd1830422010-01-15 08:02:39 -080025
26#include <fcntl.h>
27#include <dirent.h>
28
29#define LOG_TAG "Netd"
30
31#include "cutils/log.h"
32
33#include "CommandListener.h"
34#include "NetlinkManager.h"
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070035#include "DnsProxyListener.h"
Robert Greenwalt745e09f2012-03-29 14:45:54 -070036#include "MDnsSdListener.h"
Chad Brubakerd2617932013-06-21 15:26:35 -070037#include "UidMarkMap.h"
San Mehatd1830422010-01-15 08:02:39 -080038
39static void coldboot(const char *path);
San Mehat5c1b8af2010-01-21 15:37:10 -080040static void sigchld_handler(int sig);
Selim Gurunc1044d42012-03-09 08:59:28 -080041static void blockSigpipe();
San Mehatd1830422010-01-15 08:02:39 -080042
43int main() {
44
45 CommandListener *cl;
46 NetlinkManager *nm;
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070047 DnsProxyListener *dpl;
Robert Greenwalt745e09f2012-03-29 14:45:54 -070048 MDnsSdListener *mdnsl;
San Mehatd1830422010-01-15 08:02:39 -080049
Steve Block08b58b62012-01-04 20:05:11 +000050 ALOGI("Netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080051
San Mehatf1c368a2010-01-27 17:13:32 -080052// signal(SIGCHLD, sigchld_handler);
Selim Gurunc1044d42012-03-09 08:59:28 -080053 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080054
San Mehatd1830422010-01-15 08:02:39 -080055 if (!(nm = NetlinkManager::Instance())) {
Steve Block5ea0c052012-01-06 19:18:11 +000056 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080057 exit(1);
58 };
59
Chad Brubakerd2617932013-06-21 15:26:35 -070060 UidMarkMap *rangeMap = new UidMarkMap();
San Mehatd1830422010-01-15 08:02:39 -080061
Chad Brubakerd2617932013-06-21 15:26:35 -070062 cl = new CommandListener(rangeMap);
San Mehatd1830422010-01-15 08:02:39 -080063 nm->setBroadcaster((SocketListener *) cl);
64
65 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000066 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080067 exit(1);
68 }
69
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070070 // Set local DNS mode, to prevent bionic from proxying
71 // back to this service, recursively.
72 setenv("ANDROID_DNS_MODE", "local", 1);
Chad Brubakerd2617932013-06-21 15:26:35 -070073 dpl = new DnsProxyListener(rangeMap);
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070074 if (dpl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000075 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070076 exit(1);
77 }
78
Robert Greenwalt745e09f2012-03-29 14:45:54 -070079 mdnsl = new MDnsSdListener();
80 if (mdnsl->startListener()) {
81 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
82 exit(1);
83 }
San Mehatd1830422010-01-15 08:02:39 -080084 /*
85 * Now that we're up, we can respond to commands
86 */
87 if (cl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000088 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080089 exit(1);
90 }
91
92 // Eventually we'll become the monitoring thread
93 while(1) {
94 sleep(1000);
95 }
96
Steve Block08b58b62012-01-04 20:05:11 +000097 ALOGI("Netd exiting");
San Mehatd1830422010-01-15 08:02:39 -080098 exit(0);
99}
100
101static void do_coldboot(DIR *d, int lvl)
102{
103 struct dirent *de;
104 int dfd, fd;
105
106 dfd = dirfd(d);
107
108 fd = openat(dfd, "uevent", O_WRONLY);
109 if(fd >= 0) {
110 write(fd, "add\n", 4);
111 close(fd);
112 }
113
114 while((de = readdir(d))) {
115 DIR *d2;
116
117 if (de->d_name[0] == '.')
118 continue;
119
120 if (de->d_type != DT_DIR && lvl > 0)
121 continue;
122
123 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
124 if(fd < 0)
125 continue;
126
127 d2 = fdopendir(fd);
128 if(d2 == 0)
129 close(fd);
130 else {
131 do_coldboot(d2, lvl + 1);
132 closedir(d2);
133 }
134 }
135}
136
137static void coldboot(const char *path)
138{
139 DIR *d = opendir(path);
140 if(d) {
141 do_coldboot(d, 0);
142 closedir(d);
143 }
144}
San Mehat5c1b8af2010-01-21 15:37:10 -0800145
146static void sigchld_handler(int sig) {
147 pid_t pid = wait(NULL);
Steve Block7b984e32011-12-20 16:22:42 +0000148 ALOGD("Child process %d exited", pid);
San Mehat5c1b8af2010-01-21 15:37:10 -0800149}
Selim Gurunc1044d42012-03-09 08:59:28 -0800150
151static void blockSigpipe()
152{
153 sigset_t mask;
154
155 sigemptyset(&mask);
156 sigaddset(&mask, SIGPIPE);
157 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
158 ALOGW("WARNING: SIGPIPE not blocked\n");
159}