blob: 0f5716b93a71f61284a41106103a6a9efbff2438 [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
Logan Chien3f461482018-04-23 14:31:32 +080031#include "log/log.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090032#include "utils/RWLock.h"
33
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 Colittie4d626e2016-02-02 17:19:04 +090051using android::status_t;
52using android::sp;
53using android::IPCThreadState;
54using android::ProcessState;
55using android::defaultServiceManager;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090056using android::net::CommandListener;
57using android::net::DnsProxyListener;
58using android::net::FwmarkServer;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070059using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090060using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090061using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090062using android::net::NFLogListener;
63using android::net::makeNFLogListener;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090064
Robert Greenwalt347f6932014-10-31 18:54:06 -070065const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
San Mehatd1830422010-01-15 08:02:39 -080066
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090067android::RWLock android::net::gBigNetdLock;
68
San Mehatd1830422010-01-15 08:02:39 -080069int main() {
Erik Klineb31fd692018-06-06 20:50:11 +090070 using android::net::gLog;
Pierre Imai1cfa5432016-02-24 18:00:03 +090071 using android::net::gCtls;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090072 Stopwatch s;
Erik Klineb31fd692018-06-06 20:50:11 +090073 gLog.info("netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080074
Erik Kline85890042018-05-25 19:19:11 +090075 android::net::process::removePidFile(PID_FILE_PATH);
76 android::net::process::blockSigPipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080077
Lorenzo Colitti548bbd42017-08-28 23:05:12 +090078 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
79 // FrameworkListener does this on initialization as well, but we only initialize these
80 // components after having initialized other subsystems that can fork.
81 for (const auto& sock : { CommandListener::SOCKET_NAME,
82 DnsProxyListener::SOCKET_NAME,
83 FwmarkServer::SOCKET_NAME,
84 MDnsSdListener::SOCKET_NAME }) {
85 setCloseOnExec(sock);
86 }
87
Pierre Imai1cfa5432016-02-24 18:00:03 +090088 NetlinkManager *nm = NetlinkManager::Instance();
89 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000090 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080091 exit(1);
92 };
93
Pierre Imai1cfa5432016-02-24 18:00:03 +090094 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090095 gCtls->init();
96
Pierre Imai1cfa5432016-02-24 18:00:03 +090097 CommandListener cl;
98 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -080099
100 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000101 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800102 exit(1);
103 }
104
Joel Scherpelz685deb52017-06-14 10:27:47 +0900105 std::unique_ptr<NFLogListener> logListener;
106 {
107 auto result = makeNFLogListener();
108 if (!isOk(result)) {
109 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
110 exit(1);
111 }
112 logListener = std::move(result.value());
113 auto status = gCtls->wakeupCtrl.init(logListener.get());
114 if (!isOk(result)) {
Erik Klineb31fd692018-06-06 20:50:11 +0900115 gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
Joel Scherpelz685deb52017-06-14 10:27:47 +0900116 // We can still continue without wakeup packet logging.
117 }
118 }
119
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700120 // Set local DNS mode, to prevent bionic from proxying
121 // back to this service, recursively.
122 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +0100123 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900124 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000125 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700126 exit(1);
127 }
128
Pierre Imai1cfa5432016-02-24 18:00:03 +0900129 MDnsSdListener mdnsl;
130 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700131 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
132 exit(1);
133 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700134
Chenbo Feng9944ba82017-10-10 17:33:20 -0700135 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900136 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700137 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
138 exit(1);
139 }
140
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900141 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900142 status_t ret;
143 if ((ret = NetdNativeService::start()) != android::OK) {
144 ALOGE("Unable to start NetdNativeService: %d", ret);
145 exit(1);
146 }
Erik Klineb31fd692018-06-06 20:50:11 +0900147 gLog.info("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900148
San Mehatd1830422010-01-15 08:02:39 -0800149 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900150 * Now that we're up, we can respond to commands. Starting the listener also tells
151 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800152 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900153 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000154 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800155 exit(1);
156 }
Erik Klineb31fd692018-06-06 20:50:11 +0900157 gLog.info("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800158
Erik Kline85890042018-05-25 19:19:11 +0900159 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
Robert Greenwalt347f6932014-10-31 18:54:06 -0700160
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700161 // Now that netd is ready to process commands, advertise service
162 // availability for HAL clients.
163 NetdHwService mHwSvc;
164 if ((ret = mHwSvc.start()) != android::OK) {
165 ALOGE("Unable to start NetdHwService: %d", ret);
166 exit(1);
167 }
Erik Klineb31fd692018-06-06 20:50:11 +0900168 gLog.info("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700169
Erik Klineb31fd692018-06-06 20:50:11 +0900170 gLog.info("Netd started in %dms", static_cast<int>(s.timeTaken()));
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900171
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900172 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800173
Erik Klineb31fd692018-06-06 20:50:11 +0900174 gLog.info("netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900175
San Mehatd1830422010-01-15 08:02:39 -0800176 exit(0);
177}