blob: b9ff33f0e90730941c51b516ac0b85a3d37083e6 [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>
Luke Huangd1ee4622018-06-29 13:49:58 +080022#include <mutex>
San Mehatd1830422010-01-15 08:02:39 -080023#include <sys/stat.h>
24#include <sys/types.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080025#include <sys/wait.h>
San Mehatd1830422010-01-15 08:02:39 -080026
27#include <fcntl.h>
28#include <dirent.h>
29
30#define LOG_TAG "Netd"
31
Logan Chien3f461482018-04-23 14:31:32 +080032#include "log/log.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090033
34#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
36#include <binder/ProcessState.h>
San Mehatd1830422010-01-15 08:02:39 -080037
38#include "CommandListener.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090039#include "Controllers.h"
40#include "DnsProxyListener.h"
41#include "FwmarkServer.h"
42#include "MDnsSdListener.h"
43#include "NFLogListener.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090044#include "NetdConstants.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070045#include "NetdHwService.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090046#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080047#include "NetlinkManager.h"
Erik Kline85890042018-05-25 19:19:11 +090048#include "Process.h"
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090049#include "Stopwatch.h"
San Mehatd1830422010-01-15 08:02:39 -080050
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090051#include "netd_resolv/resolv_stub.h"
52
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090053using android::status_t;
54using android::sp;
55using android::IPCThreadState;
56using android::ProcessState;
57using android::defaultServiceManager;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090058using android::net::CommandListener;
59using android::net::DnsProxyListener;
60using android::net::FwmarkServer;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070061using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090062using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090063using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090064using android::net::NFLogListener;
65using android::net::makeNFLogListener;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090066
Robert Greenwalt347f6932014-10-31 18:54:06 -070067const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
San Mehatd1830422010-01-15 08:02:39 -080068
Luke Huangd1ee4622018-06-29 13:49:58 +080069std::mutex android::net::gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090070
San Mehatd1830422010-01-15 08:02:39 -080071int main() {
Erik Klineb31fd692018-06-06 20:50:11 +090072 using android::net::gLog;
Pierre Imai1cfa5432016-02-24 18:00:03 +090073 using android::net::gCtls;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090074 Stopwatch s;
Erik Klineb31fd692018-06-06 20:50:11 +090075 gLog.info("netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080076
Erik Kline85890042018-05-25 19:19:11 +090077 android::net::process::removePidFile(PID_FILE_PATH);
78 android::net::process::blockSigPipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080079
Lorenzo Colitti548bbd42017-08-28 23:05:12 +090080 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
81 // FrameworkListener does this on initialization as well, but we only initialize these
82 // components after having initialized other subsystems that can fork.
83 for (const auto& sock : { CommandListener::SOCKET_NAME,
84 DnsProxyListener::SOCKET_NAME,
85 FwmarkServer::SOCKET_NAME,
86 MDnsSdListener::SOCKET_NAME }) {
87 setCloseOnExec(sock);
88 }
89
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090090 // Before we start any threads, populate the resolver stub pointers.
91 resolv_stub_init();
92
Pierre Imai1cfa5432016-02-24 18:00:03 +090093 NetlinkManager *nm = NetlinkManager::Instance();
94 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000095 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080096 exit(1);
97 };
98
Pierre Imai1cfa5432016-02-24 18:00:03 +090099 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900100 gCtls->init();
101
Pierre Imai1cfa5432016-02-24 18:00:03 +0900102 CommandListener cl;
103 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -0800104
105 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000106 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800107 exit(1);
108 }
109
Joel Scherpelz685deb52017-06-14 10:27:47 +0900110 std::unique_ptr<NFLogListener> logListener;
111 {
112 auto result = makeNFLogListener();
113 if (!isOk(result)) {
114 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
115 exit(1);
116 }
117 logListener = std::move(result.value());
118 auto status = gCtls->wakeupCtrl.init(logListener.get());
119 if (!isOk(result)) {
Erik Klineb31fd692018-06-06 20:50:11 +0900120 gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
Joel Scherpelz685deb52017-06-14 10:27:47 +0900121 // We can still continue without wakeup packet logging.
122 }
123 }
124
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700125 // Set local DNS mode, to prevent bionic from proxying
126 // back to this service, recursively.
127 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +0100128 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900129 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000130 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700131 exit(1);
132 }
133
Pierre Imai1cfa5432016-02-24 18:00:03 +0900134 MDnsSdListener mdnsl;
135 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700136 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
137 exit(1);
138 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700139
Chenbo Feng9944ba82017-10-10 17:33:20 -0700140 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900141 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700142 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
143 exit(1);
144 }
145
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900146 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900147 status_t ret;
148 if ((ret = NetdNativeService::start()) != android::OK) {
149 ALOGE("Unable to start NetdNativeService: %d", ret);
150 exit(1);
151 }
Erik Klineb31fd692018-06-06 20:50:11 +0900152 gLog.info("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900153
San Mehatd1830422010-01-15 08:02:39 -0800154 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900155 * Now that we're up, we can respond to commands. Starting the listener also tells
156 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800157 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900158 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000159 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800160 exit(1);
161 }
Erik Klineb31fd692018-06-06 20:50:11 +0900162 gLog.info("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800163
Erik Kline85890042018-05-25 19:19:11 +0900164 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
Robert Greenwalt347f6932014-10-31 18:54:06 -0700165
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700166 // Now that netd is ready to process commands, advertise service
167 // availability for HAL clients.
168 NetdHwService mHwSvc;
169 if ((ret = mHwSvc.start()) != android::OK) {
170 ALOGE("Unable to start NetdHwService: %d", ret);
171 exit(1);
172 }
Erik Klineb31fd692018-06-06 20:50:11 +0900173 gLog.info("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700174
Erik Klineb31fd692018-06-06 20:50:11 +0900175 gLog.info("Netd started in %dms", static_cast<int>(s.timeTaken()));
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900176
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900177 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800178
Erik Klineb31fd692018-06-06 20:50:11 +0900179 gLog.info("netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900180
San Mehatd1830422010-01-15 08:02:39 -0800181 exit(0);
182}