blob: 11d4d40b7591b8a65363aab423e3a48a7cf12a16 [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);
Paul Jensen7dd69a12014-04-17 17:25:43 -040069 dpl = new DnsProxyListener(CommandListener::sNetCtrl, CommandListener::sPermissionsController);
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
81 fwmarkServer = new FwmarkServer(CommandListener::sNetCtrl,
82 CommandListener::sPermissionsController);
83 if (fwmarkServer->startListener()) {
84 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
85 exit(1);
86 }
87
San Mehatd1830422010-01-15 08:02:39 -080088 /*
89 * Now that we're up, we can respond to commands
90 */
91 if (cl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000092 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080093 exit(1);
94 }
95
96 // Eventually we'll become the monitoring thread
97 while(1) {
98 sleep(1000);
99 }
100
Steve Block08b58b62012-01-04 20:05:11 +0000101 ALOGI("Netd exiting");
San Mehatd1830422010-01-15 08:02:39 -0800102 exit(0);
103}
104
Selim Gurunc1044d42012-03-09 08:59:28 -0800105static void blockSigpipe()
106{
107 sigset_t mask;
108
109 sigemptyset(&mask);
110 sigaddset(&mask, SIGPIPE);
111 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
112 ALOGW("WARNING: SIGPIPE not blocked\n");
113}