blob: 4b06ada1f86d2d1099de9a8f4dd0229e85fb6e10 [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
Joel Fernandesa03aced2019-01-10 11:24:11 -050017#include <chrono>
San Mehatd1830422010-01-15 08:02:39 -080018#include <stdio.h>
19#include <stdlib.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080020#include <signal.h>
San Mehatd1830422010-01-15 08:02:39 -080021#include <errno.h>
22#include <string.h>
Luke Huangd1ee4622018-06-29 13:49:58 +080023#include <mutex>
San Mehatd1830422010-01-15 08:02:39 -080024#include <sys/stat.h>
25#include <sys/types.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080026#include <sys/wait.h>
San Mehatd1830422010-01-15 08:02:39 -080027
28#include <fcntl.h>
29#include <dirent.h>
30
31#define LOG_TAG "Netd"
32
Logan Chien3f461482018-04-23 14:31:32 +080033#include "log/log.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090034
Joel Fernandesa03aced2019-01-10 11:24:11 -050035#include <android-base/properties.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090036#include <binder/IPCThreadState.h>
37#include <binder/IServiceManager.h>
38#include <binder/ProcessState.h>
San Mehatd1830422010-01-15 08:02:39 -080039
40#include "CommandListener.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090041#include "Controllers.h"
42#include "DnsProxyListener.h"
43#include "FwmarkServer.h"
44#include "MDnsSdListener.h"
45#include "NFLogListener.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090046#include "NetdConstants.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070047#include "NetdHwService.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090048#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080049#include "NetlinkManager.h"
Erik Kline85890042018-05-25 19:19:11 +090050#include "Process.h"
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090051#include "Stopwatch.h"
San Mehatd1830422010-01-15 08:02:39 -080052
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090053#include "netd_resolv/resolv_stub.h"
54
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090055using android::status_t;
56using android::sp;
57using android::IPCThreadState;
58using android::ProcessState;
59using android::defaultServiceManager;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090060using android::net::CommandListener;
61using android::net::DnsProxyListener;
62using android::net::FwmarkServer;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070063using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090064using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090065using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090066using android::net::NFLogListener;
67using android::net::makeNFLogListener;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090068
Robert Greenwalt347f6932014-10-31 18:54:06 -070069const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
San Mehatd1830422010-01-15 08:02:39 -080070
Luke Huangd1ee4622018-06-29 13:49:58 +080071std::mutex android::net::gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090072
San Mehatd1830422010-01-15 08:02:39 -080073int main() {
Erik Klineb31fd692018-06-06 20:50:11 +090074 using android::net::gLog;
Pierre Imai1cfa5432016-02-24 18:00:03 +090075 using android::net::gCtls;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090076 Stopwatch s;
Erik Klineb31fd692018-06-06 20:50:11 +090077 gLog.info("netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080078
Erik Kline85890042018-05-25 19:19:11 +090079 android::net::process::removePidFile(PID_FILE_PATH);
80 android::net::process::blockSigPipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080081
Lorenzo Colitti548bbd42017-08-28 23:05:12 +090082 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
83 // FrameworkListener does this on initialization as well, but we only initialize these
84 // components after having initialized other subsystems that can fork.
85 for (const auto& sock : { CommandListener::SOCKET_NAME,
86 DnsProxyListener::SOCKET_NAME,
87 FwmarkServer::SOCKET_NAME,
88 MDnsSdListener::SOCKET_NAME }) {
89 setCloseOnExec(sock);
90 }
91
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090092 // Before we start any threads, populate the resolver stub pointers.
93 resolv_stub_init();
94
Joel Fernandesa03aced2019-01-10 11:24:11 -050095 // Make sure BPF programs are loaded before doing anything
96 while (!android::base::WaitForProperty("bpf.progs_loaded", "1",
97 std::chrono::seconds(5))) {
98 ALOGD("netd waited 5s for bpf.progs_loaded, still waiting...");
99 }
100
Pierre Imai1cfa5432016-02-24 18:00:03 +0900101 NetlinkManager *nm = NetlinkManager::Instance();
102 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +0000103 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -0800104 exit(1);
105 };
106
Pierre Imai1cfa5432016-02-24 18:00:03 +0900107 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900108 gCtls->init();
109
Pierre Imai1cfa5432016-02-24 18:00:03 +0900110 CommandListener cl;
111 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -0800112
113 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000114 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800115 exit(1);
116 }
117
Joel Scherpelz685deb52017-06-14 10:27:47 +0900118 std::unique_ptr<NFLogListener> logListener;
119 {
120 auto result = makeNFLogListener();
121 if (!isOk(result)) {
122 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
123 exit(1);
124 }
125 logListener = std::move(result.value());
126 auto status = gCtls->wakeupCtrl.init(logListener.get());
127 if (!isOk(result)) {
Erik Klineb31fd692018-06-06 20:50:11 +0900128 gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
Joel Scherpelz685deb52017-06-14 10:27:47 +0900129 // We can still continue without wakeup packet logging.
130 }
131 }
132
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700133 // Set local DNS mode, to prevent bionic from proxying
134 // back to this service, recursively.
135 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +0100136 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900137 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000138 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700139 exit(1);
140 }
141
Pierre Imai1cfa5432016-02-24 18:00:03 +0900142 MDnsSdListener mdnsl;
143 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700144 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
145 exit(1);
146 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700147
Chenbo Feng9944ba82017-10-10 17:33:20 -0700148 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900149 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700150 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
151 exit(1);
152 }
153
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900154 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900155 status_t ret;
156 if ((ret = NetdNativeService::start()) != android::OK) {
157 ALOGE("Unable to start NetdNativeService: %d", ret);
158 exit(1);
159 }
Erik Klineb31fd692018-06-06 20:50:11 +0900160 gLog.info("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900161
San Mehatd1830422010-01-15 08:02:39 -0800162 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900163 * Now that we're up, we can respond to commands. Starting the listener also tells
164 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800165 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900166 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000167 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800168 exit(1);
169 }
Erik Klineb31fd692018-06-06 20:50:11 +0900170 gLog.info("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800171
Erik Kline85890042018-05-25 19:19:11 +0900172 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
Robert Greenwalt347f6932014-10-31 18:54:06 -0700173
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700174 // Now that netd is ready to process commands, advertise service
175 // availability for HAL clients.
176 NetdHwService mHwSvc;
177 if ((ret = mHwSvc.start()) != android::OK) {
178 ALOGE("Unable to start NetdHwService: %d", ret);
179 exit(1);
180 }
Erik Klineb31fd692018-06-06 20:50:11 +0900181 gLog.info("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700182
Erik Klineb31fd692018-06-06 20:50:11 +0900183 gLog.info("Netd started in %dms", static_cast<int>(s.timeTaken()));
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900184
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900185 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800186
Erik Klineb31fd692018-06-06 20:50:11 +0900187 gLog.info("netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900188
San Mehatd1830422010-01-15 08:02:39 -0800189 exit(0);
190}