blob: aab15d6629270eb08a88803f7122803bed9b8a88 [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();
82 CommandListener cl;
83 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -080084
85 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000086 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080087 exit(1);
88 }
89
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070090 // Set local DNS mode, to prevent bionic from proxying
91 // back to this service, recursively.
92 setenv("ANDROID_DNS_MODE", "local", 1);
Michal Karpinskid5440112016-10-06 16:56:04 +010093 DnsProxyListener dpl(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +090094 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000095 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070096 exit(1);
97 }
98
Pierre Imai1cfa5432016-02-24 18:00:03 +090099 MDnsSdListener mdnsl;
100 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700101 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
102 exit(1);
103 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700104
Michal Karpinski7d374532016-10-06 19:33:55 +0100105 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900106 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700107 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
108 exit(1);
109 }
110
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900111 status_t ret;
112 if ((ret = NetdNativeService::start()) != android::OK) {
113 ALOGE("Unable to start NetdNativeService: %d", ret);
114 exit(1);
115 }
116
San Mehatd1830422010-01-15 08:02:39 -0800117 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900118 * Now that we're up, we can respond to commands. Starting the listener also tells
119 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800120 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900121 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000122 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800123 exit(1);
124 }
125
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900126 write_pid_file();
Robert Greenwalt347f6932014-10-31 18:54:06 -0700127
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900128 ALOGI("Netd started in %dms", static_cast<int>(s.timeTaken()));
129
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900130 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800131
Steve Block08b58b62012-01-04 20:05:11 +0000132 ALOGI("Netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900133
Robert Greenwalt347f6932014-10-31 18:54:06 -0700134 remove_pid_file();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900135
San Mehatd1830422010-01-15 08:02:39 -0800136 exit(0);
137}
138
Robert Greenwalt347f6932014-10-31 18:54:06 -0700139static bool write_pid_file() {
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +0900140 char pid_buf[INT32_STRLEN];
141 snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int) getpid());
Robert Greenwalt347f6932014-10-31 18:54:06 -0700142
143 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
144 if (fd == -1) {
145 ALOGE("Unable to create pid file (%s)", strerror(errno));
146 return false;
147 }
148
149 // File creation is affected by umask, so make sure the right mode bits are set.
150 if (fchmod(fd, PID_FILE_MODE) == -1) {
151 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
152 close(fd);
153 remove_pid_file();
154 return false;
155 }
156
157 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
158 ALOGE("Unable to write to pid file (%s)", strerror(errno));
159 close(fd);
160 remove_pid_file();
161 return false;
162 }
163 close(fd);
164 return true;
165}
166
167static void remove_pid_file() {
168 unlink(PID_FILE_PATH);
169}
170
Selim Gurunc1044d42012-03-09 08:59:28 -0800171static void blockSigpipe()
172{
173 sigset_t mask;
174
175 sigemptyset(&mask);
176 sigaddset(&mask, SIGPIPE);
177 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
178 ALOGW("WARNING: SIGPIPE not blocked\n");
179}