blob: 6af1e4eb675b7ca4f0465687c2961ba5a61a2275 [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"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070037#include "FwmarkServer.h"
San Mehatd1830422010-01-15 08:02:39 -080038
Selim Gurunc1044d42012-03-09 08:59:28 -080039static void blockSigpipe();
San Mehatd1830422010-01-15 08:02:39 -080040
41int main() {
42
43 CommandListener *cl;
44 NetlinkManager *nm;
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070045 DnsProxyListener *dpl;
Robert Greenwalt745e09f2012-03-29 14:45:54 -070046 MDnsSdListener *mdnsl;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070047 FwmarkServer* fwmarkServer;
San Mehatd1830422010-01-15 08:02:39 -080048
Steve Block08b58b62012-01-04 20:05:11 +000049 ALOGI("Netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080050
Selim Gurunc1044d42012-03-09 08:59:28 -080051 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080052
San Mehatd1830422010-01-15 08:02:39 -080053 if (!(nm = NetlinkManager::Instance())) {
Steve Block5ea0c052012-01-06 19:18:11 +000054 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080055 exit(1);
56 };
57
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050058 cl = new CommandListener();
San Mehatd1830422010-01-15 08:02:39 -080059 nm->setBroadcaster((SocketListener *) cl);
60
61 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000062 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080063 exit(1);
64 }
65
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070066 // Set local DNS mode, to prevent bionic from proxying
67 // back to this service, recursively.
68 setenv("ANDROID_DNS_MODE", "local", 1);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070069 dpl = new DnsProxyListener(CommandListener::sNetCtrl);
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070070 if (dpl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000071 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070072 exit(1);
73 }
74
Robert Greenwalt745e09f2012-03-29 14:45:54 -070075 mdnsl = new MDnsSdListener();
76 if (mdnsl->startListener()) {
77 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
78 exit(1);
79 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070080
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070081 fwmarkServer = new FwmarkServer(CommandListener::sNetCtrl);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070082 if (fwmarkServer->startListener()) {
83 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
84 exit(1);
85 }
86
San Mehatd1830422010-01-15 08:02:39 -080087 /*
88 * Now that we're up, we can respond to commands
89 */
90 if (cl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000091 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080092 exit(1);
93 }
94
95 // Eventually we'll become the monitoring thread
96 while(1) {
97 sleep(1000);
98 }
99
Steve Block08b58b62012-01-04 20:05:11 +0000100 ALOGI("Netd exiting");
San Mehatd1830422010-01-15 08:02:39 -0800101 exit(0);
102}
103
Selim Gurunc1044d42012-03-09 08:59:28 -0800104static void blockSigpipe()
105{
106 sigset_t mask;
107
108 sigemptyset(&mask);
109 sigaddset(&mask, SIGPIPE);
110 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
111 ALOGW("WARNING: SIGPIPE not blocked\n");
112}