blob: 9215f219e8a01724c36c8cd152c3ae2af45a6dba [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
Pierre Imai1cfa5432016-02-24 18:00:03 +090038#include "Controllers.h"
San Mehatd1830422010-01-15 08:02:39 -080039#include "CommandListener.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090040#include "NetdConstants.h"
41#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080042#include "NetlinkManager.h"
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090043#include "Stopwatch.h"
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070044#include "DnsProxyListener.h"
Robert Greenwalt745e09f2012-03-29 14:45:54 -070045#include "MDnsSdListener.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070046#include "FwmarkServer.h"
San Mehatd1830422010-01-15 08:02:39 -080047
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090048using android::status_t;
49using android::sp;
50using android::IPCThreadState;
51using android::ProcessState;
52using android::defaultServiceManager;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090053using android::net::CommandListener;
54using android::net::DnsProxyListener;
55using android::net::FwmarkServer;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090056using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090057using android::net::NetlinkManager;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090058
Robert Greenwalt347f6932014-10-31 18:54:06 -070059static void remove_pid_file();
60static bool write_pid_file();
61
62const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
63const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
64const 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 -080065
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090066android::RWLock android::net::gBigNetdLock;
67
San Mehatd1830422010-01-15 08:02:39 -080068int main() {
Pierre Imai1cfa5432016-02-24 18:00:03 +090069 using android::net::gCtls;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090070 Stopwatch s;
San Mehatd1830422010-01-15 08:02:39 -080071
Steve Block08b58b62012-01-04 20:05:11 +000072 ALOGI("Netd 1.0 starting");
Robert Greenwalt347f6932014-10-31 18:54:06 -070073 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -080074
Selim Gurunc1044d42012-03-09 08:59:28 -080075 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080076
Pierre Imai1cfa5432016-02-24 18:00:03 +090077 NetlinkManager *nm = NetlinkManager::Instance();
78 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000079 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080080 exit(1);
81 };
82
Pierre Imai1cfa5432016-02-24 18:00:03 +090083 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090084 gCtls->init();
85
Pierre Imai1cfa5432016-02-24 18:00:03 +090086 CommandListener cl;
87 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -080088
89 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000090 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080091 exit(1);
92 }
93
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070094 // Set local DNS mode, to prevent bionic from proxying
95 // back to this service, recursively.
96 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +010097 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +090098 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000099 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700100 exit(1);
101 }
102
Pierre Imai1cfa5432016-02-24 18:00:03 +0900103 MDnsSdListener mdnsl;
104 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700105 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
106 exit(1);
107 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700108
Michal Karpinski7d374532016-10-06 19:33:55 +0100109 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900110 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700111 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
112 exit(1);
113 }
114
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900115 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900116 status_t ret;
117 if ((ret = NetdNativeService::start()) != android::OK) {
118 ALOGE("Unable to start NetdNativeService: %d", ret);
119 exit(1);
120 }
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900121 ALOGI("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900122
San Mehatd1830422010-01-15 08:02:39 -0800123 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900124 * Now that we're up, we can respond to commands. Starting the listener also tells
125 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800126 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900127 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000128 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800129 exit(1);
130 }
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900131 ALOGI("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800132
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900133 write_pid_file();
Robert Greenwalt347f6932014-10-31 18:54:06 -0700134
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900135 ALOGI("Netd started in %dms", static_cast<int>(s.timeTaken()));
136
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900137 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800138
Steve Block08b58b62012-01-04 20:05:11 +0000139 ALOGI("Netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900140
Robert Greenwalt347f6932014-10-31 18:54:06 -0700141 remove_pid_file();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900142
San Mehatd1830422010-01-15 08:02:39 -0800143 exit(0);
144}
145
Robert Greenwalt347f6932014-10-31 18:54:06 -0700146static bool write_pid_file() {
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +0900147 char pid_buf[INT32_STRLEN];
148 snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid());
Robert Greenwalt347f6932014-10-31 18:54:06 -0700149
150 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
151 if (fd == -1) {
152 ALOGE("Unable to create pid file (%s)", strerror(errno));
153 return false;
154 }
155
156 // File creation is affected by umask, so make sure the right mode bits are set.
157 if (fchmod(fd, PID_FILE_MODE) == -1) {
158 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
159 close(fd);
160 remove_pid_file();
161 return false;
162 }
163
164 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
165 ALOGE("Unable to write to pid file (%s)", strerror(errno));
166 close(fd);
167 remove_pid_file();
168 return false;
169 }
170 close(fd);
171 return true;
172}
173
174static void remove_pid_file() {
175 unlink(PID_FILE_PATH);
176}