blob: 9e84c1c36b08b00a488728f2979aac0ab2551f12 [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"
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070043#include "DnsProxyListener.h"
Robert Greenwalt745e09f2012-03-29 14:45:54 -070044#include "MDnsSdListener.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070045#include "FwmarkServer.h"
San Mehatd1830422010-01-15 08:02:39 -080046
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090047using android::status_t;
48using android::sp;
49using android::IPCThreadState;
50using android::ProcessState;
51using android::defaultServiceManager;
52using android::net::NetdNativeService;
53
Selim Gurunc1044d42012-03-09 08:59:28 -080054static void blockSigpipe();
Robert Greenwalt347f6932014-10-31 18:54:06 -070055static void remove_pid_file();
56static bool write_pid_file();
57
58const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
59const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
60const 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 -080061
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090062android::RWLock android::net::gBigNetdLock;
63
San Mehatd1830422010-01-15 08:02:39 -080064int main() {
Pierre Imai1cfa5432016-02-24 18:00:03 +090065 using android::net::gCtls;
San Mehatd1830422010-01-15 08:02:39 -080066
Steve Block08b58b62012-01-04 20:05:11 +000067 ALOGI("Netd 1.0 starting");
Robert Greenwalt347f6932014-10-31 18:54:06 -070068 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -080069
Selim Gurunc1044d42012-03-09 08:59:28 -080070 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080071
Pierre Imai1cfa5432016-02-24 18:00:03 +090072 NetlinkManager *nm = NetlinkManager::Instance();
73 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000074 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080075 exit(1);
76 };
77
Pierre Imai1cfa5432016-02-24 18:00:03 +090078 gCtls = new android::net::Controllers();
79 CommandListener cl;
80 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -080081
82 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000083 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080084 exit(1);
85 }
86
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070087 // Set local DNS mode, to prevent bionic from proxying
88 // back to this service, recursively.
89 setenv("ANDROID_DNS_MODE", "local", 1);
Pierre Imai1cfa5432016-02-24 18:00:03 +090090 DnsProxyListener dpl(&gCtls->netCtrl);
91 if (dpl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000092 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070093 exit(1);
94 }
95
Pierre Imai1cfa5432016-02-24 18:00:03 +090096 MDnsSdListener mdnsl;
97 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -070098 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
99 exit(1);
100 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700101
Pierre Imai1cfa5432016-02-24 18:00:03 +0900102 FwmarkServer fwmarkServer(&gCtls->netCtrl);
103 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700104 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
105 exit(1);
106 }
107
San Mehatd1830422010-01-15 08:02:39 -0800108 /*
109 * Now that we're up, we can respond to commands
110 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900111 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000112 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800113 exit(1);
114 }
115
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900116 write_pid_file();
Robert Greenwalt347f6932014-10-31 18:54:06 -0700117
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900118 IPCThreadState::self()->disableBackgroundScheduling(true);
119 NetdNativeService::publishAndJoinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800120
Steve Block08b58b62012-01-04 20:05:11 +0000121 ALOGI("Netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900122
Robert Greenwalt347f6932014-10-31 18:54:06 -0700123 remove_pid_file();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900124
San Mehatd1830422010-01-15 08:02:39 -0800125 exit(0);
126}
127
Robert Greenwalt347f6932014-10-31 18:54:06 -0700128static bool write_pid_file() {
129 char pid_buf[20]; // current pid_max is 32768, so plenty of room
130 snprintf(pid_buf, sizeof(pid_buf), "%ld\n", (long)getpid());
131
132 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
133 if (fd == -1) {
134 ALOGE("Unable to create pid file (%s)", strerror(errno));
135 return false;
136 }
137
138 // File creation is affected by umask, so make sure the right mode bits are set.
139 if (fchmod(fd, PID_FILE_MODE) == -1) {
140 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
141 close(fd);
142 remove_pid_file();
143 return false;
144 }
145
146 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
147 ALOGE("Unable to write to pid file (%s)", strerror(errno));
148 close(fd);
149 remove_pid_file();
150 return false;
151 }
152 close(fd);
153 return true;
154}
155
156static void remove_pid_file() {
157 unlink(PID_FILE_PATH);
158}
159
Selim Gurunc1044d42012-03-09 08:59:28 -0800160static void blockSigpipe()
161{
162 sigset_t mask;
163
164 sigemptyset(&mask);
165 sigaddset(&mask, SIGPIPE);
166 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
167 ALOGW("WARNING: SIGPIPE not blocked\n");
168}