blob: 49bb973ab162db1ae41bcf46e8099e35e406d093 [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"
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090048#include "Stopwatch.h"
San Mehatd1830422010-01-15 08:02:39 -080049
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090050using android::status_t;
51using android::sp;
52using android::IPCThreadState;
53using android::ProcessState;
54using android::defaultServiceManager;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090055using android::net::CommandListener;
56using android::net::DnsProxyListener;
57using android::net::FwmarkServer;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070058using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090059using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090060using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090061using android::net::NFLogListener;
62using android::net::makeNFLogListener;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090063
Robert Greenwalt347f6932014-10-31 18:54:06 -070064static void remove_pid_file();
65static bool write_pid_file();
66
67const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
68const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
69const mode_t PID_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
San Mehatd1830422010-01-15 08:02:39 -080070
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090071android::RWLock android::net::gBigNetdLock;
72
San Mehatd1830422010-01-15 08:02:39 -080073int main() {
Pierre Imai1cfa5432016-02-24 18:00:03 +090074 using android::net::gCtls;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090075 Stopwatch s;
San Mehatd1830422010-01-15 08:02:39 -080076
Steve Block08b58b62012-01-04 20:05:11 +000077 ALOGI("Netd 1.0 starting");
Robert Greenwalt347f6932014-10-31 18:54:06 -070078 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -080079
Selim Gurunc1044d42012-03-09 08:59:28 -080080 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
Pierre Imai1cfa5432016-02-24 18:00:03 +090092 NetlinkManager *nm = NetlinkManager::Instance();
93 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000094 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080095 exit(1);
96 };
97
Pierre Imai1cfa5432016-02-24 18:00:03 +090098 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090099 gCtls->init();
100
Pierre Imai1cfa5432016-02-24 18:00:03 +0900101 CommandListener cl;
102 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -0800103
104 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000105 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800106 exit(1);
107 }
108
Joel Scherpelz685deb52017-06-14 10:27:47 +0900109 std::unique_ptr<NFLogListener> logListener;
110 {
111 auto result = makeNFLogListener();
112 if (!isOk(result)) {
113 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
114 exit(1);
115 }
116 logListener = std::move(result.value());
117 auto status = gCtls->wakeupCtrl.init(logListener.get());
118 if (!isOk(result)) {
119 ALOGE("Unable to init WakeupController: %s", toString(result).c_str());
120 // We can still continue without wakeup packet logging.
121 }
122 }
123
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700124 // Set local DNS mode, to prevent bionic from proxying
125 // back to this service, recursively.
126 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +0100127 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900128 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000129 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700130 exit(1);
131 }
132
Pierre Imai1cfa5432016-02-24 18:00:03 +0900133 MDnsSdListener mdnsl;
134 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700135 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
136 exit(1);
137 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700138
Chenbo Feng9944ba82017-10-10 17:33:20 -0700139 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900140 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700141 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
142 exit(1);
143 }
144
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900145 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900146 status_t ret;
147 if ((ret = NetdNativeService::start()) != android::OK) {
148 ALOGE("Unable to start NetdNativeService: %d", ret);
149 exit(1);
150 }
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900151 ALOGI("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900152
San Mehatd1830422010-01-15 08:02:39 -0800153 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900154 * Now that we're up, we can respond to commands. Starting the listener also tells
155 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800156 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900157 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000158 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800159 exit(1);
160 }
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900161 ALOGI("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800162
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900163 write_pid_file();
Robert Greenwalt347f6932014-10-31 18:54:06 -0700164
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700165 // Now that netd is ready to process commands, advertise service
166 // availability for HAL clients.
167 NetdHwService mHwSvc;
168 if ((ret = mHwSvc.start()) != android::OK) {
169 ALOGE("Unable to start NetdHwService: %d", ret);
170 exit(1);
171 }
172 ALOGI("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
173
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900174 ALOGI("Netd started in %dms", static_cast<int>(s.timeTaken()));
175
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900176 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800177
Steve Block08b58b62012-01-04 20:05:11 +0000178 ALOGI("Netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900179
Robert Greenwalt347f6932014-10-31 18:54:06 -0700180 remove_pid_file();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900181
San Mehatd1830422010-01-15 08:02:39 -0800182 exit(0);
183}
184
Robert Greenwalt347f6932014-10-31 18:54:06 -0700185static bool write_pid_file() {
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +0900186 char pid_buf[INT32_STRLEN];
187 snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid());
Robert Greenwalt347f6932014-10-31 18:54:06 -0700188
189 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
190 if (fd == -1) {
191 ALOGE("Unable to create pid file (%s)", strerror(errno));
192 return false;
193 }
194
195 // File creation is affected by umask, so make sure the right mode bits are set.
196 if (fchmod(fd, PID_FILE_MODE) == -1) {
197 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
198 close(fd);
199 remove_pid_file();
200 return false;
201 }
202
203 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
204 ALOGE("Unable to write to pid file (%s)", strerror(errno));
205 close(fd);
206 remove_pid_file();
207 return false;
208 }
209 close(fd);
210 return true;
211}
212
213static void remove_pid_file() {
214 unlink(PID_FILE_PATH);
215}