blob: 2de00917f87a60c14a3d1b1e1829230e80b08c62 [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"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090039#include "NetdConstants.h"
40#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080041#include "NetlinkManager.h"
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070042#include "DnsProxyListener.h"
Robert Greenwalt745e09f2012-03-29 14:45:54 -070043#include "MDnsSdListener.h"
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070044#include "FwmarkServer.h"
San Mehatd1830422010-01-15 08:02:39 -080045
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090046using android::status_t;
47using android::sp;
48using android::IPCThreadState;
49using android::ProcessState;
50using android::defaultServiceManager;
51using android::net::NetdNativeService;
52
Selim Gurunc1044d42012-03-09 08:59:28 -080053static void blockSigpipe();
Robert Greenwalt347f6932014-10-31 18:54:06 -070054static void remove_pid_file();
55static bool write_pid_file();
56
57const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
58const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
59const 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 -080060
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090061
62android::RWLock android::net::gBigNetdLock;
63
64
San Mehatd1830422010-01-15 08:02:39 -080065int main() {
66
67 CommandListener *cl;
68 NetlinkManager *nm;
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070069 DnsProxyListener *dpl;
Robert Greenwalt745e09f2012-03-29 14:45:54 -070070 MDnsSdListener *mdnsl;
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -070071 FwmarkServer* fwmarkServer;
San Mehatd1830422010-01-15 08:02:39 -080072
Steve Block08b58b62012-01-04 20:05:11 +000073 ALOGI("Netd 1.0 starting");
Robert Greenwalt347f6932014-10-31 18:54:06 -070074 remove_pid_file();
San Mehatd1830422010-01-15 08:02:39 -080075
Selim Gurunc1044d42012-03-09 08:59:28 -080076 blockSigpipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080077
San Mehatd1830422010-01-15 08:02:39 -080078 if (!(nm = NetlinkManager::Instance())) {
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
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050083 cl = new CommandListener();
San Mehatd1830422010-01-15 08:02:39 -080084 nm->setBroadcaster((SocketListener *) cl);
85
86 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +000087 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -080088 exit(1);
89 }
90
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070091 // Set local DNS mode, to prevent bionic from proxying
92 // back to this service, recursively.
93 setenv("ANDROID_DNS_MODE", "local", 1);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070094 dpl = new DnsProxyListener(CommandListener::sNetCtrl);
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070095 if (dpl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +000096 ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
Brad Fitzpatrick007e9872010-10-27 11:39:52 -070097 exit(1);
98 }
99
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700100 mdnsl = new MDnsSdListener();
101 if (mdnsl->startListener()) {
102 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
103 exit(1);
104 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700105
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700106 fwmarkServer = new FwmarkServer(CommandListener::sNetCtrl);
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700107 if (fwmarkServer->startListener()) {
108 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
109 exit(1);
110 }
111
San Mehatd1830422010-01-15 08:02:39 -0800112 /*
113 * Now that we're up, we can respond to commands
114 */
115 if (cl->startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000116 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800117 exit(1);
118 }
119
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900120 write_pid_file();
Robert Greenwalt347f6932014-10-31 18:54:06 -0700121
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900122 IPCThreadState::self()->disableBackgroundScheduling(true);
123 NetdNativeService::publishAndJoinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800124
Steve Block08b58b62012-01-04 20:05:11 +0000125 ALOGI("Netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900126
Robert Greenwalt347f6932014-10-31 18:54:06 -0700127 remove_pid_file();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900128
San Mehatd1830422010-01-15 08:02:39 -0800129 exit(0);
130}
131
Robert Greenwalt347f6932014-10-31 18:54:06 -0700132static bool write_pid_file() {
133 char pid_buf[20]; // current pid_max is 32768, so plenty of room
134 snprintf(pid_buf, sizeof(pid_buf), "%ld\n", (long)getpid());
135
136 int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
137 if (fd == -1) {
138 ALOGE("Unable to create pid file (%s)", strerror(errno));
139 return false;
140 }
141
142 // File creation is affected by umask, so make sure the right mode bits are set.
143 if (fchmod(fd, PID_FILE_MODE) == -1) {
144 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
145 close(fd);
146 remove_pid_file();
147 return false;
148 }
149
150 if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
151 ALOGE("Unable to write to pid file (%s)", strerror(errno));
152 close(fd);
153 remove_pid_file();
154 return false;
155 }
156 close(fd);
157 return true;
158}
159
160static void remove_pid_file() {
161 unlink(PID_FILE_PATH);
162}
163
Selim Gurunc1044d42012-03-09 08:59:28 -0800164static void blockSigpipe()
165{
166 sigset_t mask;
167
168 sigemptyset(&mask);
169 sigaddset(&mask, SIGPIPE);
170 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
171 ALOGW("WARNING: SIGPIPE not blocked\n");
172}