blob: 7b15a7687d4793a388d23a6d359ccc04a2fec71e [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>
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090021#include <math.h>
San Mehatd1830422010-01-15 08:02:39 -080022#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080025#include <sys/wait.h>
San Mehatd1830422010-01-15 08:02:39 -080026
27#include <fcntl.h>
28#include <dirent.h>
29
30#define LOG_TAG "Netd"
31
32#include "cutils/log.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090033#include "utils/RWLock.h"
34
35#include <binder/IPCThreadState.h>
36#include <binder/IServiceManager.h>
37#include <binder/ProcessState.h>
San Mehatd1830422010-01-15 08:02:39 -080038
Pierre Imai1cfa5432016-02-24 18:00:03 +090039#include "Controllers.h"
San Mehatd1830422010-01-15 08:02:39 -080040#include "CommandListener.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090041#include "NetdConstants.h"
42#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080043#include "NetlinkManager.h"
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090044#include "Stopwatch.h"
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070045#include "DnsProxyListener.h"
Robert Greenwalt745e09f2012-03-29 14:45:54 -070046#include "MDnsSdListener.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070047#include "FwmarkServer.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;
54using android::net::NetdNativeService;
55
Selim Gurunc1044d42012-03-09 08:59:28 -080056static void blockSigpipe();
Robert Greenwalt347f6932014-10-31 18:54:06 -070057static void remove_pid_file();
58static bool write_pid_file();
59
60const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
61const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
62const 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 -080063
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090064android::RWLock android::net::gBigNetdLock;
65
San Mehatd1830422010-01-15 08:02:39 -080066int main() {
Pierre Imai1cfa5432016-02-24 18:00:03 +090067 using android::net::gCtls;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090068 Stopwatch s;
San Mehatd1830422010-01-15 08:02:39 -080069
Steve Block08b58b62012-01-04 20:05:11 +000070 ALOGI("Netd 1.0 starting");
Robert Greenwalt347f6932014-10-31 18:54:06 -070071 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -080072
Selim Gurunc1044d42012-03-09 08:59:28 -080073 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080074
Pierre Imai1cfa5432016-02-24 18:00:03 +090075 NetlinkManager *nm = NetlinkManager::Instance();
76 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000077 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080078 exit(1);
79 };
80
Pierre Imai1cfa5432016-02-24 18:00:03 +090081 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +090082 gCtls->init();
83
Pierre Imai1cfa5432016-02-24 18:00:03 +090084 CommandListener cl;
85 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -080086
87 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000088 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080089 exit(1);
90 }
91
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070092 // Set local DNS mode, to prevent bionic from proxying
93 // back to this service, recursively.
94 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +010095 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +090096 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000097 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070098 exit(1);
99 }
100
Pierre Imai1cfa5432016-02-24 18:00:03 +0900101 MDnsSdListener mdnsl;
102 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700103 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
104 exit(1);
105 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700106
Michal Karpinski7d374532016-10-06 19:33:55 +0100107 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900108 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700109 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
110 exit(1);
111 }
112
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900113 status_t ret;
114 if ((ret = NetdNativeService::start()) != android::OK) {
115 ALOGE("Unable to start NetdNativeService: %d", ret);
116 exit(1);
117 }
118
San Mehatd1830422010-01-15 08:02:39 -0800119 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900120 * Now that we're up, we can respond to commands. Starting the listener also tells
121 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800122 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900123 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000124 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800125 exit(1);
126 }
127
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900128 write_pid_file();
Robert Greenwalt347f6932014-10-31 18:54:06 -0700129
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900130 ALOGI("Netd started in %dms", static_cast<int>(s.timeTaken()));
131
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900132 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800133
Steve Block08b58b62012-01-04 20:05:11 +0000134 ALOGI("Netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900135
Robert Greenwalt347f6932014-10-31 18:54:06 -0700136 remove_pid_file();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900137
San Mehatd1830422010-01-15 08:02:39 -0800138 exit(0);
139}
140
Robert Greenwalt347f6932014-10-31 18:54:06 -0700141static bool write_pid_file() {
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +0900142 char pid_buf[INT32_STRLEN];
143 snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid());
Robert Greenwalt347f6932014-10-31 18:54:06 -0700144
145 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
146 if (fd == -1) {
147 ALOGE("Unable to create pid file (%s)", strerror(errno));
148 return false;
149 }
150
151 // File creation is affected by umask, so make sure the right mode bits are set.
152 if (fchmod(fd, PID_FILE_MODE) == -1) {
153 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
154 close(fd);
155 remove_pid_file();
156 return false;
157 }
158
159 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
160 ALOGE("Unable to write to pid file (%s)", strerror(errno));
161 close(fd);
162 remove_pid_file();
163 return false;
164 }
165 close(fd);
166 return true;
167}
168
169static void remove_pid_file() {
170 unlink(PID_FILE_PATH);
171}
172
Selim Gurunc1044d42012-03-09 08:59:28 -0800173static void blockSigpipe()
174{
175 sigset_t mask;
176
177 sigemptyset(&mask);
178 sigaddset(&mask, SIGPIPE);
179 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
180 ALOGW("WARNING: SIGPIPE not blocked\n");
181}