blob: 40b549ea0b4568a850e0e7c9e8e4e05a6fa89c1b [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"
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"
45#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080046#include "NetlinkManager.h"
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090047#include "Stopwatch.h"
San Mehatd1830422010-01-15 08:02:39 -080048
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090049using android::status_t;
50using android::sp;
51using android::IPCThreadState;
52using android::ProcessState;
53using android::defaultServiceManager;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090054using android::net::CommandListener;
55using android::net::DnsProxyListener;
56using android::net::FwmarkServer;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090057using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090058using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090059using android::net::NFLogListener;
60using android::net::makeNFLogListener;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090061
Robert Greenwalt347f6932014-10-31 18:54:06 -070062static void remove_pid_file();
63static bool write_pid_file();
64
65const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
66const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
67const 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 -080068
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090069android::RWLock android::net::gBigNetdLock;
70
San Mehatd1830422010-01-15 08:02:39 -080071int main() {
Pierre Imai1cfa5432016-02-24 18:00:03 +090072 using android::net::gCtls;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090073 Stopwatch s;
San Mehatd1830422010-01-15 08:02:39 -080074
Steve Block08b58b62012-01-04 20:05:11 +000075 ALOGI("Netd 1.0 starting");
Robert Greenwalt347f6932014-10-31 18:54:06 -070076 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -080077
Selim Gurunc1044d42012-03-09 08:59:28 -080078 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
Pierre Imai1cfa5432016-02-24 18:00:03 +090090 NetlinkManager *nm = NetlinkManager::Instance();
91 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000092 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080093 exit(1);
94 };
95
Pierre Imai1cfa5432016-02-24 18:00:03 +090096 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090097 gCtls->init();
98
Pierre Imai1cfa5432016-02-24 18:00:03 +090099 CommandListener cl;
100 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -0800101
102 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000103 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800104 exit(1);
105 }
106
Joel Scherpelz519ffc32017-06-14 10:27:47 +0900107 std::unique_ptr<NFLogListener> logListener;
108 {
109 auto result = makeNFLogListener();
110 if (!isOk(result)) {
111 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
112 exit(1);
113 }
114 logListener = std::move(result.value());
115 auto status = gCtls->wakeupCtrl.init(logListener.get());
116 if (!isOk(result)) {
117 ALOGE("Unable to init WakeupController: %s", toString(result).c_str());
118 // We can still continue without wakeup packet logging.
119 }
120 }
121
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700122 // Set local DNS mode, to prevent bionic from proxying
123 // back to this service, recursively.
124 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +0100125 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900126 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000127 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700128 exit(1);
129 }
130
Pierre Imai1cfa5432016-02-24 18:00:03 +0900131 MDnsSdListener mdnsl;
132 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700133 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
134 exit(1);
135 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700136
Michal Karpinski7d374532016-10-06 19:33:55 +0100137 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900138 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700139 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
140 exit(1);
141 }
142
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900143 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900144 status_t ret;
145 if ((ret = NetdNativeService::start()) != android::OK) {
146 ALOGE("Unable to start NetdNativeService: %d", ret);
147 exit(1);
148 }
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900149 ALOGI("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900150
San Mehatd1830422010-01-15 08:02:39 -0800151 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900152 * Now that we're up, we can respond to commands. Starting the listener also tells
153 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800154 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900155 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000156 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800157 exit(1);
158 }
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900159 ALOGI("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800160
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900161 write_pid_file();
Robert Greenwalt347f6932014-10-31 18:54:06 -0700162
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900163 ALOGI("Netd started in %dms", static_cast<int>(s.timeTaken()));
164
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900165 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800166
Steve Block08b58b62012-01-04 20:05:11 +0000167 ALOGI("Netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900168
Robert Greenwalt347f6932014-10-31 18:54:06 -0700169 remove_pid_file();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900170
San Mehatd1830422010-01-15 08:02:39 -0800171 exit(0);
172}
173
Robert Greenwalt347f6932014-10-31 18:54:06 -0700174static bool write_pid_file() {
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +0900175 char pid_buf[INT32_STRLEN];
176 snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid());
Robert Greenwalt347f6932014-10-31 18:54:06 -0700177
178 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
179 if (fd == -1) {
180 ALOGE("Unable to create pid file (%s)", strerror(errno));
181 return false;
182 }
183
184 // File creation is affected by umask, so make sure the right mode bits are set.
185 if (fchmod(fd, PID_FILE_MODE) == -1) {
186 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
187 close(fd);
188 remove_pid_file();
189 return false;
190 }
191
192 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
193 ALOGE("Unable to write to pid file (%s)", strerror(errno));
194 close(fd);
195 remove_pid_file();
196 return false;
197 }
198 close(fd);
199 return true;
200}
201
202static void remove_pid_file() {
203 unlink(PID_FILE_PATH);
204}